2 |
- |
1 |
#pragma src "/sys/src/libmp"
|
|
|
2 |
#pragma lib "libmp.a"
|
|
|
3 |
|
|
|
4 |
#define _MPINT 1
|
|
|
5 |
|
|
|
6 |
/*
|
|
|
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
|
|
|
10 |
*/
|
|
|
11 |
typedef struct mpint mpint;
|
|
|
12 |
|
|
|
13 |
struct mpint
|
|
|
14 |
{
|
|
|
15 |
int sign; /* +1 or -1 */
|
|
|
16 |
int size; /* allocated digits */
|
|
|
17 |
int top; /* significant digits */
|
|
|
18 |
mpdigit *p;
|
|
|
19 |
char flags;
|
|
|
20 |
};
|
|
|
21 |
|
|
|
22 |
enum
|
|
|
23 |
{
|
33 |
7u83 |
24 |
MPstatic= 0x01, /* static constant */
|
|
|
25 |
MPnorm= 0x02, /* normalization status */
|
|
|
26 |
MPtimesafe= 0x04, /* request time invariant computation */
|
|
|
27 |
MPfield= 0x08, /* this mpint is a field modulus */
|
|
|
28 |
|
2 |
- |
29 |
Dbytes= sizeof(mpdigit), /* bytes per digit */
|
|
|
30 |
Dbits= Dbytes*8 /* bits per digit */
|
|
|
31 |
};
|
|
|
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 */
|
33 |
7u83 |
38 |
mpint* mpnorm(mpint *b); /* dump leading zeros */
|
2 |
- |
39 |
mpint* mpcopy(mpint *b);
|
|
|
40 |
void mpassign(mpint *old, mpint *new);
|
|
|
41 |
|
|
|
42 |
/* random bits */
|
|
|
43 |
mpint* mprand(int bits, void (*gen)(uchar*, int), mpint *b);
|
33 |
7u83 |
44 |
/* return uniform random [0..n-1] */
|
|
|
45 |
mpint* mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b);
|
2 |
- |
46 |
|
|
|
47 |
/* conversion */
|
|
|
48 |
mpint* strtomp(char*, char**, int, mpint*); /* ascii */
|
|
|
49 |
int mpfmt(Fmt*);
|
|
|
50 |
char* mptoa(mpint*, int, char*, int);
|
|
|
51 |
mpint* letomp(uchar*, uint, mpint*); /* byte array, little-endian */
|
|
|
52 |
int mptole(mpint*, uchar*, uint, uchar**);
|
33 |
7u83 |
53 |
void mptolel(mpint *b, uchar *p, int n);
|
|
|
54 |
mpint* betomp(uchar*, uint, mpint*); /* byte array, big-endian */
|
2 |
- |
55 |
int mptobe(mpint*, uchar*, uint, uchar**);
|
33 |
7u83 |
56 |
void mptober(mpint *b, uchar *p, int n);
|
2 |
- |
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*);
|
33 |
7u83 |
65 |
double mptod(mpint*); /* double */
|
|
|
66 |
mpint* dtomp(double, mpint*);
|
2 |
- |
67 |
|
|
|
68 |
/* divide 2 digits by one */
|
|
|
69 |
void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
|
|
|
70 |
|
|
|
71 |
/* in the following, the result mpint may be */
|
|
|
72 |
/* the same as one of the inputs. */
|
|
|
73 |
void mpadd(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
|
|
|
74 |
void mpsub(mpint *b1, mpint *b2, mpint *diff); /* diff = b1-b2 */
|
|
|
75 |
void mpleft(mpint *b, int shift, mpint *res); /* res = b<<shift */
|
|
|
76 |
void mpright(mpint *b, int shift, mpint *res); /* res = b>>shift */
|
|
|
77 |
void mpmul(mpint *b1, mpint *b2, mpint *prod); /* prod = b1*b2 */
|
|
|
78 |
void mpexp(mpint *b, mpint *e, mpint *m, mpint *res); /* res = b**e mod m */
|
|
|
79 |
void mpmod(mpint *b, mpint *m, mpint *remainder); /* remainder = b mod m */
|
|
|
80 |
|
33 |
7u83 |
81 |
/* logical operations */
|
|
|
82 |
void mpand(mpint *b1, mpint *b2, mpint *res);
|
|
|
83 |
void mpbic(mpint *b1, mpint *b2, mpint *res);
|
|
|
84 |
void mpor(mpint *b1, mpint *b2, mpint *res);
|
|
|
85 |
void mpnot(mpint *b, mpint *res);
|
|
|
86 |
void mpxor(mpint *b1, mpint *b2, mpint *res);
|
|
|
87 |
void mptrunc(mpint *b, int n, mpint *res);
|
|
|
88 |
void mpxtend(mpint *b, int n, mpint *res);
|
|
|
89 |
void mpasr(mpint *b, int shift, mpint *res);
|
|
|
90 |
|
|
|
91 |
/* modular arithmetic, time invariant when 0≤b1≤m-1 and 0≤b2≤m-1 */
|
|
|
92 |
void mpmodadd(mpint *b1, mpint *b2, mpint *m, mpint *sum); /* sum = b1+b2 % m */
|
|
|
93 |
void mpmodsub(mpint *b1, mpint *b2, mpint *m, mpint *diff); /* diff = b1-b2 % m */
|
|
|
94 |
void mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod); /* prod = b1*b2 % m */
|
|
|
95 |
|
2 |
- |
96 |
/* quotient = dividend/divisor, remainder = dividend % divisor */
|
|
|
97 |
void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder);
|
|
|
98 |
|
|
|
99 |
/* return neg, 0, pos as b1-b2 is neg, 0, pos */
|
|
|
100 |
int mpcmp(mpint *b1, mpint *b2);
|
|
|
101 |
|
33 |
7u83 |
102 |
/* res = s != 0 ? b1 : b2 */
|
|
|
103 |
void mpsel(int s, mpint *b1, mpint *b2, mpint *res);
|
|
|
104 |
|
|
|
105 |
/* return n! */
|
|
|
106 |
mpint* mpfactorial(ulong n);
|
|
|
107 |
|
2 |
- |
108 |
/* extended gcd return d, x, and y, s.t. d = gcd(a,b) and ax+by = d */
|
|
|
109 |
void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y);
|
|
|
110 |
|
|
|
111 |
/* res = b**-1 mod m */
|
|
|
112 |
void mpinvert(mpint *b, mpint *m, mpint *res);
|
|
|
113 |
|
|
|
114 |
/* bit counting */
|
|
|
115 |
int mpsignif(mpint*); /* number of sigificant bits in mantissa */
|
|
|
116 |
int mplowbits0(mpint*); /* k, where n = 2**k * q for odd q */
|
|
|
117 |
|
|
|
118 |
/* well known constants */
|
|
|
119 |
extern mpint *mpzero, *mpone, *mptwo;
|
|
|
120 |
|
|
|
121 |
/* sum[0:alen] = a[0:alen-1] + b[0:blen-1] */
|
|
|
122 |
/* prereq: alen >= blen, sum has room for alen+1 digits */
|
|
|
123 |
void mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *sum);
|
|
|
124 |
|
|
|
125 |
/* diff[0:alen-1] = a[0:alen-1] - b[0:blen-1] */
|
|
|
126 |
/* prereq: alen >= blen, diff has room for alen digits */
|
|
|
127 |
void mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *diff);
|
|
|
128 |
|
|
|
129 |
/* p[0:n] += m * b[0:n-1] */
|
|
|
130 |
/* prereq: p has room for n+1 digits */
|
|
|
131 |
void mpvecdigmuladd(mpdigit *b, int n, mpdigit m, mpdigit *p);
|
|
|
132 |
|
|
|
133 |
/* p[0:n] -= m * b[0:n-1] */
|
|
|
134 |
/* prereq: p has room for n+1 digits */
|
|
|
135 |
int mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p);
|
|
|
136 |
|
33 |
7u83 |
137 |
/* p[0:alen+blen-1] = a[0:alen-1] * b[0:blen-1] */
|
2 |
- |
138 |
/* prereq: alen >= blen, p has room for m*n digits */
|
|
|
139 |
void mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
|
33 |
7u83 |
140 |
void mpvectsmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
|
2 |
- |
141 |
|
|
|
142 |
/* sign of a - b or zero if the same */
|
|
|
143 |
int mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen);
|
33 |
7u83 |
144 |
int mpvectscmp(mpdigit *a, int alen, mpdigit *b, int blen);
|
2 |
- |
145 |
|
|
|
146 |
/* divide the 2 digit dividend by the one digit divisor and stick in quotient */
|
|
|
147 |
/* we assume that the result is one digit - overflow is all 1's */
|
|
|
148 |
void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
|
|
|
149 |
|
|
|
150 |
/* playing with magnitudes */
|
|
|
151 |
int mpmagcmp(mpint *b1, mpint *b2);
|
|
|
152 |
void mpmagadd(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
|
|
|
153 |
void mpmagsub(mpint *b1, mpint *b2, mpint *sum); /* sum = b1+b2 */
|
|
|
154 |
|
|
|
155 |
/* chinese remainder theorem */
|
|
|
156 |
typedef struct CRTpre CRTpre; /* precomputed values for converting */
|
|
|
157 |
/* twixt residues and mpint */
|
|
|
158 |
typedef struct CRTres CRTres; /* residue form of an mpint */
|
|
|
159 |
|
|
|
160 |
#pragma incomplete CRTpre
|
|
|
161 |
|
|
|
162 |
struct CRTres
|
|
|
163 |
{
|
|
|
164 |
int n; /* number of residues */
|
|
|
165 |
mpint *r[1]; /* residues */
|
|
|
166 |
};
|
|
|
167 |
|
|
|
168 |
CRTpre* crtpre(int, mpint**); /* precompute conversion values */
|
|
|
169 |
CRTres* crtin(CRTpre*, mpint*); /* convert mpint to residues */
|
|
|
170 |
void crtout(CRTpre*, CRTres*, mpint*); /* convert residues to mpint */
|
|
|
171 |
void crtprefree(CRTpre*);
|
|
|
172 |
void crtresfree(CRTres*);
|
|
|
173 |
|
33 |
7u83 |
174 |
/* fast field arithmetic */
|
|
|
175 |
typedef struct Mfield Mfield;
|
2 |
- |
176 |
|
33 |
7u83 |
177 |
struct Mfield
|
|
|
178 |
{
|
|
|
179 |
mpint;
|
|
|
180 |
int (*reduce)(Mfield*, mpint*, mpint*);
|
|
|
181 |
};
|
|
|
182 |
|
|
|
183 |
mpint *mpfield(mpint*);
|
|
|
184 |
|
|
|
185 |
Mfield *gmfield(mpint*);
|
|
|
186 |
Mfield *cnfield(mpint*);
|
|
|
187 |
|
2 |
- |
188 |
#pragma varargck type "B" mpint*
|