| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mp-arith.c,v 1.6 2000/06/17 11:45:09 mdw Exp $ |
| 4 | * |
| 5 | * Basic arithmetic on multiprecision integers |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 23 | * |
| 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, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 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). |
| 37 | * |
| 38 | * Revision 1.5 1999/12/22 15:54:41 mdw |
| 39 | * Adjust Karatsuba parameters. Calculate destination size better. |
| 40 | * |
| 41 | * Revision 1.4 1999/12/13 15:35:16 mdw |
| 42 | * Slightly different rules on memory allocation. |
| 43 | * |
| 44 | * Revision 1.3 1999/12/11 10:57:43 mdw |
| 45 | * Karatsuba squaring algorithm. |
| 46 | * |
| 47 | * Revision 1.2 1999/12/10 23:18:39 mdw |
| 48 | * Change interface for suggested destinations. |
| 49 | * |
| 50 | * Revision 1.1 1999/11/17 18:02:16 mdw |
| 51 | * New multiprecision integer arithmetic suite. |
| 52 | * |
| 53 | */ |
| 54 | |
| 55 | /*----- Header files ------------------------------------------------------*/ |
| 56 | |
| 57 | #include "mp.h" |
| 58 | |
| 59 | /*----- Macros ------------------------------------------------------------*/ |
| 60 | |
| 61 | #define MAX(x, y) ((x) >= (y) ? (x) : (y)) |
| 62 | |
| 63 | /*----- Main code ---------------------------------------------------------*/ |
| 64 | |
| 65 | /* --- @mp_2c@ --- * |
| 66 | * |
| 67 | * Arguments: @mp *a@ = source |
| 68 | * |
| 69 | * Returns: Result, @a@ converted to two's complement notation. |
| 70 | */ |
| 71 | |
| 72 | mp *mp_2c(mp *d, mp *a) |
| 73 | { |
| 74 | if (!(a->f & MP_NEG)) |
| 75 | return (MP_COPY(a)); |
| 76 | |
| 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; |
| 80 | MP_SHRINK(d); |
| 81 | return (d); |
| 82 | } |
| 83 | |
| 84 | /* --- @mp_sm@ --- * |
| 85 | * |
| 86 | * Arguments: @mp *d@ = destination |
| 87 | * @mp *a@ = source |
| 88 | * |
| 89 | * Returns: Result, @a@ converted to the native signed-magnitude |
| 90 | * notation. |
| 91 | */ |
| 92 | |
| 93 | mp *mp_sm(mp *d, mp *a) |
| 94 | { |
| 95 | if (!MP_LEN(a) || a->vl[-1] < MPW_MAX / 2) |
| 96 | return (MP_COPY(a)); |
| 97 | |
| 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; |
| 101 | MP_SHRINK(d); |
| 102 | return (d); |
| 103 | } |
| 104 | |
| 105 | /* --- @mp_lsl@ --- * |
| 106 | * |
| 107 | * Arguments: @mp *d@ = destination |
| 108 | * @mp *a@ = source |
| 109 | * @size_t n@ = number of bits to move |
| 110 | * |
| 111 | * Returns: Result, @a@ shifted left by @n@. |
| 112 | */ |
| 113 | |
| 114 | mp *mp_lsl(mp *d, mp *a, size_t n) |
| 115 | { |
| 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); |
| 119 | MP_SHRINK(d); |
| 120 | return (d); |
| 121 | } |
| 122 | |
| 123 | /* --- @mp_lsr@ --- * |
| 124 | * |
| 125 | * Arguments: @mp *d@ = destination |
| 126 | * @mp *a@ = source |
| 127 | * @size_t n@ = number of bits to move |
| 128 | * |
| 129 | * Returns: Result, @a@ shifted left by @n@. |
| 130 | */ |
| 131 | |
| 132 | mp *mp_lsr(mp *d, mp *a, size_t n) |
| 133 | { |
| 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); |
| 137 | MP_SHRINK(d); |
| 138 | return (d); |
| 139 | } |
| 140 | |
| 141 | /* --- @mp_cmp@ --- * |
| 142 | * |
| 143 | * Arguments: @const mp *a, *b@ = two numbers |
| 144 | * |
| 145 | * Returns: Less than, equal to or greater than zero, according to |
| 146 | * whether @a@ is less than, equal to or greater than @b@. |
| 147 | */ |
| 148 | |
| 149 | int mp_cmp(const mp *a, const mp *b) |
| 150 | { |
| 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) |
| 154 | return (-1); |
| 155 | else |
| 156 | return (+1); |
| 157 | } |
| 158 | |
| 159 | /* --- @mp_add@ --- * |
| 160 | * |
| 161 | * Arguments: @mp *d@ = destination |
| 162 | * @mp *a, *b@ = sources |
| 163 | * |
| 164 | * Returns: Result, @a@ added to @b@. |
| 165 | */ |
| 166 | |
| 167 | mp *mp_add(mp *d, mp *a, mp *b) |
| 168 | { |
| 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); |
| 172 | else { |
| 173 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 174 | mp *t = a; a = b; b = t; |
| 175 | } |
| 176 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 177 | } |
| 178 | d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG); |
| 179 | MP_SHRINK(d); |
| 180 | return (d); |
| 181 | } |
| 182 | |
| 183 | /* --- @mp_sub@ --- * |
| 184 | * |
| 185 | * Arguments: @mp *d@ = destination |
| 186 | * @mp *a, *b@ = sources |
| 187 | * |
| 188 | * Returns: Result, @b@ subtracted from @a@. |
| 189 | */ |
| 190 | |
| 191 | mp *mp_sub(mp *d, mp *a, mp *b) |
| 192 | { |
| 193 | unsigned sgn = 0; |
| 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); |
| 197 | else { |
| 198 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 199 | mp *t = a; a = b; b = t; |
| 200 | sgn = MP_NEG; |
| 201 | } |
| 202 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 203 | } |
| 204 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG); |
| 205 | MP_SHRINK(d); |
| 206 | return (d); |
| 207 | } |
| 208 | |
| 209 | /* --- @mp_mul@ --- * |
| 210 | * |
| 211 | * Arguments: @mp *d@ = destination |
| 212 | * @mp *a, *b@ = sources |
| 213 | * |
| 214 | * Returns: Result, @a@ multiplied by @b@. |
| 215 | */ |
| 216 | |
| 217 | mp *mp_mul(mp *d, mp *a, mp *b) |
| 218 | { |
| 219 | a = MP_COPY(a); |
| 220 | b = MP_COPY(b); |
| 221 | |
| 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); |
| 225 | } else { |
| 226 | size_t m = 2 * MAX(MP_LEN(a), MP_LEN(b)) + 2; |
| 227 | mpw *s; |
| 228 | MP_DEST(d, m, a->f | b->f | MP_UNDEF); |
| 229 | m += KARATSUBA_SLOP; |
| 230 | s = mpalloc(d->a, m); |
| 231 | mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + m); |
| 232 | mpfree(d->a, s); |
| 233 | } |
| 234 | |
| 235 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG); |
| 236 | MP_SHRINK(d); |
| 237 | MP_DROP(a); |
| 238 | MP_DROP(b); |
| 239 | return (d); |
| 240 | } |
| 241 | |
| 242 | /* --- @mp_sqr@ --- * |
| 243 | * |
| 244 | * Arguments: @mp *d@ = destination |
| 245 | * @mp *a@ = source |
| 246 | * |
| 247 | * Returns: Result, @a@ squared. |
| 248 | */ |
| 249 | |
| 250 | mp *mp_sqr(mp *d, mp *a) |
| 251 | { |
| 252 | size_t m = MP_LEN(a); |
| 253 | |
| 254 | a = MP_COPY(a); |
| 255 | MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF); |
| 256 | if (m > KARATSUBA_CUTOFF) { |
| 257 | mpw *s; |
| 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); |
| 261 | mpfree(d->a, s); |
| 262 | } else |
| 263 | mpx_usqr(d->v, d->vl, a->v, a->vl); |
| 264 | d->f = a->f & MP_BURN; |
| 265 | MP_SHRINK(d); |
| 266 | MP_DROP(a); |
| 267 | return (d); |
| 268 | } |
| 269 | |
| 270 | /* --- @mp_div@ --- * |
| 271 | * |
| 272 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder |
| 273 | * @mp *a, *b@ = sources |
| 274 | * |
| 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@. |
| 280 | * |
| 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 |
| 286 | * straightforward. |
| 287 | */ |
| 288 | |
| 289 | void mp_div(mp **qq, mp **rr, mp *a, mp *b) |
| 290 | { |
| 291 | mp *r = rr ? *rr : MP_NEW; |
| 292 | mp *q = qq ? *qq : MP_NEW; |
| 293 | mpw *sv, *svl; |
| 294 | |
| 295 | /* --- Set the remainder up right --- * |
| 296 | * |
| 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. |
| 299 | */ |
| 300 | |
| 301 | b = MP_COPY(b); |
| 302 | a = MP_COPY(a); |
| 303 | if (r) |
| 304 | MP_DROP(r); |
| 305 | r = a; |
| 306 | MP_DEST(r, MP_LEN(a) + 2, a->f | b->f); |
| 307 | |
| 308 | /* --- Fix up the quotient too --- */ |
| 309 | |
| 310 | r = MP_COPY(r); |
| 311 | MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF); |
| 312 | MP_DROP(r); |
| 313 | |
| 314 | /* --- Set up some temporary workspace --- */ |
| 315 | |
| 316 | { |
| 317 | size_t rq = MP_LEN(b) + 1; |
| 318 | sv = mpalloc(r->a, rq); |
| 319 | svl = sv + rq; |
| 320 | } |
| 321 | |
| 322 | /* --- Perform the calculation --- */ |
| 323 | |
| 324 | mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl); |
| 325 | |
| 326 | /* --- Sort out the sign of the results --- * |
| 327 | * |
| 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@. |
| 331 | */ |
| 332 | |
| 333 | q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG); |
| 334 | if (q->f & MP_NEG) { |
| 335 | mpw *v; |
| 336 | for (v = r->v; v < r->vl; v++) { |
| 337 | if (*v) { |
| 338 | MPX_UADDN(q->v, q->vl, 1); |
| 339 | mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl); |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG); |
| 346 | |
| 347 | /* --- Store the return values --- */ |
| 348 | |
| 349 | mpfree(r->a, sv); |
| 350 | MP_DROP(b); |
| 351 | |
| 352 | if (!qq) |
| 353 | MP_DROP(q); |
| 354 | else { |
| 355 | MP_SHRINK(q); |
| 356 | *qq = q; |
| 357 | } |
| 358 | |
| 359 | if (!rr) |
| 360 | MP_DROP(r); |
| 361 | else { |
| 362 | MP_SHRINK(r); |
| 363 | *rr = r; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /*----- Test rig ----------------------------------------------------------*/ |
| 368 | |
| 369 | #ifdef TEST_RIG |
| 370 | |
| 371 | static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b) |
| 372 | { |
| 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); |
| 379 | fputc('\n', stderr); |
| 380 | return (0); |
| 381 | } |
| 382 | return (1); |
| 383 | } |
| 384 | |
| 385 | #define RIG(name, op) \ |
| 386 | static int t##name(dstr *v) \ |
| 387 | { \ |
| 388 | mp *a = *(mp **)v[0].buf; \ |
| 389 | mpw n = *(int *)v[1].buf; \ |
| 390 | mp b; \ |
| 391 | mp *r = *(mp **)v[2].buf; \ |
| 392 | mp *c = op(MP_NEW, a, n); \ |
| 393 | int ok; \ |
| 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); \ |
| 398 | return (ok); \ |
| 399 | } |
| 400 | |
| 401 | RIG(lsl, mp_lsl) |
| 402 | RIG(lsr, mp_lsr) |
| 403 | |
| 404 | #undef RIG |
| 405 | |
| 406 | #define RIG(name, op) \ |
| 407 | static int t##name(dstr *v) \ |
| 408 | { \ |
| 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); \ |
| 416 | return (ok); \ |
| 417 | } |
| 418 | |
| 419 | RIG(add, mp_add) |
| 420 | RIG(sub, mp_sub) |
| 421 | RIG(mul, mp_mul) |
| 422 | |
| 423 | #undef RIG |
| 424 | |
| 425 | static int tdiv(dstr *v) |
| 426 | { |
| 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; |
| 432 | int ok = 1; |
| 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); |
| 438 | return (ok); |
| 439 | } |
| 440 | |
| 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 } }, |
| 448 | { 0, 0, { 0 } }, |
| 449 | }; |
| 450 | |
| 451 | int main(int argc, char *argv[]) |
| 452 | { |
| 453 | sub_init(); |
| 454 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); |
| 455 | return (0); |
| 456 | } |
| 457 | |
| 458 | #endif |
| 459 | |
| 460 | /*----- That's all, folks -------------------------------------------------*/ |