Reliability fixes.
[u/mdw/catacomb] / mp-arith.c
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
81578196 3 * $Id: mp-arith.c,v 1.16 2003/05/16 09:09:24 mdw Exp $
d3409d5e 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 $
81578196 33 * Revision 1.16 2003/05/16 09:09:24 mdw
34 * Fix @mp_lsl2c@. Turns out to be surprisingly tricky.
35 *
75263f25 36 * Revision 1.15 2002/10/19 17:56:50 mdw
37 * Fix bit operations. Test them (a bit) better.
38 *
397041a9 39 * Revision 1.14 2002/10/15 19:18:31 mdw
40 * New operation to negate numbers.
41 *
09d00c6b 42 * Revision 1.13 2002/10/15 00:19:40 mdw
43 * Bit setting and clearing functions.
44 *
dd22938e 45 * Revision 1.12 2002/10/09 00:36:03 mdw
46 * Fix bounds on workspace for Karatsuba operations.
47 *
f09e814a 48 * Revision 1.11 2002/10/06 22:52:50 mdw
49 * Pile of changes for supporting two's complement properly.
50 *
0f32e0f8 51 * Revision 1.10 2001/04/03 19:36:05 mdw
52 * Add some simple bitwise operations so that Perl can use them.
53 *
52cdaca9 54 * Revision 1.9 2000/10/08 15:48:35 mdw
55 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
56 *
4b536f42 57 * Revision 1.8 2000/10/08 12:02:21 mdw
58 * Use @MP_EQ@ instead of @MP_CMP@.
59 *
f1713c63 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 *
d34decd2 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 *
bba03f55 70 * Revision 1.5 1999/12/22 15:54:41 mdw
71 * Adjust Karatsuba parameters. Calculate destination size better.
72 *
8017495b 73 * Revision 1.4 1999/12/13 15:35:16 mdw
74 * Slightly different rules on memory allocation.
75 *
5bf74dea 76 * Revision 1.3 1999/12/11 10:57:43 mdw
77 * Karatsuba squaring algorithm.
78 *
ef5f4810 79 * Revision 1.2 1999/12/10 23:18:39 mdw
80 * Change interface for suggested destinations.
81 *
d3409d5e 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
ef5f4810 91/*----- Macros ------------------------------------------------------------*/
92
93#define MAX(x, y) ((x) >= (y) ? (x) : (y))
94
d3409d5e 95/*----- Main code ---------------------------------------------------------*/
96
81578196 97/* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- *
d3409d5e 98 *
f09e814a 99 * Arguments: @mp *d@ = destination
100 * @mp *a@ = source
101 * @size_t n@ = number of bits to move
d3409d5e 102 *
f09e814a 103 * Returns: Result, @a@ shifted left or right by @n@.
81578196 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.
d3409d5e 109 */
110
f09e814a 111mp *mp_lsl(mp *d, mp *a, size_t n)
d3409d5e 112{
f09e814a 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}
d3409d5e 119
81578196 120mp *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
f09e814a 129mp *mp_lsr(mp *d, mp *a, size_t n)
130{
d34decd2 131 MP_DEST(d, MP_LEN(a), a->f);
f09e814a 132 mpx_lsr(d->v, d->vl, a->v, a->vl, n);
133 d->f = a->f & (MP_NEG | MP_BURN);
d3409d5e 134 MP_SHRINK(d);
135 return (d);
136}
137
f09e814a 138/* --- @mp_lsl2c@, @mp_lsr2c@ --- *
d3409d5e 139 *
140 * Arguments: @mp *d@ = destination
141 * @mp *a@ = source
f09e814a 142 * @size_t n@ = number of bits to move
d3409d5e 143 *
f09e814a 144 * Returns: Result, @a@ shifted left or right by @n@. Handles the
145 * pretence of sign-extension for negative numbers.
d3409d5e 146 */
147
f09e814a 148mp *mp_lsl2c(mp *d, mp *a, size_t n)
d3409d5e 149{
f09e814a 150 if (!(a->f & MP_NEG))
151 return (mp_lsl(d, a, n));
152 d = mp_not2c(d, a);
81578196 153 d = mp_lslc(d, d, n);
f09e814a 154 d = mp_not2c(d, d);
155 return (d);
156}
d3409d5e 157
f09e814a 158mp *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);
d3409d5e 166}
167
f09e814a 168/* --- @mp_testbit@ --- *
d3409d5e 169 *
f09e814a 170 * Arguments: @mp *x@ = a large integer
09d00c6b 171 * @unsigned long n@ = which bit to test
d3409d5e 172 *
f09e814a 173 * Returns: Nonzero if the bit is set, zero if not.
d3409d5e 174 */
175
09d00c6b 176int mp_testbit(mp *x, unsigned long n)
d3409d5e 177{
f09e814a 178 if (n > MPW_BITS * MP_LEN(x))
179 return (0);
09d00c6b 180 return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u);
d3409d5e 181}
182
f09e814a 183/* --- @mp_testbit2c@ --- *
d3409d5e 184 *
f09e814a 185 * Arguments: @mp *x@ = a large integer
09d00c6b 186 * @unsigned long n@ = which bit to test
d3409d5e 187 *
f09e814a 188 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
189 * complement representation.
d3409d5e 190 */
191
09d00c6b 192int mp_testbit2c(mp *x, unsigned long n)
d3409d5e 193{
f09e814a 194 int r;
09d00c6b 195 if (!(x->f & MP_NEG))
f09e814a 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);
d3409d5e 201}
202
09d00c6b 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
212mp *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
226mp *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
250mp *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
260mp *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
4b536f42 270/* --- @mp_eq@ --- *
271 *
272 * Arguments: @const mp *a, *b@ = two numbers
273 *
274 * Returns: Nonzero if the numbers are equal.
275 */
276
277int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); }
278
d3409d5e 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
287int 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
397041a9 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
307mp *mp_neg(mp *d, mp *a)
308{
309 /* --- Surprising amounts of messing about required --- */
310
311 MP_SHRINK(a);
312 MP_COPY(a);
81578196 313 if (d)
314 MP_DROP(d);
315 if (a->v == a->vl)
397041a9 316 return (a);
397041a9 317 MP_DEST(a, MP_LEN(a), a->f);
318 a->f ^= MP_NEG;
319 return (a);
320}
321
f09e814a 322/* --- @mp_bitop@ --- *
0f32e0f8 323 *
324 * Arguments: @mp *d@ = destination
325 * @mp *a, *b@ = sources
326 *
f09e814a 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@
0f32e0f8 335 */
336
f09e814a 337#define MP_BITBINOP(string) \
0f32e0f8 338 \
f09e814a 339mp *mp_bit##string(mp *d, mp *a, mp *b) \
0f32e0f8 340{ \
75263f25 341 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \
f09e814a 342 mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \
0f32e0f8 343 d->f = (a->f | b->f) & MP_BURN; \
344 MP_SHRINK(d); \
345 return (d); \
346}
347
f09e814a 348MPX_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 */
0f32e0f8 357
358mp *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
f09e814a 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 \
398mp *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
427MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS, POS, POS, POS)
428MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS, POS, POS, NEG)
429MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS, NEG, POS, POS)
430MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS, NEG, POS, NEG)
431MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS, POS, NEG, POS)
432MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS, POS, NEG, NEG)
433MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS, NEG, NEG, POS)
434MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS, NEG, NEG, NEG)
435MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG, POS, POS, POS)
436MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG, POS, POS, NEG)
437MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG, NEG, POS, POS)
438MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG, NEG, POS, NEG)
439MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG, POS, NEG, POS)
440MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG, POS, NEG, NEG)
441MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG, NEG, NEG, POS)
442MP_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
454mp *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
d3409d5e 475/* --- @mp_add@ --- *
476 *
477 * Arguments: @mp *d@ = destination
ef5f4810 478 * @mp *a, *b@ = sources
d3409d5e 479 *
480 * Returns: Result, @a@ added to @b@.
481 */
482
ef5f4810 483mp *mp_add(mp *d, mp *a, mp *b)
d3409d5e 484{
d34decd2 485 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
d3409d5e 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)) {
ef5f4810 490 mp *t = a; a = b; b = t;
d3409d5e 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
ef5f4810 502 * @mp *a, *b@ = sources
d3409d5e 503 *
504 * Returns: Result, @b@ subtracted from @a@.
505 */
506
ef5f4810 507mp *mp_sub(mp *d, mp *a, mp *b)
d3409d5e 508{
509 unsigned sgn = 0;
d34decd2 510 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
d3409d5e 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)) {
ef5f4810 515 mp *t = a; a = b; b = t;
d3409d5e 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
ef5f4810 528 * @mp *a, *b@ = sources
d3409d5e 529 *
530 * Returns: Result, @a@ multiplied by @b@.
531 */
532
ef5f4810 533mp *mp_mul(mp *d, mp *a, mp *b)
d3409d5e 534{
ef5f4810 535 a = MP_COPY(a);
536 b = MP_COPY(b);
537
52cdaca9 538 if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= MPK_THRESH) {
d34decd2 539 MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF);
ef5f4810 540 mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
8017495b 541 } else {
dd22938e 542 size_t m = MAX(MP_LEN(a), MP_LEN(b));
ef5f4810 543 mpw *s;
dd22938e 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);
d34decd2 547 mpfree(d->a, s);
ef5f4810 548 }
549
d3409d5e 550 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
551 MP_SHRINK(d);
ef5f4810 552 MP_DROP(a);
553 MP_DROP(b);
d3409d5e 554 return (d);
555}
556
557/* --- @mp_sqr@ --- *
558 *
559 * Arguments: @mp *d@ = destination
ef5f4810 560 * @mp *a@ = source
d3409d5e 561 *
562 * Returns: Result, @a@ squared.
563 */
564
ef5f4810 565mp *mp_sqr(mp *d, mp *a)
d3409d5e 566{
ef5f4810 567 size_t m = MP_LEN(a);
568
569 a = MP_COPY(a);
52cdaca9 570 if (m > MPK_THRESH) {
ef5f4810 571 mpw *s;
dd22938e 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);
d34decd2 575 mpfree(d->a, s);
dd22938e 576 } else {
577 MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF);
ef5f4810 578 mpx_usqr(d->v, d->vl, a->v, a->vl);
dd22938e 579 }
d3409d5e 580 d->f = a->f & MP_BURN;
581 MP_SHRINK(d);
ef5f4810 582 MP_DROP(a);
d3409d5e 583 return (d);
584}
585
586/* --- @mp_div@ --- *
587 *
588 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
ef5f4810 589 * @mp *a, *b@ = sources
d3409d5e 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
ef5f4810 605void mp_div(mp **qq, mp **rr, mp *a, mp *b)
d3409d5e 606 {
607 mp *r = rr ? *rr : MP_NEW;
608 mp *q = qq ? *qq : MP_NEW;
609 mpw *sv, *svl;
610
d3409d5e 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
d34decd2 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);
d3409d5e 623
624 /* --- Fix up the quotient too --- */
625
d34decd2 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 }
d3409d5e 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
d34decd2 649 q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG);
d3409d5e 650 if (q->f & MP_NEG) {
ef5f4810 651 mpw *v;
652 for (v = r->v; v < r->vl; v++) {
d3409d5e 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
d34decd2 661 r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG);
d3409d5e 662
663 /* --- Store the return values --- */
664
d34decd2 665 mpfree(r->a, sv);
666 MP_DROP(b);
667
d3409d5e 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 }
d3409d5e 681}
682
f1713c63 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
696mp *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
d3409d5e 726/*----- Test rig ----------------------------------------------------------*/
727
728#ifdef TEST_RIG
729
730static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
731{
4b536f42 732 if (!MP_EQ(expect, result)) {
d3409d5e 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) \
ef5f4810 745 static int t##name(dstr *v) \
d3409d5e 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); \
ef5f4810 756 assert(mparena_count(MPARENA_GLOBAL) == 0); \
d3409d5e 757 return (ok); \
758 }
759
760RIG(lsl, mp_lsl)
761RIG(lsr, mp_lsr)
f09e814a 762RIG(lsl2c, mp_lsl2c)
763RIG(lsr2c, mp_lsr2c)
d3409d5e 764
765#undef RIG
766
767#define RIG(name, op) \
ef5f4810 768 static int t##name(dstr *v) \
d3409d5e 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); \
ef5f4810 776 assert(mparena_count(MPARENA_GLOBAL) == 0); \
d3409d5e 777 return (ok); \
778 }
779
780RIG(add, mp_add)
781RIG(sub, mp_sub)
782RIG(mul, mp_mul)
783
784#undef RIG
785
786static 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);
ef5f4810 798 assert(mparena_count(MPARENA_GLOBAL) == 0);
d3409d5e 799 return (ok);
800}
801
f09e814a 802static int tbin(dstr *v)
803{
804 static mp *(*fn[])(mp *, mp *, mp *) = {
805#define DO(string) mp_bit##string##2c,
806MPX_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
09d00c6b 837static 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
869static 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
397041a9 901static 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
f1713c63 931static 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);
4b536f42 940 if (s != rs || !MP_EQ(t, rt)) {
f1713c63 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);
09d00c6b 953 assert(mparena_count(MPARENA_GLOBAL) == 0);
f1713c63 954 return (ok);
955}
956
d3409d5e 957static test_chunk tests[] = {
f09e814a 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 } },
09d00c6b 962 { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } },
963 { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } },
d3409d5e 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 } },
f09e814a 968 { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } },
f1713c63 969 { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } },
397041a9 970 { "neg", tneg, { &type_mp, &type_mp, 0 } },
d3409d5e 971 { 0, 0, { 0 } },
972};
973
974int 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 -------------------------------------------------*/