3 * $Id: mpmont.c,v 1.1 1999/11/17 18:02:16 mdw Exp $
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/11/17 18:02:16 mdw
34 * New multiprecision integer arithmetic suite.
38 /*----- Header files ------------------------------------------------------*/
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @mpmont_create@ --- *
47 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
48 * @mp *m@ = modulus to use
52 * Use: Initializes a Montgomery reduction context ready for use.
55 void mpmont_create(mpmont *mm, mp *m)
57 /* --- Take a copy of the modulus --- */
62 /* --- Find the magic value @mi@ --- *
64 * This is a slightly grungy way of solving the problem, but it does work.
73 mp_build(&a, av, av + 2);
74 mp_build(&b, m->v, m->v + 1);
75 mp_gcd(0, 0, &i, &a, &b);
83 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
87 mp *r = mp_create(l + 1);
89 mm->shift = l * MPW_BITS;
90 MPX_ZERO(r->v, r->vl - 1);
92 mm->r = mm->r2 = MP_NEW;
93 mp_div(0, &mm->r, r, m);
95 mp_div(0, &mm->r2, r, m);
100 /* --- @mpmont_destroy@ --- *
102 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
106 * Use: Disposes of a context when it's no longer of any use to
110 void mpmont_destroy(mpmont *mm)
117 /* --- @mpmont_reduce@ --- *
119 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
120 * @mp *d@ = destination
121 * @const mp *a@ = source, assumed positive
123 * Returns: Result, %$a R^{-1} \bmod m$%.
126 mp *mpmont_reduce(mpmont *mm, mp *d, const mp *a)
132 /* --- Initial conditioning of the arguments --- */
140 memcpy(d->v, a->v, MPWS(MP_LEN(a)));
141 memset(d->v + MP_LEN(a), 0, MPWS(MP_LEN(d) - MP_LEN(a)));
144 dv = d->v; dvl = d->vl;
145 mv = mm->m->v; mvl = mm->m->vl;
147 /* --- Let's go to work --- */
150 mpw u = MPW(*dv * mm->mi);
151 MPX_UMLAN(dv, dvl, mv, mvl, u);
157 memmove(d->v, dv, MPWS(dvl - dv));
160 d->f = a->f & MP_BURN;
164 /* --- @mpmont_mul@ --- *
166 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
167 * @mp *d@ = destination
168 * @const mp *a, *b@ = sources, assumed positive
170 * Returns: Result, %$a b R^{-1} \bmod m$%.
173 mp *mpmont_mul(mpmont *mm, mp *d, const mp *a, const mp *b)
182 /* --- Initial conditioning of the arguments --- */
184 if (MP_LEN(a) > MP_LEN(b)) {
185 const mp *t = a; a = b; b = t;
189 MP_MODIFY(d, 2 * n + 1);
190 dv = d->v; dvl = d->vl;
192 av = a->v; avl = a->vl;
193 bv = b->v; bvl = b->vl;
194 mv = mm->m->v; mvl = mm->m->vl;
197 /* --- Montgomery multiplication phase --- */
200 while (i < n && av < avl) {
202 mpw u = MPW((*dv + x * y) * mm->mi);
203 MPX_UMLAN(dv, dvl, bv, bvl, x);
204 MPX_UMLAN(dv, dvl, mv, mvl, u);
209 /* --- Simpler Montgomery reduction phase --- */
212 mpw u = MPW(*dv * mm->mi);
213 MPX_UMLAN(dv, dvl, mv, mvl, u);
220 memmove(d->v, dv, MPWS(dvl - dv));
223 d->f = (a->f | b->f) & MP_BURN;
227 /* --- @mpmont_exp@ --- *
229 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
230 * @const mp *a@ = base
231 * @const mp *e@ = exponent
233 * Returns: Result, %$a^e \bmod m$%.
236 mp *mpmont_exp(mpmont *mm, const mp *a, const mp *e)
239 mp *ar = mpmont_mul(mm, MP_NEW, a, mm->r2);
240 mp *d = MP_COPY(mm->r);
248 dd = mpmont_mul(mm, MP_NEW, d, ar);
254 dd = mpmont_mul(mm, MP_NEW, ar, ar);
260 return (mpmont_reduce(mm, d, d));
263 /*----- Test rig ----------------------------------------------------------*/
267 static int tcreate(dstr *v)
269 mp *m = *(mp **)v[0].buf;
270 mp *mi = *(mp **)v[1].buf;
271 mp *r = *(mp **)v[2].buf;
272 mp *r2 = *(mp **)v[3].buf;
277 mpmont_create(&mm, m);
279 if (mm.mi != mi->v[0]) {
280 fprintf(stderr, "\n*** bad mi: found %lu, expected %lu",
281 (unsigned long)mm.mi, (unsigned long)mi->v[0]);
282 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
287 if (MP_CMP(mm.r, !=, r)) {
288 fputs("\n*** bad r", stderr);
289 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
290 fputs("\nexpected ", stderr); mp_writefile(r, stderr, 10);
291 fputs("\n found ", stderr); mp_writefile(r, stderr, 10);
296 if (MP_CMP(mm.r2, !=, r2)) {
297 fputs("\n*** bad r2", stderr);
298 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
299 fputs("\nexpected ", stderr); mp_writefile(r2, stderr, 10);
300 fputs("\n found ", stderr); mp_writefile(r2, stderr, 10);
313 static int tmul(dstr *v)
315 mp *m = *(mp **)v[0].buf;
316 mp *a = *(mp **)v[1].buf;
317 mp *b = *(mp **)v[2].buf;
318 mp *r = *(mp **)v[3].buf;
323 mpmont_create(&mm, m);
326 mp *ar = mpmont_mul(&mm, MP_NEW, a, mm.r2);
327 mp *br = mpmont_mul(&mm, MP_NEW, b, mm.r2);
328 mr = mpmont_mul(&mm, MP_NEW, ar, br);
329 mr = mpmont_reduce(&mm, mr, mr);
330 MP_DROP(ar); MP_DROP(br);
333 qr = mp_mul(MP_NEW, a, b);
334 mp_div(0, &qr, qr, m);
336 if (MP_CMP(qr, !=, r)) {
337 fputs("\n*** classical modmul failed", stderr);
338 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
339 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
340 fputs("\n b = ", stderr); mp_writefile(b, stderr, 10);
341 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
342 fputs("\nqr = ", stderr); mp_writefile(qr, stderr, 10);
347 if (MP_CMP(mr, !=, r)) {
348 fputs("\n*** montgomery modmul failed", stderr);
349 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
350 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
351 fputs("\n b = ", stderr); mp_writefile(b, stderr, 10);
352 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
353 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
368 static int texp(dstr *v)
370 mp *m = *(mp **)v[0].buf;
371 mp *a = *(mp **)v[1].buf;
372 mp *b = *(mp **)v[2].buf;
373 mp *r = *(mp **)v[3].buf;
378 mpmont_create(&mm, m);
380 mr = mpmont_exp(&mm, a, b);
382 if (MP_CMP(mr, !=, r)) {
383 fputs("\n*** montgomery modexp failed", stderr);
384 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
385 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
386 fputs("\n e = ", stderr); mp_writefile(b, stderr, 10);
387 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
388 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
403 static test_chunk tests[] = {
404 { "create", tcreate, { &type_mp, &type_mp, &type_mp, &type_mp } },
405 { "mul", tmul, { &type_mp, &type_mp, &type_mp, &type_mp } },
406 { "exp", texp, { &type_mp, &type_mp, &type_mp, &type_mp } },
410 int main(int argc, char *argv[])
413 test_run(argc, argv, tests, SRCDIR "/tests/mpmont");
419 /*----- That's all, folks -------------------------------------------------*/