General utilities cleanup. Add signature support to catcrypt. Throw in
[u/mdw/catacomb] / mpmont.c
1 /* -*-c-*-
2 *
3 * $Id: mpmont.c,v 1.19 2004/04/08 01:36:15 mdw Exp $
4 *
5 * Montgomery reduction
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 /*----- Header files ------------------------------------------------------*/
31
32 #include "mp.h"
33 #include "mpmont.h"
34
35 /*----- Tweakables --------------------------------------------------------*/
36
37 /* --- @MPMONT_DISABLE@ --- *
38 *
39 * Replace all the clever Montgomery reduction with good old-fashioned long
40 * division.
41 */
42
43 /* #define MPMONT_DISABLE */
44
45 /*----- Reduction and multiplication --------------------------------------*/
46
47 /* --- @mpmont_create@ --- *
48 *
49 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
50 * @mp *m@ = modulus to use
51 *
52 * Returns: ---
53 *
54 * Use: Initializes a Montgomery reduction context ready for use.
55 * The argument @m@ must be a positive odd integer.
56 */
57
58 #ifdef MPMONT_DISABLE
59
60 void mpmont_create(mpmont *mm, mp *m)
61 {
62 mp_shrink(m);
63 mm->m = MP_COPY(m);
64 mm->r = MP_ONE;
65 mm->r2 = MP_ONE;
66 mm->mi = MP_ONE;
67 }
68
69 #else
70
71 void mpmont_create(mpmont *mm, mp *m)
72 {
73 size_t n = MP_LEN(m);
74 mp *r2 = mp_new(2 * n + 1, 0);
75 mp r;
76
77 /* --- Take a copy of the modulus --- */
78
79 assert(MP_ISPOS(m) && MP_ISODD(m));
80 mm->m = MP_COPY(m);
81
82 /* --- Determine %$R^2$% --- */
83
84 mm->n = n;
85 MPX_ZERO(r2->v, r2->vl - 1);
86 r2->vl[-1] = 1;
87
88 /* --- Find the magic value @mi@ --- */
89
90 mp_build(&r, r2->v + n, r2->vl);
91 mm->mi = mp_modinv(MP_NEW, m, &r);
92 mm->mi = mp_sub(mm->mi, &r, mm->mi);
93
94 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
95
96 mm->r2 = MP_NEW;
97 mp_div(0, &mm->r2, r2, m);
98 mm->r = mpmont_reduce(mm, MP_NEW, mm->r2);
99 MP_DROP(r2);
100 }
101
102 #endif
103
104 /* --- @mpmont_destroy@ --- *
105 *
106 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
107 *
108 * Returns: ---
109 *
110 * Use: Disposes of a context when it's no longer of any use to
111 * anyone.
112 */
113
114 void mpmont_destroy(mpmont *mm)
115 {
116 MP_DROP(mm->m);
117 MP_DROP(mm->r);
118 MP_DROP(mm->r2);
119 MP_DROP(mm->mi);
120 }
121
122 /* --- @mpmont_reduce@ --- *
123 *
124 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
125 * @mp *d@ = destination
126 * @mp *a@ = source, assumed positive
127 *
128 * Returns: Result, %$a R^{-1} \bmod m$%.
129 */
130
131 #ifdef MPMONT_DISABLE
132
133 mp *mpmont_reduce(mpmont *mm, mp *d, mp *a)
134 {
135 mp_div(0, &d, a, mm->m);
136 return (d);
137 }
138
139 #else
140
141 mp *mpmont_reduce(mpmont *mm, mp *d, mp *a)
142 {
143 size_t n = mm->n;
144
145 /* --- Check for serious Karatsuba reduction --- */
146
147 if (n > MPK_THRESH * 3) {
148 mp al;
149 mpw *vl;
150 mp *u;
151
152 if (MP_LEN(a) >= n)
153 vl = a->v + n;
154 else
155 vl = a->vl;
156 mp_build(&al, a->v, vl);
157 u = mp_mul(MP_NEW, &al, mm->mi);
158 if (MP_LEN(u) > n)
159 u->vl = u->v + n;
160 u = mp_mul(u, u, mm->m);
161 d = mp_add(d, a, u);
162 mp_drop(u);
163 }
164
165 /* --- Otherwise do it the hard way --- */
166
167 else {
168 mpw *dv, *dvl;
169 mpw *mv, *mvl;
170 mpw mi;
171 size_t k = n;
172
173 /* --- Initial conditioning of the arguments --- */
174
175 a = MP_COPY(a);
176 if (d)
177 MP_DROP(d);
178 d = a;
179 MP_DEST(d, 2 * n + 1, a->f);
180
181 dv = d->v; dvl = d->vl;
182 mv = mm->m->v; mvl = mm->m->vl;
183
184 /* --- Let's go to work --- */
185
186 mi = mm->mi->v[0];
187 while (k--) {
188 mpw u = MPW(*dv * mi);
189 MPX_UMLAN(dv, dvl, mv, mvl, u);
190 dv++;
191 }
192 }
193
194 /* --- Wrap everything up --- */
195
196 memmove(d->v, d->v + n, MPWS(MP_LEN(d) - n));
197 d->vl -= n;
198 if (MPX_UCMP(d->v, d->vl, >=, mm->m->v, mm->m->vl))
199 mpx_usub(d->v, d->vl, d->v, d->vl, mm->m->v, mm->m->vl);
200 if (d->f & MP_NEG) {
201 mpx_usub(d->v, d->vl, mm->m->v, mm->m->vl, d->v, d->vl);
202 d->f &= ~MP_NEG;
203 }
204 MP_SHRINK(d);
205 return (d);
206 }
207
208 #endif
209
210 /* --- @mpmont_mul@ --- *
211 *
212 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
213 * @mp *d@ = destination
214 * @mp *a, *b@ = sources, assumed positive
215 *
216 * Returns: Result, %$a b R^{-1} \bmod m$%.
217 */
218
219 #ifdef MPMONT_DISABLE
220
221 mp *mpmont_mul(mpmont *mm, mp *d, mp *a, mp *b)
222 {
223 d = mp_mul(d, a, b);
224 mp_div(0, &d, d, mm->m);
225 return (d);
226 }
227
228 #else
229
230 mp *mpmont_mul(mpmont *mm, mp *d, mp *a, mp *b)
231 {
232 if (mm->n > MPK_THRESH * 3) {
233 d = mp_mul(d, a, b);
234 d = mpmont_reduce(mm, d, d);
235 } else {
236 mpw *dv, *dvl;
237 mpw *av, *avl;
238 mpw *bv, *bvl;
239 mpw *mv, *mvl;
240 mpw y;
241 size_t n, i;
242 mpw mi;
243
244 /* --- Initial conditioning of the arguments --- */
245
246 if (MP_LEN(a) > MP_LEN(b)) {
247 mp *t = a; a = b; b = t;
248 }
249 n = MP_LEN(mm->m);
250
251 a = MP_COPY(a);
252 b = MP_COPY(b);
253 MP_DEST(d, 2 * n + 1, a->f | b->f | MP_UNDEF);
254 dv = d->v; dvl = d->vl;
255 MPX_ZERO(dv, dvl);
256 av = a->v; avl = a->vl;
257 bv = b->v; bvl = b->vl;
258 mv = mm->m->v; mvl = mm->m->vl;
259 y = *bv;
260
261 /* --- Montgomery multiplication phase --- */
262
263 i = 0;
264 mi = mm->mi->v[0];
265 while (i < n && av < avl) {
266 mpw x = *av++;
267 mpw u = MPW((*dv + x * y) * mi);
268 MPX_UMLAN(dv, dvl, bv, bvl, x);
269 MPX_UMLAN(dv, dvl, mv, mvl, u);
270 dv++;
271 i++;
272 }
273
274 /* --- Simpler Montgomery reduction phase --- */
275
276 while (i < n) {
277 mpw u = MPW(*dv * mi);
278 MPX_UMLAN(dv, dvl, mv, mvl, u);
279 dv++;
280 i++;
281 }
282
283 /* --- Done --- */
284
285 memmove(d->v, dv, MPWS(dvl - dv));
286 d->vl -= dv - d->v;
287 if (MPX_UCMP(d->v, d->vl, >=, mm->m->v, mm->m->vl))
288 mpx_usub(d->v, d->vl, d->v, d->vl, mm->m->v, mm->m->vl);
289 if ((a->f ^ b->f) & MP_NEG)
290 mpx_usub(d->v, d->vl, mm->m->v, mm->m->vl, d->v, d->vl);
291 MP_SHRINK(d);
292 d->f = (a->f | b->f) & MP_BURN;
293 MP_DROP(a);
294 MP_DROP(b);
295 }
296
297 return (d);
298 }
299
300 #endif
301
302 /*----- Test rig ----------------------------------------------------------*/
303
304 #ifdef TEST_RIG
305
306 static int tcreate(dstr *v)
307 {
308 mp *m = *(mp **)v[0].buf;
309 mp *mi = *(mp **)v[1].buf;
310 mp *r = *(mp **)v[2].buf;
311 mp *r2 = *(mp **)v[3].buf;
312
313 mpmont mm;
314 int ok = 1;
315
316 mpmont_create(&mm, m);
317
318 if (mm.mi->v[0] != mi->v[0]) {
319 fprintf(stderr, "\n*** bad mi: found %lu, expected %lu",
320 (unsigned long)mm.mi->v[0], (unsigned long)mi->v[0]);
321 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
322 fputc('\n', stderr);
323 ok = 0;
324 }
325
326 if (!MP_EQ(mm.r, r)) {
327 fputs("\n*** bad r", stderr);
328 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
329 fputs("\nexpected ", stderr); mp_writefile(r, stderr, 10);
330 fputs("\n found ", stderr); mp_writefile(mm.r, stderr, 10);
331 fputc('\n', stderr);
332 ok = 0;
333 }
334
335 if (!MP_EQ(mm.r2, r2)) {
336 fputs("\n*** bad r2", stderr);
337 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
338 fputs("\nexpected ", stderr); mp_writefile(r2, stderr, 10);
339 fputs("\n found ", stderr); mp_writefile(mm.r2, stderr, 10);
340 fputc('\n', stderr);
341 ok = 0;
342 }
343
344 MP_DROP(m);
345 MP_DROP(mi);
346 MP_DROP(r);
347 MP_DROP(r2);
348 mpmont_destroy(&mm);
349 assert(mparena_count(MPARENA_GLOBAL) == 0);
350 return (ok);
351 }
352
353 static int tmul(dstr *v)
354 {
355 mp *m = *(mp **)v[0].buf;
356 mp *a = *(mp **)v[1].buf;
357 mp *b = *(mp **)v[2].buf;
358 mp *r = *(mp **)v[3].buf;
359 int ok = 1;
360
361 mpmont mm;
362 mpmont_create(&mm, m);
363
364 {
365 mp *qr = mp_mul(MP_NEW, a, b);
366 mp_div(0, &qr, qr, m);
367
368 if (!MP_EQ(qr, r)) {
369 fputs("\n*** classical modmul failed", stderr);
370 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
371 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
372 fputs("\n b = ", stderr); mp_writefile(b, stderr, 10);
373 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
374 fputs("\nqr = ", stderr); mp_writefile(qr, stderr, 10);
375 fputc('\n', stderr);
376 ok = 0;
377 }
378
379 mp_drop(qr);
380 }
381
382 {
383 mp *ar = mpmont_mul(&mm, MP_NEW, a, mm.r2);
384 mp *br = mpmont_mul(&mm, MP_NEW, b, mm.r2);
385 mp *mr = mpmont_mul(&mm, MP_NEW, ar, br);
386 mr = mpmont_reduce(&mm, mr, mr);
387 if (!MP_EQ(mr, r)) {
388 fputs("\n*** montgomery modmul failed", stderr);
389 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
390 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
391 fputs("\n b = ", stderr); mp_writefile(b, stderr, 10);
392 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
393 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
394 fputc('\n', stderr);
395 ok = 0;
396 }
397 MP_DROP(ar); MP_DROP(br);
398 mp_drop(mr);
399 }
400
401
402 MP_DROP(m);
403 MP_DROP(a);
404 MP_DROP(b);
405 MP_DROP(r);
406 mpmont_destroy(&mm);
407 assert(mparena_count(MPARENA_GLOBAL) == 0);
408 return ok;
409 }
410
411 static test_chunk tests[] = {
412 { "create", tcreate, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
413 { "mul", tmul, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
414 { 0, 0, { 0 } },
415 };
416
417 int main(int argc, char *argv[])
418 {
419 sub_init();
420 test_run(argc, argv, tests, SRCDIR "/tests/mpmont");
421 return (0);
422 }
423
424 #endif
425
426 /*----- That's all, folks -------------------------------------------------*/