3 * $Id: mp-arith.c,v 1.8 2000/10/08 12:02:21 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.8 2000/10/08 12:02:21 mdw
34 * Use @MP_EQ@ instead of @MP_CMP@.
36 * Revision 1.7 2000/06/22 19:02:53 mdw
37 * New function @mp_odd@ to extract powers of two from an integer. This is
38 * common code from the Rabin-Miller test, RSA key recovery and modular
39 * square-root extraction.
41 * Revision 1.6 2000/06/17 11:45:09 mdw
42 * Major memory management overhaul. Added arena support. Use the secure
43 * arena for secret integers. Replace and improve the MP management macros
44 * (e.g., replace MP_MODIFY by MP_DEST).
46 * Revision 1.5 1999/12/22 15:54:41 mdw
47 * Adjust Karatsuba parameters. Calculate destination size better.
49 * Revision 1.4 1999/12/13 15:35:16 mdw
50 * Slightly different rules on memory allocation.
52 * Revision 1.3 1999/12/11 10:57:43 mdw
53 * Karatsuba squaring algorithm.
55 * Revision 1.2 1999/12/10 23:18:39 mdw
56 * Change interface for suggested destinations.
58 * Revision 1.1 1999/11/17 18:02:16 mdw
59 * New multiprecision integer arithmetic suite.
63 /*----- Header files ------------------------------------------------------*/
67 /*----- Macros ------------------------------------------------------------*/
69 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
71 /*----- Main code ---------------------------------------------------------*/
75 * Arguments: @mp *a@ = source
77 * Returns: Result, @a@ converted to two's complement notation.
80 mp
*mp_2c(mp
*d
, mp
*a
)
85 MP_DEST(d
, MP_LEN(a
), a
->f
);
86 mpx_2c(d
->v
, d
->vl
, a
->v
, a
->vl
);
87 d
->f
= a
->f
& MP_BURN
;
94 * Arguments: @mp *d@ = destination
97 * Returns: Result, @a@ converted to the native signed-magnitude
101 mp
*mp_sm(mp
*d
, mp
*a
)
103 if (!MP_LEN(a
) || a
->vl
[-1] < MPW_MAX
/ 2)
106 MP_DEST(d
, MP_LEN(a
), a
->f
);
107 mpx_2c(d
->v
, d
->vl
, a
->v
, a
->vl
);
108 d
->f
= (a
->f
& (MP_BURN
| MP_NEG
)) ^ MP_NEG
;
113 /* --- @mp_lsl@ --- *
115 * Arguments: @mp *d@ = destination
117 * @size_t n@ = number of bits to move
119 * Returns: Result, @a@ shifted left by @n@.
122 mp
*mp_lsl(mp
*d
, mp
*a
, size_t n
)
124 MP_DEST(d
, MP_LEN(a
) + (n
+ MPW_BITS
- 1) / MPW_BITS
, a
->f
);
125 mpx_lsl(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
126 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
131 /* --- @mp_lsr@ --- *
133 * Arguments: @mp *d@ = destination
135 * @size_t n@ = number of bits to move
137 * Returns: Result, @a@ shifted left by @n@.
140 mp
*mp_lsr(mp
*d
, mp
*a
, size_t n
)
142 MP_DEST(d
, MP_LEN(a
), a
->f
);
143 mpx_lsr(d
->v
, d
->vl
, a
->v
, a
->vl
, n
);
144 d
->f
= a
->f
& (MP_NEG
| MP_BURN
);
151 * Arguments: @const mp *a, *b@ = two numbers
153 * Returns: Nonzero if the numbers are equal.
156 int mp_eq(const mp
*a
, const mp
*b
) { return (MP_EQ(a
, b
)); }
158 /* --- @mp_cmp@ --- *
160 * Arguments: @const mp *a, *b@ = two numbers
162 * Returns: Less than, equal to or greater than zero, according to
163 * whether @a@ is less than, equal to or greater than @b@.
166 int mp_cmp(const mp
*a
, const mp
*b
)
168 if (!((a
->f
^ b
->f
) & MP_NEG
))
169 return (mpx_ucmp(a
->v
, a
->vl
, b
->v
, b
->vl
));
170 else if (a
->f
& MP_NEG
)
176 /* --- @mp_add@ --- *
178 * Arguments: @mp *d@ = destination
179 * @mp *a, *b@ = sources
181 * Returns: Result, @a@ added to @b@.
184 mp
*mp_add(mp
*d
, mp
*a
, mp
*b
)
186 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
187 if (!((a
->f
^ b
->f
) & MP_NEG
))
188 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
190 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
191 mp
*t
= a
; a
= b
; b
= t
;
193 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
195 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | (a
->f
& MP_NEG
);
200 /* --- @mp_sub@ --- *
202 * Arguments: @mp *d@ = destination
203 * @mp *a, *b@ = sources
205 * Returns: Result, @b@ subtracted from @a@.
208 mp
*mp_sub(mp
*d
, mp
*a
, mp
*b
)
211 MP_DEST(d
, MAX(MP_LEN(a
), MP_LEN(b
)) + 1, a
->f
| b
->f
);
212 if ((a
->f
^ b
->f
) & MP_NEG
)
213 mpx_uadd(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
215 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
216 mp
*t
= a
; a
= b
; b
= t
;
219 mpx_usub(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
221 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ sgn
) & MP_NEG
);
226 /* --- @mp_mul@ --- *
228 * Arguments: @mp *d@ = destination
229 * @mp *a, *b@ = sources
231 * Returns: Result, @a@ multiplied by @b@.
234 mp
*mp_mul(mp
*d
, mp
*a
, mp
*b
)
239 if (MP_LEN(a
) <= KARATSUBA_CUTOFF
|| MP_LEN(b
) <= KARATSUBA_CUTOFF
) {
240 MP_DEST(d
, MP_LEN(a
) + MP_LEN(b
), a
->f
| b
->f
| MP_UNDEF
);
241 mpx_umul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
);
243 size_t m
= 2 * MAX(MP_LEN(a
), MP_LEN(b
)) + 2;
245 MP_DEST(d
, m
, a
->f
| b
->f
| MP_UNDEF
);
247 s
= mpalloc(d
->a
, m
);
248 mpx_kmul(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
, s
, s
+ m
);
252 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ b
->f
) & MP_NEG
);
259 /* --- @mp_sqr@ --- *
261 * Arguments: @mp *d@ = destination
264 * Returns: Result, @a@ squared.
267 mp
*mp_sqr(mp
*d
, mp
*a
)
269 size_t m
= MP_LEN(a
);
272 MP_DEST(d
, 2 * m
+ 2, a
->f
| MP_UNDEF
);
273 if (m
> KARATSUBA_CUTOFF
) {
275 m
= 2 * (m
+ 1) + KARATSUBA_SLOP
;
276 s
= mpalloc(d
->a
, m
);
277 mpx_ksqr(d
->v
, d
->vl
, a
->v
, a
->vl
, s
, s
+ m
);
280 mpx_usqr(d
->v
, d
->vl
, a
->v
, a
->vl
);
281 d
->f
= a
->f
& MP_BURN
;
287 /* --- @mp_div@ --- *
289 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
290 * @mp *a, *b@ = sources
292 * Use: Calculates the quotient and remainder when @a@ is divided by
293 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
294 * Either of @qq@ or @rr@ may be null to indicate that the
295 * result is irrelevant. (Discarding both results is silly.)
296 * There is a performance advantage if @a == *rr@.
298 * The behaviour when @a@ and @b@ have the same sign is
299 * straightforward. When the signs differ, this implementation
300 * chooses @r@ to have the same sign as @b@, rather than the
301 * more normal choice that the remainder has the same sign as
302 * the dividend. This makes modular arithmetic a little more
306 void mp_div(mp
**qq
, mp
**rr
, mp
*a
, mp
*b
)
308 mp
*r
= rr ?
*rr
: MP_NEW
;
309 mp
*q
= qq ?
*qq
: MP_NEW
;
312 /* --- Set the remainder up right --- *
314 * Just in case the divisor is larger, be able to cope with this. It's not
315 * important in @mpx_udiv@, but it is here because of the sign correction.
323 MP_DEST(r
, MP_LEN(a
) + 2, a
->f
| b
->f
);
325 /* --- Fix up the quotient too --- */
328 MP_DEST(q
, MP_LEN(r
), r
->f
| MP_UNDEF
);
331 /* --- Set up some temporary workspace --- */
334 size_t rq
= MP_LEN(b
) + 1;
335 sv
= mpalloc(r
->a
, rq
);
339 /* --- Perform the calculation --- */
341 mpx_udiv(q
->v
, q
->vl
, r
->v
, r
->vl
, b
->v
, b
->vl
, sv
, svl
);
343 /* --- Sort out the sign of the results --- *
345 * If the signs of the arguments differ, and the remainder is nonzero, I
346 * must add one to the absolute value of the quotient and subtract the
347 * remainder from @b@.
350 q
->f
= ((r
->f
| b
->f
) & MP_BURN
) | ((r
->f
^ b
->f
) & MP_NEG
);
353 for (v
= r
->v
; v
< r
->vl
; v
++) {
355 MPX_UADDN(q
->v
, q
->vl
, 1);
356 mpx_usub(r
->v
, r
->vl
, b
->v
, b
->vl
, r
->v
, r
->vl
);
362 r
->f
= ((r
->f
| b
->f
) & MP_BURN
) | (b
->f
& MP_NEG
);
364 /* --- Store the return values --- */
384 /* --- @mp_odd@ --- *
386 * Arguments: @mp *d@ = pointer to destination integer
387 * @mp *m@ = pointer to source integer
388 * @size_t *s@ = where to store the power of 2
390 * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%.
392 * Use: Computes a power of two and an odd integer which, when
393 * multiplied, give a specified result. This sort of thing is
394 * useful in number theory quite often.
397 mp
*mp_odd(mp
*d
, mp
*m
, size_t *s
)
404 for (; !*v
&& v
< vl
; v
++)
411 unsigned z
= MPW_BITS
/ 2;
424 return (mp_lsr(d
, m
, ss
));
427 /*----- Test rig ----------------------------------------------------------*/
431 static int verify(const char *op
, mp
*expect
, mp
*result
, mp
*a
, mp
*b
)
433 if (!MP_EQ(expect
, result
)) {
434 fprintf(stderr
, "\n*** %s failed", op
);
435 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
436 fputs("\n*** b = ", stderr
); mp_writefile(b
, stderr
, 10);
437 fputs("\n*** result = ", stderr
); mp_writefile(result
, stderr
, 10);
438 fputs("\n*** expect = ", stderr
); mp_writefile(expect
, stderr
, 10);
445 #define RIG(name, op) \
446 static int t##name(dstr *v) \
448 mp *a = *(mp **)v[0].buf; \
449 mpw n = *(int *)v[1].buf; \
451 mp *r = *(mp **)v[2].buf; \
452 mp *c = op(MP_NEW, a, n); \
454 mp_build(&b, &n, &n + 1); \
455 ok = verify(#name, r, c, a, &b); \
456 mp_drop(a); mp_drop(c); mp_drop(r); \
457 assert(mparena_count(MPARENA_GLOBAL) == 0); \
466 #define RIG(name, op) \
467 static int t##name(dstr *v) \
469 mp *a = *(mp **)v[0].buf; \
470 mp *b = *(mp **)v[1].buf; \
471 mp *r = *(mp **)v[2].buf; \
472 mp *c = op(MP_NEW, a, b); \
473 int ok = verify(#name, r, c, a, b); \
474 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
475 assert(mparena_count(MPARENA_GLOBAL) == 0); \
485 static int tdiv(dstr
*v
)
487 mp
*a
= *(mp
**)v
[0].buf
;
488 mp
*b
= *(mp
**)v
[1].buf
;
489 mp
*q
= *(mp
**)v
[2].buf
;
490 mp
*r
= *(mp
**)v
[3].buf
;
491 mp
*c
= MP_NEW
, *d
= MP_NEW
;
493 mp_div(&c
, &d
, a
, b
);
494 ok
&= verify("div(quotient)", q
, c
, a
, b
);
495 ok
&= verify("div(remainder)", r
, d
, a
, b
);
496 mp_drop(a
); mp_drop(b
); mp_drop(c
); mp_drop(d
); mp_drop(r
); mp_drop(q
);
497 assert(mparena_count(MPARENA_GLOBAL
) == 0);
501 static int todd(dstr
*v
)
503 mp
*a
= *(mp
**)v
[0].buf
;
504 size_t rs
= *(uint32
*)v
[1].buf
;
505 mp
*rt
= *(mp
**)v
[2].buf
;
509 t
= mp_odd(MP_NEW
, a
, &s
);
510 if (s
!= rs
|| !MP_EQ(t
, rt
)) {
512 fprintf(stderr
, "\n*** odd failed");
513 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
514 fprintf(stderr
, "\n*** s = %lu", (unsigned long)s
);
515 fputs("\n*** t = ", stderr
); mp_writefile(t
, stderr
, 10);
516 fprintf(stderr
, "\n*** rs = %lu", (unsigned long)rs
);
517 fputs("\n*** rt = ", stderr
); mp_writefile(rt
, stderr
, 10);
526 static test_chunk tests
[] = {
527 { "lsl", tlsl
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
528 { "lsr", tlsr
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
529 { "add", tadd
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
530 { "sub", tsub
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
531 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
532 { "div", tdiv
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
533 { "odd", todd
, { &type_mp
, &type_uint32
, &type_mp
, 0 } },
537 int main(int argc
, char *argv
[])
540 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
546 /*----- That's all, folks -------------------------------------------------*/