| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mp-arith.c,v 1.13 2002/10/15 00:19:40 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.13 2002/10/15 00:19:40 mdw |
| 34 | * Bit setting and clearing functions. |
| 35 | * |
| 36 | * Revision 1.12 2002/10/09 00:36:03 mdw |
| 37 | * Fix bounds on workspace for Karatsuba operations. |
| 38 | * |
| 39 | * Revision 1.11 2002/10/06 22:52:50 mdw |
| 40 | * Pile of changes for supporting two's complement properly. |
| 41 | * |
| 42 | * Revision 1.10 2001/04/03 19:36:05 mdw |
| 43 | * Add some simple bitwise operations so that Perl can use them. |
| 44 | * |
| 45 | * Revision 1.9 2000/10/08 15:48:35 mdw |
| 46 | * Rename Karatsuba constants now that we have @gfx_kmul@ too. |
| 47 | * |
| 48 | * Revision 1.8 2000/10/08 12:02:21 mdw |
| 49 | * Use @MP_EQ@ instead of @MP_CMP@. |
| 50 | * |
| 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. |
| 55 | * |
| 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). |
| 60 | * |
| 61 | * Revision 1.5 1999/12/22 15:54:41 mdw |
| 62 | * Adjust Karatsuba parameters. Calculate destination size better. |
| 63 | * |
| 64 | * Revision 1.4 1999/12/13 15:35:16 mdw |
| 65 | * Slightly different rules on memory allocation. |
| 66 | * |
| 67 | * Revision 1.3 1999/12/11 10:57:43 mdw |
| 68 | * Karatsuba squaring algorithm. |
| 69 | * |
| 70 | * Revision 1.2 1999/12/10 23:18:39 mdw |
| 71 | * Change interface for suggested destinations. |
| 72 | * |
| 73 | * Revision 1.1 1999/11/17 18:02:16 mdw |
| 74 | * New multiprecision integer arithmetic suite. |
| 75 | * |
| 76 | */ |
| 77 | |
| 78 | /*----- Header files ------------------------------------------------------*/ |
| 79 | |
| 80 | #include "mp.h" |
| 81 | |
| 82 | /*----- Macros ------------------------------------------------------------*/ |
| 83 | |
| 84 | #define MAX(x, y) ((x) >= (y) ? (x) : (y)) |
| 85 | |
| 86 | /*----- Main code ---------------------------------------------------------*/ |
| 87 | |
| 88 | /* --- @mp_lsl@, @mp_lsr@ --- * |
| 89 | * |
| 90 | * Arguments: @mp *d@ = destination |
| 91 | * @mp *a@ = source |
| 92 | * @size_t n@ = number of bits to move |
| 93 | * |
| 94 | * Returns: Result, @a@ shifted left or right by @n@. |
| 95 | */ |
| 96 | |
| 97 | mp *mp_lsl(mp *d, mp *a, size_t n) |
| 98 | { |
| 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); |
| 102 | MP_SHRINK(d); |
| 103 | return (d); |
| 104 | } |
| 105 | |
| 106 | mp *mp_lsr(mp *d, mp *a, size_t n) |
| 107 | { |
| 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); |
| 111 | MP_SHRINK(d); |
| 112 | return (d); |
| 113 | } |
| 114 | |
| 115 | /* --- @mp_lsl2c@, @mp_lsr2c@ --- * |
| 116 | * |
| 117 | * Arguments: @mp *d@ = destination |
| 118 | * @mp *a@ = source |
| 119 | * @size_t n@ = number of bits to move |
| 120 | * |
| 121 | * Returns: Result, @a@ shifted left or right by @n@. Handles the |
| 122 | * pretence of sign-extension for negative numbers. |
| 123 | */ |
| 124 | |
| 125 | mp *mp_lsl2c(mp *d, mp *a, size_t n) |
| 126 | { |
| 127 | if (!(a->f & MP_NEG)) |
| 128 | return (mp_lsl(d, a, n)); |
| 129 | d = mp_not2c(d, a); |
| 130 | d = mp_lsl(d, d, n); |
| 131 | d = mp_not2c(d, d); |
| 132 | return (d); |
| 133 | } |
| 134 | |
| 135 | mp *mp_lsr2c(mp *d, mp *a, size_t n) |
| 136 | { |
| 137 | if (!(a->f & MP_NEG)) |
| 138 | return (mp_lsr(d, a, n)); |
| 139 | d = mp_not2c(d, a); |
| 140 | d = mp_lsr(d, d, n); |
| 141 | d = mp_not2c(d, d); |
| 142 | return (d); |
| 143 | } |
| 144 | |
| 145 | /* --- @mp_testbit@ --- * |
| 146 | * |
| 147 | * Arguments: @mp *x@ = a large integer |
| 148 | * @unsigned long n@ = which bit to test |
| 149 | * |
| 150 | * Returns: Nonzero if the bit is set, zero if not. |
| 151 | */ |
| 152 | |
| 153 | int mp_testbit(mp *x, unsigned long n) |
| 154 | { |
| 155 | if (n > MPW_BITS * MP_LEN(x)) |
| 156 | return (0); |
| 157 | return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u); |
| 158 | } |
| 159 | |
| 160 | /* --- @mp_testbit2c@ --- * |
| 161 | * |
| 162 | * Arguments: @mp *x@ = a large integer |
| 163 | * @unsigned long n@ = which bit to test |
| 164 | * |
| 165 | * Returns: Nonzero if the bit is set, zero if not. Fakes up two's |
| 166 | * complement representation. |
| 167 | */ |
| 168 | |
| 169 | int mp_testbit2c(mp *x, unsigned long n) |
| 170 | { |
| 171 | int r; |
| 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); |
| 176 | MP_DROP(x); |
| 177 | return (r); |
| 178 | } |
| 179 | |
| 180 | /* --- @mp_setbit@, @mp_clearbit@ --- * |
| 181 | * |
| 182 | * Arguments: @mp *d@ = a destination |
| 183 | * @mp *x@ = a large integer |
| 184 | * @unsigned long n@ = which bit to modify |
| 185 | * |
| 186 | * Returns: The argument @x@, with the appropriate bit set or cleared. |
| 187 | */ |
| 188 | |
| 189 | mp *mp_setbit(mp *d, mp *x, unsigned long n) |
| 190 | { |
| 191 | size_t rq; |
| 192 | |
| 193 | rq = n + MPW_BITS; rq -= rq % MPW_BITS; |
| 194 | if (d != x) { |
| 195 | if (d) MP_DROP(d); |
| 196 | d = MP_COPY(x); |
| 197 | } |
| 198 | MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); |
| 199 | d->v[n/MPW_BITS] |= 1 << n%MPW_BITS; |
| 200 | return (d); |
| 201 | } |
| 202 | |
| 203 | mp *mp_clearbit(mp *d, mp *x, unsigned long n) |
| 204 | { |
| 205 | size_t rq; |
| 206 | |
| 207 | rq = n + MPW_BITS; rq -= rq % MPW_BITS; |
| 208 | if (d != x) { |
| 209 | if (d) MP_DROP(d); |
| 210 | d = MP_COPY(x); |
| 211 | } |
| 212 | MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); |
| 213 | d->v[n/MPW_BITS] &= ~(1 << n%MPW_BITS); |
| 214 | return (d); |
| 215 | } |
| 216 | |
| 217 | /* --- @mp_setbit2c@, @mp_clearbit2c@ --- * |
| 218 | * |
| 219 | * Arguments: @mp *d@ = a destination |
| 220 | * @mp *x@ = a large integer |
| 221 | * @unsigned long n@ = which bit to modify |
| 222 | * |
| 223 | * Returns: The argument @x@, with the appropriate bit set or cleared. |
| 224 | * Fakes up two's complement representation. |
| 225 | */ |
| 226 | |
| 227 | mp *mp_setbit2c(mp *d, mp *x, unsigned long n) |
| 228 | { |
| 229 | if (!(x->f & MP_NEG)) |
| 230 | return mp_setbit(d, x, n); |
| 231 | d = mp_not2c(d, x); |
| 232 | d = mp_clearbit(d, d, n); |
| 233 | d = mp_not2c(d, d); |
| 234 | return (d); |
| 235 | } |
| 236 | |
| 237 | mp *mp_clearbit2c(mp *d, mp *x, unsigned long n) |
| 238 | { |
| 239 | if (!(x->f & MP_NEG)) |
| 240 | return mp_clearbit(d, x, n); |
| 241 | d = mp_not2c(d, x); |
| 242 | d = mp_setbit(d, d, n); |
| 243 | d = mp_not2c(d, d); |
| 244 | return (d); |
| 245 | } |
| 246 | |
| 247 | /* --- @mp_eq@ --- * |
| 248 | * |
| 249 | * Arguments: @const mp *a, *b@ = two numbers |
| 250 | * |
| 251 | * Returns: Nonzero if the numbers are equal. |
| 252 | */ |
| 253 | |
| 254 | int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); } |
| 255 | |
| 256 | /* --- @mp_cmp@ --- * |
| 257 | * |
| 258 | * Arguments: @const mp *a, *b@ = two numbers |
| 259 | * |
| 260 | * Returns: Less than, equal to or greater than zero, according to |
| 261 | * whether @a@ is less than, equal to or greater than @b@. |
| 262 | */ |
| 263 | |
| 264 | int mp_cmp(const mp *a, const mp *b) |
| 265 | { |
| 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) |
| 269 | return (-1); |
| 270 | else |
| 271 | return (+1); |
| 272 | } |
| 273 | |
| 274 | /* --- @mp_bitop@ --- * |
| 275 | * |
| 276 | * Arguments: @mp *d@ = destination |
| 277 | * @mp *a, *b@ = sources |
| 278 | * |
| 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: |
| 283 | * |
| 284 | * a: 0011 |
| 285 | * b: 0101 |
| 286 | * @mpx_bitXXXX@ |
| 287 | */ |
| 288 | |
| 289 | #define MP_BITBINOP(string) \ |
| 290 | \ |
| 291 | mp *mp_bit##string(mp *d, mp *a, mp *b) \ |
| 292 | { \ |
| 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; \ |
| 296 | MP_SHRINK(d); \ |
| 297 | return (d); \ |
| 298 | } |
| 299 | |
| 300 | MPX_DOBIN(MP_BITBINOP) |
| 301 | |
| 302 | /* --- @mp_not@ --- * |
| 303 | * |
| 304 | * Arguments: @mp *d@ = destination |
| 305 | * @mp *a@ = source |
| 306 | * |
| 307 | * Returns: The bitwise complement of the source. |
| 308 | */ |
| 309 | |
| 310 | mp *mp_not(mp *d, mp *a) |
| 311 | { |
| 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; |
| 315 | MP_SHRINK(d); |
| 316 | return (d); |
| 317 | } |
| 318 | |
| 319 | /* --- @mp_bitop2c@ --- * |
| 320 | * |
| 321 | * Arguments: @mp *d@ = destination |
| 322 | * @mp *a, *b@ = sources |
| 323 | * |
| 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 |
| 327 | * they generate: |
| 328 | * |
| 329 | * a: 0011 |
| 330 | * b: 0101 |
| 331 | * @mpx_bitXXXX@ |
| 332 | */ |
| 333 | |
| 334 | /* --- How this actually works --- * |
| 335 | * |
| 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 |
| 340 | * sign-swap. |
| 341 | * |
| 342 | * Start with: wxyz WXYZ |
| 343 | * If @a@ negative: yzwx or YZWX |
| 344 | * If @b@ negative: xwzy XWZY |
| 345 | * If both negative: zyxw ZYXW |
| 346 | */ |
| 347 | |
| 348 | #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \ |
| 349 | \ |
| 350 | mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \ |
| 351 | { \ |
| 352 | if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \ |
| 353 | d = mp_bit##base(d, a, b); \ |
| 354 | p_base \ |
| 355 | } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \ |
| 356 | MP_COPY(b); \ |
| 357 | d = mp_not2c(d, a); \ |
| 358 | d = mp_bit##an(d, d, b); \ |
| 359 | MP_DROP(b); \ |
| 360 | p_an \ |
| 361 | } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \ |
| 362 | MP_COPY(a); \ |
| 363 | d = mp_not2c(d, b); \ |
| 364 | d = mp_bit##bn(d, a, d); \ |
| 365 | MP_DROP(a); \ |
| 366 | p_bn \ |
| 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); \ |
| 371 | MP_DROP(t); \ |
| 372 | p_abn \ |
| 373 | } \ |
| 374 | return (d); \ |
| 375 | } \ |
| 376 | |
| 377 | #define NEG d = mp_not2c(d, d); |
| 378 | #define POS |
| 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) |
| 395 | #undef NEG |
| 396 | #undef POS |
| 397 | |
| 398 | /* --- @mp_not2c@ --- * |
| 399 | * |
| 400 | * Arguments: @mp *d@ = destination |
| 401 | * @mp *a@ = source |
| 402 | * |
| 403 | * Returns: The sign-extended complement of the argument. |
| 404 | */ |
| 405 | |
| 406 | mp *mp_not2c(mp *d, mp *a) |
| 407 | { |
| 408 | mpw one = 1; |
| 409 | |
| 410 | MP_DEST(d, MP_LEN(a) + 1, a->f); |
| 411 | if (d == a) { |
| 412 | if (a->f & MP_NEG) |
| 413 | MPX_USUBN(d->v, d->vl, 1); |
| 414 | else |
| 415 | MPX_UADDN(d->v, d->vl, 1); |
| 416 | } else { |
| 417 | if (a->f & MP_NEG) |
| 418 | mpx_usub(d->v, d->vl, a->v, a->vl, &one, &one + 1); |
| 419 | else |
| 420 | mpx_uadd(d->v, d->vl, a->v, a->vl, &one, &one + 1); |
| 421 | } |
| 422 | d->f = (a->f & (MP_NEG | MP_BURN)) ^ MP_NEG; |
| 423 | MP_SHRINK(d); |
| 424 | return (d); |
| 425 | } |
| 426 | |
| 427 | /* --- @mp_add@ --- * |
| 428 | * |
| 429 | * Arguments: @mp *d@ = destination |
| 430 | * @mp *a, *b@ = sources |
| 431 | * |
| 432 | * Returns: Result, @a@ added to @b@. |
| 433 | */ |
| 434 | |
| 435 | mp *mp_add(mp *d, mp *a, mp *b) |
| 436 | { |
| 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); |
| 440 | else { |
| 441 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 442 | mp *t = a; a = b; b = t; |
| 443 | } |
| 444 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 445 | } |
| 446 | d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG); |
| 447 | MP_SHRINK(d); |
| 448 | return (d); |
| 449 | } |
| 450 | |
| 451 | /* --- @mp_sub@ --- * |
| 452 | * |
| 453 | * Arguments: @mp *d@ = destination |
| 454 | * @mp *a, *b@ = sources |
| 455 | * |
| 456 | * Returns: Result, @b@ subtracted from @a@. |
| 457 | */ |
| 458 | |
| 459 | mp *mp_sub(mp *d, mp *a, mp *b) |
| 460 | { |
| 461 | unsigned sgn = 0; |
| 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); |
| 465 | else { |
| 466 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 467 | mp *t = a; a = b; b = t; |
| 468 | sgn = MP_NEG; |
| 469 | } |
| 470 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 471 | } |
| 472 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG); |
| 473 | MP_SHRINK(d); |
| 474 | return (d); |
| 475 | } |
| 476 | |
| 477 | /* --- @mp_mul@ --- * |
| 478 | * |
| 479 | * Arguments: @mp *d@ = destination |
| 480 | * @mp *a, *b@ = sources |
| 481 | * |
| 482 | * Returns: Result, @a@ multiplied by @b@. |
| 483 | */ |
| 484 | |
| 485 | mp *mp_mul(mp *d, mp *a, mp *b) |
| 486 | { |
| 487 | a = MP_COPY(a); |
| 488 | b = MP_COPY(b); |
| 489 | |
| 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); |
| 493 | } else { |
| 494 | size_t m = MAX(MP_LEN(a), MP_LEN(b)); |
| 495 | mpw *s; |
| 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); |
| 499 | mpfree(d->a, s); |
| 500 | } |
| 501 | |
| 502 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG); |
| 503 | MP_SHRINK(d); |
| 504 | MP_DROP(a); |
| 505 | MP_DROP(b); |
| 506 | return (d); |
| 507 | } |
| 508 | |
| 509 | /* --- @mp_sqr@ --- * |
| 510 | * |
| 511 | * Arguments: @mp *d@ = destination |
| 512 | * @mp *a@ = source |
| 513 | * |
| 514 | * Returns: Result, @a@ squared. |
| 515 | */ |
| 516 | |
| 517 | mp *mp_sqr(mp *d, mp *a) |
| 518 | { |
| 519 | size_t m = MP_LEN(a); |
| 520 | |
| 521 | a = MP_COPY(a); |
| 522 | if (m > MPK_THRESH) { |
| 523 | mpw *s; |
| 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); |
| 527 | mpfree(d->a, s); |
| 528 | } else { |
| 529 | MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF); |
| 530 | mpx_usqr(d->v, d->vl, a->v, a->vl); |
| 531 | } |
| 532 | d->f = a->f & MP_BURN; |
| 533 | MP_SHRINK(d); |
| 534 | MP_DROP(a); |
| 535 | return (d); |
| 536 | } |
| 537 | |
| 538 | /* --- @mp_div@ --- * |
| 539 | * |
| 540 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder |
| 541 | * @mp *a, *b@ = sources |
| 542 | * |
| 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@. |
| 548 | * |
| 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 |
| 554 | * straightforward. |
| 555 | */ |
| 556 | |
| 557 | void mp_div(mp **qq, mp **rr, mp *a, mp *b) |
| 558 | { |
| 559 | mp *r = rr ? *rr : MP_NEW; |
| 560 | mp *q = qq ? *qq : MP_NEW; |
| 561 | mpw *sv, *svl; |
| 562 | |
| 563 | /* --- Set the remainder up right --- * |
| 564 | * |
| 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. |
| 567 | */ |
| 568 | |
| 569 | b = MP_COPY(b); |
| 570 | a = MP_COPY(a); |
| 571 | if (r) |
| 572 | MP_DROP(r); |
| 573 | r = a; |
| 574 | MP_DEST(r, MP_LEN(a) + 2, a->f | b->f); |
| 575 | |
| 576 | /* --- Fix up the quotient too --- */ |
| 577 | |
| 578 | r = MP_COPY(r); |
| 579 | MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF); |
| 580 | MP_DROP(r); |
| 581 | |
| 582 | /* --- Set up some temporary workspace --- */ |
| 583 | |
| 584 | { |
| 585 | size_t rq = MP_LEN(b) + 1; |
| 586 | sv = mpalloc(r->a, rq); |
| 587 | svl = sv + rq; |
| 588 | } |
| 589 | |
| 590 | /* --- Perform the calculation --- */ |
| 591 | |
| 592 | mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl); |
| 593 | |
| 594 | /* --- Sort out the sign of the results --- * |
| 595 | * |
| 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@. |
| 599 | */ |
| 600 | |
| 601 | q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG); |
| 602 | if (q->f & MP_NEG) { |
| 603 | mpw *v; |
| 604 | for (v = r->v; v < r->vl; v++) { |
| 605 | if (*v) { |
| 606 | MPX_UADDN(q->v, q->vl, 1); |
| 607 | mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl); |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG); |
| 614 | |
| 615 | /* --- Store the return values --- */ |
| 616 | |
| 617 | mpfree(r->a, sv); |
| 618 | MP_DROP(b); |
| 619 | |
| 620 | if (!qq) |
| 621 | MP_DROP(q); |
| 622 | else { |
| 623 | MP_SHRINK(q); |
| 624 | *qq = q; |
| 625 | } |
| 626 | |
| 627 | if (!rr) |
| 628 | MP_DROP(r); |
| 629 | else { |
| 630 | MP_SHRINK(r); |
| 631 | *rr = r; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /* --- @mp_odd@ --- * |
| 636 | * |
| 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 |
| 640 | * |
| 641 | * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%. |
| 642 | * |
| 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. |
| 646 | */ |
| 647 | |
| 648 | mp *mp_odd(mp *d, mp *m, size_t *s) |
| 649 | { |
| 650 | size_t ss = 0; |
| 651 | const mpw *v, *vl; |
| 652 | |
| 653 | v = m->v; |
| 654 | vl = m->vl; |
| 655 | for (; !*v && v < vl; v++) |
| 656 | ss += MPW_BITS; |
| 657 | if (v >= vl) |
| 658 | ss = 0; |
| 659 | else { |
| 660 | mpw x = *v; |
| 661 | mpw mask = MPW_MAX; |
| 662 | unsigned z = MPW_BITS / 2; |
| 663 | |
| 664 | while (z) { |
| 665 | mask >>= z; |
| 666 | if (!(x & mask)) { |
| 667 | x >>= z; |
| 668 | ss += z; |
| 669 | } |
| 670 | z >>= 1; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | *s = ss; |
| 675 | return (mp_lsr(d, m, ss)); |
| 676 | } |
| 677 | |
| 678 | /*----- Test rig ----------------------------------------------------------*/ |
| 679 | |
| 680 | #ifdef TEST_RIG |
| 681 | |
| 682 | static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b) |
| 683 | { |
| 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); |
| 690 | fputc('\n', stderr); |
| 691 | return (0); |
| 692 | } |
| 693 | return (1); |
| 694 | } |
| 695 | |
| 696 | #define RIG(name, op) \ |
| 697 | static int t##name(dstr *v) \ |
| 698 | { \ |
| 699 | mp *a = *(mp **)v[0].buf; \ |
| 700 | mpw n = *(int *)v[1].buf; \ |
| 701 | mp b; \ |
| 702 | mp *r = *(mp **)v[2].buf; \ |
| 703 | mp *c = op(MP_NEW, a, n); \ |
| 704 | int ok; \ |
| 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); \ |
| 709 | return (ok); \ |
| 710 | } |
| 711 | |
| 712 | RIG(lsl, mp_lsl) |
| 713 | RIG(lsr, mp_lsr) |
| 714 | RIG(lsl2c, mp_lsl2c) |
| 715 | RIG(lsr2c, mp_lsr2c) |
| 716 | |
| 717 | #undef RIG |
| 718 | |
| 719 | #define RIG(name, op) \ |
| 720 | static int t##name(dstr *v) \ |
| 721 | { \ |
| 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); \ |
| 729 | return (ok); \ |
| 730 | } |
| 731 | |
| 732 | RIG(add, mp_add) |
| 733 | RIG(sub, mp_sub) |
| 734 | RIG(mul, mp_mul) |
| 735 | |
| 736 | #undef RIG |
| 737 | |
| 738 | static int tdiv(dstr *v) |
| 739 | { |
| 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; |
| 745 | int ok = 1; |
| 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); |
| 751 | return (ok); |
| 752 | } |
| 753 | |
| 754 | static int tbin(dstr *v) |
| 755 | { |
| 756 | static mp *(*fn[])(mp *, mp *, mp *) = { |
| 757 | #define DO(string) mp_bit##string##2c, |
| 758 | MPX_DOBIN(DO) |
| 759 | #undef DO |
| 760 | }; |
| 761 | int ok = 1; |
| 762 | unsigned op = 0; |
| 763 | mp *a = *(mp **)v[1].buf; |
| 764 | mp *b = *(mp **)v[2].buf; |
| 765 | mp *r = *(mp **)v[3].buf; |
| 766 | mp *c; |
| 767 | |
| 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; |
| 773 | else { |
| 774 | char *p = v[0].buf; |
| 775 | while (*p) { |
| 776 | op <<= 1; |
| 777 | if (*p++ == '1') |
| 778 | op |= 1; |
| 779 | } |
| 780 | } |
| 781 | |
| 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); |
| 786 | return (ok); |
| 787 | } |
| 788 | |
| 789 | static int tset(dstr *v) |
| 790 | { |
| 791 | mp *a = *(mp **)v[0].buf; |
| 792 | unsigned long n = *(unsigned long *)v[1].buf; |
| 793 | mp *r = *(mp **)v[2].buf; |
| 794 | mp *c; |
| 795 | int ok = 1; |
| 796 | |
| 797 | c = mp_setbit2c(MP_NEW, a, n); |
| 798 | if (!MP_EQ(c, r)) { |
| 799 | ok = 0; |
| 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); |
| 805 | fputc('\n', stderr); |
| 806 | } |
| 807 | if (!mp_testbit2c(r, n)) { |
| 808 | ok = 0; |
| 809 | fprintf(stderr, "\n***setbit (test) failed"); |
| 810 | fprintf(stderr, "\n*** n = %lu", n); |
| 811 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
| 812 | fputc('\n', stderr); |
| 813 | } |
| 814 | mp_drop(a); |
| 815 | mp_drop(r); |
| 816 | mp_drop(c); |
| 817 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 818 | return (ok); |
| 819 | } |
| 820 | |
| 821 | static int tclr(dstr *v) |
| 822 | { |
| 823 | mp *a = *(mp **)v[0].buf; |
| 824 | unsigned long n = *(unsigned long *)v[1].buf; |
| 825 | mp *r = *(mp **)v[2].buf; |
| 826 | mp *c; |
| 827 | int ok = 1; |
| 828 | |
| 829 | c = mp_clearbit2c(MP_NEW, a, n); |
| 830 | if (!MP_EQ(c, r)) { |
| 831 | ok = 0; |
| 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); |
| 837 | fputc('\n', stderr); |
| 838 | } |
| 839 | if (mp_testbit2c(r, n)) { |
| 840 | ok = 0; |
| 841 | fprintf(stderr, "\n***clrbit (test) failed"); |
| 842 | fprintf(stderr, "\n*** n = %lu", n); |
| 843 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
| 844 | fputc('\n', stderr); |
| 845 | } |
| 846 | mp_drop(a); |
| 847 | mp_drop(c); |
| 848 | mp_drop(r); |
| 849 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 850 | return (ok); |
| 851 | } |
| 852 | |
| 853 | static int todd(dstr *v) |
| 854 | { |
| 855 | mp *a = *(mp **)v[0].buf; |
| 856 | size_t rs = *(uint32 *)v[1].buf; |
| 857 | mp *rt = *(mp **)v[2].buf; |
| 858 | int ok = 1; |
| 859 | mp *t; |
| 860 | size_t s; |
| 861 | t = mp_odd(MP_NEW, a, &s); |
| 862 | if (s != rs || !MP_EQ(t, rt)) { |
| 863 | ok = 0; |
| 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); |
| 870 | fputc('\n', stderr); |
| 871 | } |
| 872 | mp_drop(a); |
| 873 | mp_drop(rt); |
| 874 | mp_drop(t); |
| 875 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 876 | return (ok); |
| 877 | } |
| 878 | |
| 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 } }, |
| 892 | { 0, 0, { 0 } }, |
| 893 | }; |
| 894 | |
| 895 | int main(int argc, char *argv[]) |
| 896 | { |
| 897 | sub_init(); |
| 898 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); |
| 899 | return (0); |
| 900 | } |
| 901 | |
| 902 | #endif |
| 903 | |
| 904 | /*----- That's all, folks -------------------------------------------------*/ |