mp.h 5.8 KB

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