mp.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma src "/sys/src/lib/mp"
  2. #pragma lib "libmp.a"
  3. #define _MPINT 1
  4. /*
  5. * the code assumes mpdigit to be at least an int
  6. * mpdigit must be an atomic type. mpdigit is defined
  7. * in the architecture specific u.h
  8. */
  9. typedef struct mpint mpint;
  10. struct mpint
  11. {
  12. int sign; /* +1 or -1 */
  13. int size; /* allocated digits */
  14. int top; /* significant digits */
  15. mpdigit *p;
  16. char flags;
  17. };
  18. enum
  19. {
  20. MPstatic= 0x01, /* static constant */
  21. MPnorm= 0x02, /* normalization status */
  22. MPtimesafe= 0x04, /* request time invariant computation */
  23. MPfield= 0x08, /* this mpint is a field modulus */
  24. Dbytes= sizeof(mpdigit), /* bytes per digit */
  25. Dbits= Dbytes*8 /* bits per digit */
  26. };
  27. /* allocation */
  28. void mpsetminbits(int n); /* newly created mpint's get at least n bits */
  29. mpint* mpnew(int n); /* create a new mpint with at least n bits */
  30. void mpfree(mpint *b);
  31. void mpbits(mpint *b, int n); /* ensure that b has at least n bits */
  32. mpint* mpnorm(mpint *b); /* dump leading zeros */
  33. mpint* mpcopy(mpint *b);
  34. void mpassign(mpint *old, mpint *new);
  35. /* random bits */
  36. mpint* mprand(int bits, void (*gen)(uint8_t*, int), mpint *b);
  37. /* return uniform random [0..n-1] */
  38. mpint* mpnrand(mpint *n, void (*gen)(uint8_t*, int), mpint *b);
  39. /* conversion */
  40. mpint* strtomp(char*, char**, int, mpint*); /* ascii */
  41. int mpfmt(Fmt*);
  42. char* mptoa(mpint*, int, char*, int);
  43. mpint* letomp(uint8_t*, uint32_t, mpint*); /* byte array, little-endian */
  44. int mptole(mpint*, uint8_t*, uint32_t, uint8_t**);
  45. void mptolel(mpint *b, uint8_t *p, int n);
  46. mpint* betomp(uint8_t*, uint32_t, mpint*); /* byte array, big-endian */
  47. int mptobe(mpint*, uint8_t*, uint32_t, uint8_t**);
  48. void mptober(mpint *b, uint8_t *p, int n);
  49. uint32_t mptoui(mpint*); /* unsigned int */
  50. mpint* uitomp(uint32_t, mpint*);
  51. int mptoi(mpint*); /* int */
  52. mpint* itomp(int, mpint*);
  53. uint64_t mptouv(mpint*); /* unsigned int64_t */
  54. mpint* uvtomp(uint64_t, mpint*);
  55. int64_t mptov(mpint*); /* int64_t */
  56. mpint* vtomp(int64_t, mpint*);
  57. /* divide 2 digits by one */
  58. void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
  59. /* in the following, the result mpint may be */
  60. /* the same as one of the inputs. */
  61. void mpadd(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
  62. void mpsub(mpint *b1, mpint *b2, mpint *diff); /* diff = b1-b2 */
  63. void mpleft(mpint *b, int shift, mpint *res); /* res = b<<shift */
  64. void mpright(mpint *b, int shift, mpint *res); /* res = b>>shift */
  65. void mpmul(mpint *b1, mpint *b2, mpint *prod); /* prod = b1*b2 */
  66. void mpexp(mpint *b, mpint *e, mpint *m, mpint *res); /* res = b**e mod m */
  67. void mpmod(mpint *b, mpint *m, mpint *remainder); /* remainder = b mod m */
  68. /* logical operations */
  69. void mpand(mpint *b1, mpint *b2, mpint *res);
  70. void mpbic(mpint *b1, mpint *b2, mpint *res);
  71. void mpor(mpint *b1, mpint *b2, mpint *res);
  72. void mpnot(mpint *b, mpint *res);
  73. void mpxor(mpint *b1, mpint *b2, mpint *res);
  74. void mptrunc(mpint *b, int n, mpint *res);
  75. void mpxtend(mpint *b, int n, mpint *res);
  76. /* modular arithmetic, time invariant when 0≤b1≤m-1 and 0≤b2≤m-1 */
  77. void mpmodadd(mpint *b1, mpint *b2, mpint *m, mpint *sum); /* sum = b1+b2 % m */
  78. void mpmodsub(mpint *b1, mpint *b2, mpint *m, mpint *diff); /* diff = b1-b2 % m */
  79. void mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod); /* prod = b1*b2 % m */
  80. /* quotient = dividend/divisor, remainder = dividend % divisor */
  81. void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder);
  82. /* return neg, 0, pos as b1-b2 is neg, 0, pos */
  83. int mpcmp(mpint *b1, mpint *b2);
  84. /* res = s != 0 ? b1 : b2 */
  85. void mpsel(int s, mpint *b1, mpint *b2, mpint *res);
  86. /* extended gcd return d, x, and y, s.t. d = gcd(a,b) and ax+by = d */
  87. void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y);
  88. /* res = b**-1 mod m */
  89. void mpinvert(mpint *b, mpint *m, mpint *res);
  90. /* bit counting */
  91. int mpsignif(mpint*); /* number of sigificant bits in mantissa */
  92. int mplowbits0(mpint*); /* k, where n = 2**k * q for odd q */
  93. /* well known constants */
  94. extern mpint *mpzero, *mpone, *mptwo;
  95. /* sum[0:alen] = a[0:alen-1] + b[0:blen-1] */
  96. /* prereq: alen >= blen, sum has room for alen+1 digits */
  97. void mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *sum);
  98. /* diff[0:alen-1] = a[0:alen-1] - b[0:blen-1] */
  99. /* prereq: alen >= blen, diff has room for alen digits */
  100. void mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *diff);
  101. /* p[0:n] += m * b[0:n-1] */
  102. /* prereq: p has room for n+1 digits */
  103. void mpvecdigmuladd(mpdigit *b, int n, mpdigit m, mpdigit *p);
  104. /* p[0:n] -= m * b[0:n-1] */
  105. /* prereq: p has room for n+1 digits */
  106. int mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p);
  107. /* p[0:alen+blen-1] = a[0:alen-1] * b[0:blen-1] */
  108. /* prereq: alen >= blen, p has room for m*n digits */
  109. void mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
  110. void mpvectsmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
  111. /* sign of a - b or zero if the same */
  112. int mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen);
  113. int mpvectscmp(mpdigit *a, int alen, mpdigit *b, int blen);
  114. /* divide the 2 digit dividend by the one digit divisor and stick in quotient */
  115. /* we assume that the result is one digit - overflow is all 1's */
  116. void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
  117. /* playing with magnitudes */
  118. int mpmagcmp(mpint *b1, mpint *b2);
  119. void mpmagadd(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
  120. void mpmagsub(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
  121. /* chinese remainder theorem */
  122. typedef struct CRTpre CRTpre; /* precomputed values for converting */
  123. /* twixt residues and mpint */
  124. typedef struct CRTres CRTres; /* residue form of an mpint */
  125. #pragma incomplete CRTpre
  126. struct CRTres
  127. {
  128. int n; /* number of residues */
  129. mpint *r[1]; /* residues */
  130. };
  131. CRTpre* crtpre(int, mpint**); /* precompute conversion values */
  132. CRTres* crtin(CRTpre*, mpint*); /* convert mpint to residues */
  133. void crtout(CRTpre*, CRTres*, mpint*); /* convert residues to mpint */
  134. void crtprefree(CRTpre*);
  135. void crtresfree(CRTres*);
  136. /* fast field arithmetic */
  137. typedef struct Mfield Mfield;
  138. struct Mfield
  139. {
  140. mpint;
  141. int (*reduce)(Mfield*, mpint*, mpint*);
  142. };
  143. mpint *mpfield(mpint*);
  144. Mfield *gmfield(mpint*);
  145. Mfield *cnfield(mpint*);
  146. #pragma varargck type "B" mpint*