| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mp-arith.c,v 1.16.2.2 2004/03/20 00:14:03 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.16.2.2 2004/03/20 00:14:03 mdw |
| 34 | * Bug fix. |
| 35 | * |
| 36 | * Revision 1.16.2.1 2003/06/10 13:21:10 mdw |
| 37 | * Fix bug dividing small things by large ones. |
| 38 | * |
| 39 | * Revision 1.16 2003/05/16 09:09:24 mdw |
| 40 | * Fix @mp_lsl2c@. Turns out to be surprisingly tricky. |
| 41 | * |
| 42 | * Revision 1.15 2002/10/19 17:56:50 mdw |
| 43 | * Fix bit operations. Test them (a bit) better. |
| 44 | * |
| 45 | * Revision 1.14 2002/10/15 19:18:31 mdw |
| 46 | * New operation to negate numbers. |
| 47 | * |
| 48 | * Revision 1.13 2002/10/15 00:19:40 mdw |
| 49 | * Bit setting and clearing functions. |
| 50 | * |
| 51 | * Revision 1.12 2002/10/09 00:36:03 mdw |
| 52 | * Fix bounds on workspace for Karatsuba operations. |
| 53 | * |
| 54 | * Revision 1.11 2002/10/06 22:52:50 mdw |
| 55 | * Pile of changes for supporting two's complement properly. |
| 56 | * |
| 57 | * Revision 1.10 2001/04/03 19:36:05 mdw |
| 58 | * Add some simple bitwise operations so that Perl can use them. |
| 59 | * |
| 60 | * Revision 1.9 2000/10/08 15:48:35 mdw |
| 61 | * Rename Karatsuba constants now that we have @gfx_kmul@ too. |
| 62 | * |
| 63 | * Revision 1.8 2000/10/08 12:02:21 mdw |
| 64 | * Use @MP_EQ@ instead of @MP_CMP@. |
| 65 | * |
| 66 | * Revision 1.7 2000/06/22 19:02:53 mdw |
| 67 | * New function @mp_odd@ to extract powers of two from an integer. This is |
| 68 | * common code from the Rabin-Miller test, RSA key recovery and modular |
| 69 | * square-root extraction. |
| 70 | * |
| 71 | * Revision 1.6 2000/06/17 11:45:09 mdw |
| 72 | * Major memory management overhaul. Added arena support. Use the secure |
| 73 | * arena for secret integers. Replace and improve the MP management macros |
| 74 | * (e.g., replace MP_MODIFY by MP_DEST). |
| 75 | * |
| 76 | * Revision 1.5 1999/12/22 15:54:41 mdw |
| 77 | * Adjust Karatsuba parameters. Calculate destination size better. |
| 78 | * |
| 79 | * Revision 1.4 1999/12/13 15:35:16 mdw |
| 80 | * Slightly different rules on memory allocation. |
| 81 | * |
| 82 | * Revision 1.3 1999/12/11 10:57:43 mdw |
| 83 | * Karatsuba squaring algorithm. |
| 84 | * |
| 85 | * Revision 1.2 1999/12/10 23:18:39 mdw |
| 86 | * Change interface for suggested destinations. |
| 87 | * |
| 88 | * Revision 1.1 1999/11/17 18:02:16 mdw |
| 89 | * New multiprecision integer arithmetic suite. |
| 90 | * |
| 91 | */ |
| 92 | |
| 93 | /*----- Header files ------------------------------------------------------*/ |
| 94 | |
| 95 | #include "mp.h" |
| 96 | |
| 97 | /*----- Macros ------------------------------------------------------------*/ |
| 98 | |
| 99 | #define MAX(x, y) ((x) >= (y) ? (x) : (y)) |
| 100 | |
| 101 | /*----- Main code ---------------------------------------------------------*/ |
| 102 | |
| 103 | /* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- * |
| 104 | * |
| 105 | * Arguments: @mp *d@ = destination |
| 106 | * @mp *a@ = source |
| 107 | * @size_t n@ = number of bits to move |
| 108 | * |
| 109 | * Returns: Result, @a@ shifted left or right by @n@. |
| 110 | * |
| 111 | * Use: Bitwise shift operators. @mp_lslc@ fills the bits introduced |
| 112 | * on the right with ones instead of zeroes: it's used |
| 113 | * internally by @mp_lsl2c@, though it may be useful on its |
| 114 | * own. |
| 115 | */ |
| 116 | |
| 117 | mp *mp_lsl(mp *d, mp *a, size_t n) |
| 118 | { |
| 119 | MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f); |
| 120 | mpx_lsl(d->v, d->vl, a->v, a->vl, n); |
| 121 | d->f = a->f & (MP_NEG | MP_BURN); |
| 122 | MP_SHRINK(d); |
| 123 | return (d); |
| 124 | } |
| 125 | |
| 126 | mp *mp_lslc(mp *d, mp *a, size_t n) |
| 127 | { |
| 128 | MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f); |
| 129 | mpx_lslc(d->v, d->vl, a->v, a->vl, n); |
| 130 | d->f = a->f & (MP_NEG | MP_BURN); |
| 131 | MP_SHRINK(d); |
| 132 | return (d); |
| 133 | } |
| 134 | |
| 135 | mp *mp_lsr(mp *d, mp *a, size_t n) |
| 136 | { |
| 137 | MP_DEST(d, MP_LEN(a), a->f); |
| 138 | mpx_lsr(d->v, d->vl, a->v, a->vl, n); |
| 139 | d->f = a->f & (MP_NEG | MP_BURN); |
| 140 | MP_SHRINK(d); |
| 141 | return (d); |
| 142 | } |
| 143 | |
| 144 | /* --- @mp_lsl2c@, @mp_lsr2c@ --- * |
| 145 | * |
| 146 | * Arguments: @mp *d@ = destination |
| 147 | * @mp *a@ = source |
| 148 | * @size_t n@ = number of bits to move |
| 149 | * |
| 150 | * Returns: Result, @a@ shifted left or right by @n@. Handles the |
| 151 | * pretence of sign-extension for negative numbers. |
| 152 | */ |
| 153 | |
| 154 | mp *mp_lsl2c(mp *d, mp *a, size_t n) |
| 155 | { |
| 156 | if (!(a->f & MP_NEG)) |
| 157 | return (mp_lsl(d, a, n)); |
| 158 | d = mp_not2c(d, a); |
| 159 | d = mp_lslc(d, d, n); |
| 160 | d = mp_not2c(d, d); |
| 161 | return (d); |
| 162 | } |
| 163 | |
| 164 | mp *mp_lsr2c(mp *d, mp *a, size_t n) |
| 165 | { |
| 166 | if (!(a->f & MP_NEG)) |
| 167 | return (mp_lsr(d, a, n)); |
| 168 | d = mp_not2c(d, a); |
| 169 | d = mp_lsr(d, d, n); |
| 170 | d = mp_not2c(d, d); |
| 171 | return (d); |
| 172 | } |
| 173 | |
| 174 | /* --- @mp_testbit@ --- * |
| 175 | * |
| 176 | * Arguments: @mp *x@ = a large integer |
| 177 | * @unsigned long n@ = which bit to test |
| 178 | * |
| 179 | * Returns: Nonzero if the bit is set, zero if not. |
| 180 | */ |
| 181 | |
| 182 | int mp_testbit(mp *x, unsigned long n) |
| 183 | { |
| 184 | if (n > MPW_BITS * MP_LEN(x)) |
| 185 | return (0); |
| 186 | return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u); |
| 187 | } |
| 188 | |
| 189 | /* --- @mp_testbit2c@ --- * |
| 190 | * |
| 191 | * Arguments: @mp *x@ = a large integer |
| 192 | * @unsigned long n@ = which bit to test |
| 193 | * |
| 194 | * Returns: Nonzero if the bit is set, zero if not. Fakes up two's |
| 195 | * complement representation. |
| 196 | */ |
| 197 | |
| 198 | int mp_testbit2c(mp *x, unsigned long n) |
| 199 | { |
| 200 | int r; |
| 201 | if (!(x->f & MP_NEG)) |
| 202 | return (mp_testbit(x, n)); |
| 203 | x = mp_not2c(MP_NEW, x); |
| 204 | r = !mp_testbit(x, n); |
| 205 | MP_DROP(x); |
| 206 | return (r); |
| 207 | } |
| 208 | |
| 209 | /* --- @mp_setbit@, @mp_clearbit@ --- * |
| 210 | * |
| 211 | * Arguments: @mp *d@ = a destination |
| 212 | * @mp *x@ = a large integer |
| 213 | * @unsigned long n@ = which bit to modify |
| 214 | * |
| 215 | * Returns: The argument @x@, with the appropriate bit set or cleared. |
| 216 | */ |
| 217 | |
| 218 | mp *mp_setbit(mp *d, mp *x, unsigned long n) |
| 219 | { |
| 220 | size_t rq; |
| 221 | |
| 222 | rq = n + MPW_BITS; rq -= rq % MPW_BITS; |
| 223 | if (d != x) { |
| 224 | if (d) MP_DROP(d); |
| 225 | d = MP_COPY(x); |
| 226 | } |
| 227 | MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); |
| 228 | d->v[n/MPW_BITS] |= 1 << n%MPW_BITS; |
| 229 | return (d); |
| 230 | } |
| 231 | |
| 232 | mp *mp_clearbit(mp *d, mp *x, unsigned long n) |
| 233 | { |
| 234 | size_t rq; |
| 235 | |
| 236 | rq = n + MPW_BITS; rq -= rq % MPW_BITS; |
| 237 | if (d != x) { |
| 238 | if (d) MP_DROP(d); |
| 239 | d = MP_COPY(x); |
| 240 | } |
| 241 | MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); |
| 242 | d->v[n/MPW_BITS] &= ~(1 << n%MPW_BITS); |
| 243 | return (d); |
| 244 | } |
| 245 | |
| 246 | /* --- @mp_setbit2c@, @mp_clearbit2c@ --- * |
| 247 | * |
| 248 | * Arguments: @mp *d@ = a destination |
| 249 | * @mp *x@ = a large integer |
| 250 | * @unsigned long n@ = which bit to modify |
| 251 | * |
| 252 | * Returns: The argument @x@, with the appropriate bit set or cleared. |
| 253 | * Fakes up two's complement representation. |
| 254 | */ |
| 255 | |
| 256 | mp *mp_setbit2c(mp *d, mp *x, unsigned long n) |
| 257 | { |
| 258 | if (!(x->f & MP_NEG)) |
| 259 | return mp_setbit(d, x, n); |
| 260 | d = mp_not2c(d, x); |
| 261 | d = mp_clearbit(d, d, n); |
| 262 | d = mp_not2c(d, d); |
| 263 | return (d); |
| 264 | } |
| 265 | |
| 266 | mp *mp_clearbit2c(mp *d, mp *x, unsigned long n) |
| 267 | { |
| 268 | if (!(x->f & MP_NEG)) |
| 269 | return mp_clearbit(d, x, n); |
| 270 | d = mp_not2c(d, x); |
| 271 | d = mp_setbit(d, d, n); |
| 272 | d = mp_not2c(d, d); |
| 273 | return (d); |
| 274 | } |
| 275 | |
| 276 | /* --- @mp_eq@ --- * |
| 277 | * |
| 278 | * Arguments: @const mp *a, *b@ = two numbers |
| 279 | * |
| 280 | * Returns: Nonzero if the numbers are equal. |
| 281 | */ |
| 282 | |
| 283 | int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); } |
| 284 | |
| 285 | /* --- @mp_cmp@ --- * |
| 286 | * |
| 287 | * Arguments: @const mp *a, *b@ = two numbers |
| 288 | * |
| 289 | * Returns: Less than, equal to or greater than zero, according to |
| 290 | * whether @a@ is less than, equal to or greater than @b@. |
| 291 | */ |
| 292 | |
| 293 | int mp_cmp(const mp *a, const mp *b) |
| 294 | { |
| 295 | if (!((a->f ^ b->f) & MP_NEG)) |
| 296 | return (mpx_ucmp(a->v, a->vl, b->v, b->vl)); |
| 297 | else if (a->f & MP_NEG) |
| 298 | return (-1); |
| 299 | else |
| 300 | return (+1); |
| 301 | } |
| 302 | |
| 303 | /* --- @mp_neg@ --- * |
| 304 | * |
| 305 | * Arguments: @mp *d@ = destination |
| 306 | * @mp *a@ = argument |
| 307 | * |
| 308 | * Returns: The negation of the argument. |
| 309 | * |
| 310 | * Use: Negates its argument. |
| 311 | */ |
| 312 | |
| 313 | mp *mp_neg(mp *d, mp *a) |
| 314 | { |
| 315 | /* --- Surprising amounts of messing about required --- */ |
| 316 | |
| 317 | MP_SHRINK(a); |
| 318 | MP_COPY(a); |
| 319 | if (d) |
| 320 | MP_DROP(d); |
| 321 | if (a->v == a->vl) |
| 322 | return (a); |
| 323 | MP_DEST(a, MP_LEN(a), a->f); |
| 324 | a->f ^= MP_NEG; |
| 325 | return (a); |
| 326 | } |
| 327 | |
| 328 | /* --- @mp_bitop@ --- * |
| 329 | * |
| 330 | * Arguments: @mp *d@ = destination |
| 331 | * @mp *a, *b@ = sources |
| 332 | * |
| 333 | * Returns: The result of the given bitwise operation. These functions |
| 334 | * don't handle negative numbers at all sensibly. For that, use |
| 335 | * the @...2c@ variants. The functions are named after the |
| 336 | * truth tables they generate: |
| 337 | * |
| 338 | * a: 0011 |
| 339 | * b: 0101 |
| 340 | * @mpx_bitXXXX@ |
| 341 | */ |
| 342 | |
| 343 | #define MP_BITBINOP(string) \ |
| 344 | \ |
| 345 | mp *mp_bit##string(mp *d, mp *a, mp *b) \ |
| 346 | { \ |
| 347 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \ |
| 348 | mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \ |
| 349 | d->f = (a->f | b->f) & MP_BURN; \ |
| 350 | MP_SHRINK(d); \ |
| 351 | return (d); \ |
| 352 | } |
| 353 | |
| 354 | MPX_DOBIN(MP_BITBINOP) |
| 355 | |
| 356 | /* --- @mp_not@ --- * |
| 357 | * |
| 358 | * Arguments: @mp *d@ = destination |
| 359 | * @mp *a@ = source |
| 360 | * |
| 361 | * Returns: The bitwise complement of the source. |
| 362 | */ |
| 363 | |
| 364 | mp *mp_not(mp *d, mp *a) |
| 365 | { |
| 366 | MP_DEST(d, MP_LEN(a), a->f); |
| 367 | mpx_not(d->v, d->vl, a->v, a->vl); |
| 368 | d->f = a->f & MP_BURN; |
| 369 | MP_SHRINK(d); |
| 370 | return (d); |
| 371 | } |
| 372 | |
| 373 | /* --- @mp_bitop2c@ --- * |
| 374 | * |
| 375 | * Arguments: @mp *d@ = destination |
| 376 | * @mp *a, *b@ = sources |
| 377 | * |
| 378 | * Returns: The result of the given bitwise operation. Negative numbers |
| 379 | * are treated as two's complement, sign-extended infinitely to |
| 380 | * the left. The functions are named after the truth tables |
| 381 | * they generate: |
| 382 | * |
| 383 | * a: 0011 |
| 384 | * b: 0101 |
| 385 | * @mpx_bitXXXX@ |
| 386 | */ |
| 387 | |
| 388 | /* --- How this actually works --- * |
| 389 | * |
| 390 | * The two arguments are inverted (with a sign-swap) if they're currently |
| 391 | * negative. This means that we end up using a different function (one which |
| 392 | * reinverts as we go) for the main operation. Also, if the sign would be |
| 393 | * negative at the end, we preinvert the output and then invert again with a |
| 394 | * sign-swap. |
| 395 | * |
| 396 | * Start with: wxyz WXYZ |
| 397 | * If @a@ negative: yzwx or YZWX |
| 398 | * If @b@ negative: xwzy XWZY |
| 399 | * If both negative: zyxw ZYXW |
| 400 | */ |
| 401 | |
| 402 | #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \ |
| 403 | \ |
| 404 | mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \ |
| 405 | { \ |
| 406 | if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \ |
| 407 | d = mp_bit##base(d, a, b); \ |
| 408 | p_base \ |
| 409 | } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \ |
| 410 | MP_COPY(b); \ |
| 411 | d = mp_not2c(d, a); \ |
| 412 | d = mp_bit##an(d, d, b); \ |
| 413 | MP_DROP(b); \ |
| 414 | p_an \ |
| 415 | } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \ |
| 416 | MP_COPY(a); \ |
| 417 | d = mp_not2c(d, b); \ |
| 418 | d = mp_bit##bn(d, a, d); \ |
| 419 | MP_DROP(a); \ |
| 420 | p_bn \ |
| 421 | } else { /* Both negative */ \ |
| 422 | mp *t = mp_not2c(MP_NEW, a); \ |
| 423 | mp *d = mp_not2c(d, b); \ |
| 424 | d = mp_bit##abn(d, t, d); \ |
| 425 | MP_DROP(t); \ |
| 426 | p_abn \ |
| 427 | } \ |
| 428 | return (d); \ |
| 429 | } \ |
| 430 | |
| 431 | #define NEG d = mp_not2c(d, d); |
| 432 | #define POS |
| 433 | MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS, POS, POS, POS) |
| 434 | MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS, POS, POS, NEG) |
| 435 | MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS, NEG, POS, POS) |
| 436 | MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS, NEG, POS, NEG) |
| 437 | MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS, POS, NEG, POS) |
| 438 | MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS, POS, NEG, NEG) |
| 439 | MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS, NEG, NEG, POS) |
| 440 | MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS, NEG, NEG, NEG) |
| 441 | MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG, POS, POS, POS) |
| 442 | MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG, POS, POS, NEG) |
| 443 | MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG, NEG, POS, POS) |
| 444 | MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG, NEG, POS, NEG) |
| 445 | MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG, POS, NEG, POS) |
| 446 | MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG, POS, NEG, NEG) |
| 447 | MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG, NEG, NEG, POS) |
| 448 | MP_BIT2CBINOP(1111, 0000, 0000, 0000, 0000, NEG, NEG, NEG, NEG) |
| 449 | #undef NEG |
| 450 | #undef POS |
| 451 | |
| 452 | /* --- @mp_not2c@ --- * |
| 453 | * |
| 454 | * Arguments: @mp *d@ = destination |
| 455 | * @mp *a@ = source |
| 456 | * |
| 457 | * Returns: The sign-extended complement of the argument. |
| 458 | */ |
| 459 | |
| 460 | mp *mp_not2c(mp *d, mp *a) |
| 461 | { |
| 462 | mpw one = 1; |
| 463 | |
| 464 | MP_DEST(d, MP_LEN(a) + 1, a->f); |
| 465 | if (d == a) { |
| 466 | if (a->f & MP_NEG) |
| 467 | MPX_USUBN(d->v, d->vl, 1); |
| 468 | else |
| 469 | MPX_UADDN(d->v, d->vl, 1); |
| 470 | } else { |
| 471 | if (a->f & MP_NEG) |
| 472 | mpx_usub(d->v, d->vl, a->v, a->vl, &one, &one + 1); |
| 473 | else |
| 474 | mpx_uadd(d->v, d->vl, a->v, a->vl, &one, &one + 1); |
| 475 | } |
| 476 | d->f = (a->f & (MP_NEG | MP_BURN)) ^ MP_NEG; |
| 477 | MP_SHRINK(d); |
| 478 | return (d); |
| 479 | } |
| 480 | |
| 481 | /* --- @mp_add@ --- * |
| 482 | * |
| 483 | * Arguments: @mp *d@ = destination |
| 484 | * @mp *a, *b@ = sources |
| 485 | * |
| 486 | * Returns: Result, @a@ added to @b@. |
| 487 | */ |
| 488 | |
| 489 | mp *mp_add(mp *d, mp *a, mp *b) |
| 490 | { |
| 491 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f); |
| 492 | if (!((a->f ^ b->f) & MP_NEG)) |
| 493 | mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 494 | else { |
| 495 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 496 | mp *t = a; a = b; b = t; |
| 497 | } |
| 498 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 499 | } |
| 500 | d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG); |
| 501 | MP_SHRINK(d); |
| 502 | return (d); |
| 503 | } |
| 504 | |
| 505 | /* --- @mp_sub@ --- * |
| 506 | * |
| 507 | * Arguments: @mp *d@ = destination |
| 508 | * @mp *a, *b@ = sources |
| 509 | * |
| 510 | * Returns: Result, @b@ subtracted from @a@. |
| 511 | */ |
| 512 | |
| 513 | mp *mp_sub(mp *d, mp *a, mp *b) |
| 514 | { |
| 515 | unsigned sgn = 0; |
| 516 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f); |
| 517 | if ((a->f ^ b->f) & MP_NEG) |
| 518 | mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 519 | else { |
| 520 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 521 | mp *t = a; a = b; b = t; |
| 522 | sgn = MP_NEG; |
| 523 | } |
| 524 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 525 | } |
| 526 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG); |
| 527 | MP_SHRINK(d); |
| 528 | return (d); |
| 529 | } |
| 530 | |
| 531 | /* --- @mp_mul@ --- * |
| 532 | * |
| 533 | * Arguments: @mp *d@ = destination |
| 534 | * @mp *a, *b@ = sources |
| 535 | * |
| 536 | * Returns: Result, @a@ multiplied by @b@. |
| 537 | */ |
| 538 | |
| 539 | mp *mp_mul(mp *d, mp *a, mp *b) |
| 540 | { |
| 541 | a = MP_COPY(a); |
| 542 | b = MP_COPY(b); |
| 543 | |
| 544 | if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= MPK_THRESH) { |
| 545 | MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF); |
| 546 | mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 547 | } else { |
| 548 | size_t m = MAX(MP_LEN(a), MP_LEN(b)); |
| 549 | mpw *s; |
| 550 | MP_DEST(d, 3 * m, a->f | b->f | MP_UNDEF); |
| 551 | s = mpalloc(d->a, 5 * m); |
| 552 | mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + 5 * m); |
| 553 | mpfree(d->a, s); |
| 554 | } |
| 555 | |
| 556 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG); |
| 557 | MP_SHRINK(d); |
| 558 | MP_DROP(a); |
| 559 | MP_DROP(b); |
| 560 | return (d); |
| 561 | } |
| 562 | |
| 563 | /* --- @mp_sqr@ --- * |
| 564 | * |
| 565 | * Arguments: @mp *d@ = destination |
| 566 | * @mp *a@ = source |
| 567 | * |
| 568 | * Returns: Result, @a@ squared. |
| 569 | */ |
| 570 | |
| 571 | mp *mp_sqr(mp *d, mp *a) |
| 572 | { |
| 573 | size_t m = MP_LEN(a); |
| 574 | |
| 575 | a = MP_COPY(a); |
| 576 | if (m > MPK_THRESH) { |
| 577 | mpw *s; |
| 578 | MP_DEST(d, 3 * m, a->f | MP_UNDEF); |
| 579 | s = mpalloc(d->a, 5 * m); |
| 580 | mpx_ksqr(d->v, d->vl, a->v, a->vl, s, s + 5 * m); |
| 581 | mpfree(d->a, s); |
| 582 | } else { |
| 583 | MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF); |
| 584 | mpx_usqr(d->v, d->vl, a->v, a->vl); |
| 585 | } |
| 586 | d->f = a->f & MP_BURN; |
| 587 | MP_SHRINK(d); |
| 588 | MP_DROP(a); |
| 589 | return (d); |
| 590 | } |
| 591 | |
| 592 | /* --- @mp_div@ --- * |
| 593 | * |
| 594 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder |
| 595 | * @mp *a, *b@ = sources |
| 596 | * |
| 597 | * Use: Calculates the quotient and remainder when @a@ is divided by |
| 598 | * @b@. The destinations @*qq@ and @*rr@ must be distinct. |
| 599 | * Either of @qq@ or @rr@ may be null to indicate that the |
| 600 | * result is irrelevant. (Discarding both results is silly.) |
| 601 | * There is a performance advantage if @a == *rr@. |
| 602 | * |
| 603 | * The behaviour when @a@ and @b@ have the same sign is |
| 604 | * straightforward. When the signs differ, this implementation |
| 605 | * chooses @r@ to have the same sign as @b@, rather than the |
| 606 | * more normal choice that the remainder has the same sign as |
| 607 | * the dividend. This makes modular arithmetic a little more |
| 608 | * straightforward. |
| 609 | */ |
| 610 | |
| 611 | void mp_div(mp **qq, mp **rr, mp *a, mp *b) |
| 612 | { |
| 613 | mp *r = rr ? *rr : MP_NEW; |
| 614 | mp *q = qq ? *qq : MP_NEW; |
| 615 | mpw *sv, *svl; |
| 616 | |
| 617 | /* --- Set the remainder up right --- * |
| 618 | * |
| 619 | * Just in case the divisor is larger, be able to cope with this. It's not |
| 620 | * important in @mpx_udiv@, but it is here because of the sign correction. |
| 621 | */ |
| 622 | |
| 623 | b = MP_COPY(b); |
| 624 | a = MP_COPY(a); |
| 625 | if (r) |
| 626 | MP_DROP(r); |
| 627 | r = a; |
| 628 | MP_DEST(r, MAX(MP_LEN(a), MP_LEN(b)) + 2, a->f | b->f); |
| 629 | |
| 630 | /* --- Fix up the quotient too --- */ |
| 631 | |
| 632 | r = MP_COPY(r); |
| 633 | MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF); |
| 634 | MP_DROP(r); |
| 635 | |
| 636 | /* --- Set up some temporary workspace --- */ |
| 637 | |
| 638 | { |
| 639 | size_t rq = MP_LEN(b) + 1; |
| 640 | sv = mpalloc(r->a, rq); |
| 641 | svl = sv + rq; |
| 642 | } |
| 643 | |
| 644 | /* --- Perform the calculation --- */ |
| 645 | |
| 646 | mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl); |
| 647 | |
| 648 | /* --- Sort out the sign of the results --- * |
| 649 | * |
| 650 | * If the signs of the arguments differ, and the remainder is nonzero, I |
| 651 | * must add one to the absolute value of the quotient and subtract the |
| 652 | * remainder from @b@. |
| 653 | */ |
| 654 | |
| 655 | q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG); |
| 656 | if (q->f & MP_NEG) { |
| 657 | mpw *v; |
| 658 | for (v = r->v; v < r->vl; v++) { |
| 659 | if (*v) { |
| 660 | MPX_UADDN(q->v, q->vl, 1); |
| 661 | mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl); |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG); |
| 668 | |
| 669 | /* --- Store the return values --- */ |
| 670 | |
| 671 | mpfree(r->a, sv); |
| 672 | MP_DROP(b); |
| 673 | |
| 674 | if (!qq) |
| 675 | MP_DROP(q); |
| 676 | else { |
| 677 | MP_SHRINK(q); |
| 678 | *qq = q; |
| 679 | } |
| 680 | |
| 681 | if (!rr) |
| 682 | MP_DROP(r); |
| 683 | else { |
| 684 | MP_SHRINK(r); |
| 685 | *rr = r; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* --- @mp_odd@ --- * |
| 690 | * |
| 691 | * Arguments: @mp *d@ = pointer to destination integer |
| 692 | * @mp *m@ = pointer to source integer |
| 693 | * @size_t *s@ = where to store the power of 2 |
| 694 | * |
| 695 | * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%. |
| 696 | * |
| 697 | * Use: Computes a power of two and an odd integer which, when |
| 698 | * multiplied, give a specified result. This sort of thing is |
| 699 | * useful in number theory quite often. |
| 700 | */ |
| 701 | |
| 702 | mp *mp_odd(mp *d, mp *m, size_t *s) |
| 703 | { |
| 704 | size_t ss = 0; |
| 705 | const mpw *v, *vl; |
| 706 | |
| 707 | v = m->v; |
| 708 | vl = m->vl; |
| 709 | for (; !*v && v < vl; v++) |
| 710 | ss += MPW_BITS; |
| 711 | if (v >= vl) |
| 712 | ss = 0; |
| 713 | else { |
| 714 | mpw x = *v; |
| 715 | mpw mask = MPW_MAX; |
| 716 | unsigned z = MPW_BITS / 2; |
| 717 | |
| 718 | while (z) { |
| 719 | mask >>= z; |
| 720 | if (!(x & mask)) { |
| 721 | x >>= z; |
| 722 | ss += z; |
| 723 | } |
| 724 | z >>= 1; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | *s = ss; |
| 729 | return (mp_lsr(d, m, ss)); |
| 730 | } |
| 731 | |
| 732 | /*----- Test rig ----------------------------------------------------------*/ |
| 733 | |
| 734 | #ifdef TEST_RIG |
| 735 | |
| 736 | static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b) |
| 737 | { |
| 738 | if (!MP_EQ(expect, result)) { |
| 739 | fprintf(stderr, "\n*** %s failed", op); |
| 740 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); |
| 741 | fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10); |
| 742 | fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10); |
| 743 | fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10); |
| 744 | fputc('\n', stderr); |
| 745 | return (0); |
| 746 | } |
| 747 | return (1); |
| 748 | } |
| 749 | |
| 750 | #define RIG(name, op) \ |
| 751 | static int t##name(dstr *v) \ |
| 752 | { \ |
| 753 | mp *a = *(mp **)v[0].buf; \ |
| 754 | mpw n = *(int *)v[1].buf; \ |
| 755 | mp b; \ |
| 756 | mp *r = *(mp **)v[2].buf; \ |
| 757 | mp *c = op(MP_NEW, a, n); \ |
| 758 | int ok; \ |
| 759 | mp_build(&b, &n, &n + 1); \ |
| 760 | ok = verify(#name, r, c, a, &b); \ |
| 761 | mp_drop(a); mp_drop(c); mp_drop(r); \ |
| 762 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
| 763 | return (ok); \ |
| 764 | } |
| 765 | |
| 766 | RIG(lsl, mp_lsl) |
| 767 | RIG(lsr, mp_lsr) |
| 768 | RIG(lsl2c, mp_lsl2c) |
| 769 | RIG(lsr2c, mp_lsr2c) |
| 770 | |
| 771 | #undef RIG |
| 772 | |
| 773 | #define RIG(name, op) \ |
| 774 | static int t##name(dstr *v) \ |
| 775 | { \ |
| 776 | mp *a = *(mp **)v[0].buf; \ |
| 777 | mp *b = *(mp **)v[1].buf; \ |
| 778 | mp *r = *(mp **)v[2].buf; \ |
| 779 | mp *c = op(MP_NEW, a, b); \ |
| 780 | int ok = verify(#name, r, c, a, b); \ |
| 781 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \ |
| 782 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
| 783 | return (ok); \ |
| 784 | } |
| 785 | |
| 786 | RIG(add, mp_add) |
| 787 | RIG(sub, mp_sub) |
| 788 | RIG(mul, mp_mul) |
| 789 | |
| 790 | #undef RIG |
| 791 | |
| 792 | static int tdiv(dstr *v) |
| 793 | { |
| 794 | mp *a = *(mp **)v[0].buf; |
| 795 | mp *b = *(mp **)v[1].buf; |
| 796 | mp *q = *(mp **)v[2].buf; |
| 797 | mp *r = *(mp **)v[3].buf; |
| 798 | mp *c = MP_NEW, *d = MP_NEW; |
| 799 | int ok = 1; |
| 800 | mp_div(&c, &d, a, b); |
| 801 | ok &= verify("div(quotient)", q, c, a, b); |
| 802 | ok &= verify("div(remainder)", r, d, a, b); |
| 803 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q); |
| 804 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 805 | return (ok); |
| 806 | } |
| 807 | |
| 808 | static int tbin(dstr *v) |
| 809 | { |
| 810 | static mp *(*fn[])(mp *, mp *, mp *) = { |
| 811 | #define DO(string) mp_bit##string##2c, |
| 812 | MPX_DOBIN(DO) |
| 813 | #undef DO |
| 814 | }; |
| 815 | int ok = 1; |
| 816 | unsigned op = 0; |
| 817 | mp *a = *(mp **)v[1].buf; |
| 818 | mp *b = *(mp **)v[2].buf; |
| 819 | mp *r = *(mp **)v[3].buf; |
| 820 | mp *c; |
| 821 | |
| 822 | if (strcmp(v[0].buf, "and") == 0) op = 1; |
| 823 | else if (strcmp(v[0].buf, "or") == 0) op = 7; |
| 824 | else if (strcmp(v[0].buf, "nand") == 0) op = 14; |
| 825 | else if (strcmp(v[0].buf, "nor") == 0) op = 8; |
| 826 | else if (strcmp(v[0].buf, "xor") == 0) op = 6; |
| 827 | else { |
| 828 | char *p = v[0].buf; |
| 829 | while (*p) { |
| 830 | op <<= 1; |
| 831 | if (*p++ == '1') |
| 832 | op |= 1; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | c = fn[op](MP_NEW, a, b); |
| 837 | ok = verify(v[0].buf, r, c, a, b); |
| 838 | mp_drop(a); mp_drop(b); mp_drop(r); mp_drop(c); |
| 839 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 840 | return (ok); |
| 841 | } |
| 842 | |
| 843 | static int tset(dstr *v) |
| 844 | { |
| 845 | mp *a = *(mp **)v[0].buf; |
| 846 | unsigned long n = *(unsigned long *)v[1].buf; |
| 847 | mp *r = *(mp **)v[2].buf; |
| 848 | mp *c; |
| 849 | int ok = 1; |
| 850 | |
| 851 | c = mp_setbit2c(MP_NEW, a, n); |
| 852 | if (!MP_EQ(c, r)) { |
| 853 | ok = 0; |
| 854 | fprintf(stderr, "\n***setbit (set) failed"); |
| 855 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); |
| 856 | fprintf(stderr, "\n*** n = %lu", n); |
| 857 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
| 858 | fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); |
| 859 | fputc('\n', stderr); |
| 860 | } |
| 861 | if (!mp_testbit2c(r, n)) { |
| 862 | ok = 0; |
| 863 | fprintf(stderr, "\n***setbit (test) failed"); |
| 864 | fprintf(stderr, "\n*** n = %lu", n); |
| 865 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
| 866 | fputc('\n', stderr); |
| 867 | } |
| 868 | mp_drop(a); |
| 869 | mp_drop(r); |
| 870 | mp_drop(c); |
| 871 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 872 | return (ok); |
| 873 | } |
| 874 | |
| 875 | static int tclr(dstr *v) |
| 876 | { |
| 877 | mp *a = *(mp **)v[0].buf; |
| 878 | unsigned long n = *(unsigned long *)v[1].buf; |
| 879 | mp *r = *(mp **)v[2].buf; |
| 880 | mp *c; |
| 881 | int ok = 1; |
| 882 | |
| 883 | c = mp_clearbit2c(MP_NEW, a, n); |
| 884 | if (!MP_EQ(c, r)) { |
| 885 | ok = 0; |
| 886 | fprintf(stderr, "\n***clrbit (set) failed"); |
| 887 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); |
| 888 | fprintf(stderr, "\n*** n = %lu", n); |
| 889 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
| 890 | fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); |
| 891 | fputc('\n', stderr); |
| 892 | } |
| 893 | if (mp_testbit2c(r, n)) { |
| 894 | ok = 0; |
| 895 | fprintf(stderr, "\n***clrbit (test) failed"); |
| 896 | fprintf(stderr, "\n*** n = %lu", n); |
| 897 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
| 898 | fputc('\n', stderr); |
| 899 | } |
| 900 | mp_drop(a); |
| 901 | mp_drop(c); |
| 902 | mp_drop(r); |
| 903 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 904 | return (ok); |
| 905 | } |
| 906 | |
| 907 | static int tneg(dstr *v) |
| 908 | { |
| 909 | mp *a = *(mp **)v[0].buf; |
| 910 | mp *r = *(mp **)v[1].buf; |
| 911 | int ok = 1; |
| 912 | mp *n = mp_neg(MP_NEW, a); |
| 913 | if (!MP_EQ(r, n)) { |
| 914 | ok = 0; |
| 915 | fprintf(stderr, "\n*** neg failed\n"); |
| 916 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); |
| 917 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); |
| 918 | fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); |
| 919 | fputc('\n', stderr); |
| 920 | } |
| 921 | mp_drop(n); |
| 922 | n = mp_neg(a, a); |
| 923 | if (!MP_EQ(r, n)) { |
| 924 | ok = 0; |
| 925 | fprintf(stderr, "\n*** neg failed\n"); |
| 926 | fputs("\n*** a* = ", stderr); mp_writefile(a, stderr, 10); |
| 927 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); |
| 928 | fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); |
| 929 | fputc('\n', stderr); |
| 930 | } |
| 931 | mp_drop(a); |
| 932 | mp_drop(r); |
| 933 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 934 | return (ok); |
| 935 | } |
| 936 | |
| 937 | static int todd(dstr *v) |
| 938 | { |
| 939 | mp *a = *(mp **)v[0].buf; |
| 940 | size_t rs = *(uint32 *)v[1].buf; |
| 941 | mp *rt = *(mp **)v[2].buf; |
| 942 | int ok = 1; |
| 943 | mp *t; |
| 944 | size_t s; |
| 945 | t = mp_odd(MP_NEW, a, &s); |
| 946 | if (s != rs || !MP_EQ(t, rt)) { |
| 947 | ok = 0; |
| 948 | fprintf(stderr, "\n*** odd failed"); |
| 949 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); |
| 950 | fprintf(stderr, "\n*** s = %lu", (unsigned long)s); |
| 951 | fputs("\n*** t = ", stderr); mp_writefile(t, stderr, 10); |
| 952 | fprintf(stderr, "\n*** rs = %lu", (unsigned long)rs); |
| 953 | fputs("\n*** rt = ", stderr); mp_writefile(rt, stderr, 10); |
| 954 | fputc('\n', stderr); |
| 955 | } |
| 956 | mp_drop(a); |
| 957 | mp_drop(rt); |
| 958 | mp_drop(t); |
| 959 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 960 | return (ok); |
| 961 | } |
| 962 | |
| 963 | static test_chunk tests[] = { |
| 964 | { "lsl", tlsl, { &type_mp, &type_int, &type_mp, 0 } }, |
| 965 | { "lsr", tlsr, { &type_mp, &type_int, &type_mp, 0 } }, |
| 966 | { "lsl2c", tlsl2c, { &type_mp, &type_int, &type_mp, 0 } }, |
| 967 | { "lsr2c", tlsr2c, { &type_mp, &type_int, &type_mp, 0 } }, |
| 968 | { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } }, |
| 969 | { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } }, |
| 970 | { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 971 | { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 972 | { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 973 | { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, |
| 974 | { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } }, |
| 975 | { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } }, |
| 976 | { "neg", tneg, { &type_mp, &type_mp, 0 } }, |
| 977 | { 0, 0, { 0 } }, |
| 978 | }; |
| 979 | |
| 980 | int main(int argc, char *argv[]) |
| 981 | { |
| 982 | sub_init(); |
| 983 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); |
| 984 | return (0); |
| 985 | } |
| 986 | |
| 987 | #endif |
| 988 | |
| 989 | /*----- That's all, folks -------------------------------------------------*/ |