3 * $Id: mp-arith.c,v 1.6 2000/06/17 11:45:09 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.6 2000/06/17 11:45:09 mdw
34 * Major memory management overhaul. Added arena support. Use the secure
35 * arena for secret integers. Replace and improve the MP management macros
36 * (e.g., replace MP_MODIFY by MP_DEST).
38 * Revision 1.5 1999/12/22 15:54:41 mdw
39 * Adjust Karatsuba parameters. Calculate destination size better.
41 * Revision 1.4 1999/12/13 15:35:16 mdw
42 * Slightly different rules on memory allocation.
44 * Revision 1.3 1999/12/11 10:57:43 mdw
45 * Karatsuba squaring algorithm.
47 * Revision 1.2 1999/12/10 23:18:39 mdw
48 * Change interface for suggested destinations.
50 * Revision 1.1 1999/11/17 18:02:16 mdw
51 * New multiprecision integer arithmetic suite.
55 /*----- Header files ------------------------------------------------------*/
59 /*----- Macros ------------------------------------------------------------*/
61 #define MAX(x, y) ((x) >= (y) ? (x) : (y))
63 /*----- Main code ---------------------------------------------------------*/
67 * Arguments: @mp *a@ = source
69 * Returns: Result, @a@ converted to two's complement notation.
72 mp *mp_2c(mp *d, mp *a)
77 MP_DEST(d, MP_LEN(a), a->f);
78 mpx_2c(d->v, d->vl, a->v, a->vl);
79 d->f = a->f & MP_BURN;
86 * Arguments: @mp *d@ = destination
89 * Returns: Result, @a@ converted to the native signed-magnitude
93 mp *mp_sm(mp *d, mp *a)
95 if (!MP_LEN(a) || a->vl[-1] < MPW_MAX / 2)
98 MP_DEST(d, MP_LEN(a), a->f);
99 mpx_2c(d->v, d->vl, a->v, a->vl);
100 d->f = (a->f & (MP_BURN | MP_NEG)) ^ MP_NEG;
105 /* --- @mp_lsl@ --- *
107 * Arguments: @mp *d@ = destination
109 * @size_t n@ = number of bits to move
111 * Returns: Result, @a@ shifted left by @n@.
114 mp *mp_lsl(mp *d, mp *a, size_t n)
116 MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f);
117 mpx_lsl(d->v, d->vl, a->v, a->vl, n);
118 d->f = a->f & (MP_NEG | MP_BURN);
123 /* --- @mp_lsr@ --- *
125 * Arguments: @mp *d@ = destination
127 * @size_t n@ = number of bits to move
129 * Returns: Result, @a@ shifted left by @n@.
132 mp *mp_lsr(mp *d, mp *a, size_t n)
134 MP_DEST(d, MP_LEN(a), a->f);
135 mpx_lsr(d->v, d->vl, a->v, a->vl, n);
136 d->f = a->f & (MP_NEG | MP_BURN);
141 /* --- @mp_cmp@ --- *
143 * Arguments: @const mp *a, *b@ = two numbers
145 * Returns: Less than, equal to or greater than zero, according to
146 * whether @a@ is less than, equal to or greater than @b@.
149 int mp_cmp(const mp *a, const mp *b)
151 if (!((a->f ^ b->f) & MP_NEG))
152 return (mpx_ucmp(a->v, a->vl, b->v, b->vl));
153 else if (a->f & MP_NEG)
159 /* --- @mp_add@ --- *
161 * Arguments: @mp *d@ = destination
162 * @mp *a, *b@ = sources
164 * Returns: Result, @a@ added to @b@.
167 mp *mp_add(mp *d, mp *a, mp *b)
169 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
170 if (!((a->f ^ b->f) & MP_NEG))
171 mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
173 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
174 mp *t = a; a = b; b = t;
176 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
178 d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG);
183 /* --- @mp_sub@ --- *
185 * Arguments: @mp *d@ = destination
186 * @mp *a, *b@ = sources
188 * Returns: Result, @b@ subtracted from @a@.
191 mp *mp_sub(mp *d, mp *a, mp *b)
194 MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f);
195 if ((a->f ^ b->f) & MP_NEG)
196 mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
198 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
199 mp *t = a; a = b; b = t;
202 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
204 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG);
209 /* --- @mp_mul@ --- *
211 * Arguments: @mp *d@ = destination
212 * @mp *a, *b@ = sources
214 * Returns: Result, @a@ multiplied by @b@.
217 mp *mp_mul(mp *d, mp *a, mp *b)
222 if (MP_LEN(a) <= KARATSUBA_CUTOFF || MP_LEN(b) <= KARATSUBA_CUTOFF) {
223 MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF);
224 mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
226 size_t m = 2 * MAX(MP_LEN(a), MP_LEN(b)) + 2;
228 MP_DEST(d, m, a->f | b->f | MP_UNDEF);
230 s = mpalloc(d->a, m);
231 mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + m);
235 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
242 /* --- @mp_sqr@ --- *
244 * Arguments: @mp *d@ = destination
247 * Returns: Result, @a@ squared.
250 mp *mp_sqr(mp *d, mp *a)
252 size_t m = MP_LEN(a);
255 MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF);
256 if (m > KARATSUBA_CUTOFF) {
258 m = 2 * (m + 1) + KARATSUBA_SLOP;
259 s = mpalloc(d->a, m);
260 mpx_ksqr(d->v, d->vl, a->v, a->vl, s, s + m);
263 mpx_usqr(d->v, d->vl, a->v, a->vl);
264 d->f = a->f & MP_BURN;
270 /* --- @mp_div@ --- *
272 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
273 * @mp *a, *b@ = sources
275 * Use: Calculates the quotient and remainder when @a@ is divided by
276 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
277 * Either of @qq@ or @rr@ may be null to indicate that the
278 * result is irrelevant. (Discarding both results is silly.)
279 * There is a performance advantage if @a == *rr@.
281 * The behaviour when @a@ and @b@ have the same sign is
282 * straightforward. When the signs differ, this implementation
283 * chooses @r@ to have the same sign as @b@, rather than the
284 * more normal choice that the remainder has the same sign as
285 * the dividend. This makes modular arithmetic a little more
289 void mp_div(mp **qq, mp **rr, mp *a, mp *b)
291 mp *r = rr ? *rr : MP_NEW;
292 mp *q = qq ? *qq : MP_NEW;
295 /* --- Set the remainder up right --- *
297 * Just in case the divisor is larger, be able to cope with this. It's not
298 * important in @mpx_udiv@, but it is here because of the sign correction.
306 MP_DEST(r, MP_LEN(a) + 2, a->f | b->f);
308 /* --- Fix up the quotient too --- */
311 MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF);
314 /* --- Set up some temporary workspace --- */
317 size_t rq = MP_LEN(b) + 1;
318 sv = mpalloc(r->a, rq);
322 /* --- Perform the calculation --- */
324 mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl);
326 /* --- Sort out the sign of the results --- *
328 * If the signs of the arguments differ, and the remainder is nonzero, I
329 * must add one to the absolute value of the quotient and subtract the
330 * remainder from @b@.
333 q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG);
336 for (v = r->v; v < r->vl; v++) {
338 MPX_UADDN(q->v, q->vl, 1);
339 mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl);
345 r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG);
347 /* --- Store the return values --- */
367 /*----- Test rig ----------------------------------------------------------*/
371 static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
373 if (MP_CMP(expect, !=, result)) {
374 fprintf(stderr, "\n*** %s failed", op);
375 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
376 fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10);
377 fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10);
378 fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10);
385 #define RIG(name, op) \
386 static int t##name(dstr *v) \
388 mp *a = *(mp **)v[0].buf; \
389 mpw n = *(int *)v[1].buf; \
391 mp *r = *(mp **)v[2].buf; \
392 mp *c = op(MP_NEW, a, n); \
394 mp_build(&b, &n, &n + 1); \
395 ok = verify(#name, r, c, a, &b); \
396 mp_drop(a); mp_drop(c); mp_drop(r); \
397 assert(mparena_count(MPARENA_GLOBAL) == 0); \
406 #define RIG(name, op) \
407 static int t##name(dstr *v) \
409 mp *a = *(mp **)v[0].buf; \
410 mp *b = *(mp **)v[1].buf; \
411 mp *r = *(mp **)v[2].buf; \
412 mp *c = op(MP_NEW, a, b); \
413 int ok = verify(#name, r, c, a, b); \
414 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
415 assert(mparena_count(MPARENA_GLOBAL) == 0); \
425 static int tdiv(dstr *v)
427 mp *a = *(mp **)v[0].buf;
428 mp *b = *(mp **)v[1].buf;
429 mp *q = *(mp **)v[2].buf;
430 mp *r = *(mp **)v[3].buf;
431 mp *c = MP_NEW, *d = MP_NEW;
433 mp_div(&c, &d, a, b);
434 ok &= verify("div(quotient)", q, c, a, b);
435 ok &= verify("div(remainder)", r, d, a, b);
436 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
437 assert(mparena_count(MPARENA_GLOBAL) == 0);
441 static test_chunk tests[] = {
442 { "lsl", tlsl, { &type_mp, &type_mp, &type_mp, 0 } },
443 { "lsr", tlsr, { &type_mp, &type_mp, &type_mp, 0 } },
444 { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } },
445 { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } },
446 { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } },
447 { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
451 int main(int argc, char *argv[])
454 test_run(argc, argv, tests, SRCDIR "/tests/mp");
460 /*----- That's all, folks -------------------------------------------------*/