3 * $Id: mpmont.c,v 1.14 2001/02/22 09:04:26 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.14 2001/02/22 09:04:26 mdw
36 * Revision 1.13 2001/02/03 12:00:29 mdw
37 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
38 * it. Note that the macro version @MP_DROP@ doesn't do this.
40 * Revision 1.12 2000/10/08 15:48:35 mdw
41 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
43 * Revision 1.11 2000/10/08 12:04:27 mdw
44 * (mpmont_reduce, mpmont_mul): Cope with negative numbers.
46 * Revision 1.10 2000/07/29 17:05:43 mdw
47 * (mpmont_expr): Use sliding window exponentiation, with a drop-through
48 * for small exponents to use a simple left-to-right bitwise routine. This
49 * can reduce modexp times by up to a quarter.
51 * Revision 1.9 2000/06/17 11:45:09 mdw
52 * Major memory management overhaul. Added arena support. Use the secure
53 * arena for secret integers. Replace and improve the MP management macros
54 * (e.g., replace MP_MODIFY by MP_DEST).
56 * Revision 1.8 1999/12/22 15:55:00 mdw
57 * Adjust Karatsuba parameters.
59 * Revision 1.7 1999/12/11 01:51:14 mdw
60 * Use a Karatsuba-based reduction for large moduli.
62 * Revision 1.6 1999/12/10 23:18:39 mdw
63 * Change interface for suggested destinations.
65 * Revision 1.5 1999/11/22 13:58:40 mdw
66 * Add an option to disable Montgomery reduction, so that performance
67 * comparisons can be done.
69 * Revision 1.4 1999/11/21 12:27:06 mdw
70 * Remove a division from the Montgomery setup by calculating
71 * %$R^2 \bmod m$% first and then %$R \bmod m$% by Montgomery reduction of
74 * Revision 1.3 1999/11/21 11:35:10 mdw
75 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
76 * @mpmont_mul@ for squaring in exponentiation.
78 * Revision 1.2 1999/11/19 13:17:26 mdw
79 * Add extra interface to exponentiation which returns a Montgomerized
82 * Revision 1.1 1999/11/17 18:02:16 mdw
83 * New multiprecision integer arithmetic suite.
87 /*----- Header files ------------------------------------------------------*/
92 /*----- Tweakables --------------------------------------------------------*/
94 /* --- @MPMONT_DISABLE@ --- *
96 * Replace all the clever Montgomery reduction with good old-fashioned long
100 /* #define MPMONT_DISABLE */
102 /*----- Main code ---------------------------------------------------------*/
104 /* --- @mpmont_create@ --- *
106 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
107 * @mp *m@ = modulus to use
111 * Use: Initializes a Montgomery reduction context ready for use.
112 * The argument @m@ must be a positive odd integer.
115 #ifdef MPMONT_DISABLE
117 void mpmont_create(mpmont
*mm
, mp
*m
)
128 void mpmont_create(mpmont
*mm
, mp
*m
)
130 size_t n
= MP_LEN(m
);
131 mp
*r2
= mp_new(2 * n
+ 1, 0);
134 /* --- Validate the arguments --- */
136 assert(((void)"Montgomery modulus must be positive",
137 (m
->f
& MP_NEG
) == 0));
138 assert(((void)"Montgomery modulus must be odd", m
->v
[0] & 1));
140 /* --- Take a copy of the modulus --- */
145 /* --- Determine %$R^2$% --- */
148 MPX_ZERO(r2
->v
, r2
->vl
- 1);
151 /* --- Find the magic value @mi@ --- */
153 mp_build(&r
, r2
->v
+ n
, r2
->vl
);
155 mp_gcd(0, 0, &mm
->mi
, &r
, m
);
156 mm
->mi
= mp_sub(mm
->mi
, &r
, mm
->mi
);
158 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
161 mp_div(0, &mm
->r2
, r2
, m
);
162 mm
->r
= mpmont_reduce(mm
, MP_NEW
, mm
->r2
);
168 /* --- @mpmont_destroy@ --- *
170 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
174 * Use: Disposes of a context when it's no longer of any use to
178 void mpmont_destroy(mpmont
*mm
)
186 /* --- @mpmont_reduce@ --- *
188 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
189 * @mp *d@ = destination
190 * @mp *a@ = source, assumed positive
192 * Returns: Result, %$a R^{-1} \bmod m$%.
195 #ifdef MPMONT_DISABLE
197 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
199 mp_div(0, &d
, a
, mm
->m
);
205 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
209 /* --- Check for serious Karatsuba reduction --- */
211 if (n
> MPK_THRESH
* 3) {
220 mp_build(&al
, a
->v
, vl
);
221 u
= mp_mul(MP_NEW
, &al
, mm
->mi
);
224 u
= mp_mul(u
, u
, mm
->m
);
229 /* --- Otherwise do it the hard way --- */
237 /* --- Initial conditioning of the arguments --- */
243 MP_DEST(d
, 2 * n
+ 1, a
->f
);
245 dv
= d
->v
; dvl
= d
->vl
;
246 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
248 /* --- Let's go to work --- */
252 mpw u
= MPW(*dv
* mi
);
253 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
258 /* --- Wrap everything up --- */
260 memmove(d
->v
, d
->v
+ n
, MPWS(MP_LEN(d
) - n
));
262 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
263 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
265 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
274 /* --- @mpmont_mul@ --- *
276 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
277 * @mp *d@ = destination
278 * @mp *a, *b@ = sources, assumed positive
280 * Returns: Result, %$a b R^{-1} \bmod m$%.
283 #ifdef MPMONT_DISABLE
285 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
288 mp_div(0, &d
, d
, mm
->m
);
294 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
296 if (mm
->n
> MPK_THRESH
* 3) {
298 d
= mpmont_reduce(mm
, d
, d
);
308 /* --- Initial conditioning of the arguments --- */
310 if (MP_LEN(a
) > MP_LEN(b
)) {
311 mp
*t
= a
; a
= b
; b
= t
;
317 MP_DEST(d
, 2 * n
+ 1, a
->f
| b
->f
| MP_UNDEF
);
318 dv
= d
->v
; dvl
= d
->vl
;
320 av
= a
->v
; avl
= a
->vl
;
321 bv
= b
->v
; bvl
= b
->vl
;
322 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
325 /* --- Montgomery multiplication phase --- */
329 while (i
< n
&& av
< avl
) {
331 mpw u
= MPW((*dv
+ x
* y
) * mi
);
332 MPX_UMLAN(dv
, dvl
, bv
, bvl
, x
);
333 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
338 /* --- Simpler Montgomery reduction phase --- */
341 mpw u
= MPW(*dv
* mi
);
342 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
349 memmove(d
->v
, dv
, MPWS(dvl
- dv
));
351 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
352 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
353 if ((a
->f
^ b
->f
) & MP_NEG
)
354 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
356 d
->f
= (a
->f
| b
->f
) & MP_BURN
;
366 /* --- @mpmont_expr@ --- *
368 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
369 * @mp *d@ = fake destination
373 * Returns: Result, %$a^e R \bmod m$%.
377 #define TABSZ (1 << (WINSZ - 1))
379 #define THRESH (((MPW_BITS / WINSZ) << 2) + 1)
381 static mp
*exp_simple(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
385 mp
*x
= MP_COPY(mm
->r
);
386 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
392 while (!MP_RBIT(&sc
))
395 /* --- Do the main body of the work --- */
397 ar
= mpmont_mul(mm
, MP_NEW
, a
, mm
->r2
);
402 y
= mp_sqr(spare
, x
);
403 y
= mpmont_reduce(mm
, y
, y
);
407 { mp
*y
= mpmont_mul(mm
, spare
, x
, ar
); spare
= x
; x
= y
; }
418 /* --- Do a final round of squaring --- */
423 y
= mp_sqr(spare
, x
);
424 y
= mpmont_reduce(mm
, y
, y
);
440 mp
*mpmont_expr(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
444 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
445 mp
*x
= MP_COPY(mm
->r
);
449 /* --- Do we bother? --- */
454 if (MP_LEN(e
) < THRESH
) {
456 return (exp_simple(mm
, d
, a
, e
));
459 /* --- Do the precomputation --- */
461 ar
= mpmont_mul(mm
, MP_NEW
, a
, mm
->r2
);
462 a2
= mp_sqr(MP_NEW
, ar
);
463 a2
= mpmont_reduce(mm
, a2
, a2
);
464 tab
= xmalloc(TABSZ
* sizeof(mp
*));
466 for (i
= 1; i
< TABSZ
; i
++)
467 tab
[i
] = mpmont_mul(mm
, MP_NEW
, tab
[i
- 1], a2
);
471 /* --- Skip top-end zero bits --- *
473 * If the initial step worked, there must be a set bit somewhere, so keep
474 * stepping until I find it.
478 while (!MP_RBIT(&sc
))
481 /* --- Now for the main work --- */
487 /* --- The next bit is set, so read a window index --- *
489 * Reset @i@ to zero and increment @sq@. Then, until either I read
490 * @WINSZ@ bits or I run out of bits, scan in a bit: if it's clear, bump
491 * the @z@ counter; if it's set, push a set bit into @i@, shift it over
492 * by @z@ bits, bump @sq@ by @z + 1@ and clear @z@. By the end of this
493 * palaver, @i@ is an index to the precomputed value in @tab@.
500 if (l
>= WINSZ
|| !MP_RSTEP(&sc
))
505 i
= ((i
<< 1) | 1) << z
;
511 /* --- Do the squaring --- *
513 * Remember that @sq@ carries over from the zero-skipping stuff below.
518 y
= mp_sqr(spare
, x
);
519 y
= mpmont_reduce(mm
, y
, y
);
524 /* --- Do the multiply --- */
526 { mp
*y
= mpmont_mul(mm
, spare
, x
, tab
[i
]); spare
= x
; x
= y
; }
528 /* --- Now grind along through the rest of the bits --- */
540 /* --- Do a final round of squaring --- */
545 y
= mp_sqr(spare
, x
);
546 y
= mpmont_reduce(mm
, y
, y
);
553 for (i
= 0; i
< TABSZ
; i
++)
562 /* --- @mpmont_exp@ --- *
564 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
565 * @mp *d@ = fake destination
569 * Returns: Result, %$a^e \bmod m$%.
572 mp
*mpmont_exp(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
574 d
= mpmont_expr(mm
, d
, a
, e
);
575 d
= mpmont_reduce(mm
, d
, d
);
579 /*----- Test rig ----------------------------------------------------------*/
583 static int tcreate(dstr
*v
)
585 mp
*m
= *(mp
**)v
[0].buf
;
586 mp
*mi
= *(mp
**)v
[1].buf
;
587 mp
*r
= *(mp
**)v
[2].buf
;
588 mp
*r2
= *(mp
**)v
[3].buf
;
593 mpmont_create(&mm
, m
);
595 if (mm
.mi
->v
[0] != mi
->v
[0]) {
596 fprintf(stderr
, "\n*** bad mi: found %lu, expected %lu",
597 (unsigned long)mm
.mi
->v
[0], (unsigned long)mi
->v
[0]);
598 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
603 if (!MP_EQ(mm
.r
, r
)) {
604 fputs("\n*** bad r", stderr
);
605 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
606 fputs("\nexpected ", stderr
); mp_writefile(r
, stderr
, 10);
607 fputs("\n found ", stderr
); mp_writefile(mm
.r
, stderr
, 10);
612 if (!MP_EQ(mm
.r2
, r2
)) {
613 fputs("\n*** bad r2", stderr
);
614 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
615 fputs("\nexpected ", stderr
); mp_writefile(r2
, stderr
, 10);
616 fputs("\n found ", stderr
); mp_writefile(mm
.r2
, stderr
, 10);
626 assert(mparena_count(MPARENA_GLOBAL
) == 0);
630 static int tmul(dstr
*v
)
632 mp
*m
= *(mp
**)v
[0].buf
;
633 mp
*a
= *(mp
**)v
[1].buf
;
634 mp
*b
= *(mp
**)v
[2].buf
;
635 mp
*r
= *(mp
**)v
[3].buf
;
639 mpmont_create(&mm
, m
);
642 mp
*qr
= mp_mul(MP_NEW
, a
, b
);
643 mp_div(0, &qr
, qr
, m
);
646 fputs("\n*** classical modmul failed", stderr
);
647 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
648 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
649 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
650 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
651 fputs("\nqr = ", stderr
); mp_writefile(qr
, stderr
, 10);
660 mp
*ar
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
661 mp
*br
= mpmont_mul(&mm
, MP_NEW
, b
, mm
.r2
);
662 mp
*mr
= mpmont_mul(&mm
, MP_NEW
, ar
, br
);
663 mr
= mpmont_reduce(&mm
, mr
, mr
);
665 fputs("\n*** montgomery modmul failed", stderr
);
666 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
667 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
668 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
669 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
670 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
674 MP_DROP(ar
); MP_DROP(br
);
684 assert(mparena_count(MPARENA_GLOBAL
) == 0);
688 static int texp(dstr
*v
)
690 mp
*m
= *(mp
**)v
[0].buf
;
691 mp
*a
= *(mp
**)v
[1].buf
;
692 mp
*b
= *(mp
**)v
[2].buf
;
693 mp
*r
= *(mp
**)v
[3].buf
;
698 mpmont_create(&mm
, m
);
700 mr
= mpmont_exp(&mm
, MP_NEW
, a
, b
);
703 fputs("\n*** montgomery modexp failed", stderr
);
704 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
705 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
706 fputs("\n e = ", stderr
); mp_writefile(b
, stderr
, 10);
707 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
708 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
719 assert(mparena_count(MPARENA_GLOBAL
) == 0);
724 static test_chunk tests
[] = {
725 { "create", tcreate
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
726 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
727 { "exp", texp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
731 int main(int argc
, char *argv
[])
734 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
740 /*----- That's all, folks -------------------------------------------------*/