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 */
26 Dbytes= sizeof(mpdigit), /* bytes per digit */
27 Dbits= Dbytes*8 /* bits per digit */
31 void mpsetminbits(int n); /* newly created mpint's get at least n bits */
32 mpint* mpnew(int n); /* create a new mpint with at least n bits */
33 void mpfree(mpint *b);
34 void mpbits(mpint *b, int n); /* ensure that b has at least n bits */
35 void mpnorm(mpint *b); /* dump leading zeros */
36 mpint* mpcopy(mpint *b);
37 void mpassign(mpint *old, mpint *new);
40 mpint* mprand(int bits, void (*gen)(uchar*, int), mpint *b);
43 mpint* strtomp(char*, char**, int, mpint*); /* ascii */
45 char* mptoa(mpint*, int, char*, int);
46 mpint* letomp(uchar*, uint, mpint*); /* byte array, little-endian */
47 int mptole(mpint*, uchar*, uint, uchar**);
48 mpint* betomp(uchar*, uint, mpint*); /* byte array, little-endian */
49 int mptobe(mpint*, uchar*, uint, uchar**);
50 uint mptoui(mpint*); /* unsigned int */
51 mpint* uitomp(uint, mpint*);
52 int mptoi(mpint*); /* int */
53 mpint* itomp(int, mpint*);
54 uvlong mptouv(mpint*); /* unsigned vlong */
55 mpint* uvtomp(uvlong, mpint*);
56 vlong mptov(mpint*); /* vlong */
57 mpint* vtomp(vlong, mpint*);
59 /* divide 2 digits by one */
60 void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
62 /* in the following, the result mpint may be */
63 /* the same as one of the inputs. */
64 void mpadd(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
65 void mpsub(mpint *b1, mpint *b2, mpint *diff); /* diff = b1-b2 */
66 void mpleft(mpint *b, int shift, mpint *res); /* res = b<<shift */
67 void mpright(mpint *b, int shift, mpint *res); /* res = b>>shift */
68 void mpmul(mpint *b1, mpint *b2, mpint *prod); /* prod = b1*b2 */
69 void mpexp(mpint *b, mpint *e, mpint *m, mpint *res); /* res = b**e mod m */
70 void mpmod(mpint *b, mpint *m, mpint *remainder); /* remainder = b mod m */
72 /* quotient = dividend/divisor, remainder = dividend % divisor */
73 void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder);
75 /* return neg, 0, pos as b1-b2 is neg, 0, pos */
76 int mpcmp(mpint *b1, mpint *b2);
78 /* extended gcd return d, x, and y, s.t. d = gcd(a,b) and ax+by = d */
79 void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y);
81 /* res = b**-1 mod m */
82 void mpinvert(mpint *b, mpint *m, mpint *res);
85 int mpsignif(mpint*); /* number of sigificant bits in mantissa */
86 int mplowbits0(mpint*); /* k, where n = 2**k * q for odd q */
88 /* well known constants */
89 extern mpint *mpzero, *mpone, *mptwo;
91 /* sum[0:alen] = a[0:alen-1] + b[0:blen-1] */
92 /* prereq: alen >= blen, sum has room for alen+1 digits */
93 void mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *sum);
95 /* diff[0:alen-1] = a[0:alen-1] - b[0:blen-1] */
96 /* prereq: alen >= blen, diff has room for alen digits */
97 void mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *diff);
99 /* p[0:n] += m * b[0:n-1] */
100 /* prereq: p has room for n+1 digits */
101 void mpvecdigmuladd(mpdigit *b, int n, mpdigit m, mpdigit *p);
103 /* p[0:n] -= m * b[0:n-1] */
104 /* prereq: p has room for n+1 digits */
105 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);
111 /* sign of a - b or zero if the same */
112 int mpveccmp(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);
118 /* playing with magnitudes */
119 int mpmagcmp(mpint *b1, mpint *b2);
120 void mpmagadd(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
121 void mpmagsub(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
123 /* chinese remainder theorem */
124 typedef struct CRTpre CRTpre; /* precomputed values for converting */
125 /* twixt residues and mpint */
126 typedef struct CRTres CRTres; /* residue form of an mpint */
128 #pragma incomplete CRTpre
132 int n; /* number of residues */
133 mpint *r[1]; /* residues */
136 CRTpre* crtpre(int, mpint**); /* precompute conversion values */
137 CRTres* crtin(CRTpre*, mpint*); /* convert mpint to residues */
138 void crtout(CRTpre*, CRTres*, mpint*); /* convert residues to mpint */
139 void crtprefree(CRTpre*);
140 void crtresfree(CRTres*);
143 #pragma varargck type "B" mpint*