Fix leftovers bug in reading.
[u/mdw/catacomb] / mp-arith.c
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
397041a9 3 * $Id: mp-arith.c,v 1.14 2002/10/15 19:18:31 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 $
397041a9 33 * Revision 1.14 2002/10/15 19:18:31 mdw
34 * New operation to negate numbers.
35 *
09d00c6b 36 * Revision 1.13 2002/10/15 00:19:40 mdw
37 * Bit setting and clearing functions.
38 *
dd22938e 39 * Revision 1.12 2002/10/09 00:36:03 mdw
40 * Fix bounds on workspace for Karatsuba operations.
41 *
f09e814a 42 * Revision 1.11 2002/10/06 22:52:50 mdw
43 * Pile of changes for supporting two's complement properly.
44 *
0f32e0f8 45 * Revision 1.10 2001/04/03 19:36:05 mdw
46 * Add some simple bitwise operations so that Perl can use them.
47 *
52cdaca9 48 * Revision 1.9 2000/10/08 15:48:35 mdw
49 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
50 *
4b536f42 51 * Revision 1.8 2000/10/08 12:02:21 mdw
52 * Use @MP_EQ@ instead of @MP_CMP@.
53 *
f1713c63 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 *
d34decd2 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 *
bba03f55 64 * Revision 1.5 1999/12/22 15:54:41 mdw
65 * Adjust Karatsuba parameters. Calculate destination size better.
66 *
8017495b 67 * Revision 1.4 1999/12/13 15:35:16 mdw
68 * Slightly different rules on memory allocation.
69 *
5bf74dea 70 * Revision 1.3 1999/12/11 10:57:43 mdw
71 * Karatsuba squaring algorithm.
72 *
ef5f4810 73 * Revision 1.2 1999/12/10 23:18:39 mdw
74 * Change interface for suggested destinations.
75 *
d3409d5e 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
ef5f4810 85/*----- Macros ------------------------------------------------------------*/
86
87#define MAX(x, y) ((x) >= (y) ? (x) : (y))
88
d3409d5e 89/*----- Main code ---------------------------------------------------------*/
90
f09e814a 91/* --- @mp_lsl@, @mp_lsr@ --- *
d3409d5e 92 *
f09e814a 93 * Arguments: @mp *d@ = destination
94 * @mp *a@ = source
95 * @size_t n@ = number of bits to move
d3409d5e 96 *
f09e814a 97 * Returns: Result, @a@ shifted left or right by @n@.
d3409d5e 98 */
99
f09e814a 100mp *mp_lsl(mp *d, mp *a, size_t n)
d3409d5e 101{
f09e814a 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}
d3409d5e 108
f09e814a 109mp *mp_lsr(mp *d, mp *a, size_t n)
110{
d34decd2 111 MP_DEST(d, MP_LEN(a), a->f);
f09e814a 112 mpx_lsr(d->v, d->vl, a->v, a->vl, n);
113 d->f = a->f & (MP_NEG | MP_BURN);
d3409d5e 114 MP_SHRINK(d);
115 return (d);
116}
117
f09e814a 118/* --- @mp_lsl2c@, @mp_lsr2c@ --- *
d3409d5e 119 *
120 * Arguments: @mp *d@ = destination
121 * @mp *a@ = source
f09e814a 122 * @size_t n@ = number of bits to move
d3409d5e 123 *
f09e814a 124 * Returns: Result, @a@ shifted left or right by @n@. Handles the
125 * pretence of sign-extension for negative numbers.
d3409d5e 126 */
127
f09e814a 128mp *mp_lsl2c(mp *d, mp *a, size_t n)
d3409d5e 129{
f09e814a 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}
d3409d5e 137
f09e814a 138mp *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);
d3409d5e 146}
147
f09e814a 148/* --- @mp_testbit@ --- *
d3409d5e 149 *
f09e814a 150 * Arguments: @mp *x@ = a large integer
09d00c6b 151 * @unsigned long n@ = which bit to test
d3409d5e 152 *
f09e814a 153 * Returns: Nonzero if the bit is set, zero if not.
d3409d5e 154 */
155
09d00c6b 156int mp_testbit(mp *x, unsigned long n)
d3409d5e 157{
f09e814a 158 if (n > MPW_BITS * MP_LEN(x))
159 return (0);
09d00c6b 160 return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u);
d3409d5e 161}
162
f09e814a 163/* --- @mp_testbit2c@ --- *
d3409d5e 164 *
f09e814a 165 * Arguments: @mp *x@ = a large integer
09d00c6b 166 * @unsigned long n@ = which bit to test
d3409d5e 167 *
f09e814a 168 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
169 * complement representation.
d3409d5e 170 */
171
09d00c6b 172int mp_testbit2c(mp *x, unsigned long n)
d3409d5e 173{
f09e814a 174 int r;
09d00c6b 175 if (!(x->f & MP_NEG))
f09e814a 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);
d3409d5e 181}
182
09d00c6b 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
192mp *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
206mp *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
230mp *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
240mp *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
4b536f42 250/* --- @mp_eq@ --- *
251 *
252 * Arguments: @const mp *a, *b@ = two numbers
253 *
254 * Returns: Nonzero if the numbers are equal.
255 */
256
257int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); }
258
d3409d5e 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
267int 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
397041a9 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
287mp *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
f09e814a 302/* --- @mp_bitop@ --- *
0f32e0f8 303 *
304 * Arguments: @mp *d@ = destination
305 * @mp *a, *b@ = sources
306 *
f09e814a 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@
0f32e0f8 315 */
316
f09e814a 317#define MP_BITBINOP(string) \
0f32e0f8 318 \
f09e814a 319mp *mp_bit##string(mp *d, mp *a, mp *b) \
0f32e0f8 320{ \
321 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \
f09e814a 322 mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \
0f32e0f8 323 d->f = (a->f | b->f) & MP_BURN; \
324 MP_SHRINK(d); \
325 return (d); \
326}
327
f09e814a 328MPX_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 */
0f32e0f8 337
338mp *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
f09e814a 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 \
378mp *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
407MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS, POS, POS, POS)
408MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS, POS, POS, NEG)
409MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS, NEG, POS, POS)
410MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS, NEG, POS, NEG)
411MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS, POS, NEG, POS)
412MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS, POS, NEG, NEG)
413MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS, NEG, NEG, POS)
414MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS, NEG, NEG, NEG)
415MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG, POS, POS, POS)
416MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG, POS, POS, NEG)
417MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG, NEG, POS, POS)
418MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG, NEG, POS, NEG)
419MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG, POS, NEG, POS)
420MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG, POS, NEG, NEG)
421MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG, NEG, NEG, POS)
422MP_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
434mp *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
d3409d5e 455/* --- @mp_add@ --- *
456 *
457 * Arguments: @mp *d@ = destination
ef5f4810 458 * @mp *a, *b@ = sources
d3409d5e 459 *
460 * Returns: Result, @a@ added to @b@.
461 */
462
ef5f4810 463mp *mp_add(mp *d, mp *a, mp *b)
d3409d5e 464{
d34decd2 465 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
d3409d5e 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)) {
ef5f4810 470 mp *t = a; a = b; b = t;
d3409d5e 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
ef5f4810 482 * @mp *a, *b@ = sources
d3409d5e 483 *
484 * Returns: Result, @b@ subtracted from @a@.
485 */
486
ef5f4810 487mp *mp_sub(mp *d, mp *a, mp *b)
d3409d5e 488{
489 unsigned sgn = 0;
d34decd2 490 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
d3409d5e 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)) {
ef5f4810 495 mp *t = a; a = b; b = t;
d3409d5e 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
ef5f4810 508 * @mp *a, *b@ = sources
d3409d5e 509 *
510 * Returns: Result, @a@ multiplied by @b@.
511 */
512
ef5f4810 513mp *mp_mul(mp *d, mp *a, mp *b)
d3409d5e 514{
ef5f4810 515 a = MP_COPY(a);
516 b = MP_COPY(b);
517
52cdaca9 518 if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= MPK_THRESH) {
d34decd2 519 MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF);
ef5f4810 520 mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
8017495b 521 } else {
dd22938e 522 size_t m = MAX(MP_LEN(a), MP_LEN(b));
ef5f4810 523 mpw *s;
dd22938e 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);
d34decd2 527 mpfree(d->a, s);
ef5f4810 528 }
529
d3409d5e 530 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
531 MP_SHRINK(d);
ef5f4810 532 MP_DROP(a);
533 MP_DROP(b);
d3409d5e 534 return (d);
535}
536
537/* --- @mp_sqr@ --- *
538 *
539 * Arguments: @mp *d@ = destination
ef5f4810 540 * @mp *a@ = source
d3409d5e 541 *
542 * Returns: Result, @a@ squared.
543 */
544
ef5f4810 545mp *mp_sqr(mp *d, mp *a)
d3409d5e 546{
ef5f4810 547 size_t m = MP_LEN(a);
548
549 a = MP_COPY(a);
52cdaca9 550 if (m > MPK_THRESH) {
ef5f4810 551 mpw *s;
dd22938e 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);
d34decd2 555 mpfree(d->a, s);
dd22938e 556 } else {
557 MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF);
ef5f4810 558 mpx_usqr(d->v, d->vl, a->v, a->vl);
dd22938e 559 }
d3409d5e 560 d->f = a->f & MP_BURN;
561 MP_SHRINK(d);
ef5f4810 562 MP_DROP(a);
d3409d5e 563 return (d);
564}
565
566/* --- @mp_div@ --- *
567 *
568 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
ef5f4810 569 * @mp *a, *b@ = sources
d3409d5e 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
ef5f4810 585void mp_div(mp **qq, mp **rr, mp *a, mp *b)
d3409d5e 586 {
587 mp *r = rr ? *rr : MP_NEW;
588 mp *q = qq ? *qq : MP_NEW;
589 mpw *sv, *svl;
590
d3409d5e 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
d34decd2 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);
d3409d5e 603
604 /* --- Fix up the quotient too --- */
605
d34decd2 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 }
d3409d5e 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
d34decd2 629 q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG);
d3409d5e 630 if (q->f & MP_NEG) {
ef5f4810 631 mpw *v;
632 for (v = r->v; v < r->vl; v++) {
d3409d5e 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
d34decd2 641 r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG);
d3409d5e 642
643 /* --- Store the return values --- */
644
d34decd2 645 mpfree(r->a, sv);
646 MP_DROP(b);
647
d3409d5e 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 }
d3409d5e 661}
662
f1713c63 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
676mp *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
d3409d5e 706/*----- Test rig ----------------------------------------------------------*/
707
708#ifdef TEST_RIG
709
710static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
711{
4b536f42 712 if (!MP_EQ(expect, result)) {
d3409d5e 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) \
ef5f4810 725 static int t##name(dstr *v) \
d3409d5e 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); \
ef5f4810 736 assert(mparena_count(MPARENA_GLOBAL) == 0); \
d3409d5e 737 return (ok); \
738 }
739
740RIG(lsl, mp_lsl)
741RIG(lsr, mp_lsr)
f09e814a 742RIG(lsl2c, mp_lsl2c)
743RIG(lsr2c, mp_lsr2c)
d3409d5e 744
745#undef RIG
746
747#define RIG(name, op) \
ef5f4810 748 static int t##name(dstr *v) \
d3409d5e 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); \
ef5f4810 756 assert(mparena_count(MPARENA_GLOBAL) == 0); \
d3409d5e 757 return (ok); \
758 }
759
760RIG(add, mp_add)
761RIG(sub, mp_sub)
762RIG(mul, mp_mul)
763
764#undef RIG
765
766static 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);
ef5f4810 778 assert(mparena_count(MPARENA_GLOBAL) == 0);
d3409d5e 779 return (ok);
780}
781
f09e814a 782static int tbin(dstr *v)
783{
784 static mp *(*fn[])(mp *, mp *, mp *) = {
785#define DO(string) mp_bit##string##2c,
786MPX_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
09d00c6b 817static 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
849static 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
397041a9 881static 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
f1713c63 911static 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);
4b536f42 920 if (s != rs || !MP_EQ(t, rt)) {
f1713c63 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);
09d00c6b 933 assert(mparena_count(MPARENA_GLOBAL) == 0);
f1713c63 934 return (ok);
935}
936
d3409d5e 937static test_chunk tests[] = {
f09e814a 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 } },
09d00c6b 942 { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } },
943 { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } },
d3409d5e 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 } },
f09e814a 948 { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } },
f1713c63 949 { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } },
397041a9 950 { "neg", tneg, { &type_mp, &type_mp, 0 } },
d3409d5e 951 { 0, 0, { 0 } },
952};
953
954int 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 -------------------------------------------------*/