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