1 #pragma src "/sys/src/libmp"
7 * the code assumes mpdigit to be at least an int
8 * mpdigit must be an atomic type. mpdigit is defined
9 * in the architecture specific u.h
12 typedef struct mpint mpint;
16 int sign; /* +1 or -1 */
17 int size; /* allocated digits */
18 int top; /* significant digits */
25 MPstatic= 0x01, /* static constant */
26 MPnorm= 0x02, /* normalization status */
27 MPtimesafe= 0x04, /* request time invariant computation */
29 Dbytes= sizeof(mpdigit), /* bytes per digit */
30 Dbits= Dbytes*8 /* bits per digit */
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 mpint* mpnorm(mpint *b); /* dump leading zeros */
39 mpint* mpcopy(mpint *b);
40 void mpassign(mpint *old, mpint *new);
43 mpint* mprand(int bits, void (*gen)(uchar*, int), mpint *b);
44 /* return uniform random [0..n-1] */
45 mpint* mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b);
48 mpint* strtomp(char*, char**, int, mpint*); /* ascii */
50 char* mptoa(mpint*, int, char*, int);
51 mpint* letomp(uchar*, uint, mpint*); /* byte array, little-endian */
52 int mptole(mpint*, uchar*, uint, uchar**);
53 void mptolel(mpint *b, uchar *p, int n);
54 mpint* betomp(uchar*, uint, mpint*); /* byte array, big-endian */
55 int mptobe(mpint*, uchar*, uint, uchar**);
56 void mptober(mpint *b, uchar *p, int n);
57 uint mptoui(mpint*); /* unsigned int */
58 mpint* uitomp(uint, mpint*);
59 int mptoi(mpint*); /* int */
60 mpint* itomp(int, mpint*);
61 uvlong mptouv(mpint*); /* unsigned vlong */
62 mpint* uvtomp(uvlong, mpint*);
63 vlong mptov(mpint*); /* vlong */
64 mpint* vtomp(vlong, mpint*);
66 /* divide 2 digits by one */
67 void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
69 /* in the following, the result mpint may be */
70 /* the same as one of the inputs. */
71 void mpadd(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
72 void mpsub(mpint *b1, mpint *b2, mpint *diff); /* diff = b1-b2 */
73 void mpleft(mpint *b, int shift, mpint *res); /* res = b<<shift */
74 void mpright(mpint *b, int shift, mpint *res); /* res = b>>shift */
75 void mpmul(mpint *b1, mpint *b2, mpint *prod); /* prod = b1*b2 */
76 void mpexp(mpint *b, mpint *e, mpint *m, mpint *res); /* res = b**e mod m */
77 void mpmod(mpint *b, mpint *m, mpint *remainder); /* remainder = b mod m */
79 /* modular arithmetic, time invariant when 0≤b1≤m-1 and 0≤b2≤m-1 */
80 void mpmodadd(mpint *b1, mpint *b2, mpint *m, mpint *sum); /* sum = b1+b2 % m */
81 void mpmodsub(mpint *b1, mpint *b2, mpint *m, mpint *diff); /* diff = b1-b2 % m */
82 void mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod); /* prod = b1*b2 % m */
84 /* quotient = dividend/divisor, remainder = dividend % divisor */
85 void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder);
87 /* return neg, 0, pos as b1-b2 is neg, 0, pos */
88 int mpcmp(mpint *b1, mpint *b2);
90 /* res = s != 0 ? b1 : b2 */
91 void mpsel(int s, mpint *b1, mpint *b2, mpint *res);
93 /* extended gcd return d, x, and y, s.t. d = gcd(a,b) and ax+by = d */
94 void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y);
96 /* res = b**-1 mod m */
97 void mpinvert(mpint *b, mpint *m, mpint *res);
100 int mpsignif(mpint*); /* number of sigificant bits in mantissa */
101 int mplowbits0(mpint*); /* k, where n = 2**k * q for odd q */
103 /* well known constants */
104 extern mpint *mpzero, *mpone, *mptwo;
106 /* sum[0:alen] = a[0:alen-1] + b[0:blen-1] */
107 /* prereq: alen >= blen, sum has room for alen+1 digits */
108 void mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *sum);
110 /* diff[0:alen-1] = a[0:alen-1] - b[0:blen-1] */
111 /* prereq: alen >= blen, diff has room for alen digits */
112 void mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *diff);
114 /* p[0:n] += m * b[0:n-1] */
115 /* prereq: p has room for n+1 digits */
116 void mpvecdigmuladd(mpdigit *b, int n, mpdigit m, mpdigit *p);
118 /* p[0:n] -= m * b[0:n-1] */
119 /* prereq: p has room for n+1 digits */
120 int mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p);
122 /* p[0:alen+blen-1] = a[0:alen-1] * b[0:blen-1] */
123 /* prereq: alen >= blen, p has room for m*n digits */
124 void mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
125 void mpvectsmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
127 /* sign of a - b or zero if the same */
128 int mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen);
129 int mpvectscmp(mpdigit *a, int alen, mpdigit *b, int blen);
131 /* divide the 2 digit dividend by the one digit divisor and stick in quotient */
132 /* we assume that the result is one digit - overflow is all 1's */
133 void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
135 /* playing with magnitudes */
136 int mpmagcmp(mpint *b1, mpint *b2);
137 void mpmagadd(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
138 void mpmagsub(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
140 /* chinese remainder theorem */
141 typedef struct CRTpre CRTpre; /* precomputed values for converting */
142 /* twixt residues and mpint */
143 typedef struct CRTres CRTres; /* residue form of an mpint */
145 #pragma incomplete CRTpre
149 int n; /* number of residues */
150 mpint *r[1]; /* residues */
153 CRTpre* crtpre(int, mpint**); /* precompute conversion values */
154 CRTres* crtin(CRTpre*, mpint*); /* convert mpint to residues */
155 void crtout(CRTpre*, CRTres*, mpint*); /* convert residues to mpint */
156 void crtprefree(CRTpre*);
157 void crtresfree(CRTres*);
160 #pragma varargck type "B" mpint*