d03ab969 |
1 | /* -*-c-*- |
2 | * |
5fbe3846 |
3 | * $Id: mp.h,v 1.9 2000/07/29 17:03:31 mdw Exp $ |
d03ab969 |
4 | * |
d3409d5e |
5 | * Simple multiprecision arithmetic |
d03ab969 |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: mp.h,v $ |
5fbe3846 |
33 | * Revision 1.9 2000/07/29 17:03:31 mdw |
34 | * Add support for left-to-right bitscanning, for use in modular |
35 | * exponentiation. |
36 | * |
22a073c0 |
37 | * Revision 1.8 2000/06/22 19:02:01 mdw |
38 | * Add new functions. |
39 | * |
d34decd2 |
40 | * Revision 1.7 2000/06/17 11:45:09 mdw |
41 | * Major memory management overhaul. Added arena support. Use the secure |
42 | * arena for secret integers. Replace and improve the MP management macros |
43 | * (e.g., replace MP_MODIFY by MP_DEST). |
44 | * |
d9a8ae10 |
45 | * Revision 1.6 1999/12/10 23:19:46 mdw |
46 | * Minor bugfixes. New interface for suggested destinations. |
47 | * |
5b00a0ea |
48 | * Revision 1.5 1999/11/22 20:50:37 mdw |
49 | * Add support for computing Jacobi symbols. |
50 | * |
24e1e862 |
51 | * Revision 1.4 1999/11/21 22:13:02 mdw |
52 | * Add mp version of MPX_BITS. |
53 | * |
a790733d |
54 | * Revision 1.3 1999/11/19 13:19:14 mdw |
55 | * Fix const annotation. |
56 | * |
d3409d5e |
57 | * Revision 1.2 1999/11/17 18:02:16 mdw |
58 | * New multiprecision integer arithmetic suite. |
d03ab969 |
59 | * |
60 | */ |
61 | |
d9a8ae10 |
62 | #ifndef CATACOMB_MP_H |
63 | #define CATACOMB_MP_H |
d03ab969 |
64 | |
65 | #ifdef __cplusplus |
66 | extern "C" { |
67 | #endif |
68 | |
69 | /*----- Header files ------------------------------------------------------*/ |
70 | |
d3409d5e |
71 | #include <assert.h> |
d03ab969 |
72 | #include <string.h> |
73 | |
d3409d5e |
74 | #include <mLib/sub.h> |
75 | |
d9a8ae10 |
76 | #ifndef CATACOMB_MPW_H |
d3409d5e |
77 | # include "mpw.h" |
d03ab969 |
78 | #endif |
79 | |
d34decd2 |
80 | #ifndef CATACOMB_ARENA_H |
81 | # include "arena.h" |
82 | #endif |
83 | |
84 | #ifndef CATACOMB_MPARENA_H |
85 | # include "mparena.h" |
86 | #endif |
87 | |
d9a8ae10 |
88 | #ifndef CATACOMB_MPX_H |
d03ab969 |
89 | # include "mpx.h" |
90 | #endif |
91 | |
d03ab969 |
92 | /*----- Data structures ---------------------------------------------------*/ |
93 | |
94 | typedef struct mp { |
d3409d5e |
95 | mpw *v, *vl; |
96 | size_t sz; |
d34decd2 |
97 | mparena *a; |
d3409d5e |
98 | unsigned f; |
99 | unsigned ref; |
d03ab969 |
100 | } mp; |
101 | |
d3409d5e |
102 | #define MP_NEG 1u |
103 | #define MP_BURN 2u |
104 | #define MP_CONST 4u |
105 | #define MP_UNDEF 8u |
d9a8ae10 |
106 | #define MP_DESTROYED 16u |
d03ab969 |
107 | |
d3409d5e |
108 | /*----- Useful constants --------------------------------------------------*/ |
d03ab969 |
109 | |
d3409d5e |
110 | extern mp mp_const[]; |
d03ab969 |
111 | |
d3409d5e |
112 | #define MP_ZERO (&mp_const[0]) |
113 | #define MP_ONE (&mp_const[1]) |
114 | #define MP_TWO (&mp_const[2]) |
115 | #define MP_THREE (&mp_const[3]) |
116 | #define MP_FOUR (&mp_const[4]) |
117 | #define MP_FIVE (&mp_const[5]) |
118 | #define MP_TEN (&mp_const[6]) |
d34decd2 |
119 | #define MP_256 (&mp_const[7]) |
120 | #define MP_MONE (&mp_const[8]) |
d03ab969 |
121 | |
d3409d5e |
122 | #define MP_NEW ((mp *)0) |
d34decd2 |
123 | #define MP_NEWSEC (&mp_const[9]) |
d03ab969 |
124 | |
d34decd2 |
125 | /*----- Trivial macros ----------------------------------------------------*/ |
d03ab969 |
126 | |
d34decd2 |
127 | /* --- @MP_LEN@ --- * |
d03ab969 |
128 | * |
d34decd2 |
129 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
d03ab969 |
130 | * |
d34decd2 |
131 | * Returns: Length of the integer, in words. |
d03ab969 |
132 | */ |
133 | |
d34decd2 |
134 | #define MP_LEN(m) ((m)->vl - ((m)->v)) |
d03ab969 |
135 | |
d34decd2 |
136 | /*----- Memory management and reference counting --------------------------*/ |
d3409d5e |
137 | |
d34decd2 |
138 | /* --- @mp_new@ --- * |
d03ab969 |
139 | * |
d34decd2 |
140 | * Arguments: @size_t sz@ = size of vector required |
141 | * @unsigned f@ = flags to set |
d03ab969 |
142 | * |
d34decd2 |
143 | * Returns: Pointer to a new MP structure. |
d03ab969 |
144 | * |
d34decd2 |
145 | * Use: Allocates a new multiprecision integer. The data space is |
146 | * allocated from either the standard global or secret arena, |
147 | * depending on the initial flags requested. |
d03ab969 |
148 | */ |
149 | |
d34decd2 |
150 | extern mp *mp_new(size_t /*sz*/, unsigned /*f*/); |
d03ab969 |
151 | |
d34decd2 |
152 | /* --- @mp_create@ --- * |
d03ab969 |
153 | * |
d34decd2 |
154 | * Arguments: @size_t sz@ = size of vector required |
d03ab969 |
155 | * |
d34decd2 |
156 | * Returns: Pointer to pristine new MP structure with enough memory |
157 | * bolted onto it. |
158 | * |
159 | * Use: Creates a new multiprecision integer with indeterminate |
160 | * contents. The integer has a single reference. |
d03ab969 |
161 | */ |
162 | |
d34decd2 |
163 | extern mp *mp_create(size_t /*sz*/); |
d3409d5e |
164 | |
d34decd2 |
165 | /* --- @mp_createsecure@ --- * |
d03ab969 |
166 | * |
d3409d5e |
167 | * Arguments: @size_t sz@ = size of vector required |
d03ab969 |
168 | * |
d3409d5e |
169 | * Returns: Pointer to pristine new MP structure with enough memory |
170 | * bolted onto it. |
d03ab969 |
171 | * |
d3409d5e |
172 | * Use: Creates a new multiprecision integer with indeterminate |
d34decd2 |
173 | * contents. The integer has a single reference. The integer's |
174 | * data space is allocated from the secure arena. Its burn flag |
175 | * is set. |
d03ab969 |
176 | */ |
177 | |
d34decd2 |
178 | extern mp *mp_createsecure(size_t /*sz*/); |
d03ab969 |
179 | |
d3409d5e |
180 | /* --- @mp_build@ --- * |
d03ab969 |
181 | * |
d3409d5e |
182 | * Arguments: @mp *m@ = pointer to an MP block to fill in |
183 | * @mpw *v@ = pointer to a word array |
184 | * @mpw *vl@ = pointer just past end of array |
d03ab969 |
185 | * |
186 | * Returns: --- |
187 | * |
d3409d5e |
188 | * Use: Creates a multiprecision integer representing some smallish |
189 | * number. You must provide storage for the number and dispose |
190 | * of it when you've finished with it. The number is marked as |
191 | * constant while it exists. |
d03ab969 |
192 | */ |
193 | |
d3409d5e |
194 | extern void mp_build(mp */*m*/, mpw */*v*/, mpw */*vl*/); |
d03ab969 |
195 | |
d3409d5e |
196 | /* --- @mp_destroy@ --- * |
d03ab969 |
197 | * |
d3409d5e |
198 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
d03ab969 |
199 | * |
200 | * Returns: --- |
201 | * |
d3409d5e |
202 | * Use: Destroys a multiprecision integer. The reference count isn't |
203 | * checked. Don't use this function if you don't know what |
204 | * you're doing: use @mp_drop@ instead. |
d03ab969 |
205 | */ |
206 | |
d3409d5e |
207 | extern void mp_destroy(mp */*m*/); |
d03ab969 |
208 | |
d3409d5e |
209 | /* --- @mp_copy@ --- * |
d03ab969 |
210 | * |
d3409d5e |
211 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
d03ab969 |
212 | * |
d3409d5e |
213 | * Returns: A copy of the given multiprecision integer. |
214 | * |
215 | * Use: Copies the given integer. In fact you just get another |
216 | * reference to the same old one again. |
d03ab969 |
217 | */ |
218 | |
d3409d5e |
219 | extern mp *mp_copy(mp */*m*/); |
d03ab969 |
220 | |
d3409d5e |
221 | #define MP_COPY(m) ((m)->ref++, (m)) |
222 | |
223 | /* --- @mp_drop@ --- * |
d03ab969 |
224 | * |
d3409d5e |
225 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
d03ab969 |
226 | * |
227 | * Returns: --- |
228 | * |
d3409d5e |
229 | * Use: Drops a reference to an integer which isn't wanted any more. |
230 | * If there are no more references, the integer is destroyed. |
d03ab969 |
231 | */ |
232 | |
d3409d5e |
233 | extern void mp_drop(mp */*m*/); |
d03ab969 |
234 | |
d3409d5e |
235 | #define MP_DROP(m) do { \ |
236 | mp *_mm = (m); \ |
d34decd2 |
237 | _mm->ref--; \ |
238 | if (_mm->ref == 0 && !(_mm->f & MP_CONST)) \ |
d3409d5e |
239 | mp_destroy(_mm); \ |
240 | } while (0) |
241 | |
242 | /* --- @mp_split@ --- * |
d03ab969 |
243 | * |
d3409d5e |
244 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
d03ab969 |
245 | * |
d3409d5e |
246 | * Returns: A reference to the same integer, possibly with a different |
247 | * address. |
d03ab969 |
248 | * |
d3409d5e |
249 | * Use: Splits off a modifiable version of the integer referred to. |
d03ab969 |
250 | */ |
251 | |
d3409d5e |
252 | extern mp *mp_split(mp */*m*/); |
253 | |
254 | #define MP_SPLIT(m) do { \ |
d34decd2 |
255 | mp *_m = (m); \ |
256 | if ((_m->f & MP_CONST) || _m->ref > 1) { \ |
257 | size_t _len = MP_LEN(_m); \ |
258 | mp *_mm = mp_new(_len, _m->f); \ |
259 | if (!(_m->f & MP_UNDEF)) \ |
260 | memcpy(_mm->v, _m->v, MPWS(_len)); \ |
261 | _m->ref--; \ |
262 | _m = _mm; \ |
d3409d5e |
263 | } \ |
d34decd2 |
264 | (m) = _m; \ |
d3409d5e |
265 | } while (0) |
d03ab969 |
266 | |
d3409d5e |
267 | /* --- @mp_resize@ --- * |
d03ab969 |
268 | * |
d3409d5e |
269 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
270 | * @size_t sz@ = new size |
d03ab969 |
271 | * |
d3409d5e |
272 | * Returns: --- |
d03ab969 |
273 | * |
d3409d5e |
274 | * Use: Resizes the vector containing the integer's digits. The new |
275 | * size must be at least as large as the current integer's |
d34decd2 |
276 | * length. This isn't really intended for client use. |
d03ab969 |
277 | */ |
278 | |
d3409d5e |
279 | extern void mp_resize(mp */*m*/, size_t /*sz*/); |
280 | |
281 | #define MP_RESIZE(m, ssz) do { \ |
282 | mp *_m = (m); \ |
283 | size_t _sz = (ssz); \ |
d34decd2 |
284 | mparena *_a = (_m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; \ |
285 | mpw *_v; \ |
d3409d5e |
286 | size_t _len = MP_LEN(_m); \ |
d34decd2 |
287 | assert(((void)"can't make size less than length", _sz >= _len)); \ |
288 | _v = mpalloc(_a, _sz); \ |
d9a8ae10 |
289 | if (!(_m->f & MP_UNDEF)) \ |
290 | memcpy(_v, _m->v, MPWS(_len)); \ |
d3409d5e |
291 | if (_m->f & MP_BURN) \ |
292 | memset(_m->v, 0, MPWS(_m->sz)); \ |
d34decd2 |
293 | mpfree(_m->a, _m->v); \ |
294 | _m->a = _a; \ |
d3409d5e |
295 | _m->v = _v; \ |
296 | _m->vl = _v + _len; \ |
d3409d5e |
297 | } while (0) |
d03ab969 |
298 | |
d3409d5e |
299 | /* --- @mp_ensure@ --- * |
d03ab969 |
300 | * |
d3409d5e |
301 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
302 | * @size_t sz@ = required size |
d03ab969 |
303 | * |
304 | * Returns: --- |
305 | * |
d3409d5e |
306 | * Use: Ensures that the integer has enough space for @sz@ digits. |
307 | * The value is not changed. |
d03ab969 |
308 | */ |
309 | |
d3409d5e |
310 | extern void mp_ensure(mp */*m*/, size_t /*sz*/); |
311 | |
312 | #define MP_ENSURE(m, ssz) do { \ |
d34decd2 |
313 | mp *_m = (m); \ |
d3409d5e |
314 | size_t _ssz = (ssz); \ |
d34decd2 |
315 | size_t _len = MP_LEN(_m); \ |
316 | if (_ssz >= _len) { \ |
317 | if (_ssz > _m->sz) \ |
318 | mp_resize(_m, _ssz); \ |
319 | if (!(_m->f & MP_UNDEF) && _ssz > _len) \ |
320 | memset(_m->vl, 0, MPWS(_ssz - _len)); \ |
321 | _m->vl = _m->v + _ssz; \ |
322 | } \ |
d3409d5e |
323 | } while (0) |
d03ab969 |
324 | |
d34decd2 |
325 | /* --- @mp_dest@ --- * |
d03ab969 |
326 | * |
d34decd2 |
327 | * Arguments: @mp *m@ = a suggested destination integer |
328 | * @size_t sz@ = size required for result, in digits |
329 | * @unsigned f@ = various flags |
330 | * |
331 | * Returns: A pointer to an appropriate destination. |
332 | * |
333 | * Use: Converts a suggested destination into a real destination with |
334 | * the required properties. If the real destination is @d@, |
335 | * then the following properties will hold: |
336 | * |
337 | * * @d@ will have exactly one reference. |
d03ab969 |
338 | * |
d34decd2 |
339 | * * If @m@ is not @MP_NEW@, then the contents of @m@ will not |
340 | * change, unless @f@ has the @MP_UNDEF@ flag set. |
d03ab969 |
341 | * |
d34decd2 |
342 | * * If @m@ is not @MP_NEW@, then he reference count of @m@ on |
343 | * entry is equal to the sum of the counts of @d@ and @m@ on |
344 | * exit. |
345 | * |
346 | * * The size of @d@ will be at least @sz@. |
347 | * |
348 | * * If @f@ has the @MP_BURN@ flag set, then @d@ will be |
349 | * allocated from @MPARENA_SECURE@. |
350 | * |
351 | * Understanding this function is crucial to using Catacomb's |
352 | * multiprecision integer library effectively. |
d03ab969 |
353 | */ |
354 | |
d34decd2 |
355 | extern mp *mp_dest(mp */*m*/, size_t /*sz*/, unsigned /*f*/); |
d03ab969 |
356 | |
d34decd2 |
357 | #define MP_DEST(m, ssz, f) do { \ |
d3409d5e |
358 | mp *_m = (m); \ |
d34decd2 |
359 | size_t _ssz = (ssz); \ |
360 | unsigned _f = (f); \ |
361 | _m = mp_dest(_m, _ssz, _f); \ |
d3409d5e |
362 | (m) = _m; \ |
363 | } while (0) |
364 | |
365 | /*----- Size manipulation -------------------------------------------------*/ |
366 | |
367 | /* --- @mp_shrink@ --- * |
d03ab969 |
368 | * |
d3409d5e |
369 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
d03ab969 |
370 | * |
371 | * Returns: --- |
372 | * |
d3409d5e |
373 | * Use: Reduces the recorded length of an integer. This doesn't |
374 | * reduce the amount of memory used, although it can improve |
375 | * performance a bit. To reduce memory, use @mp_minimize@ |
376 | * instead. This can't change the value of an integer, and is |
377 | * therefore safe to use even when there are multiple |
378 | * references. |
d03ab969 |
379 | */ |
380 | |
d3409d5e |
381 | extern void mp_shrink(mp */*m*/); |
d03ab969 |
382 | |
d3409d5e |
383 | #define MP_SHRINK(m) do { \ |
384 | mp *_mm = (m); \ |
385 | MPX_SHRINK(_mm->v, _mm->vl); \ |
386 | if (!MP_LEN(_mm)) \ |
387 | _mm->f &= ~MP_NEG; \ |
388 | } while (0) |
389 | |
390 | /* --- @mp_minimize@ --- * |
d03ab969 |
391 | * |
d3409d5e |
392 | * Arguments: @mp *m@ = pointer to a multiprecision integer |
d03ab969 |
393 | * |
394 | * Returns: --- |
395 | * |
d3409d5e |
396 | * Use: Reduces the amount of memory an integer uses. It's best to |
397 | * do this to numbers which aren't going to change in the |
398 | * future. |
d03ab969 |
399 | */ |
400 | |
d3409d5e |
401 | extern void mp_minimize(mp */*m*/); |
d03ab969 |
402 | |
d3409d5e |
403 | /*----- Bit scanning ------------------------------------------------------*/ |
d03ab969 |
404 | |
d9a8ae10 |
405 | #ifndef CATACOMB_MPSCAN_H |
d3409d5e |
406 | # include "mpscan.h" |
407 | #endif |
408 | |
409 | /* --- @mp_scan@ --- * |
d03ab969 |
410 | * |
d3409d5e |
411 | * Arguments: @mpscan *sc@ = pointer to bitscanner block |
412 | * @const mp *m@ = pointer to a multiprecision integer |
d03ab969 |
413 | * |
414 | * Returns: --- |
415 | * |
d3409d5e |
416 | * Use: Initializes a bitscanner on a multiprecision integer. |
d03ab969 |
417 | */ |
418 | |
d3409d5e |
419 | extern void mp_scan(mpscan */*sc*/, const mp */*m*/); |
420 | |
421 | #define MP_SCAN(sc, m) do { \ |
a790733d |
422 | const mp *_mm = (m); \ |
d3409d5e |
423 | mpscan *_sc = (sc); \ |
424 | MPSCAN_INITX(_sc, _mm->v, _mm->vl); \ |
425 | } while (0) |
426 | |
5fbe3846 |
427 | /* --- @mp_rscan@ --- * |
428 | * |
429 | * Arguments: @mpscan *sc@ = pointer to bitscanner block |
430 | * @const mp *m@ = pointer to a multiprecision integer |
431 | * |
432 | * Returns: --- |
433 | * |
434 | * Use: Initializes a reverse bitscanner on a multiprecision |
435 | * integer. |
436 | */ |
437 | |
438 | extern void mp_rscan(mpscan */*sc*/, const mp */*m*/); |
439 | |
440 | #define MP_RSCAN(sc, m) do { \ |
441 | const mp *_mm = (m); \ |
442 | mpscan *_sc = (sc); \ |
443 | MPSCAN_RINITX(_sc, _mm->v, _mm->vl); \ |
444 | } while (0) |
445 | |
d3409d5e |
446 | /* --- Other bitscanning aliases --- */ |
447 | |
448 | #define mp_step mpscan_step |
449 | #define mp_bit mpscan_bit |
5fbe3846 |
450 | #define mp_rstep mpscan_rstep |
451 | #define mp_rbit mpscan_rbit |
d3409d5e |
452 | |
453 | #define MP_STEP MPSCAN_STEP |
454 | #define MP_BIT MPSCAN_BIT |
5fbe3846 |
455 | #define MP_RSTEP MPSCAN_RSTEP |
456 | #define MP_RBIT MPSCAN_RBIT |
d3409d5e |
457 | |
458 | /*----- Loading and storing -----------------------------------------------*/ |
d03ab969 |
459 | |
d3409d5e |
460 | /* --- @mp_octets@ --- * |
d03ab969 |
461 | * |
d3409d5e |
462 | * Arguments: @const mp *m@ = a multiprecision integer |
d03ab969 |
463 | * |
d3409d5e |
464 | * Returns: The number of octets required to represent @m@. |
d03ab969 |
465 | * |
d3409d5e |
466 | * Use: Calculates the external storage required for a multiprecision |
467 | * integer. |
d03ab969 |
468 | */ |
469 | |
24e1e862 |
470 | extern size_t mp_octets(const mp */*m*/); |
471 | |
472 | /* --- @mp_bits@ --- * |
473 | * |
474 | * Arguments: @const mp *m@ = a multiprecision integer |
475 | * |
476 | * Returns: The number of bits required to represent @m@. |
477 | * |
478 | * Use: Calculates the external storage required for a multiprecision |
479 | * integer. |
480 | */ |
481 | |
482 | extern unsigned long mp_bits(const mp */*m*/); |
d03ab969 |
483 | |
d3409d5e |
484 | /* --- @mp_loadl@ --- * |
d03ab969 |
485 | * |
d3409d5e |
486 | * Arguments: @mp *d@ = destination |
487 | * @const void *pv@ = pointer to source data |
488 | * @size_t sz@ = size of the source data |
d03ab969 |
489 | * |
d3409d5e |
490 | * Returns: Resulting multiprecision number. |
d03ab969 |
491 | * |
d3409d5e |
492 | * Use: Loads a multiprecision number from an array of octets. The |
493 | * first byte in the array is the least significant. More |
494 | * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$% |
495 | * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%. |
d03ab969 |
496 | */ |
497 | |
d3409d5e |
498 | extern mp *mp_loadl(mp */*d*/, const void */*pv*/, size_t /*sz*/); |
d03ab969 |
499 | |
d3409d5e |
500 | /* --- @mp_storel@ --- * |
501 | * |
502 | * Arguments: @const mp *m@ = source |
503 | * @void *pv@ = pointer to output array |
504 | * @size_t sz@ = size of the output array |
505 | * |
506 | * Returns: --- |
507 | * |
508 | * Use: Stores a multiprecision number in an array of octets. The |
509 | * first byte in the array is the least significant. If the |
510 | * array is too small to represent the number, high-order bits |
511 | * are truncated; if the array is too large, high order bytes |
512 | * are filled with zeros. More formally, if the number is |
513 | * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%, |
514 | * then the array is %$b_0, b_1, \ldots, b_{n-1}$%. |
515 | */ |
d03ab969 |
516 | |
d3409d5e |
517 | extern void mp_storel(const mp */*m*/, void */*pv*/, size_t /*sz*/); |
d03ab969 |
518 | |
d3409d5e |
519 | /* --- @mp_loadb@ --- * |
d03ab969 |
520 | * |
d3409d5e |
521 | * Arguments: @mp *d@ = destination |
522 | * @const void *pv@ = pointer to source data |
523 | * @size_t sz@ = size of the source data |
d03ab969 |
524 | * |
d3409d5e |
525 | * Returns: Resulting multiprecision number. |
d03ab969 |
526 | * |
d3409d5e |
527 | * Use: Loads a multiprecision number from an array of octets. The |
528 | * last byte in the array is the least significant. More |
529 | * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$% |
530 | * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%. |
d03ab969 |
531 | */ |
532 | |
d3409d5e |
533 | extern mp *mp_loadb(mp */*d*/, const void */*pv*/, size_t /*sz*/); |
d03ab969 |
534 | |
d3409d5e |
535 | /* --- @mp_storeb@ --- * |
d03ab969 |
536 | * |
d3409d5e |
537 | * Arguments: @const mp *m@ = source |
538 | * @void *pv@ = pointer to output array |
539 | * @size_t sz@ = size of the output array |
d03ab969 |
540 | * |
541 | * Returns: --- |
542 | * |
d3409d5e |
543 | * Use: Stores a multiprecision number in an array of octets. The |
544 | * last byte in the array is the least significant. If the |
545 | * array is too small to represent the number, high-order bits |
546 | * are truncated; if the array is too large, high order bytes |
547 | * are filled with zeros. More formally, if the number is |
548 | * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%, |
549 | * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%. |
d03ab969 |
550 | */ |
551 | |
d3409d5e |
552 | extern void mp_storeb(const mp */*m*/, void */*pv*/, size_t /*sz*/); |
d03ab969 |
553 | |
d3409d5e |
554 | /*----- Simple arithmetic -------------------------------------------------*/ |
d03ab969 |
555 | |
d3409d5e |
556 | /* --- @mp_2c@ --- * |
d03ab969 |
557 | * |
d3409d5e |
558 | * Arguments: @mp *d@ = destination |
559 | * @mp *a@ = source |
d03ab969 |
560 | * |
d3409d5e |
561 | * Returns: Result, @a@ converted to two's complement notation. |
562 | */ |
563 | |
564 | extern mp *mp_2c(mp */*d*/, mp */*a*/); |
565 | |
566 | /* --- @mp_sm@ --- * |
567 | * |
568 | * Arguments: @mp *d@ = destination |
569 | * @mp *a@ = source |
d03ab969 |
570 | * |
d3409d5e |
571 | * Returns: Result, @a@ converted to the native signed-magnitude |
572 | * notation. |
d03ab969 |
573 | */ |
574 | |
d3409d5e |
575 | extern mp *mp_sm(mp */*d*/, mp */*a*/); |
d03ab969 |
576 | |
d3409d5e |
577 | /* --- @mp_lsl@ --- * |
d03ab969 |
578 | * |
d3409d5e |
579 | * Arguments: @mp *d@ = destination |
d9a8ae10 |
580 | * @mp *a@ = source |
d3409d5e |
581 | * @size_t n@ = number of bits to move |
d03ab969 |
582 | * |
d3409d5e |
583 | * Returns: Result, @a@ shifted left by @n@. |
584 | */ |
585 | |
d9a8ae10 |
586 | extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/); |
d3409d5e |
587 | |
588 | /* --- @mp_lsr@ --- * |
589 | * |
590 | * Arguments: @mp *d@ = destination |
d9a8ae10 |
591 | * @mp *a@ = source |
d3409d5e |
592 | * @size_t n@ = number of bits to move |
d03ab969 |
593 | * |
d3409d5e |
594 | * Returns: Result, @a@ shifted left by @n@. |
d03ab969 |
595 | */ |
596 | |
d9a8ae10 |
597 | extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/); |
d03ab969 |
598 | |
d3409d5e |
599 | /* --- @mp_cmp@ --- * |
d03ab969 |
600 | * |
d3409d5e |
601 | * Arguments: @const mp *a, *b@ = two numbers |
d03ab969 |
602 | * |
d3409d5e |
603 | * Returns: Less than, equal to or greater than zero, according to |
604 | * whether @a@ is less than, equal to or greater than @b@. |
605 | */ |
606 | |
607 | extern int mp_cmp(const mp */*a*/, const mp */*b*/); |
608 | |
609 | #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0) |
610 | |
611 | /* --- @mp_add@ --- * |
d03ab969 |
612 | * |
d3409d5e |
613 | * Arguments: @mp *d@ = destination |
d9a8ae10 |
614 | * @mp *a, *b@ = sources |
d3409d5e |
615 | * |
616 | * Returns: Result, @a@ added to @b@. |
d03ab969 |
617 | */ |
618 | |
d9a8ae10 |
619 | extern mp *mp_add(mp */*d*/, mp */*a*/, mp */*b*/); |
d03ab969 |
620 | |
d3409d5e |
621 | /* --- @mp_sub@ --- * |
622 | * |
623 | * Arguments: @mp *d@ = destination |
d9a8ae10 |
624 | * @mp *a, *b@ = sources |
d3409d5e |
625 | * |
626 | * Returns: Result, @b@ subtracted from @a@. |
627 | */ |
d03ab969 |
628 | |
d9a8ae10 |
629 | extern mp *mp_sub(mp */*d*/, mp */*a*/, mp */*b*/); |
d3409d5e |
630 | |
631 | /* --- @mp_mul@ --- * |
d03ab969 |
632 | * |
d3409d5e |
633 | * Arguments: @mp *d@ = destination |
d9a8ae10 |
634 | * @mp *a, *b@ = sources |
d03ab969 |
635 | * |
d3409d5e |
636 | * Returns: Result, @a@ multiplied by @b@. |
637 | */ |
638 | |
d9a8ae10 |
639 | extern mp *mp_mul(mp */*d*/, mp */*a*/, mp */*b*/); |
d3409d5e |
640 | |
641 | /* --- @mp_sqr@ --- * |
642 | * |
643 | * Arguments: @mp *d@ = destination |
d9a8ae10 |
644 | * @mp *a@ = source |
d3409d5e |
645 | * |
646 | * Returns: Result, @a@ squared. |
647 | */ |
648 | |
d9a8ae10 |
649 | extern mp *mp_sqr(mp */*d*/, mp */*a*/); |
d3409d5e |
650 | |
651 | /* --- @mp_div@ --- * |
d03ab969 |
652 | * |
d3409d5e |
653 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder |
d9a8ae10 |
654 | * @mp *a, *b@ = sources |
d3409d5e |
655 | * |
656 | * Use: Calculates the quotient and remainder when @a@ is divided by |
657 | * @b@. |
d03ab969 |
658 | */ |
659 | |
d9a8ae10 |
660 | extern void mp_div(mp **/*qq*/, mp **/*rr*/, mp */*a*/, mp */*b*/); |
d3409d5e |
661 | |
22a073c0 |
662 | /* --- @mp_odd@ --- * |
663 | * |
664 | * Arguments: @mp *d@ = pointer to destination integer |
665 | * @mp *m@ = pointer to source integer |
666 | * @size_t *s@ = where to store the power of 2 |
667 | * |
668 | * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%. |
669 | * |
670 | * Use: Computes a power of two and an odd integer which, when |
671 | * multiplied, give a specified result. This sort of thing is |
672 | * useful in number theory quite often. |
673 | */ |
674 | |
675 | extern mp *mp_odd(mp */*d*/, mp */*m*/, size_t */*s*/); |
676 | |
d3409d5e |
677 | /*----- More advanced algorithms ------------------------------------------*/ |
d03ab969 |
678 | |
22a073c0 |
679 | /* --- @mp_sqrt@ --- * |
680 | * |
681 | * Arguments: @mp *d@ = pointer to destination integer |
682 | * @mp *a@ = (nonnegative) integer to take square root of |
683 | * |
684 | * Returns: The largest integer %$x$% such that %$x^2 \le a$%. |
685 | * |
686 | * Use: Computes integer square roots. |
687 | * |
688 | * The current implementation isn't very good: it uses the |
689 | * Newton-Raphson method to find an approximation to %$a$%. If |
690 | * there's any demand for a better version, I'll write one. |
691 | */ |
692 | |
693 | extern mp *mp_sqrt(mp */*d*/, mp */*a*/); |
694 | |
d3409d5e |
695 | /* --- @mp_gcd@ --- * |
d03ab969 |
696 | * |
d3409d5e |
697 | * Arguments: @mp **gcd, **xx, **yy@ = where to write the results |
698 | * @mp *a, *b@ = sources (must be nonzero) |
d03ab969 |
699 | * |
700 | * Returns: --- |
701 | * |
d3409d5e |
702 | * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that |
703 | * @ax + by = gcd(a, b)@. This is useful for computing modular |
d9a8ae10 |
704 | * inverses. Neither @a@ nor @b@ may be zero. |
d03ab969 |
705 | */ |
706 | |
d3409d5e |
707 | extern void mp_gcd(mp **/*gcd*/, mp **/*xx*/, mp **/*yy*/, |
708 | mp */*a*/, mp */*b*/); |
709 | |
5b00a0ea |
710 | /* --- @mp_jacobi@ --- * |
711 | * |
712 | * Arguments: @mp *a@ = an integer less than @n@ |
713 | * @mp *n@ = an odd integer |
714 | * |
715 | * Returns: @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%. |
716 | * |
717 | * Use: Computes the Jacobi symbol. If @n@ is prime, this is the |
718 | * Legendre symbol and is equal to 1 if and only if @a@ is a |
719 | * quadratic residue mod @n@. The result is zero if and only if |
720 | * @a@ and @n@ have a common factor greater than one. |
721 | */ |
722 | |
22a073c0 |
723 | extern int mp_jacobi(mp */*a*/, mp */*n*/); |
724 | |
725 | /* --- @mp_modsqrt@ --- * |
726 | * |
727 | * Arguments: @mp *d@ = destination integer |
728 | * @mp *a@ = source integer |
729 | * @mp *p@ = modulus (must be prime) |
730 | * |
731 | * Returns: If %$a$% is a quadratic residue, a square root of %$a$%; else |
732 | * a null pointer. |
733 | * |
734 | * Use: Returns an integer %$x$% such that %$x^2 \equiv a \pmod{p}$%, |
735 | * if one exists; else a null pointer. This function will not |
736 | * work if %$p$% is composite: you must factor the modulus, take |
737 | * a square root mod each factor, and recombine the results |
738 | * using the Chinese Remainder Theorem. |
739 | */ |
740 | |
741 | extern mp *mp_modsqrt(mp */*d*/, mp */*a*/, mp */*p*/); |
5b00a0ea |
742 | |
d3409d5e |
743 | /*----- Test harness support ----------------------------------------------*/ |
744 | |
745 | #include <mLib/testrig.h> |
746 | |
d9a8ae10 |
747 | #ifndef CATACOMB_MPTEXT_H |
d3409d5e |
748 | # include "mptext.h" |
749 | #endif |
750 | |
751 | extern const test_type type_mp; |
d03ab969 |
752 | |
753 | /*----- That's all, folks -------------------------------------------------*/ |
754 | |
755 | #ifdef __cplusplus |
756 | } |
757 | #endif |
758 | |
759 | #endif |