3 * $Id: mp-arith.c,v 1.14 2002/10/15 19:18:31 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.14 2002/10/15 19:18:31 mdw
34 * New operation to negate numbers.
36 * Revision 1.13 2002/10/15 00:19:40 mdw
37 * Bit setting and clearing functions.
39 * Revision 1.12 2002/10/09 00:36:03 mdw
40 * Fix bounds on workspace for Karatsuba operations.
42 * Revision 1.11 2002/10/06 22:52:50 mdw
43 * Pile of changes for supporting two's complement properly.
45 * Revision 1.10 2001/04/03 19:36:05 mdw
46 * Add some simple bitwise operations so that Perl can use them.
48 * Revision 1.9 2000/10/08 15:48:35 mdw
49 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
51 * Revision 1.8 2000/10/08 12:02:21 mdw
52 * Use @MP_EQ@ instead of @MP_CMP@.
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.
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).
64 * Revision 1.5 1999/12/22 15:54:41 mdw
65 * Adjust Karatsuba parameters. Calculate destination size better.
67 * Revision 1.4 1999/12/13 15:35:16 mdw
68 * Slightly different rules on memory allocation.
70 * Revision 1.3 1999/12/11 10:57:43 mdw
71 * Karatsuba squaring algorithm.
73 * Revision 1.2 1999/12/10 23:18:39 mdw
74 * Change interface for suggested destinations.
76 * Revision 1.1 1999/11/17 18:02:16 mdw
77 * New multiprecision integer arithmetic suite.
81 /*----- Header files ------------------------------------------------------*/
85 /*----- Macros ------------------------------------------------------------*/
87 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
89 /*----- Main code ---------------------------------------------------------*/
91 /* --- @mp_lsl@, @mp_lsr@ --- *
93 * Arguments: @mp *d@ = destination
95 * @size_t n@ = number of bits to move
97 * Returns: Result, @a@ shifted left or right by @n@.
100 mp
*mp_lsl(mp
*d
, mp
*a
, size_t n
)
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
);
109 mp
*mp_lsr(mp
*d
, mp
*a
, size_t n
)
111 MP_DEST(d
, MP_LEN(a
), a
->f
);
112 mpx_lsr(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
113 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
118 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
120 * Arguments: @mp *d@ = destination
122 * @size_t n@ = number of bits to move
124 * Returns: Result, @a@ shifted left or right by @n@. Handles the
125 * pretence of sign-extension for negative numbers.
128 mp
*mp_lsl2c(mp
*d
, mp
*a
, size_t n
)
130 if (!(a
->f
& MP_NEG
))
131 return (mp_lsl(d
, a
, n
));
138 mp
*mp_lsr2c(mp
*d
, mp
*a
, size_t n
)
140 if (!(a
->f
& MP_NEG
))
141 return (mp_lsr(d
, a
, n
));
148 /* --- @mp_testbit@ --- *
150 * Arguments: @mp *x@ = a large integer
151 * @unsigned long n@ = which bit to test
153 * Returns: Nonzero if the bit is set, zero if not.
156 int mp_testbit(mp
*x
, unsigned long n
)
158 if (n
> MPW_BITS
* MP_LEN(x
))
160 return ((x
->v
[n
/MPW_BITS
] >> n
%MPW_BITS
) & 1u);
163 /* --- @mp_testbit2c@ --- *
165 * Arguments: @mp *x@ = a large integer
166 * @unsigned long n@ = which bit to test
168 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
169 * complement representation.
172 int mp_testbit2c(mp
*x
, unsigned long n
)
175 if (!(x
->f
& MP_NEG
))
176 return (mp_testbit(x
, n
));
177 x
= mp_not2c(MP_NEW
, x
);
178 r
= !mp_testbit(x
, n
);
183 /* --- @mp_setbit@, @mp_clearbit@ --- *
185 * Arguments: @mp *d@ = a destination
186 * @mp *x@ = a large integer
187 * @unsigned long n@ = which bit to modify
189 * Returns: The argument @x@, with the appropriate bit set or cleared.
192 mp
*mp_setbit(mp
*d
, mp
*x
, unsigned long n
)
196 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
201 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
202 d
->v
[n
/MPW_BITS
] |= 1 << n
%MPW_BITS
;
206 mp
*mp_clearbit(mp
*d
, mp
*x
, unsigned long n
)
210 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
215 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
216 d
->v
[n
/MPW_BITS
] &= ~(1 << n
%MPW_BITS
);
220 /* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
222 * Arguments: @mp *d@ = a destination
223 * @mp *x@ = a large integer
224 * @unsigned long n@ = which bit to modify
226 * Returns: The argument @x@, with the appropriate bit set or cleared.
227 * Fakes up two's complement representation.
230 mp
*mp_setbit2c(mp
*d
, mp
*x
, unsigned long n
)
232 if (!(x
->f
& MP_NEG
))
233 return mp_setbit(d
, x
, n
);
235 d
= mp_clearbit(d
, d
, n
);
240 mp
*mp_clearbit2c(mp
*d
, mp
*x
, unsigned long n
)
242 if (!(x
->f
& MP_NEG
))
243 return mp_clearbit(d
, x
, n
);
245 d
= mp_setbit(d
, d
, n
);
252 * Arguments: @const mp *a, *b@ = two numbers
254 * Returns: Nonzero if the numbers are equal.
257 int mp_eq(const mp
*a
, const mp
*b
) { return (MP_EQ(a
, b
)); }
259 /* --- @mp_cmp@ --- *
261 * Arguments: @const mp *a, *b@ = two numbers
263 * Returns: Less than, equal to or greater than zero, according to
264 * whether @a@ is less than, equal to or greater than @b@.
267 int mp_cmp(const mp
*a
, const mp
*b
)
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
)
277 /* --- @mp_neg@ --- *
279 * Arguments: @mp *d@ = destination
282 * Returns: The negation of the argument.
284 * Use: Negates its argument.
287 mp
*mp_neg(mp
*d
, mp
*a
)
289 /* --- Surprising amounts of messing about required --- */
297 MP_DEST(a
, MP_LEN(a
), a
->f
);
302 /* --- @mp_bitop@ --- *
304 * Arguments: @mp *d@ = destination
305 * @mp *a, *b@ = sources
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:
317 #define MP_BITBINOP(string) \
319 mp *mp_bit##string(mp *d, mp *a, mp *b) \
321 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \
322 mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \
323 d->f = (a->f | b->f) & MP_BURN; \
328 MPX_DOBIN(MP_BITBINOP
)
330 /* --- @mp_not@ --- *
332 * Arguments: @mp *d@ = destination
335 * Returns: The bitwise complement of the source.
338 mp
*mp_not(mp
*d
, mp
*a
)
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
;
347 /* --- @mp_bitop2c@ --- *
349 * Arguments: @mp *d@ = destination
350 * @mp *a, *b@ = sources
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
362 /* --- How this actually works --- *
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
370 * Start with: wxyz WXYZ
371 * If @a@ negative: yzwx or YZWX
372 * If @b@ negative: xwzy XWZY
373 * If both negative: zyxw ZYXW
376 #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \
378 mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \
380 if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \
381 d = mp_bit##base(d, a, b); \
383 } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \
385 d = mp_not2c(d, a); \
386 d = mp_bit##an(d, d, b); \
389 } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \
391 d = mp_not2c(d, b); \
392 d = mp_bit##bn(d, a, d); \
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); \
405 #define NEG d = mp_not2c(d, d);
407 MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS
, POS
, POS
, POS
)
408 MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS
, POS
, POS
, NEG
)
409 MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS
, NEG
, POS
, POS
)
410 MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS
, NEG
, POS
, NEG
)
411 MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS
, POS
, NEG
, POS
)
412 MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS
, POS
, NEG
, NEG
)
413 MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS
, NEG
, NEG
, POS
)
414 MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS
, NEG
, NEG
, NEG
)
415 MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG
, POS
, POS
, POS
)
416 MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG
, POS
, POS
, NEG
)
417 MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG
, NEG
, POS
, POS
)
418 MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG
, NEG
, POS
, NEG
)
419 MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG
, POS
, NEG
, POS
)
420 MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG
, POS
, NEG
, NEG
)
421 MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG
, NEG
, NEG
, POS
)
422 MP_BIT2CBINOP(1111, 0000, 0000, 0000, 0000, NEG
, NEG
, NEG
, NEG
)
426 /* --- @mp_not2c@ --- *
428 * Arguments: @mp *d@ = destination
431 * Returns: The sign-extended complement of the argument.
434 mp
*mp_not2c(mp
*d
, mp
*a
)
438 MP_DEST(d
, MP_LEN(a
) + 1, a
->f
);
441 MPX_USUBN(d
->v
, d
->vl
, 1);
443 MPX_UADDN(d
->v
, d
->vl
, 1);
446 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
448 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
450 d
->f
= (a
->f
& (MP_NEG
| MP_BURN
)) ^ MP_NEG
;
455 /* --- @mp_add@ --- *
457 * Arguments: @mp *d@ = destination
458 * @mp *a, *b@ = sources
460 * Returns: Result, @a@ added to @b@.
463 mp
*mp_add(mp
*d
, mp
*a
, mp
*b
)
465 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
466 if (!((a
->f
^ b
->f
) & MP_NEG
))
467 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
469 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
470 mp
*t
= a
; a
= b
; b
= t
;
472 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
474 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | (a
->f
& MP_NEG
);
479 /* --- @mp_sub@ --- *
481 * Arguments: @mp *d@ = destination
482 * @mp *a, *b@ = sources
484 * Returns: Result, @b@ subtracted from @a@.
487 mp
*mp_sub(mp
*d
, mp
*a
, mp
*b
)
490 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
491 if ((a
->f
^ b
->f
) & MP_NEG
)
492 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
494 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
495 mp
*t
= a
; a
= b
; b
= t
;
498 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
500 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ sgn
) & MP_NEG
);
505 /* --- @mp_mul@ --- *
507 * Arguments: @mp *d@ = destination
508 * @mp *a, *b@ = sources
510 * Returns: Result, @a@ multiplied by @b@.
513 mp
*mp_mul(mp
*d
, mp
*a
, mp
*b
)
518 if (MP_LEN(a
) <= MPK_THRESH
|| MP_LEN(b
) <= MPK_THRESH
) {
519 MP_DEST(d
, MP_LEN(a
) + MP_LEN(b
), a
->f
| b
->f
| MP_UNDEF
);
520 mpx_umul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
522 size_t m
= MAX(MP_LEN(a
), MP_LEN(b
));
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
);
530 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ b
->f
) & MP_NEG
);
537 /* --- @mp_sqr@ --- *
539 * Arguments: @mp *d@ = destination
542 * Returns: Result, @a@ squared.
545 mp
*mp_sqr(mp
*d
, mp
*a
)
547 size_t m
= MP_LEN(a
);
550 if (m
> MPK_THRESH
) {
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
);
557 MP_DEST(d
, 2 * m
+ 2, a
->f
| MP_UNDEF
);
558 mpx_usqr(d
->v
, d
->vl
, a
->v
, a
->vl
);
560 d
->f
= a
->f
& MP_BURN
;
566 /* --- @mp_div@ --- *
568 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
569 * @mp *a, *b@ = sources
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@.
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
585 void mp_div(mp
**qq
, mp
**rr
, mp
*a
, mp
*b
)
587 mp
*r
= rr ?
*rr
: MP_NEW
;
588 mp
*q
= qq ?
*qq
: MP_NEW
;
591 /* --- Set the remainder up right --- *
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.
602 MP_DEST(r
, MP_LEN(a
) + 2, a
->f
| b
->f
);
604 /* --- Fix up the quotient too --- */
607 MP_DEST(q
, MP_LEN(r
), r
->f
| MP_UNDEF
);
610 /* --- Set up some temporary workspace --- */
613 size_t rq
= MP_LEN(b
) + 1;
614 sv
= mpalloc(r
->a
, rq
);
618 /* --- Perform the calculation --- */
620 mpx_udiv(q
->v
, q
->vl
, r
->v
, r
->vl
, b
->v
, b
->vl
, sv
, svl
);
622 /* --- Sort out the sign of the results --- *
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@.
629 q
->f
= ((r
->f
| b
->f
) & MP_BURN
) | ((r
->f
^ b
->f
) & MP_NEG
);
632 for (v
= r
->v
; v
< r
->vl
; v
++) {
634 MPX_UADDN(q
->v
, q
->vl
, 1);
635 mpx_usub(r
->v
, r
->vl
, b
->v
, b
->vl
, r
->v
, r
->vl
);
641 r
->f
= ((r
->f
| b
->f
) & MP_BURN
) | (b
->f
& MP_NEG
);
643 /* --- Store the return values --- */
663 /* --- @mp_odd@ --- *
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
669 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
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.
676 mp
*mp_odd(mp
*d
, mp
*m
, size_t *s
)
683 for (; !*v
&& v
< vl
; v
++)
690 unsigned z
= MPW_BITS
/ 2;
703 return (mp_lsr(d
, m
, ss
));
706 /*----- Test rig ----------------------------------------------------------*/
710 static int verify(const char *op
, mp
*expect
, mp
*result
, mp
*a
, mp
*b
)
712 if (!MP_EQ(expect
, result
)) {
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);
724 #define RIG(name, op) \
725 static int t##name(dstr *v) \
727 mp *a = *(mp **)v[0].buf; \
728 mpw n = *(int *)v[1].buf; \
730 mp *r = *(mp **)v[2].buf; \
731 mp *c = op(MP_NEW, a, n); \
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); \
736 assert(mparena_count(MPARENA_GLOBAL) == 0); \
747 #define RIG(name, op) \
748 static int t##name(dstr *v) \
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); \
756 assert(mparena_count(MPARENA_GLOBAL) == 0); \
766 static int tdiv(dstr
*v
)
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
;
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
);
778 assert(mparena_count(MPARENA_GLOBAL
) == 0);
782 static int tbin(dstr
*v
)
784 static mp
*(*fn
[])(mp
*, mp
*, mp
*) = {
785 #define DO(string) mp_bit##string##2c,
791 mp
*a
= *(mp
**)v
[1].buf
;
792 mp
*b
= *(mp
**)v
[2].buf
;
793 mp
*r
= *(mp
**)v
[3].buf
;
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;
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);
817 static int tset(dstr
*v
)
819 mp
*a
= *(mp
**)v
[0].buf
;
820 unsigned long n
= *(unsigned long *)v
[1].buf
;
821 mp
*r
= *(mp
**)v
[2].buf
;
825 c
= mp_setbit2c(MP_NEW
, a
, n
);
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);
835 if (!mp_testbit2c(r
, n
)) {
837 fprintf(stderr
, "\n***setbit (test) failed");
838 fprintf(stderr
, "\n*** n = %lu", n
);
839 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
845 assert(mparena_count(MPARENA_GLOBAL
) == 0);
849 static int tclr(dstr
*v
)
851 mp
*a
= *(mp
**)v
[0].buf
;
852 unsigned long n
= *(unsigned long *)v
[1].buf
;
853 mp
*r
= *(mp
**)v
[2].buf
;
857 c
= mp_clearbit2c(MP_NEW
, a
, n
);
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);
867 if (mp_testbit2c(r
, n
)) {
869 fprintf(stderr
, "\n***clrbit (test) failed");
870 fprintf(stderr
, "\n*** n = %lu", n
);
871 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
877 assert(mparena_count(MPARENA_GLOBAL
) == 0);
881 static int tneg(dstr
*v
)
883 mp
*a
= *(mp
**)v
[0].buf
;
884 mp
*r
= *(mp
**)v
[1].buf
;
886 mp
*n
= mp_neg(MP_NEW
, a
);
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);
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);
907 assert(mparena_count(MPARENA_GLOBAL
) == 0);
911 static int todd(dstr
*v
)
913 mp
*a
= *(mp
**)v
[0].buf
;
914 size_t rs
= *(uint32
*)v
[1].buf
;
915 mp
*rt
= *(mp
**)v
[2].buf
;
919 t
= mp_odd(MP_NEW
, a
, &s
);
920 if (s
!= rs
|| !MP_EQ(t
, rt
)) {
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);
933 assert(mparena_count(MPARENA_GLOBAL
) == 0);
937 static test_chunk tests
[] = {
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 } },
942 { "setbit", tset
, { &type_mp
, &type_ulong
, &type_mp
, 0 } },
943 { "clrbit", tclr
, { &type_mp
, &type_ulong
, &type_mp
, 0 } },
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 } },
948 { "bin2c", tbin
, { &type_string
, &type_mp
, &type_mp
, &type_mp
, 0 } },
949 { "odd", todd
, { &type_mp
, &type_uint32
, &type_mp
, 0 } },
950 { "neg", tneg
, { &type_mp
, &type_mp
, 0 } },
954 int main(int argc
, char *argv
[])
957 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
963 /*----- That's all, folks -------------------------------------------------*/