3 * $Id: mpmont.c,v 1.18 2004/04/03 03:32:05 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.18 2004/04/03 03:32:05 mdw
34 * General robustification.
36 * Revision 1.17 2004/04/01 12:50:09 mdw
37 * Add cyclic group abstraction, with test code. Separate off exponentation
38 * functions for better static linking. Fix a buttload of bugs on the way.
39 * Generally ensure that negative exponents do inversion correctly. Add
40 * table of standard prime-field subgroups. (Binary field subgroups are
41 * currently unimplemented but easy to add if anyone ever finds a good one.)
43 * Revision 1.16 2002/01/13 13:40:31 mdw
44 * Avoid trashing arguments before we've used them.
46 * Revision 1.15 2001/06/16 13:00:20 mdw
47 * Use the generic exponentiation functions.
49 * Revision 1.14 2001/02/22 09:04:26 mdw
52 * Revision 1.13 2001/02/03 12:00:29 mdw
53 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
54 * it. Note that the macro version @MP_DROP@ doesn't do this.
56 * Revision 1.12 2000/10/08 15:48:35 mdw
57 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
59 * Revision 1.11 2000/10/08 12:04:27 mdw
60 * (mpmont_reduce, mpmont_mul): Cope with negative numbers.
62 * Revision 1.10 2000/07/29 17:05:43 mdw
63 * (mpmont_expr): Use sliding window exponentiation, with a drop-through
64 * for small exponents to use a simple left-to-right bitwise routine. This
65 * can reduce modexp times by up to a quarter.
67 * Revision 1.9 2000/06/17 11:45:09 mdw
68 * Major memory management overhaul. Added arena support. Use the secure
69 * arena for secret integers. Replace and improve the MP management macros
70 * (e.g., replace MP_MODIFY by MP_DEST).
72 * Revision 1.8 1999/12/22 15:55:00 mdw
73 * Adjust Karatsuba parameters.
75 * Revision 1.7 1999/12/11 01:51:14 mdw
76 * Use a Karatsuba-based reduction for large moduli.
78 * Revision 1.6 1999/12/10 23:18:39 mdw
79 * Change interface for suggested destinations.
81 * Revision 1.5 1999/11/22 13:58:40 mdw
82 * Add an option to disable Montgomery reduction, so that performance
83 * comparisons can be done.
85 * Revision 1.4 1999/11/21 12:27:06 mdw
86 * Remove a division from the Montgomery setup by calculating
87 * %$R^2 \bmod m$% first and then %$R \bmod m$% by Montgomery reduction of
90 * Revision 1.3 1999/11/21 11:35:10 mdw
91 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
92 * @mpmont_mul@ for squaring in exponentiation.
94 * Revision 1.2 1999/11/19 13:17:26 mdw
95 * Add extra interface to exponentiation which returns a Montgomerized
98 * Revision 1.1 1999/11/17 18:02:16 mdw
99 * New multiprecision integer arithmetic suite.
103 /*----- Header files ------------------------------------------------------*/
108 /*----- Tweakables --------------------------------------------------------*/
110 /* --- @MPMONT_DISABLE@ --- *
112 * Replace all the clever Montgomery reduction with good old-fashioned long
116 /* #define MPMONT_DISABLE */
118 /*----- Reduction and multiplication --------------------------------------*/
120 /* --- @mpmont_create@ --- *
122 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
123 * @mp *m@ = modulus to use
127 * Use: Initializes a Montgomery reduction context ready for use.
128 * The argument @m@ must be a positive odd integer.
131 #ifdef MPMONT_DISABLE
133 void mpmont_create(mpmont
*mm
, mp
*m
)
144 void mpmont_create(mpmont
*mm
, mp
*m
)
146 size_t n
= MP_LEN(m
);
147 mp
*r2
= mp_new(2 * n
+ 1, 0);
150 /* --- Take a copy of the modulus --- */
152 assert(MP_ISPOS(m
) && MP_ISODD(m
));
155 /* --- Determine %$R^2$% --- */
158 MPX_ZERO(r2
->v
, r2
->vl
- 1);
161 /* --- Find the magic value @mi@ --- */
163 mp_build(&r
, r2
->v
+ n
, r2
->vl
);
165 mp_gcd(0, 0, &mm
->mi
, &r
, m
);
166 mm
->mi
= mp_sub(mm
->mi
, &r
, mm
->mi
);
168 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
171 mp_div(0, &mm
->r2
, r2
, m
);
172 mm
->r
= mpmont_reduce(mm
, MP_NEW
, mm
->r2
);
178 /* --- @mpmont_destroy@ --- *
180 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
184 * Use: Disposes of a context when it's no longer of any use to
188 void mpmont_destroy(mpmont
*mm
)
196 /* --- @mpmont_reduce@ --- *
198 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
199 * @mp *d@ = destination
200 * @mp *a@ = source, assumed positive
202 * Returns: Result, %$a R^{-1} \bmod m$%.
205 #ifdef MPMONT_DISABLE
207 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
209 mp_div(0, &d
, a
, mm
->m
);
215 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
219 /* --- Check for serious Karatsuba reduction --- */
221 if (n
> MPK_THRESH
* 3) {
230 mp_build(&al
, a
->v
, vl
);
231 u
= mp_mul(MP_NEW
, &al
, mm
->mi
);
234 u
= mp_mul(u
, u
, mm
->m
);
239 /* --- Otherwise do it the hard way --- */
247 /* --- Initial conditioning of the arguments --- */
253 MP_DEST(d
, 2 * n
+ 1, a
->f
);
255 dv
= d
->v
; dvl
= d
->vl
;
256 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
258 /* --- Let's go to work --- */
262 mpw u
= MPW(*dv
* mi
);
263 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
268 /* --- Wrap everything up --- */
270 memmove(d
->v
, d
->v
+ n
, MPWS(MP_LEN(d
) - n
));
272 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
273 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
275 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
284 /* --- @mpmont_mul@ --- *
286 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
287 * @mp *d@ = destination
288 * @mp *a, *b@ = sources, assumed positive
290 * Returns: Result, %$a b R^{-1} \bmod m$%.
293 #ifdef MPMONT_DISABLE
295 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
298 mp_div(0, &d
, d
, mm
->m
);
304 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
306 if (mm
->n
> MPK_THRESH
* 3) {
308 d
= mpmont_reduce(mm
, d
, d
);
318 /* --- Initial conditioning of the arguments --- */
320 if (MP_LEN(a
) > MP_LEN(b
)) {
321 mp
*t
= a
; a
= b
; b
= t
;
327 MP_DEST(d
, 2 * n
+ 1, a
->f
| b
->f
| MP_UNDEF
);
328 dv
= d
->v
; dvl
= d
->vl
;
330 av
= a
->v
; avl
= a
->vl
;
331 bv
= b
->v
; bvl
= b
->vl
;
332 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
335 /* --- Montgomery multiplication phase --- */
339 while (i
< n
&& av
< avl
) {
341 mpw u
= MPW((*dv
+ x
* y
) * mi
);
342 MPX_UMLAN(dv
, dvl
, bv
, bvl
, x
);
343 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
348 /* --- Simpler Montgomery reduction phase --- */
351 mpw u
= MPW(*dv
* mi
);
352 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
359 memmove(d
->v
, dv
, MPWS(dvl
- dv
));
361 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
362 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
363 if ((a
->f
^ b
->f
) & MP_NEG
)
364 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
366 d
->f
= (a
->f
| b
->f
) & MP_BURN
;
376 /*----- Test rig ----------------------------------------------------------*/
380 static int tcreate(dstr
*v
)
382 mp
*m
= *(mp
**)v
[0].buf
;
383 mp
*mi
= *(mp
**)v
[1].buf
;
384 mp
*r
= *(mp
**)v
[2].buf
;
385 mp
*r2
= *(mp
**)v
[3].buf
;
390 mpmont_create(&mm
, m
);
392 if (mm
.mi
->v
[0] != mi
->v
[0]) {
393 fprintf(stderr
, "\n*** bad mi: found %lu, expected %lu",
394 (unsigned long)mm
.mi
->v
[0], (unsigned long)mi
->v
[0]);
395 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
400 if (!MP_EQ(mm
.r
, r
)) {
401 fputs("\n*** bad r", stderr
);
402 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
403 fputs("\nexpected ", stderr
); mp_writefile(r
, stderr
, 10);
404 fputs("\n found ", stderr
); mp_writefile(mm
.r
, stderr
, 10);
409 if (!MP_EQ(mm
.r2
, r2
)) {
410 fputs("\n*** bad r2", stderr
);
411 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
412 fputs("\nexpected ", stderr
); mp_writefile(r2
, stderr
, 10);
413 fputs("\n found ", stderr
); mp_writefile(mm
.r2
, stderr
, 10);
423 assert(mparena_count(MPARENA_GLOBAL
) == 0);
427 static int tmul(dstr
*v
)
429 mp
*m
= *(mp
**)v
[0].buf
;
430 mp
*a
= *(mp
**)v
[1].buf
;
431 mp
*b
= *(mp
**)v
[2].buf
;
432 mp
*r
= *(mp
**)v
[3].buf
;
436 mpmont_create(&mm
, m
);
439 mp
*qr
= mp_mul(MP_NEW
, a
, b
);
440 mp_div(0, &qr
, qr
, m
);
443 fputs("\n*** classical modmul failed", stderr
);
444 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
445 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
446 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
447 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
448 fputs("\nqr = ", stderr
); mp_writefile(qr
, stderr
, 10);
457 mp
*ar
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
458 mp
*br
= mpmont_mul(&mm
, MP_NEW
, b
, mm
.r2
);
459 mp
*mr
= mpmont_mul(&mm
, MP_NEW
, ar
, br
);
460 mr
= mpmont_reduce(&mm
, mr
, mr
);
462 fputs("\n*** montgomery modmul failed", stderr
);
463 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
464 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
465 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
466 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
467 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
471 MP_DROP(ar
); MP_DROP(br
);
481 assert(mparena_count(MPARENA_GLOBAL
) == 0);
485 static test_chunk tests
[] = {
486 { "create", tcreate
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
487 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
491 int main(int argc
, char *argv
[])
494 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
500 /*----- That's all, folks -------------------------------------------------*/