3 * $Id: mpmont.c,v 1.10 2000/07/29 17:05:43 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.10 2000/07/29 17:05:43 mdw
34 * (mpmont_expr): Use sliding window exponentiation, with a drop-through
35 * for small exponents to use a simple left-to-right bitwise routine. This
36 * can reduce modexp times by up to a quarter.
38 * Revision 1.9 2000/06/17 11:45:09 mdw
39 * Major memory management overhaul. Added arena support. Use the secure
40 * arena for secret integers. Replace and improve the MP management macros
41 * (e.g., replace MP_MODIFY by MP_DEST).
43 * Revision 1.8 1999/12/22 15:55:00 mdw
44 * Adjust Karatsuba parameters.
46 * Revision 1.7 1999/12/11 01:51:14 mdw
47 * Use a Karatsuba-based reduction for large moduli.
49 * Revision 1.6 1999/12/10 23:18:39 mdw
50 * Change interface for suggested destinations.
52 * Revision 1.5 1999/11/22 13:58:40 mdw
53 * Add an option to disable Montgomery reduction, so that performance
54 * comparisons can be done.
56 * Revision 1.4 1999/11/21 12:27:06 mdw
57 * Remove a division from the Montgomery setup by calculating
58 * %$R^2 \bmod m$% first and then %$R \bmod m$% by Montgomery reduction of
61 * Revision 1.3 1999/11/21 11:35:10 mdw
62 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
63 * @mpmont_mul@ for squaring in exponentiation.
65 * Revision 1.2 1999/11/19 13:17:26 mdw
66 * Add extra interface to exponentiation which returns a Montgomerized
69 * Revision 1.1 1999/11/17 18:02:16 mdw
70 * New multiprecision integer arithmetic suite.
74 /*----- Header files ------------------------------------------------------*/
79 /*----- Tweakables --------------------------------------------------------*/
81 /* --- @MPMONT_DISABLE@ --- *
83 * Replace all the clever Montgomery reduction with good old-fashioned long
87 /* #define MPMONT_DISABLE */
89 /*----- Main code ---------------------------------------------------------*/
91 /* --- @mpmont_create@ --- *
93 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
94 * @mp *m@ = modulus to use
98 * Use: Initializes a Montgomery reduction context ready for use.
99 * The argument @m@ must be a positive odd integer.
102 #ifdef MPMONT_DISABLE
104 void mpmont_create(mpmont
*mm
, mp
*m
)
115 void mpmont_create(mpmont
*mm
, mp
*m
)
117 size_t n
= MP_LEN(m
);
118 mp
*r2
= mp_new(2 * n
+ 1, 0);
121 /* --- Validate the arguments --- */
123 assert(((void)"Montgomery modulus must be positive",
124 (m
->f
& MP_NEG
) == 0));
125 assert(((void)"Montgomery modulus must be odd", m
->v
[0] & 1));
127 /* --- Take a copy of the modulus --- */
132 /* --- Determine %$R^2$% --- */
135 MPX_ZERO(r2
->v
, r2
->vl
- 1);
138 /* --- Find the magic value @mi@ --- */
140 mp_build(&r
, r2
->v
+ n
, r2
->vl
);
142 mp_gcd(0, 0, &mm
->mi
, &r
, m
);
143 mm
->mi
= mp_sub(mm
->mi
, &r
, mm
->mi
);
145 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
148 mp_div(0, &mm
->r2
, r2
, m
);
149 mm
->r
= mpmont_reduce(mm
, MP_NEW
, mm
->r2
);
155 /* --- @mpmont_destroy@ --- *
157 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
161 * Use: Disposes of a context when it's no longer of any use to
165 void mpmont_destroy(mpmont
*mm
)
173 /* --- @mpmont_reduce@ --- *
175 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
176 * @mp *d@ = destination
177 * @mp *a@ = source, assumed positive
179 * Returns: Result, %$a R^{-1} \bmod m$%.
182 #ifdef MPMONT_DISABLE
184 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
186 mp_div(0, &d
, a
, mm
->m
);
192 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
196 /* --- Check for serious Karatsuba reduction --- */
198 if (n
> KARATSUBA_CUTOFF
* 3) {
207 mp_build(&al
, a
->v
, vl
);
208 u
= mp_mul(MP_NEW
, &al
, mm
->mi
);
211 u
= mp_mul(u
, u
, mm
->m
);
216 /* --- Otherwise do it the hard way --- */
224 /* --- Initial conditioning of the arguments --- */
230 MP_DEST(d
, 2 * n
+ 1, a
->f
);
232 dv
= d
->v
; dvl
= d
->vl
;
233 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
235 /* --- Let's go to work --- */
239 mpw u
= MPW(*dv
* mi
);
240 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
245 /* --- Wrap everything up --- */
247 memmove(d
->v
, d
->v
+ n
, MPWS(MP_LEN(d
) - n
));
249 if (MP_CMP(d
, >=, mm
->m
))
250 d
= mp_sub(d
, d
, mm
->m
);
257 /* --- @mpmont_mul@ --- *
259 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
260 * @mp *d@ = destination
261 * @mp *a, *b@ = sources, assumed positive
263 * Returns: Result, %$a b R^{-1} \bmod m$%.
266 #ifdef MPMONT_DISABLE
268 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
271 mp_div(0, &d
, d
, mm
->m
);
277 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
279 if (mm
->n
> KARATSUBA_CUTOFF
* 3) {
281 d
= mpmont_reduce(mm
, d
, d
);
291 /* --- Initial conditioning of the arguments --- */
293 if (MP_LEN(a
) > MP_LEN(b
)) {
294 mp
*t
= a
; a
= b
; b
= t
;
300 MP_DEST(d
, 2 * n
+ 1, a
->f
| b
->f
| MP_UNDEF
);
301 dv
= d
->v
; dvl
= d
->vl
;
303 av
= a
->v
; avl
= a
->vl
;
304 bv
= b
->v
; bvl
= b
->vl
;
305 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
308 /* --- Montgomery multiplication phase --- */
312 while (i
< n
&& av
< avl
) {
314 mpw u
= MPW((*dv
+ x
* y
) * mi
);
315 MPX_UMLAN(dv
, dvl
, bv
, bvl
, x
);
316 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
321 /* --- Simpler Montgomery reduction phase --- */
324 mpw u
= MPW(*dv
* mi
);
325 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
332 memmove(d
->v
, dv
, MPWS(dvl
- dv
));
335 d
->f
= (a
->f
| b
->f
) & MP_BURN
;
336 if (MP_CMP(d
, >=, mm
->m
))
337 d
= mp_sub(d
, d
, mm
->m
);
347 /* --- @mpmont_expr@ --- *
349 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
350 * @mp *d@ = fake destination
354 * Returns: Result, %$a^e R \bmod m$%.
358 #define TABSZ (1 << (WINSZ - 1))
360 #define THRESH (((MPW_BITS / WINSZ) << 2) + 1)
362 static mp
*exp_simple(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
366 mp
*x
= MP_COPY(mm
->r
);
367 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
373 while (!MP_RBIT(&sc
))
376 /* --- Do the main body of the work --- */
378 ar
= mpmont_mul(mm
, MP_NEW
, a
, mm
->r2
);
383 y
= mp_sqr(spare
, x
);
384 y
= mpmont_reduce(mm
, y
, y
);
388 { mp
*y
= mpmont_mul(mm
, spare
, x
, ar
); spare
= x
; x
= y
; }
399 /* --- Do a final round of squaring --- */
404 y
= mp_sqr(spare
, x
);
405 y
= mpmont_reduce(mm
, y
, y
);
421 mp
*mpmont_expr(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
425 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
426 mp
*x
= MP_COPY(mm
->r
);
430 /* --- Do we bother? --- */
435 if (MP_LEN(e
) < THRESH
) {
437 return (exp_simple(mm
, d
, a
, e
));
440 /* --- Do the precomputation --- */
442 ar
= mpmont_mul(mm
, MP_NEW
, a
, mm
->r2
);
443 a2
= mp_sqr(MP_NEW
, ar
);
444 a2
= mpmont_reduce(mm
, a2
, a2
);
445 tab
= xmalloc(TABSZ
* sizeof(mp
*));
447 for (i
= 1; i
< TABSZ
; i
++)
448 tab
[i
] = mpmont_mul(mm
, MP_NEW
, tab
[i
- 1], a2
);
452 /* --- Skip top-end zero bits --- *
454 * If the initial step worked, there must be a set bit somewhere, so keep
455 * stepping until I find it.
459 while (!MP_RBIT(&sc
)) {
463 /* --- Now for the main work --- */
469 /* --- The next bit is set, so read a window index --- *
471 * Reset @i@ to zero and increment @sq@. Then, until either I read
472 * @WINSZ@ bits or I run out of bits, scan in a bit: if it's clear, bump
473 * the @z@ counter; if it's set, push a set bit into @i@, shift it over
474 * by @z@ bits, bump @sq@ by @z + 1@ and clear @z@. By the end of this
475 * palaver, @i@ is an index to the precomputed value in @tab@.
482 if (l
>= WINSZ
|| !MP_RSTEP(&sc
))
487 i
= ((i
<< 1) | 1) << z
;
493 /* --- Do the squaring --- *
495 * Remember that @sq@ carries over from the zero-skipping stuff below.
500 y
= mp_sqr(spare
, x
);
501 y
= mpmont_reduce(mm
, y
, y
);
506 /* --- Do the multiply --- */
508 { mp
*y
= mpmont_mul(mm
, spare
, x
, tab
[i
]); spare
= x
; x
= y
; }
510 /* --- Now grind along through the rest of the bits --- */
522 /* --- Do a final round of squaring --- */
527 y
= mp_sqr(spare
, x
);
528 y
= mpmont_reduce(mm
, y
, y
);
535 for (i
= 0; i
< TABSZ
; i
++)
546 /* --- @mpmont_exp@ --- *
548 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
549 * @mp *d@ = fake destination
553 * Returns: Result, %$a^e \bmod m$%.
556 mp
*mpmont_exp(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
558 d
= mpmont_expr(mm
, d
, a
, e
);
559 d
= mpmont_reduce(mm
, d
, d
);
563 /*----- Test rig ----------------------------------------------------------*/
567 static int tcreate(dstr
*v
)
569 mp
*m
= *(mp
**)v
[0].buf
;
570 mp
*mi
= *(mp
**)v
[1].buf
;
571 mp
*r
= *(mp
**)v
[2].buf
;
572 mp
*r2
= *(mp
**)v
[3].buf
;
577 mpmont_create(&mm
, m
);
579 if (mm
.mi
->v
[0] != mi
->v
[0]) {
580 fprintf(stderr
, "\n*** bad mi: found %lu, expected %lu",
581 (unsigned long)mm
.mi
->v
[0], (unsigned long)mi
->v
[0]);
582 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
587 if (MP_CMP(mm
.r
, !=, r
)) {
588 fputs("\n*** bad r", stderr
);
589 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
590 fputs("\nexpected ", stderr
); mp_writefile(r
, stderr
, 10);
591 fputs("\n found ", stderr
); mp_writefile(mm
.r
, stderr
, 10);
596 if (MP_CMP(mm
.r2
, !=, r2
)) {
597 fputs("\n*** bad r2", stderr
);
598 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
599 fputs("\nexpected ", stderr
); mp_writefile(r2
, stderr
, 10);
600 fputs("\n found ", stderr
); mp_writefile(mm
.r2
, stderr
, 10);
610 assert(mparena_count(MPARENA_GLOBAL
) == 0);
614 static int tmul(dstr
*v
)
616 mp
*m
= *(mp
**)v
[0].buf
;
617 mp
*a
= *(mp
**)v
[1].buf
;
618 mp
*b
= *(mp
**)v
[2].buf
;
619 mp
*r
= *(mp
**)v
[3].buf
;
623 mpmont_create(&mm
, m
);
626 mp
*qr
= mp_mul(MP_NEW
, a
, b
);
627 mp_div(0, &qr
, qr
, m
);
629 if (MP_CMP(qr
, !=, r
)) {
630 fputs("\n*** classical modmul failed", stderr
);
631 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
632 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
633 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
634 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
635 fputs("\nqr = ", stderr
); mp_writefile(qr
, stderr
, 10);
644 mp
*ar
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
645 mp
*br
= mpmont_mul(&mm
, MP_NEW
, b
, mm
.r2
);
646 mp
*mr
= mpmont_mul(&mm
, MP_NEW
, ar
, br
);
647 mr
= mpmont_reduce(&mm
, mr
, mr
);
648 if (MP_CMP(mr
, !=, r
)) {
649 fputs("\n*** montgomery modmul failed", stderr
);
650 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
651 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
652 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
653 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
654 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
658 MP_DROP(ar
); MP_DROP(br
);
668 assert(mparena_count(MPARENA_GLOBAL
) == 0);
672 static int texp(dstr
*v
)
674 mp
*m
= *(mp
**)v
[0].buf
;
675 mp
*a
= *(mp
**)v
[1].buf
;
676 mp
*b
= *(mp
**)v
[2].buf
;
677 mp
*r
= *(mp
**)v
[3].buf
;
682 mpmont_create(&mm
, m
);
684 mr
= mpmont_exp(&mm
, MP_NEW
, a
, b
);
686 if (MP_CMP(mr
, !=, r
)) {
687 fputs("\n*** montgomery modexp failed", stderr
);
688 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
689 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
690 fputs("\n e = ", stderr
); mp_writefile(b
, stderr
, 10);
691 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
692 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
703 assert(mparena_count(MPARENA_GLOBAL
) == 0);
708 static test_chunk tests
[] = {
709 { "create", tcreate
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
710 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
711 { "exp", texp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
715 int main(int argc
, char *argv
[])
718 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
724 /*----- That's all, folks -------------------------------------------------*/