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