3 * $Id: mp-arith.c,v 1.16 2003/05/16 09:09:24 mdw Exp $
5 * Basic arithmetic on multiprecision integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
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.
36 * Revision 1.15 2002/10/19 17:56:50 mdw
37 * Fix bit operations. Test them (a bit) better.
39 * Revision 1.14 2002/10/15 19:18:31 mdw
40 * New operation to negate numbers.
42 * Revision 1.13 2002/10/15 00:19:40 mdw
43 * Bit setting and clearing functions.
45 * Revision 1.12 2002/10/09 00:36:03 mdw
46 * Fix bounds on workspace for Karatsuba operations.
48 * Revision 1.11 2002/10/06 22:52:50 mdw
49 * Pile of changes for supporting two's complement properly.
51 * Revision 1.10 2001/04/03 19:36:05 mdw
52 * Add some simple bitwise operations so that Perl can use them.
54 * Revision 1.9 2000/10/08 15:48:35 mdw
55 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
57 * Revision 1.8 2000/10/08 12:02:21 mdw
58 * Use @MP_EQ@ instead of @MP_CMP@.
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.
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).
70 * Revision 1.5 1999/12/22 15:54:41 mdw
71 * Adjust Karatsuba parameters. Calculate destination size better.
73 * Revision 1.4 1999/12/13 15:35:16 mdw
74 * Slightly different rules on memory allocation.
76 * Revision 1.3 1999/12/11 10:57:43 mdw
77 * Karatsuba squaring algorithm.
79 * Revision 1.2 1999/12/10 23:18:39 mdw
80 * Change interface for suggested destinations.
82 * Revision 1.1 1999/11/17 18:02:16 mdw
83 * New multiprecision integer arithmetic suite.
87 /*----- Header files ------------------------------------------------------*/
91 /*----- Macros ------------------------------------------------------------*/
93 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
95 /*----- Main code ---------------------------------------------------------*/
97 /* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- *
99 * Arguments: @mp *d@ = destination
101 * @size_t n@ = number of bits to move
103 * Returns: Result, @a@ shifted left or right by @n@.
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
111 mp
*mp_lsl(mp
*d
, mp
*a
, size_t n
)
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
);
120 mp
*mp_lslc(mp
*d
, mp
*a
, size_t n
)
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
);
129 mp
*mp_lsr(mp
*d
, mp
*a
, size_t n
)
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
);
138 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
140 * Arguments: @mp *d@ = destination
142 * @size_t n@ = number of bits to move
144 * Returns: Result, @a@ shifted left or right by @n@. Handles the
145 * pretence of sign-extension for negative numbers.
148 mp
*mp_lsl2c(mp
*d
, mp
*a
, size_t n
)
150 if (!(a
->f
& MP_NEG
))
151 return (mp_lsl(d
, a
, n
));
153 d
= mp_lslc(d
, d
, n
);
158 mp
*mp_lsr2c(mp
*d
, mp
*a
, size_t n
)
160 if (!(a
->f
& MP_NEG
))
161 return (mp_lsr(d
, a
, n
));
168 /* --- @mp_testbit@ --- *
170 * Arguments: @mp *x@ = a large integer
171 * @unsigned long n@ = which bit to test
173 * Returns: Nonzero if the bit is set, zero if not.
176 int mp_testbit(mp
*x
, unsigned long n
)
178 if (n
> MPW_BITS
* MP_LEN(x
))
180 return ((x
->v
[n
/MPW_BITS
] >> n
%MPW_BITS
) & 1u);
183 /* --- @mp_testbit2c@ --- *
185 * Arguments: @mp *x@ = a large integer
186 * @unsigned long n@ = which bit to test
188 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
189 * complement representation.
192 int mp_testbit2c(mp
*x
, unsigned long n
)
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
);
203 /* --- @mp_setbit@, @mp_clearbit@ --- *
205 * Arguments: @mp *d@ = a destination
206 * @mp *x@ = a large integer
207 * @unsigned long n@ = which bit to modify
209 * Returns: The argument @x@, with the appropriate bit set or cleared.
212 mp
*mp_setbit(mp
*d
, mp
*x
, unsigned long n
)
216 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
221 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
222 d
->v
[n
/MPW_BITS
] |= 1 << n
%MPW_BITS
;
226 mp
*mp_clearbit(mp
*d
, mp
*x
, unsigned long n
)
230 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
235 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
236 d
->v
[n
/MPW_BITS
] &= ~(1 << n
%MPW_BITS
);
240 /* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
242 * Arguments: @mp *d@ = a destination
243 * @mp *x@ = a large integer
244 * @unsigned long n@ = which bit to modify
246 * Returns: The argument @x@, with the appropriate bit set or cleared.
247 * Fakes up two's complement representation.
250 mp
*mp_setbit2c(mp
*d
, mp
*x
, unsigned long n
)
252 if (!(x
->f
& MP_NEG
))
253 return mp_setbit(d
, x
, n
);
255 d
= mp_clearbit(d
, d
, n
);
260 mp
*mp_clearbit2c(mp
*d
, mp
*x
, unsigned long n
)
262 if (!(x
->f
& MP_NEG
))
263 return mp_clearbit(d
, x
, n
);
265 d
= mp_setbit(d
, d
, n
);
272 * Arguments: @const mp *a, *b@ = two numbers
274 * Returns: Nonzero if the numbers are equal.
277 int mp_eq(const mp
*a
, const mp
*b
) { return (MP_EQ(a
, b
)); }
279 /* --- @mp_cmp@ --- *
281 * Arguments: @const mp *a, *b@ = two numbers
283 * Returns: Less than, equal to or greater than zero, according to
284 * whether @a@ is less than, equal to or greater than @b@.
287 int mp_cmp(const mp
*a
, const mp
*b
)
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
)
297 /* --- @mp_neg@ --- *
299 * Arguments: @mp *d@ = destination
302 * Returns: The negation of the argument.
304 * Use: Negates its argument.
307 mp
*mp_neg(mp
*d
, mp
*a
)
309 /* --- Surprising amounts of messing about required --- */
317 MP_DEST(a
, MP_LEN(a
), a
->f
);
322 /* --- @mp_bitop@ --- *
324 * Arguments: @mp *d@ = destination
325 * @mp *a, *b@ = sources
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:
337 #define MP_BITBINOP(string) \
339 mp *mp_bit##string(mp *d, mp *a, mp *b) \
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; \
348 MPX_DOBIN(MP_BITBINOP
)
350 /* --- @mp_not@ --- *
352 * Arguments: @mp *d@ = destination
355 * Returns: The bitwise complement of the source.
358 mp
*mp_not(mp
*d
, mp
*a
)
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
;
367 /* --- @mp_bitop2c@ --- *
369 * Arguments: @mp *d@ = destination
370 * @mp *a, *b@ = sources
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
382 /* --- How this actually works --- *
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
390 * Start with: wxyz WXYZ
391 * If @a@ negative: yzwx or YZWX
392 * If @b@ negative: xwzy XWZY
393 * If both negative: zyxw ZYXW
396 #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \
398 mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \
400 if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \
401 d = mp_bit##base(d, a, b); \
403 } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \
405 d = mp_not2c(d, a); \
406 d = mp_bit##an(d, d, b); \
409 } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \
411 d = mp_not2c(d, b); \
412 d = mp_bit##bn(d, a, d); \
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); \
425 #define NEG d = mp_not2c(d, d);
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
)
446 /* --- @mp_not2c@ --- *
448 * Arguments: @mp *d@ = destination
451 * Returns: The sign-extended complement of the argument.
454 mp
*mp_not2c(mp
*d
, mp
*a
)
458 MP_DEST(d
, MP_LEN(a
) + 1, a
->f
);
461 MPX_USUBN(d
->v
, d
->vl
, 1);
463 MPX_UADDN(d
->v
, d
->vl
, 1);
466 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
468 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
470 d
->f
= (a
->f
& (MP_NEG
| MP_BURN
)) ^ MP_NEG
;
475 /* --- @mp_add@ --- *
477 * Arguments: @mp *d@ = destination
478 * @mp *a, *b@ = sources
480 * Returns: Result, @a@ added to @b@.
483 mp
*mp_add(mp
*d
, mp
*a
, mp
*b
)
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
);
489 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
490 mp
*t
= a
; a
= b
; b
= t
;
492 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
494 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | (a
->f
& MP_NEG
);
499 /* --- @mp_sub@ --- *
501 * Arguments: @mp *d@ = destination
502 * @mp *a, *b@ = sources
504 * Returns: Result, @b@ subtracted from @a@.
507 mp
*mp_sub(mp
*d
, mp
*a
, mp
*b
)
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
);
514 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
515 mp
*t
= a
; a
= b
; b
= t
;
518 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
520 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ sgn
) & MP_NEG
);
525 /* --- @mp_mul@ --- *
527 * Arguments: @mp *d@ = destination
528 * @mp *a, *b@ = sources
530 * Returns: Result, @a@ multiplied by @b@.
533 mp
*mp_mul(mp
*d
, mp
*a
, mp
*b
)
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
);
542 size_t m
= MAX(MP_LEN(a
), MP_LEN(b
));
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
);
550 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ b
->f
) & MP_NEG
);
557 /* --- @mp_sqr@ --- *
559 * Arguments: @mp *d@ = destination
562 * Returns: Result, @a@ squared.
565 mp
*mp_sqr(mp
*d
, mp
*a
)
567 size_t m
= MP_LEN(a
);
570 if (m
> MPK_THRESH
) {
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
);
577 MP_DEST(d
, 2 * m
+ 2, a
->f
| MP_UNDEF
);
578 mpx_usqr(d
->v
, d
->vl
, a
->v
, a
->vl
);
580 d
->f
= a
->f
& MP_BURN
;
586 /* --- @mp_div@ --- *
588 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
589 * @mp *a, *b@ = sources
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@.
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
605 void mp_div(mp
**qq
, mp
**rr
, mp
*a
, mp
*b
)
607 mp
*r
= rr ?
*rr
: MP_NEW
;
608 mp
*q
= qq ?
*qq
: MP_NEW
;
611 /* --- Set the remainder up right --- *
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.
622 MP_DEST(r
, MP_LEN(a
) + 2, a
->f
| b
->f
);
624 /* --- Fix up the quotient too --- */
627 MP_DEST(q
, MP_LEN(r
), r
->f
| MP_UNDEF
);
630 /* --- Set up some temporary workspace --- */
633 size_t rq
= MP_LEN(b
) + 1;
634 sv
= mpalloc(r
->a
, rq
);
638 /* --- Perform the calculation --- */
640 mpx_udiv(q
->v
, q
->vl
, r
->v
, r
->vl
, b
->v
, b
->vl
, sv
, svl
);
642 /* --- Sort out the sign of the results --- *
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@.
649 q
->f
= ((r
->f
| b
->f
) & MP_BURN
) | ((r
->f
^ b
->f
) & MP_NEG
);
652 for (v
= r
->v
; v
< r
->vl
; v
++) {
654 MPX_UADDN(q
->v
, q
->vl
, 1);
655 mpx_usub(r
->v
, r
->vl
, b
->v
, b
->vl
, r
->v
, r
->vl
);
661 r
->f
= ((r
->f
| b
->f
) & MP_BURN
) | (b
->f
& MP_NEG
);
663 /* --- Store the return values --- */
683 /* --- @mp_odd@ --- *
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
689 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
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.
696 mp
*mp_odd(mp
*d
, mp
*m
, size_t *s
)
703 for (; !*v
&& v
< vl
; v
++)
710 unsigned z
= MPW_BITS
/ 2;
723 return (mp_lsr(d
, m
, ss
));
726 /*----- Test rig ----------------------------------------------------------*/
730 static int verify(const char *op
, mp
*expect
, mp
*result
, mp
*a
, mp
*b
)
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);
744 #define RIG(name, op) \
745 static int t##name(dstr *v) \
747 mp *a = *(mp **)v[0].buf; \
748 mpw n = *(int *)v[1].buf; \
750 mp *r = *(mp **)v[2].buf; \
751 mp *c = op(MP_NEW, a, n); \
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); \
767 #define RIG(name, op) \
768 static int t##name(dstr *v) \
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); \
786 static int tdiv(dstr
*v
)
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
;
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);
802 static int tbin(dstr
*v
)
804 static mp
*(*fn
[])(mp
*, mp
*, mp
*) = {
805 #define DO(string) mp_bit##string##2c,
811 mp
*a
= *(mp
**)v
[1].buf
;
812 mp
*b
= *(mp
**)v
[2].buf
;
813 mp
*r
= *(mp
**)v
[3].buf
;
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;
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);
837 static int tset(dstr
*v
)
839 mp
*a
= *(mp
**)v
[0].buf
;
840 unsigned long n
= *(unsigned long *)v
[1].buf
;
841 mp
*r
= *(mp
**)v
[2].buf
;
845 c
= mp_setbit2c(MP_NEW
, a
, n
);
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);
855 if (!mp_testbit2c(r
, n
)) {
857 fprintf(stderr
, "\n***setbit (test) failed");
858 fprintf(stderr
, "\n*** n = %lu", n
);
859 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
865 assert(mparena_count(MPARENA_GLOBAL
) == 0);
869 static int tclr(dstr
*v
)
871 mp
*a
= *(mp
**)v
[0].buf
;
872 unsigned long n
= *(unsigned long *)v
[1].buf
;
873 mp
*r
= *(mp
**)v
[2].buf
;
877 c
= mp_clearbit2c(MP_NEW
, a
, n
);
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);
887 if (mp_testbit2c(r
, n
)) {
889 fprintf(stderr
, "\n***clrbit (test) failed");
890 fprintf(stderr
, "\n*** n = %lu", n
);
891 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
897 assert(mparena_count(MPARENA_GLOBAL
) == 0);
901 static int tneg(dstr
*v
)
903 mp
*a
= *(mp
**)v
[0].buf
;
904 mp
*r
= *(mp
**)v
[1].buf
;
906 mp
*n
= mp_neg(MP_NEW
, a
);
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);
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);
927 assert(mparena_count(MPARENA_GLOBAL
) == 0);
931 static int todd(dstr
*v
)
933 mp
*a
= *(mp
**)v
[0].buf
;
934 size_t rs
= *(uint32
*)v
[1].buf
;
935 mp
*rt
= *(mp
**)v
[2].buf
;
939 t
= mp_odd(MP_NEW
, a
, &s
);
940 if (s
!= rs
|| !MP_EQ(t
, rt
)) {
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);
953 assert(mparena_count(MPARENA_GLOBAL
) == 0);
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 } },
974 int main(int argc
, char *argv
[])
977 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
983 /*----- That's all, folks -------------------------------------------------*/