a070675a68b7324867c1def23ecddb4aaa0455ff
[u/mdw/catacomb] / mp-arith.c
1 /* -*-c-*-
2 *
3 * $Id: mp-arith.c,v 1.14 2002/10/15 19:18:31 mdw Exp $
4 *
5 * Basic arithmetic on multiprecision integers
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-arith.c,v $
33 * Revision 1.14 2002/10/15 19:18:31 mdw
34 * New operation to negate numbers.
35 *
36 * Revision 1.13 2002/10/15 00:19:40 mdw
37 * Bit setting and clearing functions.
38 *
39 * Revision 1.12 2002/10/09 00:36:03 mdw
40 * Fix bounds on workspace for Karatsuba operations.
41 *
42 * Revision 1.11 2002/10/06 22:52:50 mdw
43 * Pile of changes for supporting two's complement properly.
44 *
45 * Revision 1.10 2001/04/03 19:36:05 mdw
46 * Add some simple bitwise operations so that Perl can use them.
47 *
48 * Revision 1.9 2000/10/08 15:48:35 mdw
49 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
50 *
51 * Revision 1.8 2000/10/08 12:02:21 mdw
52 * Use @MP_EQ@ instead of @MP_CMP@.
53 *
54 * Revision 1.7 2000/06/22 19:02:53 mdw
55 * New function @mp_odd@ to extract powers of two from an integer. This is
56 * common code from the Rabin-Miller test, RSA key recovery and modular
57 * square-root extraction.
58 *
59 * Revision 1.6 2000/06/17 11:45:09 mdw
60 * Major memory management overhaul. Added arena support. Use the secure
61 * arena for secret integers. Replace and improve the MP management macros
62 * (e.g., replace MP_MODIFY by MP_DEST).
63 *
64 * Revision 1.5 1999/12/22 15:54:41 mdw
65 * Adjust Karatsuba parameters. Calculate destination size better.
66 *
67 * Revision 1.4 1999/12/13 15:35:16 mdw
68 * Slightly different rules on memory allocation.
69 *
70 * Revision 1.3 1999/12/11 10:57:43 mdw
71 * Karatsuba squaring algorithm.
72 *
73 * Revision 1.2 1999/12/10 23:18:39 mdw
74 * Change interface for suggested destinations.
75 *
76 * Revision 1.1 1999/11/17 18:02:16 mdw
77 * New multiprecision integer arithmetic suite.
78 *
79 */
80
81 /*----- Header files ------------------------------------------------------*/
82
83 #include "mp.h"
84
85 /*----- Macros ------------------------------------------------------------*/
86
87 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
88
89 /*----- Main code ---------------------------------------------------------*/
90
91 /* --- @mp_lsl@, @mp_lsr@ --- *
92 *
93 * Arguments: @mp *d@ = destination
94 * @mp *a@ = source
95 * @size_t n@ = number of bits to move
96 *
97 * Returns: Result, @a@ shifted left or right by @n@.
98 */
99
100 mp *mp_lsl(mp *d, mp *a, size_t n)
101 {
102 MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f);
103 mpx_lsl(d->v, d->vl, a->v, a->vl, n);
104 d->f = a->f & (MP_NEG | MP_BURN);
105 MP_SHRINK(d);
106 return (d);
107 }
108
109 mp *mp_lsr(mp *d, mp *a, size_t n)
110 {
111 MP_DEST(d, MP_LEN(a), a->f);
112 mpx_lsr(d->v, d->vl, a->v, a->vl, n);
113 d->f = a->f & (MP_NEG | MP_BURN);
114 MP_SHRINK(d);
115 return (d);
116 }
117
118 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
119 *
120 * Arguments: @mp *d@ = destination
121 * @mp *a@ = source
122 * @size_t n@ = number of bits to move
123 *
124 * Returns: Result, @a@ shifted left or right by @n@. Handles the
125 * pretence of sign-extension for negative numbers.
126 */
127
128 mp *mp_lsl2c(mp *d, mp *a, size_t n)
129 {
130 if (!(a->f & MP_NEG))
131 return (mp_lsl(d, a, n));
132 d = mp_not2c(d, a);
133 d = mp_lsl(d, d, n);
134 d = mp_not2c(d, d);
135 return (d);
136 }
137
138 mp *mp_lsr2c(mp *d, mp *a, size_t n)
139 {
140 if (!(a->f & MP_NEG))
141 return (mp_lsr(d, a, n));
142 d = mp_not2c(d, a);
143 d = mp_lsr(d, d, n);
144 d = mp_not2c(d, d);
145 return (d);
146 }
147
148 /* --- @mp_testbit@ --- *
149 *
150 * Arguments: @mp *x@ = a large integer
151 * @unsigned long n@ = which bit to test
152 *
153 * Returns: Nonzero if the bit is set, zero if not.
154 */
155
156 int mp_testbit(mp *x, unsigned long n)
157 {
158 if (n > MPW_BITS * MP_LEN(x))
159 return (0);
160 return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u);
161 }
162
163 /* --- @mp_testbit2c@ --- *
164 *
165 * Arguments: @mp *x@ = a large integer
166 * @unsigned long n@ = which bit to test
167 *
168 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
169 * complement representation.
170 */
171
172 int mp_testbit2c(mp *x, unsigned long n)
173 {
174 int r;
175 if (!(x->f & MP_NEG))
176 return (mp_testbit(x, n));
177 x = mp_not2c(MP_NEW, x);
178 r = !mp_testbit(x, n);
179 MP_DROP(x);
180 return (r);
181 }
182
183 /* --- @mp_setbit@, @mp_clearbit@ --- *
184 *
185 * Arguments: @mp *d@ = a destination
186 * @mp *x@ = a large integer
187 * @unsigned long n@ = which bit to modify
188 *
189 * Returns: The argument @x@, with the appropriate bit set or cleared.
190 */
191
192 mp *mp_setbit(mp *d, mp *x, unsigned long n)
193 {
194 size_t rq;
195
196 rq = n + MPW_BITS; rq -= rq % MPW_BITS;
197 if (d != x) {
198 if (d) MP_DROP(d);
199 d = MP_COPY(x);
200 }
201 MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN));
202 d->v[n/MPW_BITS] |= 1 << n%MPW_BITS;
203 return (d);
204 }
205
206 mp *mp_clearbit(mp *d, mp *x, unsigned long n)
207 {
208 size_t rq;
209
210 rq = n + MPW_BITS; rq -= rq % MPW_BITS;
211 if (d != x) {
212 if (d) MP_DROP(d);
213 d = MP_COPY(x);
214 }
215 MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN));
216 d->v[n/MPW_BITS] &= ~(1 << n%MPW_BITS);
217 return (d);
218 }
219
220 /* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
221 *
222 * Arguments: @mp *d@ = a destination
223 * @mp *x@ = a large integer
224 * @unsigned long n@ = which bit to modify
225 *
226 * Returns: The argument @x@, with the appropriate bit set or cleared.
227 * Fakes up two's complement representation.
228 */
229
230 mp *mp_setbit2c(mp *d, mp *x, unsigned long n)
231 {
232 if (!(x->f & MP_NEG))
233 return mp_setbit(d, x, n);
234 d = mp_not2c(d, x);
235 d = mp_clearbit(d, d, n);
236 d = mp_not2c(d, d);
237 return (d);
238 }
239
240 mp *mp_clearbit2c(mp *d, mp *x, unsigned long n)
241 {
242 if (!(x->f & MP_NEG))
243 return mp_clearbit(d, x, n);
244 d = mp_not2c(d, x);
245 d = mp_setbit(d, d, n);
246 d = mp_not2c(d, d);
247 return (d);
248 }
249
250 /* --- @mp_eq@ --- *
251 *
252 * Arguments: @const mp *a, *b@ = two numbers
253 *
254 * Returns: Nonzero if the numbers are equal.
255 */
256
257 int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); }
258
259 /* --- @mp_cmp@ --- *
260 *
261 * Arguments: @const mp *a, *b@ = two numbers
262 *
263 * Returns: Less than, equal to or greater than zero, according to
264 * whether @a@ is less than, equal to or greater than @b@.
265 */
266
267 int mp_cmp(const mp *a, const mp *b)
268 {
269 if (!((a->f ^ b->f) & MP_NEG))
270 return (mpx_ucmp(a->v, a->vl, b->v, b->vl));
271 else if (a->f & MP_NEG)
272 return (-1);
273 else
274 return (+1);
275 }
276
277 /* --- @mp_neg@ --- *
278 *
279 * Arguments: @mp *d@ = destination
280 * @mp *a@ = argument
281 *
282 * Returns: The negation of the argument.
283 *
284 * Use: Negates its argument.
285 */
286
287 mp *mp_neg(mp *d, mp *a)
288 {
289 /* --- Surprising amounts of messing about required --- */
290
291 MP_SHRINK(a);
292 MP_COPY(a);
293 if (d) MP_DROP(d);
294 if (a->v == a->vl) {
295 return (a);
296 }
297 MP_DEST(a, MP_LEN(a), a->f);
298 a->f ^= MP_NEG;
299 return (a);
300 }
301
302 /* --- @mp_bitop@ --- *
303 *
304 * Arguments: @mp *d@ = destination
305 * @mp *a, *b@ = sources
306 *
307 * Returns: The result of the given bitwise operation. These functions
308 * don't handle negative numbers at all sensibly. For that, use
309 * the @...2c@ variants. The functions are named after the
310 * truth tables they generate:
311 *
312 * a: 0011
313 * b: 0101
314 * @mpx_bitXXXX@
315 */
316
317 #define MP_BITBINOP(string) \
318 \
319 mp *mp_bit##string(mp *d, mp *a, mp *b) \
320 { \
321 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \
322 mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \
323 d->f = (a->f | b->f) & MP_BURN; \
324 MP_SHRINK(d); \
325 return (d); \
326 }
327
328 MPX_DOBIN(MP_BITBINOP)
329
330 /* --- @mp_not@ --- *
331 *
332 * Arguments: @mp *d@ = destination
333 * @mp *a@ = source
334 *
335 * Returns: The bitwise complement of the source.
336 */
337
338 mp *mp_not(mp *d, mp *a)
339 {
340 MP_DEST(d, MP_LEN(a), a->f);
341 mpx_not(d->v, d->vl, a->v, a->vl);
342 d->f = a->f & MP_BURN;
343 MP_SHRINK(d);
344 return (d);
345 }
346
347 /* --- @mp_bitop2c@ --- *
348 *
349 * Arguments: @mp *d@ = destination
350 * @mp *a, *b@ = sources
351 *
352 * Returns: The result of the given bitwise operation. Negative numbers
353 * are treated as two's complement, sign-extended infinitely to
354 * the left. The functions are named after the truth tables
355 * they generate:
356 *
357 * a: 0011
358 * b: 0101
359 * @mpx_bitXXXX@
360 */
361
362 /* --- How this actually works --- *
363 *
364 * The two arguments are inverted (with a sign-swap) if they're currently
365 * negative. This means that we end up using a different function (one which
366 * reinverts as we go) for the main operation. Also, if the sign would be
367 * negative at the end, we preinvert the output and then invert again with a
368 * sign-swap.
369 *
370 * Start with: wxyz WXYZ
371 * If @a@ negative: yzwx or YZWX
372 * If @b@ negative: xwzy XWZY
373 * If both negative: zyxw ZYXW
374 */
375
376 #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \
377 \
378 mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \
379 { \
380 if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \
381 d = mp_bit##base(d, a, b); \
382 p_base \
383 } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \
384 MP_COPY(b); \
385 d = mp_not2c(d, a); \
386 d = mp_bit##an(d, d, b); \
387 MP_DROP(b); \
388 p_an \
389 } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \
390 MP_COPY(a); \
391 d = mp_not2c(d, b); \
392 d = mp_bit##bn(d, a, d); \
393 MP_DROP(a); \
394 p_bn \
395 } else { /* Both negative */ \
396 mp *t = mp_not2c(MP_NEW, a); \
397 mp *d = mp_not2c(d, b); \
398 d = mp_bit##abn(d, t, d); \
399 MP_DROP(t); \
400 p_abn \
401 } \
402 return (d); \
403 } \
404
405 #define NEG d = mp_not2c(d, d);
406 #define POS
407 MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS, POS, POS, POS)
408 MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS, POS, POS, NEG)
409 MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS, NEG, POS, POS)
410 MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS, NEG, POS, NEG)
411 MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS, POS, NEG, POS)
412 MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS, POS, NEG, NEG)
413 MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS, NEG, NEG, POS)
414 MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS, NEG, NEG, NEG)
415 MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG, POS, POS, POS)
416 MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG, POS, POS, NEG)
417 MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG, NEG, POS, POS)
418 MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG, NEG, POS, NEG)
419 MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG, POS, NEG, POS)
420 MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG, POS, NEG, NEG)
421 MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG, NEG, NEG, POS)
422 MP_BIT2CBINOP(1111, 0000, 0000, 0000, 0000, NEG, NEG, NEG, NEG)
423 #undef NEG
424 #undef POS
425
426 /* --- @mp_not2c@ --- *
427 *
428 * Arguments: @mp *d@ = destination
429 * @mp *a@ = source
430 *
431 * Returns: The sign-extended complement of the argument.
432 */
433
434 mp *mp_not2c(mp *d, mp *a)
435 {
436 mpw one = 1;
437
438 MP_DEST(d, MP_LEN(a) + 1, a->f);
439 if (d == a) {
440 if (a->f & MP_NEG)
441 MPX_USUBN(d->v, d->vl, 1);
442 else
443 MPX_UADDN(d->v, d->vl, 1);
444 } else {
445 if (a->f & MP_NEG)
446 mpx_usub(d->v, d->vl, a->v, a->vl, &one, &one + 1);
447 else
448 mpx_uadd(d->v, d->vl, a->v, a->vl, &one, &one + 1);
449 }
450 d->f = (a->f & (MP_NEG | MP_BURN)) ^ MP_NEG;
451 MP_SHRINK(d);
452 return (d);
453 }
454
455 /* --- @mp_add@ --- *
456 *
457 * Arguments: @mp *d@ = destination
458 * @mp *a, *b@ = sources
459 *
460 * Returns: Result, @a@ added to @b@.
461 */
462
463 mp *mp_add(mp *d, mp *a, mp *b)
464 {
465 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
466 if (!((a->f ^ b->f) & MP_NEG))
467 mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
468 else {
469 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
470 mp *t = a; a = b; b = t;
471 }
472 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
473 }
474 d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG);
475 MP_SHRINK(d);
476 return (d);
477 }
478
479 /* --- @mp_sub@ --- *
480 *
481 * Arguments: @mp *d@ = destination
482 * @mp *a, *b@ = sources
483 *
484 * Returns: Result, @b@ subtracted from @a@.
485 */
486
487 mp *mp_sub(mp *d, mp *a, mp *b)
488 {
489 unsigned sgn = 0;
490 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
491 if ((a->f ^ b->f) & MP_NEG)
492 mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
493 else {
494 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
495 mp *t = a; a = b; b = t;
496 sgn = MP_NEG;
497 }
498 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
499 }
500 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG);
501 MP_SHRINK(d);
502 return (d);
503 }
504
505 /* --- @mp_mul@ --- *
506 *
507 * Arguments: @mp *d@ = destination
508 * @mp *a, *b@ = sources
509 *
510 * Returns: Result, @a@ multiplied by @b@.
511 */
512
513 mp *mp_mul(mp *d, mp *a, mp *b)
514 {
515 a = MP_COPY(a);
516 b = MP_COPY(b);
517
518 if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= MPK_THRESH) {
519 MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF);
520 mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
521 } else {
522 size_t m = MAX(MP_LEN(a), MP_LEN(b));
523 mpw *s;
524 MP_DEST(d, 3 * m, a->f | b->f | MP_UNDEF);
525 s = mpalloc(d->a, 5 * m);
526 mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + 5 * m);
527 mpfree(d->a, s);
528 }
529
530 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
531 MP_SHRINK(d);
532 MP_DROP(a);
533 MP_DROP(b);
534 return (d);
535 }
536
537 /* --- @mp_sqr@ --- *
538 *
539 * Arguments: @mp *d@ = destination
540 * @mp *a@ = source
541 *
542 * Returns: Result, @a@ squared.
543 */
544
545 mp *mp_sqr(mp *d, mp *a)
546 {
547 size_t m = MP_LEN(a);
548
549 a = MP_COPY(a);
550 if (m > MPK_THRESH) {
551 mpw *s;
552 MP_DEST(d, 3 * m, a->f | MP_UNDEF);
553 s = mpalloc(d->a, 5 * m);
554 mpx_ksqr(d->v, d->vl, a->v, a->vl, s, s + 5 * m);
555 mpfree(d->a, s);
556 } else {
557 MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF);
558 mpx_usqr(d->v, d->vl, a->v, a->vl);
559 }
560 d->f = a->f & MP_BURN;
561 MP_SHRINK(d);
562 MP_DROP(a);
563 return (d);
564 }
565
566 /* --- @mp_div@ --- *
567 *
568 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
569 * @mp *a, *b@ = sources
570 *
571 * Use: Calculates the quotient and remainder when @a@ is divided by
572 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
573 * Either of @qq@ or @rr@ may be null to indicate that the
574 * result is irrelevant. (Discarding both results is silly.)
575 * There is a performance advantage if @a == *rr@.
576 *
577 * The behaviour when @a@ and @b@ have the same sign is
578 * straightforward. When the signs differ, this implementation
579 * chooses @r@ to have the same sign as @b@, rather than the
580 * more normal choice that the remainder has the same sign as
581 * the dividend. This makes modular arithmetic a little more
582 * straightforward.
583 */
584
585 void mp_div(mp **qq, mp **rr, mp *a, mp *b)
586 {
587 mp *r = rr ? *rr : MP_NEW;
588 mp *q = qq ? *qq : MP_NEW;
589 mpw *sv, *svl;
590
591 /* --- Set the remainder up right --- *
592 *
593 * Just in case the divisor is larger, be able to cope with this. It's not
594 * important in @mpx_udiv@, but it is here because of the sign correction.
595 */
596
597 b = MP_COPY(b);
598 a = MP_COPY(a);
599 if (r)
600 MP_DROP(r);
601 r = a;
602 MP_DEST(r, MP_LEN(a) + 2, a->f | b->f);
603
604 /* --- Fix up the quotient too --- */
605
606 r = MP_COPY(r);
607 MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF);
608 MP_DROP(r);
609
610 /* --- Set up some temporary workspace --- */
611
612 {
613 size_t rq = MP_LEN(b) + 1;
614 sv = mpalloc(r->a, rq);
615 svl = sv + rq;
616 }
617
618 /* --- Perform the calculation --- */
619
620 mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl);
621
622 /* --- Sort out the sign of the results --- *
623 *
624 * If the signs of the arguments differ, and the remainder is nonzero, I
625 * must add one to the absolute value of the quotient and subtract the
626 * remainder from @b@.
627 */
628
629 q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG);
630 if (q->f & MP_NEG) {
631 mpw *v;
632 for (v = r->v; v < r->vl; v++) {
633 if (*v) {
634 MPX_UADDN(q->v, q->vl, 1);
635 mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl);
636 break;
637 }
638 }
639 }
640
641 r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG);
642
643 /* --- Store the return values --- */
644
645 mpfree(r->a, sv);
646 MP_DROP(b);
647
648 if (!qq)
649 MP_DROP(q);
650 else {
651 MP_SHRINK(q);
652 *qq = q;
653 }
654
655 if (!rr)
656 MP_DROP(r);
657 else {
658 MP_SHRINK(r);
659 *rr = r;
660 }
661 }
662
663 /* --- @mp_odd@ --- *
664 *
665 * Arguments: @mp *d@ = pointer to destination integer
666 * @mp *m@ = pointer to source integer
667 * @size_t *s@ = where to store the power of 2
668 *
669 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
670 *
671 * Use: Computes a power of two and an odd integer which, when
672 * multiplied, give a specified result. This sort of thing is
673 * useful in number theory quite often.
674 */
675
676 mp *mp_odd(mp *d, mp *m, size_t *s)
677 {
678 size_t ss = 0;
679 const mpw *v, *vl;
680
681 v = m->v;
682 vl = m->vl;
683 for (; !*v && v < vl; v++)
684 ss += MPW_BITS;
685 if (v >= vl)
686 ss = 0;
687 else {
688 mpw x = *v;
689 mpw mask = MPW_MAX;
690 unsigned z = MPW_BITS / 2;
691
692 while (z) {
693 mask >>= z;
694 if (!(x & mask)) {
695 x >>= z;
696 ss += z;
697 }
698 z >>= 1;
699 }
700 }
701
702 *s = ss;
703 return (mp_lsr(d, m, ss));
704 }
705
706 /*----- Test rig ----------------------------------------------------------*/
707
708 #ifdef TEST_RIG
709
710 static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
711 {
712 if (!MP_EQ(expect, result)) {
713 fprintf(stderr, "\n*** %s failed", op);
714 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
715 fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10);
716 fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10);
717 fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10);
718 fputc('\n', stderr);
719 return (0);
720 }
721 return (1);
722 }
723
724 #define RIG(name, op) \
725 static int t##name(dstr *v) \
726 { \
727 mp *a = *(mp **)v[0].buf; \
728 mpw n = *(int *)v[1].buf; \
729 mp b; \
730 mp *r = *(mp **)v[2].buf; \
731 mp *c = op(MP_NEW, a, n); \
732 int ok; \
733 mp_build(&b, &n, &n + 1); \
734 ok = verify(#name, r, c, a, &b); \
735 mp_drop(a); mp_drop(c); mp_drop(r); \
736 assert(mparena_count(MPARENA_GLOBAL) == 0); \
737 return (ok); \
738 }
739
740 RIG(lsl, mp_lsl)
741 RIG(lsr, mp_lsr)
742 RIG(lsl2c, mp_lsl2c)
743 RIG(lsr2c, mp_lsr2c)
744
745 #undef RIG
746
747 #define RIG(name, op) \
748 static int t##name(dstr *v) \
749 { \
750 mp *a = *(mp **)v[0].buf; \
751 mp *b = *(mp **)v[1].buf; \
752 mp *r = *(mp **)v[2].buf; \
753 mp *c = op(MP_NEW, a, b); \
754 int ok = verify(#name, r, c, a, b); \
755 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
756 assert(mparena_count(MPARENA_GLOBAL) == 0); \
757 return (ok); \
758 }
759
760 RIG(add, mp_add)
761 RIG(sub, mp_sub)
762 RIG(mul, mp_mul)
763
764 #undef RIG
765
766 static int tdiv(dstr *v)
767 {
768 mp *a = *(mp **)v[0].buf;
769 mp *b = *(mp **)v[1].buf;
770 mp *q = *(mp **)v[2].buf;
771 mp *r = *(mp **)v[3].buf;
772 mp *c = MP_NEW, *d = MP_NEW;
773 int ok = 1;
774 mp_div(&c, &d, a, b);
775 ok &= verify("div(quotient)", q, c, a, b);
776 ok &= verify("div(remainder)", r, d, a, b);
777 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
778 assert(mparena_count(MPARENA_GLOBAL) == 0);
779 return (ok);
780 }
781
782 static int tbin(dstr *v)
783 {
784 static mp *(*fn[])(mp *, mp *, mp *) = {
785 #define DO(string) mp_bit##string##2c,
786 MPX_DOBIN(DO)
787 #undef DO
788 };
789 int ok = 1;
790 unsigned op = 0;
791 mp *a = *(mp **)v[1].buf;
792 mp *b = *(mp **)v[2].buf;
793 mp *r = *(mp **)v[3].buf;
794 mp *c;
795
796 if (strcmp(v[0].buf, "and") == 0) op = 1;
797 else if (strcmp(v[0].buf, "or") == 0) op = 7;
798 else if (strcmp(v[0].buf, "nand") == 0) op = 14;
799 else if (strcmp(v[0].buf, "nor") == 0) op = 8;
800 else if (strcmp(v[0].buf, "xor") == 0) op = 6;
801 else {
802 char *p = v[0].buf;
803 while (*p) {
804 op <<= 1;
805 if (*p++ == '1')
806 op |= 1;
807 }
808 }
809
810 c = fn[op](MP_NEW, a, b);
811 ok = verify(v[0].buf, r, c, a, b);
812 mp_drop(a); mp_drop(b); mp_drop(r); mp_drop(c);
813 assert(mparena_count(MPARENA_GLOBAL) == 0);
814 return (ok);
815 }
816
817 static int tset(dstr *v)
818 {
819 mp *a = *(mp **)v[0].buf;
820 unsigned long n = *(unsigned long *)v[1].buf;
821 mp *r = *(mp **)v[2].buf;
822 mp *c;
823 int ok = 1;
824
825 c = mp_setbit2c(MP_NEW, a, n);
826 if (!MP_EQ(c, r)) {
827 ok = 0;
828 fprintf(stderr, "\n***setbit (set) failed");
829 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
830 fprintf(stderr, "\n*** n = %lu", n);
831 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
832 fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16);
833 fputc('\n', stderr);
834 }
835 if (!mp_testbit2c(r, n)) {
836 ok = 0;
837 fprintf(stderr, "\n***setbit (test) failed");
838 fprintf(stderr, "\n*** n = %lu", n);
839 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
840 fputc('\n', stderr);
841 }
842 mp_drop(a);
843 mp_drop(r);
844 mp_drop(c);
845 assert(mparena_count(MPARENA_GLOBAL) == 0);
846 return (ok);
847 }
848
849 static int tclr(dstr *v)
850 {
851 mp *a = *(mp **)v[0].buf;
852 unsigned long n = *(unsigned long *)v[1].buf;
853 mp *r = *(mp **)v[2].buf;
854 mp *c;
855 int ok = 1;
856
857 c = mp_clearbit2c(MP_NEW, a, n);
858 if (!MP_EQ(c, r)) {
859 ok = 0;
860 fprintf(stderr, "\n***clrbit (set) failed");
861 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16);
862 fprintf(stderr, "\n*** n = %lu", n);
863 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
864 fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16);
865 fputc('\n', stderr);
866 }
867 if (mp_testbit2c(r, n)) {
868 ok = 0;
869 fprintf(stderr, "\n***clrbit (test) failed");
870 fprintf(stderr, "\n*** n = %lu", n);
871 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16);
872 fputc('\n', stderr);
873 }
874 mp_drop(a);
875 mp_drop(c);
876 mp_drop(r);
877 assert(mparena_count(MPARENA_GLOBAL) == 0);
878 return (ok);
879 }
880
881 static int tneg(dstr *v)
882 {
883 mp *a = *(mp **)v[0].buf;
884 mp *r = *(mp **)v[1].buf;
885 int ok = 1;
886 mp *n = mp_neg(MP_NEW, a);
887 if (!MP_EQ(r, n)) {
888 ok = 0;
889 fprintf(stderr, "\n*** neg failed\n");
890 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
891 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10);
892 fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10);
893 fputc('\n', stderr);
894 }
895 mp_drop(n);
896 n = mp_neg(a, a);
897 if (!MP_EQ(r, n)) {
898 ok = 0;
899 fprintf(stderr, "\n*** neg failed\n");
900 fputs("\n*** a* = ", stderr); mp_writefile(a, stderr, 10);
901 fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10);
902 fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10);
903 fputc('\n', stderr);
904 }
905 mp_drop(a);
906 mp_drop(r);
907 assert(mparena_count(MPARENA_GLOBAL) == 0);
908 return (ok);
909 }
910
911 static int todd(dstr *v)
912 {
913 mp *a = *(mp **)v[0].buf;
914 size_t rs = *(uint32 *)v[1].buf;
915 mp *rt = *(mp **)v[2].buf;
916 int ok = 1;
917 mp *t;
918 size_t s;
919 t = mp_odd(MP_NEW, a, &s);
920 if (s != rs || !MP_EQ(t, rt)) {
921 ok = 0;
922 fprintf(stderr, "\n*** odd failed");
923 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
924 fprintf(stderr, "\n*** s = %lu", (unsigned long)s);
925 fputs("\n*** t = ", stderr); mp_writefile(t, stderr, 10);
926 fprintf(stderr, "\n*** rs = %lu", (unsigned long)rs);
927 fputs("\n*** rt = ", stderr); mp_writefile(rt, stderr, 10);
928 fputc('\n', stderr);
929 }
930 mp_drop(a);
931 mp_drop(rt);
932 mp_drop(t);
933 assert(mparena_count(MPARENA_GLOBAL) == 0);
934 return (ok);
935 }
936
937 static test_chunk tests[] = {
938 { "lsl", tlsl, { &type_mp, &type_int, &type_mp, 0 } },
939 { "lsr", tlsr, { &type_mp, &type_int, &type_mp, 0 } },
940 { "lsl2c", tlsl2c, { &type_mp, &type_int, &type_mp, 0 } },
941 { "lsr2c", tlsr2c, { &type_mp, &type_int, &type_mp, 0 } },
942 { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } },
943 { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } },
944 { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } },
945 { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } },
946 { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } },
947 { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
948 { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } },
949 { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } },
950 { "neg", tneg, { &type_mp, &type_mp, 0 } },
951 { 0, 0, { 0 } },
952 };
953
954 int main(int argc, char *argv[])
955 {
956 sub_init();
957 test_run(argc, argv, tests, SRCDIR "/tests/mp");
958 return (0);
959 }
960
961 #endif
962
963 /*----- That's all, folks -------------------------------------------------*/