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