mp.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #pragma src "/sys/src/libmp"
  2. #pragma lib "libmp.a"
  3. #define _MPINT 1
  4. // the code assumes mpdigit to be at least an int
  5. // mpdigit must be an atomic type. mpdigit is defined
  6. // in the architecture specific u.h
  7. typedef struct mpint mpint;
  8. struct mpint
  9. {
  10. int sign; // +1 or -1
  11. int size; // allocated digits
  12. int top; // significant digits
  13. mpdigit *p;
  14. char flags;
  15. };
  16. enum
  17. {
  18. MPstatic= 0x01,
  19. Dbytes= sizeof(mpdigit), // bytes per digit
  20. Dbits= Dbytes*8 // bits per digit
  21. };
  22. // allocation
  23. void mpsetminbits(int n); // newly created mpint's get at least n bits
  24. mpint* mpnew(int n); // create a new mpint with at least n bits
  25. void mpfree(mpint *b);
  26. void mpbits(mpint *b, int n); // ensure that b has at least n bits
  27. void mpnorm(mpint *b); // dump leading zeros
  28. mpint* mpcopy(mpint *b);
  29. void mpassign(mpint *old, mpint *new);
  30. // random bits
  31. mpint* mprand(int bits, void (*gen)(uchar*, int), mpint *b);
  32. // conversion
  33. mpint* strtomp(char*, char**, int, mpint*); // ascii
  34. int mpfmt(Fmt*);
  35. char* mptoa(mpint*, int, char*, int);
  36. mpint* letomp(uchar*, uint, mpint*); // byte array, little-endian
  37. int mptole(mpint*, uchar*, uint, uchar**);
  38. mpint* betomp(uchar*, uint, mpint*); // byte array, little-endian
  39. int mptobe(mpint*, uchar*, uint, uchar**);
  40. uint mptoui(mpint*); // unsigned int
  41. mpint* uitomp(uint, mpint*);
  42. int mptoi(mpint*); // int
  43. mpint* itomp(int, mpint*);
  44. uvlong mptouv(mpint*); // unsigned vlong
  45. mpint* uvtomp(uvlong, mpint*);
  46. vlong mptov(mpint*); // vlong
  47. mpint* vtomp(vlong, mpint*);
  48. // divide 2 digits by one
  49. void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
  50. // in the following, the result mpint may be
  51. // the same as one of the inputs.
  52. void mpadd(mpint *b1, mpint *b2, mpint *sum); // sum = b1+b2
  53. void mpsub(mpint *b1, mpint *b2, mpint *diff); // diff = b1-b2
  54. void mpleft(mpint *b, int shift, mpint *res); // res = b<<shift
  55. void mpright(mpint *b, int shift, mpint *res); // res = b>>shift
  56. void mpmul(mpint *b1, mpint *b2, mpint *prod); // prod = b1*b2
  57. void mpexp(mpint *b, mpint *e, mpint *m, mpint *res); // res = b**e mod m
  58. void mpmod(mpint *b, mpint *m, mpint *remainder); // remainder = b mod m
  59. // quotient = dividend/divisor, remainder = dividend % divisor
  60. void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder);
  61. // return neg, 0, pos as b1-b2 is neg, 0, pos
  62. int mpcmp(mpint *b1, mpint *b2);
  63. // extended gcd return d, x, and y, s.t. d = gcd(a,b) and ax+by = d
  64. void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y);
  65. // res = b**-1 mod m
  66. void mpinvert(mpint *b, mpint *m, mpint *res);
  67. // bit counting
  68. int mpsignif(mpint*); // number of sigificant bits in mantissa
  69. int mplowbits0(mpint*); // k, where n = 2**k * q for odd q
  70. // well known constants
  71. extern mpint *mpzero, *mpone, *mptwo;
  72. // sum[0:alen] = a[0:alen-1] + b[0:blen-1]
  73. // prereq: alen >= blen, sum has room for alen+1 digits
  74. void mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *sum);
  75. // diff[0:alen-1] = a[0:alen-1] - b[0:blen-1]
  76. // prereq: alen >= blen, diff has room for alen digits
  77. void mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *diff);
  78. // p[0:n] += m * b[0:n-1]
  79. // prereq: p has room for n+1 digits
  80. void mpvecdigmuladd(mpdigit *b, int n, mpdigit m, mpdigit *p);
  81. // p[0:n] -= m * b[0:n-1]
  82. // prereq: p has room for n+1 digits
  83. int mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p);
  84. // p[0:alen*blen-1] = a[0:alen-1] * b[0:blen-1]
  85. // prereq: alen >= blen, p has room for m*n digits
  86. void mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
  87. // sign of a - b or zero if the same
  88. int mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen);
  89. // divide the 2 digit dividend by the one digit divisor and stick in quotient
  90. // we assume that the result is one digit - overflow is all 1's
  91. void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
  92. // playing with magnitudes
  93. int mpmagcmp(mpint *b1, mpint *b2);
  94. void mpmagadd(mpint *b1, mpint *b2, mpint *sum); // sum = b1+b2
  95. void mpmagsub(mpint *b1, mpint *b2, mpint *sum); // sum = b1+b2
  96. // chinese remainder theorem
  97. typedef struct CRTpre CRTpre; // precomputed values for converting
  98. // twixt residues and mpint
  99. typedef struct CRTres CRTres; // residue form of an mpint
  100. struct CRTres
  101. {
  102. int n; // number of residues
  103. mpint *r[1]; // residues
  104. };
  105. CRTpre* crtpre(int, mpint**); // precompute conversion values
  106. CRTres* crtin(CRTpre*, mpint*); // convert mpint to residues
  107. void crtout(CRTpre*, CRTres*, mpint*); // convert residues to mpint
  108. void crtprefree(CRTpre*);
  109. void crtresfree(CRTres*);
  110. #pragma varargck type "B" mpint*