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