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