3 * $Id: mpmont.c,v 1.11 2000/10/08 12:04:27 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.11 2000/10/08 12:04:27 mdw
34 * (mpmont_reduce, mpmont_mul): Cope with negative numbers.
36 * Revision 1.10 2000/07/29 17:05:43 mdw
37 * (mpmont_expr): Use sliding window exponentiation, with a drop-through
38 * for small exponents to use a simple left-to-right bitwise routine. This
39 * can reduce modexp times by up to a quarter.
41 * Revision 1.9 2000/06/17 11:45:09 mdw
42 * Major memory management overhaul. Added arena support. Use the secure
43 * arena for secret integers. Replace and improve the MP management macros
44 * (e.g., replace MP_MODIFY by MP_DEST).
46 * Revision 1.8 1999/12/22 15:55:00 mdw
47 * Adjust Karatsuba parameters.
49 * Revision 1.7 1999/12/11 01:51:14 mdw
50 * Use a Karatsuba-based reduction for large moduli.
52 * Revision 1.6 1999/12/10 23:18:39 mdw
53 * Change interface for suggested destinations.
55 * Revision 1.5 1999/11/22 13:58:40 mdw
56 * Add an option to disable Montgomery reduction, so that performance
57 * comparisons can be done.
59 * Revision 1.4 1999/11/21 12:27:06 mdw
60 * Remove a division from the Montgomery setup by calculating
61 * %$R^2 \bmod m$% first and then %$R \bmod m$% by Montgomery reduction of
64 * Revision 1.3 1999/11/21 11:35:10 mdw
65 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
66 * @mpmont_mul@ for squaring in exponentiation.
68 * Revision 1.2 1999/11/19 13:17:26 mdw
69 * Add extra interface to exponentiation which returns a Montgomerized
72 * Revision 1.1 1999/11/17 18:02:16 mdw
73 * New multiprecision integer arithmetic suite.
77 /*----- Header files ------------------------------------------------------*/
82 /*----- Tweakables --------------------------------------------------------*/
84 /* --- @MPMONT_DISABLE@ --- *
86 * Replace all the clever Montgomery reduction with good old-fashioned long
90 /* #define MPMONT_DISABLE */
92 /*----- Main code ---------------------------------------------------------*/
94 /* --- @mpmont_create@ --- *
96 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
97 * @mp *m@ = modulus to use
101 * Use: Initializes a Montgomery reduction context ready for use.
102 * The argument @m@ must be a positive odd integer.
105 #ifdef MPMONT_DISABLE
107 void mpmont_create(mpmont
*mm
, mp
*m
)
118 void mpmont_create(mpmont
*mm
, mp
*m
)
120 size_t n
= MP_LEN(m
);
121 mp
*r2
= mp_new(2 * n
+ 1, 0);
124 /* --- Validate the arguments --- */
126 assert(((void)"Montgomery modulus must be positive",
127 (m
->f
& MP_NEG
) == 0));
128 assert(((void)"Montgomery modulus must be odd", m
->v
[0] & 1));
130 /* --- Take a copy of the modulus --- */
135 /* --- Determine %$R^2$% --- */
138 MPX_ZERO(r2
->v
, r2
->vl
- 1);
141 /* --- Find the magic value @mi@ --- */
143 mp_build(&r
, r2
->v
+ n
, r2
->vl
);
145 mp_gcd(0, 0, &mm
->mi
, &r
, m
);
146 mm
->mi
= mp_sub(mm
->mi
, &r
, mm
->mi
);
148 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
151 mp_div(0, &mm
->r2
, r2
, m
);
152 mm
->r
= mpmont_reduce(mm
, MP_NEW
, mm
->r2
);
158 /* --- @mpmont_destroy@ --- *
160 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
164 * Use: Disposes of a context when it's no longer of any use to
168 void mpmont_destroy(mpmont
*mm
)
176 /* --- @mpmont_reduce@ --- *
178 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
179 * @mp *d@ = destination
180 * @mp *a@ = source, assumed positive
182 * Returns: Result, %$a R^{-1} \bmod m$%.
185 #ifdef MPMONT_DISABLE
187 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
189 mp_div(0, &d
, a
, mm
->m
);
195 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
199 /* --- Check for serious Karatsuba reduction --- */
201 if (n
> KARATSUBA_CUTOFF
* 3) {
210 mp_build(&al
, a
->v
, vl
);
211 u
= mp_mul(MP_NEW
, &al
, mm
->mi
);
214 u
= mp_mul(u
, u
, mm
->m
);
219 /* --- Otherwise do it the hard way --- */
227 /* --- Initial conditioning of the arguments --- */
233 MP_DEST(d
, 2 * n
+ 1, a
->f
);
235 dv
= d
->v
; dvl
= d
->vl
;
236 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
238 /* --- Let's go to work --- */
242 mpw u
= MPW(*dv
* mi
);
243 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
248 /* --- Wrap everything up --- */
250 memmove(d
->v
, d
->v
+ n
, MPWS(MP_LEN(d
) - n
));
252 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
253 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
255 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
264 /* --- @mpmont_mul@ --- *
266 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
267 * @mp *d@ = destination
268 * @mp *a, *b@ = sources, assumed positive
270 * Returns: Result, %$a b R^{-1} \bmod m$%.
273 #ifdef MPMONT_DISABLE
275 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
278 mp_div(0, &d
, d
, mm
->m
);
284 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
286 if (mm
->n
> KARATSUBA_CUTOFF
* 3) {
288 d
= mpmont_reduce(mm
, d
, d
);
298 /* --- Initial conditioning of the arguments --- */
300 if (MP_LEN(a
) > MP_LEN(b
)) {
301 mp
*t
= a
; a
= b
; b
= t
;
307 MP_DEST(d
, 2 * n
+ 1, a
->f
| b
->f
| MP_UNDEF
);
308 dv
= d
->v
; dvl
= d
->vl
;
310 av
= a
->v
; avl
= a
->vl
;
311 bv
= b
->v
; bvl
= b
->vl
;
312 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
315 /* --- Montgomery multiplication phase --- */
319 while (i
< n
&& av
< avl
) {
321 mpw u
= MPW((*dv
+ x
* y
) * mi
);
322 MPX_UMLAN(dv
, dvl
, bv
, bvl
, x
);
323 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
328 /* --- Simpler Montgomery reduction phase --- */
331 mpw u
= MPW(*dv
* mi
);
332 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
339 memmove(d
->v
, dv
, MPWS(dvl
- dv
));
341 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
342 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
343 if ((a
->f
^ b
->f
) & MP_NEG
)
344 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
346 d
->f
= (a
->f
| b
->f
) & MP_BURN
;
356 /* --- @mpmont_expr@ --- *
358 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
359 * @mp *d@ = fake destination
363 * Returns: Result, %$a^e R \bmod m$%.
367 #define TABSZ (1 << (WINSZ - 1))
369 #define THRESH (((MPW_BITS / WINSZ) << 2) + 1)
371 static mp
*exp_simple(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
375 mp
*x
= MP_COPY(mm
->r
);
376 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
382 while (!MP_RBIT(&sc
))
385 /* --- Do the main body of the work --- */
387 ar
= mpmont_mul(mm
, MP_NEW
, a
, mm
->r2
);
392 y
= mp_sqr(spare
, x
);
393 y
= mpmont_reduce(mm
, y
, y
);
397 { mp
*y
= mpmont_mul(mm
, spare
, x
, ar
); spare
= x
; x
= y
; }
408 /* --- Do a final round of squaring --- */
413 y
= mp_sqr(spare
, x
);
414 y
= mpmont_reduce(mm
, y
, y
);
430 mp
*mpmont_expr(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
434 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
435 mp
*x
= MP_COPY(mm
->r
);
439 /* --- Do we bother? --- */
444 if (MP_LEN(e
) < THRESH
) {
446 return (exp_simple(mm
, d
, a
, e
));
449 /* --- Do the precomputation --- */
451 ar
= mpmont_mul(mm
, MP_NEW
, a
, mm
->r2
);
452 a2
= mp_sqr(MP_NEW
, ar
);
453 a2
= mpmont_reduce(mm
, a2
, a2
);
454 tab
= xmalloc(TABSZ
* sizeof(mp
*));
456 for (i
= 1; i
< TABSZ
; i
++)
457 tab
[i
] = mpmont_mul(mm
, MP_NEW
, tab
[i
- 1], a2
);
461 /* --- Skip top-end zero bits --- *
463 * If the initial step worked, there must be a set bit somewhere, so keep
464 * stepping until I find it.
468 while (!MP_RBIT(&sc
)) {
472 /* --- Now for the main work --- */
478 /* --- The next bit is set, so read a window index --- *
480 * Reset @i@ to zero and increment @sq@. Then, until either I read
481 * @WINSZ@ bits or I run out of bits, scan in a bit: if it's clear, bump
482 * the @z@ counter; if it's set, push a set bit into @i@, shift it over
483 * by @z@ bits, bump @sq@ by @z + 1@ and clear @z@. By the end of this
484 * palaver, @i@ is an index to the precomputed value in @tab@.
491 if (l
>= WINSZ
|| !MP_RSTEP(&sc
))
496 i
= ((i
<< 1) | 1) << z
;
502 /* --- Do the squaring --- *
504 * Remember that @sq@ carries over from the zero-skipping stuff below.
509 y
= mp_sqr(spare
, x
);
510 y
= mpmont_reduce(mm
, y
, y
);
515 /* --- Do the multiply --- */
517 { mp
*y
= mpmont_mul(mm
, spare
, x
, tab
[i
]); spare
= x
; x
= y
; }
519 /* --- Now grind along through the rest of the bits --- */
531 /* --- Do a final round of squaring --- */
536 y
= mp_sqr(spare
, x
);
537 y
= mpmont_reduce(mm
, y
, y
);
544 for (i
= 0; i
< TABSZ
; i
++)
555 /* --- @mpmont_exp@ --- *
557 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
558 * @mp *d@ = fake destination
562 * Returns: Result, %$a^e \bmod m$%.
565 mp
*mpmont_exp(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
567 d
= mpmont_expr(mm
, d
, a
, e
);
568 d
= mpmont_reduce(mm
, d
, d
);
572 /*----- Test rig ----------------------------------------------------------*/
576 static int tcreate(dstr
*v
)
578 mp
*m
= *(mp
**)v
[0].buf
;
579 mp
*mi
= *(mp
**)v
[1].buf
;
580 mp
*r
= *(mp
**)v
[2].buf
;
581 mp
*r2
= *(mp
**)v
[3].buf
;
586 mpmont_create(&mm
, m
);
588 if (mm
.mi
->v
[0] != mi
->v
[0]) {
589 fprintf(stderr
, "\n*** bad mi: found %lu, expected %lu",
590 (unsigned long)mm
.mi
->v
[0], (unsigned long)mi
->v
[0]);
591 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
596 if (!MP_EQ(mm
.r
, r
)) {
597 fputs("\n*** bad r", stderr
);
598 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
599 fputs("\nexpected ", stderr
); mp_writefile(r
, stderr
, 10);
600 fputs("\n found ", stderr
); mp_writefile(mm
.r
, stderr
, 10);
605 if (!MP_EQ(mm
.r2
, r2
)) {
606 fputs("\n*** bad r2", stderr
);
607 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
608 fputs("\nexpected ", stderr
); mp_writefile(r2
, stderr
, 10);
609 fputs("\n found ", stderr
); mp_writefile(mm
.r2
, stderr
, 10);
619 assert(mparena_count(MPARENA_GLOBAL
) == 0);
623 static int tmul(dstr
*v
)
625 mp
*m
= *(mp
**)v
[0].buf
;
626 mp
*a
= *(mp
**)v
[1].buf
;
627 mp
*b
= *(mp
**)v
[2].buf
;
628 mp
*r
= *(mp
**)v
[3].buf
;
632 mpmont_create(&mm
, m
);
635 mp
*qr
= mp_mul(MP_NEW
, a
, b
);
636 mp_div(0, &qr
, qr
, m
);
639 fputs("\n*** classical modmul failed", stderr
);
640 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
641 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
642 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
643 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
644 fputs("\nqr = ", stderr
); mp_writefile(qr
, stderr
, 10);
653 mp
*ar
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
654 mp
*br
= mpmont_mul(&mm
, MP_NEW
, b
, mm
.r2
);
655 mp
*mr
= mpmont_mul(&mm
, MP_NEW
, ar
, br
);
656 mr
= mpmont_reduce(&mm
, mr
, mr
);
658 fputs("\n*** montgomery modmul failed", stderr
);
659 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
660 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
661 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
662 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
663 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
667 MP_DROP(ar
); MP_DROP(br
);
677 assert(mparena_count(MPARENA_GLOBAL
) == 0);
681 static int texp(dstr
*v
)
683 mp
*m
= *(mp
**)v
[0].buf
;
684 mp
*a
= *(mp
**)v
[1].buf
;
685 mp
*b
= *(mp
**)v
[2].buf
;
686 mp
*r
= *(mp
**)v
[3].buf
;
691 mpmont_create(&mm
, m
);
693 mr
= mpmont_exp(&mm
, MP_NEW
, a
, b
);
696 fputs("\n*** montgomery modexp failed", stderr
);
697 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
698 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
699 fputs("\n e = ", stderr
); mp_writefile(b
, stderr
, 10);
700 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
701 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
712 assert(mparena_count(MPARENA_GLOBAL
) == 0);
717 static test_chunk tests
[] = {
718 { "create", tcreate
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
719 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
720 { "exp", texp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
724 int main(int argc
, char *argv
[])
727 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
733 /*----- That's all, folks -------------------------------------------------*/