3 * $Id: mp-arith.c,v 1.13 2002/10/15 00:19:40 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.13 2002/10/15 00:19:40 mdw
34 * Bit setting and clearing functions.
36 * Revision 1.12 2002/10/09 00:36:03 mdw
37 * Fix bounds on workspace for Karatsuba operations.
39 * Revision 1.11 2002/10/06 22:52:50 mdw
40 * Pile of changes for supporting two's complement properly.
42 * Revision 1.10 2001/04/03 19:36:05 mdw
43 * Add some simple bitwise operations so that Perl can use them.
45 * Revision 1.9 2000/10/08 15:48:35 mdw
46 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
48 * Revision 1.8 2000/10/08 12:02:21 mdw
49 * Use @MP_EQ@ instead of @MP_CMP@.
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.
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).
61 * Revision 1.5 1999/12/22 15:54:41 mdw
62 * Adjust Karatsuba parameters. Calculate destination size better.
64 * Revision 1.4 1999/12/13 15:35:16 mdw
65 * Slightly different rules on memory allocation.
67 * Revision 1.3 1999/12/11 10:57:43 mdw
68 * Karatsuba squaring algorithm.
70 * Revision 1.2 1999/12/10 23:18:39 mdw
71 * Change interface for suggested destinations.
73 * Revision 1.1 1999/11/17 18:02:16 mdw
74 * New multiprecision integer arithmetic suite.
78 /*----- Header files ------------------------------------------------------*/
82 /*----- Macros ------------------------------------------------------------*/
84 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
86 /*----- Main code ---------------------------------------------------------*/
88 /* --- @mp_lsl@, @mp_lsr@ --- *
90 * Arguments: @mp *d@ = destination
92 * @size_t n@ = number of bits to move
94 * Returns: Result, @a@ shifted left or right by @n@.
97 mp
*mp_lsl(mp
*d
, mp
*a
, size_t n
)
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
);
106 mp
*mp_lsr(mp
*d
, mp
*a
, size_t n
)
108 MP_DEST(d
, MP_LEN(a
), a
->f
);
109 mpx_lsr(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
110 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
115 /* --- @mp_lsl2c@, @mp_lsr2c@ --- *
117 * Arguments: @mp *d@ = destination
119 * @size_t n@ = number of bits to move
121 * Returns: Result, @a@ shifted left or right by @n@. Handles the
122 * pretence of sign-extension for negative numbers.
125 mp
*mp_lsl2c(mp
*d
, mp
*a
, size_t n
)
127 if (!(a
->f
& MP_NEG
))
128 return (mp_lsl(d
, a
, n
));
135 mp
*mp_lsr2c(mp
*d
, mp
*a
, size_t n
)
137 if (!(a
->f
& MP_NEG
))
138 return (mp_lsr(d
, a
, n
));
145 /* --- @mp_testbit@ --- *
147 * Arguments: @mp *x@ = a large integer
148 * @unsigned long n@ = which bit to test
150 * Returns: Nonzero if the bit is set, zero if not.
153 int mp_testbit(mp
*x
, unsigned long n
)
155 if (n
> MPW_BITS
* MP_LEN(x
))
157 return ((x
->v
[n
/MPW_BITS
] >> n
%MPW_BITS
) & 1u);
160 /* --- @mp_testbit2c@ --- *
162 * Arguments: @mp *x@ = a large integer
163 * @unsigned long n@ = which bit to test
165 * Returns: Nonzero if the bit is set, zero if not. Fakes up two's
166 * complement representation.
169 int mp_testbit2c(mp
*x
, unsigned long n
)
172 if (!(x
->f
& MP_NEG
))
173 return (mp_testbit(x
, n
));
174 x
= mp_not2c(MP_NEW
, x
);
175 r
= !mp_testbit(x
, n
);
180 /* --- @mp_setbit@, @mp_clearbit@ --- *
182 * Arguments: @mp *d@ = a destination
183 * @mp *x@ = a large integer
184 * @unsigned long n@ = which bit to modify
186 * Returns: The argument @x@, with the appropriate bit set or cleared.
189 mp
*mp_setbit(mp
*d
, mp
*x
, unsigned long n
)
193 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
198 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
199 d
->v
[n
/MPW_BITS
] |= 1 << n
%MPW_BITS
;
203 mp
*mp_clearbit(mp
*d
, mp
*x
, unsigned long n
)
207 rq
= n
+ MPW_BITS
; rq
-= rq
% MPW_BITS
;
212 MP_DEST(d
, rq
, x
->f
& (MP_NEG
| MP_BURN
));
213 d
->v
[n
/MPW_BITS
] &= ~(1 << n
%MPW_BITS
);
217 /* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
219 * Arguments: @mp *d@ = a destination
220 * @mp *x@ = a large integer
221 * @unsigned long n@ = which bit to modify
223 * Returns: The argument @x@, with the appropriate bit set or cleared.
224 * Fakes up two's complement representation.
227 mp
*mp_setbit2c(mp
*d
, mp
*x
, unsigned long n
)
229 if (!(x
->f
& MP_NEG
))
230 return mp_setbit(d
, x
, n
);
232 d
= mp_clearbit(d
, d
, n
);
237 mp
*mp_clearbit2c(mp
*d
, mp
*x
, unsigned long n
)
239 if (!(x
->f
& MP_NEG
))
240 return mp_clearbit(d
, x
, n
);
242 d
= mp_setbit(d
, d
, n
);
249 * Arguments: @const mp *a, *b@ = two numbers
251 * Returns: Nonzero if the numbers are equal.
254 int mp_eq(const mp
*a
, const mp
*b
) { return (MP_EQ(a
, b
)); }
256 /* --- @mp_cmp@ --- *
258 * Arguments: @const mp *a, *b@ = two numbers
260 * Returns: Less than, equal to or greater than zero, according to
261 * whether @a@ is less than, equal to or greater than @b@.
264 int mp_cmp(const mp
*a
, const mp
*b
)
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
)
274 /* --- @mp_bitop@ --- *
276 * Arguments: @mp *d@ = destination
277 * @mp *a, *b@ = sources
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:
289 #define MP_BITBINOP(string) \
291 mp *mp_bit##string(mp *d, mp *a, mp *b) \
293 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \
294 mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \
295 d->f = (a->f | b->f) & MP_BURN; \
300 MPX_DOBIN(MP_BITBINOP
)
302 /* --- @mp_not@ --- *
304 * Arguments: @mp *d@ = destination
307 * Returns: The bitwise complement of the source.
310 mp
*mp_not(mp
*d
, mp
*a
)
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
;
319 /* --- @mp_bitop2c@ --- *
321 * Arguments: @mp *d@ = destination
322 * @mp *a, *b@ = sources
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
334 /* --- How this actually works --- *
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
342 * Start with: wxyz WXYZ
343 * If @a@ negative: yzwx or YZWX
344 * If @b@ negative: xwzy XWZY
345 * If both negative: zyxw ZYXW
348 #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \
350 mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \
352 if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \
353 d = mp_bit##base(d, a, b); \
355 } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \
357 d = mp_not2c(d, a); \
358 d = mp_bit##an(d, d, b); \
361 } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \
363 d = mp_not2c(d, b); \
364 d = mp_bit##bn(d, a, d); \
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); \
377 #define NEG d = mp_not2c(d, d);
379 MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS
, POS
, POS
, POS
)
380 MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS
, POS
, POS
, NEG
)
381 MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS
, NEG
, POS
, POS
)
382 MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS
, NEG
, POS
, NEG
)
383 MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS
, POS
, NEG
, POS
)
384 MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS
, POS
, NEG
, NEG
)
385 MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS
, NEG
, NEG
, POS
)
386 MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS
, NEG
, NEG
, NEG
)
387 MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG
, POS
, POS
, POS
)
388 MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG
, POS
, POS
, NEG
)
389 MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG
, NEG
, POS
, POS
)
390 MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG
, NEG
, POS
, NEG
)
391 MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG
, POS
, NEG
, POS
)
392 MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG
, POS
, NEG
, NEG
)
393 MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG
, NEG
, NEG
, POS
)
394 MP_BIT2CBINOP(1111, 0000, 0000, 0000, 0000, NEG
, NEG
, NEG
, NEG
)
398 /* --- @mp_not2c@ --- *
400 * Arguments: @mp *d@ = destination
403 * Returns: The sign-extended complement of the argument.
406 mp
*mp_not2c(mp
*d
, mp
*a
)
410 MP_DEST(d
, MP_LEN(a
) + 1, a
->f
);
413 MPX_USUBN(d
->v
, d
->vl
, 1);
415 MPX_UADDN(d
->v
, d
->vl
, 1);
418 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
420 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, &one
, &one
+ 1);
422 d
->f
= (a
->f
& (MP_NEG
| MP_BURN
)) ^ MP_NEG
;
427 /* --- @mp_add@ --- *
429 * Arguments: @mp *d@ = destination
430 * @mp *a, *b@ = sources
432 * Returns: Result, @a@ added to @b@.
435 mp
*mp_add(mp
*d
, mp
*a
, mp
*b
)
437 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
438 if (!((a
->f
^ b
->f
) & MP_NEG
))
439 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
441 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
442 mp
*t
= a
; a
= b
; b
= t
;
444 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
446 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | (a
->f
& MP_NEG
);
451 /* --- @mp_sub@ --- *
453 * Arguments: @mp *d@ = destination
454 * @mp *a, *b@ = sources
456 * Returns: Result, @b@ subtracted from @a@.
459 mp
*mp_sub(mp
*d
, mp
*a
, mp
*b
)
462 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
463 if ((a
->f
^ b
->f
) & MP_NEG
)
464 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
466 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
467 mp
*t
= a
; a
= b
; b
= t
;
470 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
472 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ sgn
) & MP_NEG
);
477 /* --- @mp_mul@ --- *
479 * Arguments: @mp *d@ = destination
480 * @mp *a, *b@ = sources
482 * Returns: Result, @a@ multiplied by @b@.
485 mp
*mp_mul(mp
*d
, mp
*a
, mp
*b
)
490 if (MP_LEN(a
) <= MPK_THRESH
|| MP_LEN(b
) <= MPK_THRESH
) {
491 MP_DEST(d
, MP_LEN(a
) + MP_LEN(b
), a
->f
| b
->f
| MP_UNDEF
);
492 mpx_umul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
494 size_t m
= MAX(MP_LEN(a
), MP_LEN(b
));
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
);
502 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ b
->f
) & MP_NEG
);
509 /* --- @mp_sqr@ --- *
511 * Arguments: @mp *d@ = destination
514 * Returns: Result, @a@ squared.
517 mp
*mp_sqr(mp
*d
, mp
*a
)
519 size_t m
= MP_LEN(a
);
522 if (m
> MPK_THRESH
) {
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
);
529 MP_DEST(d
, 2 * m
+ 2, a
->f
| MP_UNDEF
);
530 mpx_usqr(d
->v
, d
->vl
, a
->v
, a
->vl
);
532 d
->f
= a
->f
& MP_BURN
;
538 /* --- @mp_div@ --- *
540 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
541 * @mp *a, *b@ = sources
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@.
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
557 void mp_div(mp
**qq
, mp
**rr
, mp
*a
, mp
*b
)
559 mp
*r
= rr ?
*rr
: MP_NEW
;
560 mp
*q
= qq ?
*qq
: MP_NEW
;
563 /* --- Set the remainder up right --- *
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.
574 MP_DEST(r
, MP_LEN(a
) + 2, a
->f
| b
->f
);
576 /* --- Fix up the quotient too --- */
579 MP_DEST(q
, MP_LEN(r
), r
->f
| MP_UNDEF
);
582 /* --- Set up some temporary workspace --- */
585 size_t rq
= MP_LEN(b
) + 1;
586 sv
= mpalloc(r
->a
, rq
);
590 /* --- Perform the calculation --- */
592 mpx_udiv(q
->v
, q
->vl
, r
->v
, r
->vl
, b
->v
, b
->vl
, sv
, svl
);
594 /* --- Sort out the sign of the results --- *
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@.
601 q
->f
= ((r
->f
| b
->f
) & MP_BURN
) | ((r
->f
^ b
->f
) & MP_NEG
);
604 for (v
= r
->v
; v
< r
->vl
; v
++) {
606 MPX_UADDN(q
->v
, q
->vl
, 1);
607 mpx_usub(r
->v
, r
->vl
, b
->v
, b
->vl
, r
->v
, r
->vl
);
613 r
->f
= ((r
->f
| b
->f
) & MP_BURN
) | (b
->f
& MP_NEG
);
615 /* --- Store the return values --- */
635 /* --- @mp_odd@ --- *
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
641 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
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.
648 mp
*mp_odd(mp
*d
, mp
*m
, size_t *s
)
655 for (; !*v
&& v
< vl
; v
++)
662 unsigned z
= MPW_BITS
/ 2;
675 return (mp_lsr(d
, m
, ss
));
678 /*----- Test rig ----------------------------------------------------------*/
682 static int verify(const char *op
, mp
*expect
, mp
*result
, mp
*a
, mp
*b
)
684 if (!MP_EQ(expect
, result
)) {
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);
696 #define RIG(name, op) \
697 static int t##name(dstr *v) \
699 mp *a = *(mp **)v[0].buf; \
700 mpw n = *(int *)v[1].buf; \
702 mp *r = *(mp **)v[2].buf; \
703 mp *c = op(MP_NEW, a, n); \
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); \
708 assert(mparena_count(MPARENA_GLOBAL) == 0); \
719 #define RIG(name, op) \
720 static int t##name(dstr *v) \
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); \
728 assert(mparena_count(MPARENA_GLOBAL) == 0); \
738 static int tdiv(dstr
*v
)
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
;
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
);
750 assert(mparena_count(MPARENA_GLOBAL
) == 0);
754 static int tbin(dstr
*v
)
756 static mp
*(*fn
[])(mp
*, mp
*, mp
*) = {
757 #define DO(string) mp_bit##string##2c,
763 mp
*a
= *(mp
**)v
[1].buf
;
764 mp
*b
= *(mp
**)v
[2].buf
;
765 mp
*r
= *(mp
**)v
[3].buf
;
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;
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);
789 static int tset(dstr
*v
)
791 mp
*a
= *(mp
**)v
[0].buf
;
792 unsigned long n
= *(unsigned long *)v
[1].buf
;
793 mp
*r
= *(mp
**)v
[2].buf
;
797 c
= mp_setbit2c(MP_NEW
, a
, n
);
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);
807 if (!mp_testbit2c(r
, n
)) {
809 fprintf(stderr
, "\n***setbit (test) failed");
810 fprintf(stderr
, "\n*** n = %lu", n
);
811 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
817 assert(mparena_count(MPARENA_GLOBAL
) == 0);
821 static int tclr(dstr
*v
)
823 mp
*a
= *(mp
**)v
[0].buf
;
824 unsigned long n
= *(unsigned long *)v
[1].buf
;
825 mp
*r
= *(mp
**)v
[2].buf
;
829 c
= mp_clearbit2c(MP_NEW
, a
, n
);
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);
839 if (mp_testbit2c(r
, n
)) {
841 fprintf(stderr
, "\n***clrbit (test) failed");
842 fprintf(stderr
, "\n*** n = %lu", n
);
843 fputs("\n*** r = ", stderr
); mp_writefile(r
, stderr
, 16);
849 assert(mparena_count(MPARENA_GLOBAL
) == 0);
853 static int todd(dstr
*v
)
855 mp
*a
= *(mp
**)v
[0].buf
;
856 size_t rs
= *(uint32
*)v
[1].buf
;
857 mp
*rt
= *(mp
**)v
[2].buf
;
861 t
= mp_odd(MP_NEW
, a
, &s
);
862 if (s
!= rs
|| !MP_EQ(t
, rt
)) {
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);
875 assert(mparena_count(MPARENA_GLOBAL
) == 0);
879 static test_chunk tests
[] = {
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 } },
884 { "setbit", tset
, { &type_mp
, &type_ulong
, &type_mp
, 0 } },
885 { "clrbit", tclr
, { &type_mp
, &type_ulong
, &type_mp
, 0 } },
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 } },
890 { "bin2c", tbin
, { &type_string
, &type_mp
, &type_mp
, &type_mp
, 0 } },
891 { "odd", todd
, { &type_mp
, &type_uint32
, &type_mp
, 0 } },
895 int main(int argc
, char *argv
[])
898 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
904 /*----- That's all, folks -------------------------------------------------*/