c644801393b6febb30258234deb59540d215ec5f
3 * $Id: mpmont.c,v 1.16 2002/01/13 13:40:31 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.16 2002/01/13 13:40:31 mdw
34 * Avoid trashing arguments before we've used them.
36 * Revision 1.15 2001/06/16 13:00:20 mdw
37 * Use the generic exponentiation functions.
39 * Revision 1.14 2001/02/22 09:04:26 mdw
42 * Revision 1.13 2001/02/03 12:00:29 mdw
43 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
44 * it. Note that the macro version @MP_DROP@ doesn't do this.
46 * Revision 1.12 2000/10/08 15:48:35 mdw
47 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
49 * Revision 1.11 2000/10/08 12:04:27 mdw
50 * (mpmont_reduce, mpmont_mul): Cope with negative numbers.
52 * Revision 1.10 2000/07/29 17:05:43 mdw
53 * (mpmont_expr): Use sliding window exponentiation, with a drop-through
54 * for small exponents to use a simple left-to-right bitwise routine. This
55 * can reduce modexp times by up to a quarter.
57 * Revision 1.9 2000/06/17 11:45:09 mdw
58 * Major memory management overhaul. Added arena support. Use the secure
59 * arena for secret integers. Replace and improve the MP management macros
60 * (e.g., replace MP_MODIFY by MP_DEST).
62 * Revision 1.8 1999/12/22 15:55:00 mdw
63 * Adjust Karatsuba parameters.
65 * Revision 1.7 1999/12/11 01:51:14 mdw
66 * Use a Karatsuba-based reduction for large moduli.
68 * Revision 1.6 1999/12/10 23:18:39 mdw
69 * Change interface for suggested destinations.
71 * Revision 1.5 1999/11/22 13:58:40 mdw
72 * Add an option to disable Montgomery reduction, so that performance
73 * comparisons can be done.
75 * Revision 1.4 1999/11/21 12:27:06 mdw
76 * Remove a division from the Montgomery setup by calculating
77 * %$R^2 \bmod m$% first and then %$R \bmod m$% by Montgomery reduction of
80 * Revision 1.3 1999/11/21 11:35:10 mdw
81 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
82 * @mpmont_mul@ for squaring in exponentiation.
84 * Revision 1.2 1999/11/19 13:17:26 mdw
85 * Add extra interface to exponentiation which returns a Montgomerized
88 * Revision 1.1 1999/11/17 18:02:16 mdw
89 * New multiprecision integer arithmetic suite.
93 /*----- Header files ------------------------------------------------------*/
97 #include "mpmont-exp.h"
99 /*----- Tweakables --------------------------------------------------------*/
101 /* --- @MPMONT_DISABLE@ --- *
103 * Replace all the clever Montgomery reduction with good old-fashioned long
107 /* #define MPMONT_DISABLE */
109 /*----- Reduction and multiplication --------------------------------------*/
111 /* --- @mpmont_create@ --- *
113 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
114 * @mp *m@ = modulus to use
118 * Use: Initializes a Montgomery reduction context ready for use.
119 * The argument @m@ must be a positive odd integer.
122 #ifdef MPMONT_DISABLE
124 void mpmont_create(mpmont
*mm
, mp
*m
)
135 void mpmont_create(mpmont
*mm
, mp
*m
)
137 size_t n
= MP_LEN(m
);
138 mp
*r2
= mp_new(2 * n
+ 1, 0);
141 /* --- Validate the arguments --- */
143 assert(((void)"Montgomery modulus must be positive",
144 (m
->f
& MP_NEG
) == 0));
145 assert(((void)"Montgomery modulus must be odd", m
->v
[0] & 1));
147 /* --- Take a copy of the modulus --- */
152 /* --- Determine %$R^2$% --- */
155 MPX_ZERO(r2
->v
, r2
->vl
- 1);
158 /* --- Find the magic value @mi@ --- */
160 mp_build(&r
, r2
->v
+ n
, r2
->vl
);
162 mp_gcd(0, 0, &mm
->mi
, &r
, m
);
163 mm
->mi
= mp_sub(mm
->mi
, &r
, mm
->mi
);
165 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
168 mp_div(0, &mm
->r2
, r2
, m
);
169 mm
->r
= mpmont_reduce(mm
, MP_NEW
, mm
->r2
);
175 /* --- @mpmont_destroy@ --- *
177 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
181 * Use: Disposes of a context when it's no longer of any use to
185 void mpmont_destroy(mpmont
*mm
)
193 /* --- @mpmont_reduce@ --- *
195 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
196 * @mp *d@ = destination
197 * @mp *a@ = source, assumed positive
199 * Returns: Result, %$a R^{-1} \bmod m$%.
202 #ifdef MPMONT_DISABLE
204 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
206 mp_div(0, &d
, a
, mm
->m
);
212 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
216 /* --- Check for serious Karatsuba reduction --- */
218 if (n
> MPK_THRESH
* 3) {
227 mp_build(&al
, a
->v
, vl
);
228 u
= mp_mul(MP_NEW
, &al
, mm
->mi
);
231 u
= mp_mul(u
, u
, mm
->m
);
236 /* --- Otherwise do it the hard way --- */
244 /* --- Initial conditioning of the arguments --- */
250 MP_DEST(d
, 2 * n
+ 1, a
->f
);
252 dv
= d
->v
; dvl
= d
->vl
;
253 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
255 /* --- Let's go to work --- */
259 mpw u
= MPW(*dv
* mi
);
260 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
265 /* --- Wrap everything up --- */
267 memmove(d
->v
, d
->v
+ n
, MPWS(MP_LEN(d
) - n
));
269 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
270 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
272 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
281 /* --- @mpmont_mul@ --- *
283 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
284 * @mp *d@ = destination
285 * @mp *a, *b@ = sources, assumed positive
287 * Returns: Result, %$a b R^{-1} \bmod m$%.
290 #ifdef MPMONT_DISABLE
292 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
295 mp_div(0, &d
, d
, mm
->m
);
301 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
303 if (mm
->n
> MPK_THRESH
* 3) {
305 d
= mpmont_reduce(mm
, d
, d
);
315 /* --- Initial conditioning of the arguments --- */
317 if (MP_LEN(a
) > MP_LEN(b
)) {
318 mp
*t
= a
; a
= b
; b
= t
;
324 MP_DEST(d
, 2 * n
+ 1, a
->f
| b
->f
| MP_UNDEF
);
325 dv
= d
->v
; dvl
= d
->vl
;
327 av
= a
->v
; avl
= a
->vl
;
328 bv
= b
->v
; bvl
= b
->vl
;
329 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
332 /* --- Montgomery multiplication phase --- */
336 while (i
< n
&& av
< avl
) {
338 mpw u
= MPW((*dv
+ x
* y
) * mi
);
339 MPX_UMLAN(dv
, dvl
, bv
, bvl
, x
);
340 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
345 /* --- Simpler Montgomery reduction phase --- */
348 mpw u
= MPW(*dv
* mi
);
349 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
356 memmove(d
->v
, dv
, MPWS(dvl
- dv
));
358 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
359 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
360 if ((a
->f
^ b
->f
) & MP_NEG
)
361 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
363 d
->f
= (a
->f
| b
->f
) & MP_BURN
;
373 /*----- Exponentiation ----------------------------------------------------*/
375 /* --- @mpmont_expr@ --- *
377 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
378 * @mp *d@ = fake destination
382 * Returns: Result, %$(a R^{-1})^e R \bmod m$%.
385 mp
*mpmont_expr(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
387 mp
*x
= MP_COPY(mm
->r
);
388 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
393 else if (MP_LEN(e
) < EXP_THRESH
)
402 /* --- @mpmont_exp@ --- *
404 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
405 * @mp *d@ = fake destination
409 * Returns: Result, %$a^e \bmod m$%.
412 mp
*mpmont_exp(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
415 d
= mpmont_mul(mm
, d
, a
, mm
->r2
);
416 d
= mpmont_expr(mm
, d
, d
, e
);
417 d
= mpmont_reduce(mm
, d
, d
);
422 /*----- Test rig ----------------------------------------------------------*/
426 static int tcreate(dstr
*v
)
428 mp
*m
= *(mp
**)v
[0].buf
;
429 mp
*mi
= *(mp
**)v
[1].buf
;
430 mp
*r
= *(mp
**)v
[2].buf
;
431 mp
*r2
= *(mp
**)v
[3].buf
;
436 mpmont_create(&mm
, m
);
438 if (mm
.mi
->v
[0] != mi
->v
[0]) {
439 fprintf(stderr
, "\n*** bad mi: found %lu, expected %lu",
440 (unsigned long)mm
.mi
->v
[0], (unsigned long)mi
->v
[0]);
441 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
446 if (!MP_EQ(mm
.r
, r
)) {
447 fputs("\n*** bad r", stderr
);
448 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
449 fputs("\nexpected ", stderr
); mp_writefile(r
, stderr
, 10);
450 fputs("\n found ", stderr
); mp_writefile(mm
.r
, stderr
, 10);
455 if (!MP_EQ(mm
.r2
, r2
)) {
456 fputs("\n*** bad r2", stderr
);
457 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
458 fputs("\nexpected ", stderr
); mp_writefile(r2
, stderr
, 10);
459 fputs("\n found ", stderr
); mp_writefile(mm
.r2
, stderr
, 10);
469 assert(mparena_count(MPARENA_GLOBAL
) == 0);
473 static int tmul(dstr
*v
)
475 mp
*m
= *(mp
**)v
[0].buf
;
476 mp
*a
= *(mp
**)v
[1].buf
;
477 mp
*b
= *(mp
**)v
[2].buf
;
478 mp
*r
= *(mp
**)v
[3].buf
;
482 mpmont_create(&mm
, m
);
485 mp
*qr
= mp_mul(MP_NEW
, a
, b
);
486 mp_div(0, &qr
, qr
, m
);
489 fputs("\n*** classical modmul failed", stderr
);
490 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
491 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
492 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
493 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
494 fputs("\nqr = ", stderr
); mp_writefile(qr
, stderr
, 10);
503 mp
*ar
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
504 mp
*br
= mpmont_mul(&mm
, MP_NEW
, b
, mm
.r2
);
505 mp
*mr
= mpmont_mul(&mm
, MP_NEW
, ar
, br
);
506 mr
= mpmont_reduce(&mm
, mr
, mr
);
508 fputs("\n*** montgomery modmul failed", stderr
);
509 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
510 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
511 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
512 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
513 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
517 MP_DROP(ar
); MP_DROP(br
);
527 assert(mparena_count(MPARENA_GLOBAL
) == 0);
531 static int texp(dstr
*v
)
533 mp
*m
= *(mp
**)v
[0].buf
;
534 mp
*a
= *(mp
**)v
[1].buf
;
535 mp
*b
= *(mp
**)v
[2].buf
;
536 mp
*r
= *(mp
**)v
[3].buf
;
541 mpmont_create(&mm
, m
);
543 mr
= mpmont_exp(&mm
, MP_NEW
, a
, b
);
546 fputs("\n*** montgomery modexp failed", stderr
);
547 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
548 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
549 fputs("\n e = ", stderr
); mp_writefile(b
, stderr
, 10);
550 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
551 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
562 assert(mparena_count(MPARENA_GLOBAL
) == 0);
567 static test_chunk tests
[] = {
568 { "create", tcreate
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
569 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
570 { "exp", texp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
574 int main(int argc
, char *argv
[])
577 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
583 /*----- That's all, folks -------------------------------------------------*/