2371de89f629481bd690cd06779da112db4370b4
3 * $Id: mpmont.c,v 1.15 2001/06/16 13:00:20 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.15 2001/06/16 13:00:20 mdw
34 * Use the generic exponentiation functions.
36 * Revision 1.14 2001/02/22 09:04:26 mdw
39 * Revision 1.13 2001/02/03 12:00:29 mdw
40 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
41 * it. Note that the macro version @MP_DROP@ doesn't do this.
43 * Revision 1.12 2000/10/08 15:48:35 mdw
44 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
46 * Revision 1.11 2000/10/08 12:04:27 mdw
47 * (mpmont_reduce, mpmont_mul): Cope with negative numbers.
49 * Revision 1.10 2000/07/29 17:05:43 mdw
50 * (mpmont_expr): Use sliding window exponentiation, with a drop-through
51 * for small exponents to use a simple left-to-right bitwise routine. This
52 * can reduce modexp times by up to a quarter.
54 * Revision 1.9 2000/06/17 11:45:09 mdw
55 * Major memory management overhaul. Added arena support. Use the secure
56 * arena for secret integers. Replace and improve the MP management macros
57 * (e.g., replace MP_MODIFY by MP_DEST).
59 * Revision 1.8 1999/12/22 15:55:00 mdw
60 * Adjust Karatsuba parameters.
62 * Revision 1.7 1999/12/11 01:51:14 mdw
63 * Use a Karatsuba-based reduction for large moduli.
65 * Revision 1.6 1999/12/10 23:18:39 mdw
66 * Change interface for suggested destinations.
68 * Revision 1.5 1999/11/22 13:58:40 mdw
69 * Add an option to disable Montgomery reduction, so that performance
70 * comparisons can be done.
72 * Revision 1.4 1999/11/21 12:27:06 mdw
73 * Remove a division from the Montgomery setup by calculating
74 * %$R^2 \bmod m$% first and then %$R \bmod m$% by Montgomery reduction of
77 * Revision 1.3 1999/11/21 11:35:10 mdw
78 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
79 * @mpmont_mul@ for squaring in exponentiation.
81 * Revision 1.2 1999/11/19 13:17:26 mdw
82 * Add extra interface to exponentiation which returns a Montgomerized
85 * Revision 1.1 1999/11/17 18:02:16 mdw
86 * New multiprecision integer arithmetic suite.
90 /*----- Header files ------------------------------------------------------*/
94 #include "mpmont-exp.h"
96 /*----- Tweakables --------------------------------------------------------*/
98 /* --- @MPMONT_DISABLE@ --- *
100 * Replace all the clever Montgomery reduction with good old-fashioned long
104 /* #define MPMONT_DISABLE */
106 /*----- Reduction and multiplication --------------------------------------*/
108 /* --- @mpmont_create@ --- *
110 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
111 * @mp *m@ = modulus to use
115 * Use: Initializes a Montgomery reduction context ready for use.
116 * The argument @m@ must be a positive odd integer.
119 #ifdef MPMONT_DISABLE
121 void mpmont_create(mpmont
*mm
, mp
*m
)
132 void mpmont_create(mpmont
*mm
, mp
*m
)
134 size_t n
= MP_LEN(m
);
135 mp
*r2
= mp_new(2 * n
+ 1, 0);
138 /* --- Validate the arguments --- */
140 assert(((void)"Montgomery modulus must be positive",
141 (m
->f
& MP_NEG
) == 0));
142 assert(((void)"Montgomery modulus must be odd", m
->v
[0] & 1));
144 /* --- Take a copy of the modulus --- */
149 /* --- Determine %$R^2$% --- */
152 MPX_ZERO(r2
->v
, r2
->vl
- 1);
155 /* --- Find the magic value @mi@ --- */
157 mp_build(&r
, r2
->v
+ n
, r2
->vl
);
159 mp_gcd(0, 0, &mm
->mi
, &r
, m
);
160 mm
->mi
= mp_sub(mm
->mi
, &r
, mm
->mi
);
162 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
165 mp_div(0, &mm
->r2
, r2
, m
);
166 mm
->r
= mpmont_reduce(mm
, MP_NEW
, mm
->r2
);
172 /* --- @mpmont_destroy@ --- *
174 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
178 * Use: Disposes of a context when it's no longer of any use to
182 void mpmont_destroy(mpmont
*mm
)
190 /* --- @mpmont_reduce@ --- *
192 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
193 * @mp *d@ = destination
194 * @mp *a@ = source, assumed positive
196 * Returns: Result, %$a R^{-1} \bmod m$%.
199 #ifdef MPMONT_DISABLE
201 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
203 mp_div(0, &d
, a
, mm
->m
);
209 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
213 /* --- Check for serious Karatsuba reduction --- */
215 if (n
> MPK_THRESH
* 3) {
224 mp_build(&al
, a
->v
, vl
);
225 u
= mp_mul(MP_NEW
, &al
, mm
->mi
);
228 u
= mp_mul(u
, u
, mm
->m
);
233 /* --- Otherwise do it the hard way --- */
241 /* --- Initial conditioning of the arguments --- */
247 MP_DEST(d
, 2 * n
+ 1, a
->f
);
249 dv
= d
->v
; dvl
= d
->vl
;
250 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
252 /* --- Let's go to work --- */
256 mpw u
= MPW(*dv
* mi
);
257 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
262 /* --- Wrap everything up --- */
264 memmove(d
->v
, d
->v
+ n
, MPWS(MP_LEN(d
) - n
));
266 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
267 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
269 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
278 /* --- @mpmont_mul@ --- *
280 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
281 * @mp *d@ = destination
282 * @mp *a, *b@ = sources, assumed positive
284 * Returns: Result, %$a b R^{-1} \bmod m$%.
287 #ifdef MPMONT_DISABLE
289 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
292 mp_div(0, &d
, d
, mm
->m
);
298 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
300 if (mm
->n
> MPK_THRESH
* 3) {
302 d
= mpmont_reduce(mm
, d
, d
);
312 /* --- Initial conditioning of the arguments --- */
314 if (MP_LEN(a
) > MP_LEN(b
)) {
315 mp
*t
= a
; a
= b
; b
= t
;
321 MP_DEST(d
, 2 * n
+ 1, a
->f
| b
->f
| MP_UNDEF
);
322 dv
= d
->v
; dvl
= d
->vl
;
324 av
= a
->v
; avl
= a
->vl
;
325 bv
= b
->v
; bvl
= b
->vl
;
326 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
329 /* --- Montgomery multiplication phase --- */
333 while (i
< n
&& av
< avl
) {
335 mpw u
= MPW((*dv
+ x
* y
) * mi
);
336 MPX_UMLAN(dv
, dvl
, bv
, bvl
, x
);
337 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
342 /* --- Simpler Montgomery reduction phase --- */
345 mpw u
= MPW(*dv
* mi
);
346 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
353 memmove(d
->v
, dv
, MPWS(dvl
- dv
));
355 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
356 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
357 if ((a
->f
^ b
->f
) & MP_NEG
)
358 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
360 d
->f
= (a
->f
| b
->f
) & MP_BURN
;
370 /*----- Exponentiation ----------------------------------------------------*/
372 /* --- @mpmont_expr@ --- *
374 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
375 * @mp *d@ = fake destination
379 * Returns: Result, %$(a R^{-1})^e R \bmod m$%.
382 mp
*mpmont_expr(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
384 mp
*x
= MP_COPY(mm
->r
);
385 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
390 else if (MP_LEN(e
) < EXP_THRESH
)
399 /* --- @mpmont_exp@ --- *
401 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
402 * @mp *d@ = fake destination
406 * Returns: Result, %$a^e \bmod m$%.
409 mp
*mpmont_exp(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
411 d
= mpmont_mul(mm
, d
, a
, mm
->r2
);
412 d
= mpmont_expr(mm
, d
, d
, e
);
413 d
= mpmont_reduce(mm
, d
, d
);
417 /*----- Test rig ----------------------------------------------------------*/
421 static int tcreate(dstr
*v
)
423 mp
*m
= *(mp
**)v
[0].buf
;
424 mp
*mi
= *(mp
**)v
[1].buf
;
425 mp
*r
= *(mp
**)v
[2].buf
;
426 mp
*r2
= *(mp
**)v
[3].buf
;
431 mpmont_create(&mm
, m
);
433 if (mm
.mi
->v
[0] != mi
->v
[0]) {
434 fprintf(stderr
, "\n*** bad mi: found %lu, expected %lu",
435 (unsigned long)mm
.mi
->v
[0], (unsigned long)mi
->v
[0]);
436 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
441 if (!MP_EQ(mm
.r
, r
)) {
442 fputs("\n*** bad r", stderr
);
443 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
444 fputs("\nexpected ", stderr
); mp_writefile(r
, stderr
, 10);
445 fputs("\n found ", stderr
); mp_writefile(mm
.r
, stderr
, 10);
450 if (!MP_EQ(mm
.r2
, r2
)) {
451 fputs("\n*** bad r2", stderr
);
452 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
453 fputs("\nexpected ", stderr
); mp_writefile(r2
, stderr
, 10);
454 fputs("\n found ", stderr
); mp_writefile(mm
.r2
, stderr
, 10);
464 assert(mparena_count(MPARENA_GLOBAL
) == 0);
468 static int tmul(dstr
*v
)
470 mp
*m
= *(mp
**)v
[0].buf
;
471 mp
*a
= *(mp
**)v
[1].buf
;
472 mp
*b
= *(mp
**)v
[2].buf
;
473 mp
*r
= *(mp
**)v
[3].buf
;
477 mpmont_create(&mm
, m
);
480 mp
*qr
= mp_mul(MP_NEW
, a
, b
);
481 mp_div(0, &qr
, qr
, m
);
484 fputs("\n*** classical modmul failed", stderr
);
485 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
486 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
487 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
488 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
489 fputs("\nqr = ", stderr
); mp_writefile(qr
, stderr
, 10);
498 mp
*ar
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
499 mp
*br
= mpmont_mul(&mm
, MP_NEW
, b
, mm
.r2
);
500 mp
*mr
= mpmont_mul(&mm
, MP_NEW
, ar
, br
);
501 mr
= mpmont_reduce(&mm
, mr
, mr
);
503 fputs("\n*** montgomery modmul failed", stderr
);
504 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
505 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
506 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
507 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
508 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
512 MP_DROP(ar
); MP_DROP(br
);
522 assert(mparena_count(MPARENA_GLOBAL
) == 0);
526 static int texp(dstr
*v
)
528 mp
*m
= *(mp
**)v
[0].buf
;
529 mp
*a
= *(mp
**)v
[1].buf
;
530 mp
*b
= *(mp
**)v
[2].buf
;
531 mp
*r
= *(mp
**)v
[3].buf
;
536 mpmont_create(&mm
, m
);
538 mr
= mpmont_exp(&mm
, MP_NEW
, a
, b
);
541 fputs("\n*** montgomery modexp failed", stderr
);
542 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
543 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
544 fputs("\n e = ", stderr
); mp_writefile(b
, stderr
, 10);
545 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
546 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
557 assert(mparena_count(MPARENA_GLOBAL
) == 0);
562 static test_chunk tests
[] = {
563 { "create", tcreate
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
564 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
565 { "exp", texp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
569 int main(int argc
, char *argv
[])
572 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
578 /*----- That's all, folks -------------------------------------------------*/