Blame


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