d3409d5e |
1 | /* -*-c-*- |
2 | * |
f09e814a |
3 | * $Id: mp-arith.c,v 1.11 2002/10/06 22:52: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 $ |
f09e814a |
33 | * Revision 1.11 2002/10/06 22:52:50 mdw |
34 | * Pile of changes for supporting two's complement properly. |
35 | * |
0f32e0f8 |
36 | * Revision 1.10 2001/04/03 19:36:05 mdw |
37 | * Add some simple bitwise operations so that Perl can use them. |
38 | * |
52cdaca9 |
39 | * Revision 1.9 2000/10/08 15:48:35 mdw |
40 | * Rename Karatsuba constants now that we have @gfx_kmul@ too. |
41 | * |
4b536f42 |
42 | * Revision 1.8 2000/10/08 12:02:21 mdw |
43 | * Use @MP_EQ@ instead of @MP_CMP@. |
44 | * |
f1713c63 |
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 | * |
d34decd2 |
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 | * |
bba03f55 |
55 | * Revision 1.5 1999/12/22 15:54:41 mdw |
56 | * Adjust Karatsuba parameters. Calculate destination size better. |
57 | * |
8017495b |
58 | * Revision 1.4 1999/12/13 15:35:16 mdw |
59 | * Slightly different rules on memory allocation. |
60 | * |
5bf74dea |
61 | * Revision 1.3 1999/12/11 10:57:43 mdw |
62 | * Karatsuba squaring algorithm. |
63 | * |
ef5f4810 |
64 | * Revision 1.2 1999/12/10 23:18:39 mdw |
65 | * Change interface for suggested destinations. |
66 | * |
d3409d5e |
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 | |
ef5f4810 |
76 | /*----- Macros ------------------------------------------------------------*/ |
77 | |
78 | #define MAX(x, y) ((x) >= (y) ? (x) : (y)) |
79 | |
d3409d5e |
80 | /*----- Main code ---------------------------------------------------------*/ |
81 | |
f09e814a |
82 | /* --- @mp_lsl@, @mp_lsr@ --- * |
d3409d5e |
83 | * |
f09e814a |
84 | * Arguments: @mp *d@ = destination |
85 | * @mp *a@ = source |
86 | * @size_t n@ = number of bits to move |
d3409d5e |
87 | * |
f09e814a |
88 | * Returns: Result, @a@ shifted left or right by @n@. |
d3409d5e |
89 | */ |
90 | |
f09e814a |
91 | mp *mp_lsl(mp *d, mp *a, size_t n) |
d3409d5e |
92 | { |
f09e814a |
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 | } |
d3409d5e |
99 | |
f09e814a |
100 | mp *mp_lsr(mp *d, mp *a, size_t n) |
101 | { |
d34decd2 |
102 | MP_DEST(d, MP_LEN(a), a->f); |
f09e814a |
103 | mpx_lsr(d->v, d->vl, a->v, a->vl, n); |
104 | d->f = a->f & (MP_NEG | MP_BURN); |
d3409d5e |
105 | MP_SHRINK(d); |
106 | return (d); |
107 | } |
108 | |
f09e814a |
109 | /* --- @mp_lsl2c@, @mp_lsr2c@ --- * |
d3409d5e |
110 | * |
111 | * Arguments: @mp *d@ = destination |
112 | * @mp *a@ = source |
f09e814a |
113 | * @size_t n@ = number of bits to move |
d3409d5e |
114 | * |
f09e814a |
115 | * Returns: Result, @a@ shifted left or right by @n@. Handles the |
116 | * pretence of sign-extension for negative numbers. |
d3409d5e |
117 | */ |
118 | |
f09e814a |
119 | mp *mp_lsl2c(mp *d, mp *a, size_t n) |
d3409d5e |
120 | { |
f09e814a |
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 | } |
d3409d5e |
128 | |
f09e814a |
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); |
d3409d5e |
137 | } |
138 | |
f09e814a |
139 | /* --- @mp_testbit@ --- * |
d3409d5e |
140 | * |
f09e814a |
141 | * Arguments: @mp *x@ = a large integer |
142 | * @size_t n@ = which bit to test |
d3409d5e |
143 | * |
f09e814a |
144 | * Returns: Nonzero if the bit is set, zero if not. |
d3409d5e |
145 | */ |
146 | |
f09e814a |
147 | int mp_testbit(mp *x, size_t n) |
d3409d5e |
148 | { |
f09e814a |
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); |
d3409d5e |
155 | } |
156 | |
f09e814a |
157 | /* --- @mp_testbit2c@ --- * |
d3409d5e |
158 | * |
f09e814a |
159 | * Arguments: @mp *x@ = a large integer |
160 | * @size_t n@ = which bit to test |
d3409d5e |
161 | * |
f09e814a |
162 | * Returns: Nonzero if the bit is set, zero if not. Fakes up two's |
163 | * complement representation. |
d3409d5e |
164 | */ |
165 | |
f09e814a |
166 | int mp_testbit2c(mp *x, size_t n) |
d3409d5e |
167 | { |
f09e814a |
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); |
d3409d5e |
175 | } |
176 | |
4b536f42 |
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 | |
d3409d5e |
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 | |
f09e814a |
204 | /* --- @mp_bitop@ --- * |
0f32e0f8 |
205 | * |
206 | * Arguments: @mp *d@ = destination |
207 | * @mp *a, *b@ = sources |
208 | * |
f09e814a |
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@ |
0f32e0f8 |
217 | */ |
218 | |
f09e814a |
219 | #define MP_BITBINOP(string) \ |
0f32e0f8 |
220 | \ |
f09e814a |
221 | mp *mp_bit##string(mp *d, mp *a, mp *b) \ |
0f32e0f8 |
222 | { \ |
223 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \ |
f09e814a |
224 | mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \ |
0f32e0f8 |
225 | d->f = (a->f | b->f) & MP_BURN; \ |
226 | MP_SHRINK(d); \ |
227 | return (d); \ |
228 | } |
229 | |
f09e814a |
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 | */ |
0f32e0f8 |
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 | |
f09e814a |
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 | |
d3409d5e |
357 | /* --- @mp_add@ --- * |
358 | * |
359 | * Arguments: @mp *d@ = destination |
ef5f4810 |
360 | * @mp *a, *b@ = sources |
d3409d5e |
361 | * |
362 | * Returns: Result, @a@ added to @b@. |
363 | */ |
364 | |
ef5f4810 |
365 | mp *mp_add(mp *d, mp *a, mp *b) |
d3409d5e |
366 | { |
d34decd2 |
367 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f); |
d3409d5e |
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)) { |
ef5f4810 |
372 | mp *t = a; a = b; b = t; |
d3409d5e |
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 |
ef5f4810 |
384 | * @mp *a, *b@ = sources |
d3409d5e |
385 | * |
386 | * Returns: Result, @b@ subtracted from @a@. |
387 | */ |
388 | |
ef5f4810 |
389 | mp *mp_sub(mp *d, mp *a, mp *b) |
d3409d5e |
390 | { |
391 | unsigned sgn = 0; |
d34decd2 |
392 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f); |
d3409d5e |
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)) { |
ef5f4810 |
397 | mp *t = a; a = b; b = t; |
d3409d5e |
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 |
ef5f4810 |
410 | * @mp *a, *b@ = sources |
d3409d5e |
411 | * |
412 | * Returns: Result, @a@ multiplied by @b@. |
413 | */ |
414 | |
ef5f4810 |
415 | mp *mp_mul(mp *d, mp *a, mp *b) |
d3409d5e |
416 | { |
ef5f4810 |
417 | a = MP_COPY(a); |
418 | b = MP_COPY(b); |
419 | |
52cdaca9 |
420 | if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= MPK_THRESH) { |
d34decd2 |
421 | MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF); |
ef5f4810 |
422 | mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
8017495b |
423 | } else { |
bba03f55 |
424 | size_t m = 2 * MAX(MP_LEN(a), MP_LEN(b)) + 2; |
ef5f4810 |
425 | mpw *s; |
d34decd2 |
426 | MP_DEST(d, m, a->f | b->f | MP_UNDEF); |
52cdaca9 |
427 | m += MPK_SLOP; |
d34decd2 |
428 | s = mpalloc(d->a, m); |
ef5f4810 |
429 | mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + m); |
d34decd2 |
430 | mpfree(d->a, s); |
ef5f4810 |
431 | } |
432 | |
d3409d5e |
433 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG); |
434 | MP_SHRINK(d); |
ef5f4810 |
435 | MP_DROP(a); |
436 | MP_DROP(b); |
d3409d5e |
437 | return (d); |
438 | } |
439 | |
440 | /* --- @mp_sqr@ --- * |
441 | * |
442 | * Arguments: @mp *d@ = destination |
ef5f4810 |
443 | * @mp *a@ = source |
d3409d5e |
444 | * |
445 | * Returns: Result, @a@ squared. |
446 | */ |
447 | |
ef5f4810 |
448 | mp *mp_sqr(mp *d, mp *a) |
d3409d5e |
449 | { |
ef5f4810 |
450 | size_t m = MP_LEN(a); |
451 | |
452 | a = MP_COPY(a); |
d34decd2 |
453 | MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF); |
52cdaca9 |
454 | if (m > MPK_THRESH) { |
ef5f4810 |
455 | mpw *s; |
52cdaca9 |
456 | m = 2 * (m + 1) + MPK_SLOP; |
d34decd2 |
457 | s = mpalloc(d->a, m); |
5bf74dea |
458 | mpx_ksqr(d->v, d->vl, a->v, a->vl, s, s + m); |
d34decd2 |
459 | mpfree(d->a, s); |
ef5f4810 |
460 | } else |
461 | mpx_usqr(d->v, d->vl, a->v, a->vl); |
d3409d5e |
462 | d->f = a->f & MP_BURN; |
463 | MP_SHRINK(d); |
ef5f4810 |
464 | MP_DROP(a); |
d3409d5e |
465 | return (d); |
466 | } |
467 | |
468 | /* --- @mp_div@ --- * |
469 | * |
470 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder |
ef5f4810 |
471 | * @mp *a, *b@ = sources |
d3409d5e |
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 | |
ef5f4810 |
487 | void mp_div(mp **qq, mp **rr, mp *a, mp *b) |
d3409d5e |
488 | { |
489 | mp *r = rr ? *rr : MP_NEW; |
490 | mp *q = qq ? *qq : MP_NEW; |
491 | mpw *sv, *svl; |
492 | |
d3409d5e |
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 | |
d34decd2 |
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); |
d3409d5e |
505 | |
506 | /* --- Fix up the quotient too --- */ |
507 | |
d34decd2 |
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 | } |
d3409d5e |
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 | |
d34decd2 |
531 | q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG); |
d3409d5e |
532 | if (q->f & MP_NEG) { |
ef5f4810 |
533 | mpw *v; |
534 | for (v = r->v; v < r->vl; v++) { |
d3409d5e |
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 | |
d34decd2 |
543 | r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG); |
d3409d5e |
544 | |
545 | /* --- Store the return values --- */ |
546 | |
d34decd2 |
547 | mpfree(r->a, sv); |
548 | MP_DROP(b); |
549 | |
d3409d5e |
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 | } |
d3409d5e |
563 | } |
564 | |
f1713c63 |
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 | |
d3409d5e |
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 | { |
4b536f42 |
614 | if (!MP_EQ(expect, result)) { |
d3409d5e |
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) \ |
ef5f4810 |
627 | static int t##name(dstr *v) \ |
d3409d5e |
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); \ |
ef5f4810 |
638 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
d3409d5e |
639 | return (ok); \ |
640 | } |
641 | |
642 | RIG(lsl, mp_lsl) |
643 | RIG(lsr, mp_lsr) |
f09e814a |
644 | RIG(lsl2c, mp_lsl2c) |
645 | RIG(lsr2c, mp_lsr2c) |
d3409d5e |
646 | |
647 | #undef RIG |
648 | |
649 | #define RIG(name, op) \ |
ef5f4810 |
650 | static int t##name(dstr *v) \ |
d3409d5e |
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); \ |
ef5f4810 |
658 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
d3409d5e |
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); |
ef5f4810 |
680 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
d3409d5e |
681 | return (ok); |
682 | } |
683 | |
f09e814a |
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 | |
f1713c63 |
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); |
4b536f42 |
728 | if (s != rs || !MP_EQ(t, rt)) { |
f1713c63 |
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 | |
d3409d5e |
744 | static test_chunk tests[] = { |
f09e814a |
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 } }, |
d3409d5e |
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 } }, |
f09e814a |
753 | { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } }, |
f1713c63 |
754 | { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } }, |
d3409d5e |
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 -------------------------------------------------*/ |