blob: 5cc80f01a3da000ea2a4b1cde5e5fe9c5a6e366b [file] [log] [blame]
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05001/*
2 * bn.h - the interface to the bignum routines.
3 * All functions which return ints can potentially allocate memory
4 * and return -1 if they are unable to. All "const" arguments
5 * are unmodified.
6 *
7 * This is not particularly asymmetric, as some operations are of the
8 * form a = b @ c, while others do a @= b. In general, outputs may not
9 * point to the same struct BigNums as inputs, except as specified
10 * below. This relationship is referred to as "being the same as".
11 * This is not numerical equivalence.
12 *
13 * The "Q" operations take "unsigned" inputs. Higher values of the
14 * extra input may work on some implementations, but 65535 is the
15 * highest portable value. Just because UNSIGNED_MAX is larger than
16 * that, or you know that the word size of the library is larger than that,
17 * that, does *not* mean it's allowed.
18 */
19#ifndef BN_H
20#define BN_H
21
22#ifdef __cplusplus
23extern "C"
24{
25#endif
26
27struct BigNum {
28 void *ptr;
29 unsigned size; /* Note: in (variable-sized) words */
30 unsigned allocated;
31};
32
33#ifndef SWIG
34/*
35 * User-supplied function: if non-NULL, this is called during long-running
36 * computations. You may put Yield() calls in here to give CPU time to
37 * other processes. You may also force the computation to be aborted,
38 * by returning a value < 0, which will be the return value of the
39 * bnXXX call. (You probably want the value to be someting other than
40 * -1, to distinguish it from a n out-of-memory error.)
41 *
42 * The functions that this is called from, and the intervals at which it
43 * is called, are not well defined, just "reasonably often". (Currently,
44 * once per exponent bit in nodular exponentiation, and once per two
45 * divisions in GCD and inverse computation.)
46 */
47extern int (*bnYield)(void);
48
49/* Functions */
50
51/*
52 * You usually never have to call this function explicitly, as
53 * bnBegin() takes care of it. If the program jumps to address 0,
54 * this function has bot been called.
55 */
56void bnInit(void);
57
58/*
59 * This initializes an empty struct BigNum to a zero value.
60 * Do not use this on a BigNum which has had a value stored in it!
61 */
62void bnBegin(struct BigNum *bn);
63
64/* Swap two BigNums. Cheap. */
65void bnSwap(struct BigNum *a, struct BigNum *b);
66
67/* Reset an initialized bigNum to empty, pending deallocation. */
68extern void (*bnEnd)(struct BigNum *bn);
69
70/*
71 * If you know you'll need space in the number soon, you can use this function
72 * to ensure that there is room for at least "bits" bits. Optional.
73 * Returns <0 on out of memory, but the value is unaffected.
74 */
75extern int (*bnPrealloc)(struct BigNum *bn, unsigned bits);
76
77/* Hopefully obvious. dest = src. dest may be the same as src. */
78extern int (*bnCopy)(struct BigNum *dest, struct BigNum const *src);
79
80/*
81 * Mostly done automatically, but this removes leading zero words from
82 * the internal representation of the BigNum. Use is unclear.
83 */
84extern void (*bnNorm)(struct BigNum *bn);
85
86/*
87 * Move bytes between the given buffer and the given BigNum encoded in
88 * base 256. I.e. after either of these, the buffer will be equal to
89 * (bn / 256^lsbyte) % 256^len. The difference is which is altered to
90 * match the other!
91 */
92extern void (*bnExtractBigBytes)(struct BigNum const *bn,
93 unsigned char *dest, unsigned lsbyte, unsigned len);
94extern int (*bnInsertBigBytes)(struct BigNum *bn, unsigned char const *src,
95 unsigned lsbyte, unsigned len);
96
97/* The same, but the buffer is little-endian. */
98extern void (*bnExtractLittleBytes)(struct BigNum const *bn,
99 unsigned char *dest, unsigned lsbyte, unsigned len);
100extern int (*bnInsertLittleBytes)(struct BigNum *bn, unsigned char const *src,
101 unsigned lsbyte, unsigned len);
102
103/* Return the least-significant bits (at least 16) of the BigNum */
104extern unsigned (*bnLSWord)(struct BigNum const *src);
105
106/* Return the selected bit of the BigNum (bit 0 is bn mod 2) */
107extern int (*bnReadBit)(struct BigNum const *bn, unsigned bit);
108
109/*
110 * Return the number of significant bits in the BigNum.
111 * 0 or 1+floor(log2(src))
112 */
113extern unsigned (*bnBits)(struct BigNum const *src);
114#define bnBytes(bn) ((bnBits(bn)+7)/8)
115
116/*
117 * dest += src. dest and src may be the same. Guaranteed not to
118 * allocate memory unnecessarily, so if you're sure bnBits(dest)
119 * won't change, you don't need to check the return value.
120 */
121extern int (*bnAdd)(struct BigNum *dest, struct BigNum const *src);
122
123/*
124 * dest -= src. dest and src may be the same, but bnSetQ(dest, 0) is faster.
125 * if dest < src, returns +1 and sets dest = src-dest.
126 */
127extern int (*bnSub)(struct BigNum *dest, struct BigNum const *src);
128
129/* Return sign (-1, 0, +1) of a-b. a <=> b --> bnCmpQ(a, b) <=> 0 */
130extern int (*bnCmpQ)(struct BigNum const *a, unsigned b);
131
132/* dest = src, where 0 <= src < 2^16. */
133extern int (*bnSetQ)(struct BigNum *dest, unsigned src);
134
135/* dest += src, where 0 <= src < 2^16 */
136extern int (*bnAddQ)(struct BigNum *dest, unsigned src);
137
138/* dest -= src, where 0 <= src < 2^16 */
139extern int (*bnSubQ)(struct BigNum *dest, unsigned src);
140
141/* Return sign (-1, 0, +1) of a-b. a <=> b --> bnCmp(a, b) <=> 0 */
142extern int (*bnCmp)(struct BigNum const *a, struct BigNum const *b);
143
144/* dest = src^2. dest may be the same as src, but it costs time. */
145extern int (*bnSquare)(struct BigNum *dest, struct BigNum const *src);
146
147/* dest = a * b. dest may be the same as a or b, but it costs time. */
148extern int (*bnMul)(struct BigNum *dest, struct BigNum const *a,
149 struct BigNum const *b);
150
151/* dest = a * b, where 0 <= b < 2^16. dest and a may be the same. */
152extern int (*bnMulQ)(struct BigNum *dest, struct BigNum const *a, unsigned b);
153
154/*
155 * q = n/d, r = n%d. r may be the same as n, but not d,
156 * and q may not be the same as n or d.
157 * re-entrancy issue: this temporarily modifies d, but restores
158 * it for return.
159 */
160extern int (*bnDivMod)(struct BigNum *q, struct BigNum *r,
161 struct BigNum const *n, struct BigNum const *d);
162/*
163 * dest = src % d. dest and src may be the same, but not dest and d.
164 * re-entrancy issue: this temporarily modifies d, but restores
165 * it for return.
166 */
167extern int (*bnMod)(struct BigNum *dest, struct BigNum const *src,
168 struct BigNum const *d);
169
170/* return src % d, where 0 <= d < 2^16. */
171extern unsigned int (*bnModQ)(struct BigNum const *src, unsigned d);
172
173/* n = n^exp, modulo "mod" "mod" *must* be odd */
174extern int (*bnExpMod)(struct BigNum *result, struct BigNum const *n,
175 struct BigNum const *exp, struct BigNum const *mod);
176
177/*
178 * dest = n1^e1 * n2^e2, modulo "mod". "mod" *must* be odd.
179 * dest may be the same as n1 or n2.
180 */
181extern int (*bnDoubleExpMod)(struct BigNum *dest,
182 struct BigNum const *n1, struct BigNum const *e1,
183 struct BigNum const *n2, struct BigNum const *e2,
184 struct BigNum const *mod);
185
186/* n = 2^exp, modulo "mod" "mod" *must* be odd */
187extern int (*bnTwoExpMod)(struct BigNum *n, struct BigNum const *exp,
188 struct BigNum const *mod);
189
190/* dest = gcd(a, b). The inputs may overlap arbitrarily. */
191extern int (*bnGcd)(struct BigNum *dest, struct BigNum const *a,
192 struct BigNum const *b);
193
194/* dest = src^-1, modulo "mod". dest may be the same as src. */
195extern int (*bnInv)(struct BigNum *dest, struct BigNum const *src,
196 struct BigNum const *mod);
197
198/* Shift dest left "amt" places */
199extern int (*bnLShift)(struct BigNum *dest, unsigned amt);
200/* Shift dest right "amt" places, discarding low-order bits */
201extern void (*bnRShift)(struct BigNum *dest, unsigned amt);
202
203/* For the largest 2^k that divides n, divide n by it and return k. */
204extern unsigned (*bnMakeOdd)(struct BigNum *n);
205
206/*
207 * Precomputed data for rapid base^exp (mod mod) computation with fixed
208 * base and mod.
209 */
210struct BnBasePrecomp {
211 void *array; /* Ponter to array of pointers to words */
212 unsigned msize; /* Words in modulis (normalized) */
213 unsigned bits; /* Bits per array element */
214 unsigned maxebits; /* Maximum exponent bits */
215 unsigned entries; /* Number of entries */
216 unsigned arraysize;
217};
218
219extern int (*bnBasePrecompBegin)(struct BnBasePrecomp *pre,
220 struct BigNum const *base, struct BigNum const *mod,
221 unsigned maxebits);
222extern void (*bnBasePrecompEnd)(struct BnBasePrecomp *pre);
223extern int (*bnBasePrecompExpMod)(struct BigNum *dest,
224 struct BnBasePrecomp const *pre, struct BigNum const *exp,
225 struct BigNum const *mod);
226extern int (*bnDoubleBasePrecompExpMod)(struct BigNum *dest,
227 struct BnBasePrecomp const *pre1, struct BigNum const *exp1,
228 struct BnBasePrecomp const *pre2, struct BigNum const *exp2,
229 struct BigNum const *mod);
230#endif /* SWIF */
231
232#ifdef __cplusplus
233}
234#endif
235
236#endif/* !BN_H */