mp.h 4.7 KB

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