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