3 * $Id: mpmont.c,v 1.12 2000/10/08 15:48:35 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.12 2000/10/08 15:48:35 mdw
34 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
36 * Revision 1.11 2000/10/08 12:04:27 mdw
37 * (mpmont_reduce, mpmont_mul): Cope with negative numbers.
39 * Revision 1.10 2000/07/29 17:05:43 mdw
40 * (mpmont_expr): Use sliding window exponentiation, with a drop-through
41 * for small exponents to use a simple left-to-right bitwise routine. This
42 * can reduce modexp times by up to a quarter.
44 * Revision 1.9 2000/06/17 11:45:09 mdw
45 * Major memory management overhaul. Added arena support. Use the secure
46 * arena for secret integers. Replace and improve the MP management macros
47 * (e.g., replace MP_MODIFY by MP_DEST).
49 * Revision 1.8 1999/12/22 15:55:00 mdw
50 * Adjust Karatsuba parameters.
52 * Revision 1.7 1999/12/11 01:51:14 mdw
53 * Use a Karatsuba-based reduction for large moduli.
55 * Revision 1.6 1999/12/10 23:18:39 mdw
56 * Change interface for suggested destinations.
58 * Revision 1.5 1999/11/22 13:58:40 mdw
59 * Add an option to disable Montgomery reduction, so that performance
60 * comparisons can be done.
62 * Revision 1.4 1999/11/21 12:27:06 mdw
63 * Remove a division from the Montgomery setup by calculating
64 * %$R^2 \bmod m$% first and then %$R \bmod m$% by Montgomery reduction of
67 * Revision 1.3 1999/11/21 11:35:10 mdw
68 * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
69 * @mpmont_mul@ for squaring in exponentiation.
71 * Revision 1.2 1999/11/19 13:17:26 mdw
72 * Add extra interface to exponentiation which returns a Montgomerized
75 * Revision 1.1 1999/11/17 18:02:16 mdw
76 * New multiprecision integer arithmetic suite.
80 /*----- Header files ------------------------------------------------------*/
85 /*----- Tweakables --------------------------------------------------------*/
87 /* --- @MPMONT_DISABLE@ --- *
89 * Replace all the clever Montgomery reduction with good old-fashioned long
93 /* #define MPMONT_DISABLE */
95 /*----- Main code ---------------------------------------------------------*/
97 /* --- @mpmont_create@ --- *
99 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
100 * @mp *m@ = modulus to use
104 * Use: Initializes a Montgomery reduction context ready for use.
105 * The argument @m@ must be a positive odd integer.
108 #ifdef MPMONT_DISABLE
110 void mpmont_create(mpmont
*mm
, mp
*m
)
121 void mpmont_create(mpmont
*mm
, mp
*m
)
123 size_t n
= MP_LEN(m
);
124 mp
*r2
= mp_new(2 * n
+ 1, 0);
127 /* --- Validate the arguments --- */
129 assert(((void)"Montgomery modulus must be positive",
130 (m
->f
& MP_NEG
) == 0));
131 assert(((void)"Montgomery modulus must be odd", m
->v
[0] & 1));
133 /* --- Take a copy of the modulus --- */
138 /* --- Determine %$R^2$% --- */
141 MPX_ZERO(r2
->v
, r2
->vl
- 1);
144 /* --- Find the magic value @mi@ --- */
146 mp_build(&r
, r2
->v
+ n
, r2
->vl
);
148 mp_gcd(0, 0, &mm
->mi
, &r
, m
);
149 mm
->mi
= mp_sub(mm
->mi
, &r
, mm
->mi
);
151 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
154 mp_div(0, &mm
->r2
, r2
, m
);
155 mm
->r
= mpmont_reduce(mm
, MP_NEW
, mm
->r2
);
161 /* --- @mpmont_destroy@ --- *
163 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
167 * Use: Disposes of a context when it's no longer of any use to
171 void mpmont_destroy(mpmont
*mm
)
179 /* --- @mpmont_reduce@ --- *
181 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
182 * @mp *d@ = destination
183 * @mp *a@ = source, assumed positive
185 * Returns: Result, %$a R^{-1} \bmod m$%.
188 #ifdef MPMONT_DISABLE
190 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
192 mp_div(0, &d
, a
, mm
->m
);
198 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
202 /* --- Check for serious Karatsuba reduction --- */
204 if (n
> MPK_THRESH
* 3) {
213 mp_build(&al
, a
->v
, vl
);
214 u
= mp_mul(MP_NEW
, &al
, mm
->mi
);
217 u
= mp_mul(u
, u
, mm
->m
);
222 /* --- Otherwise do it the hard way --- */
230 /* --- Initial conditioning of the arguments --- */
236 MP_DEST(d
, 2 * n
+ 1, a
->f
);
238 dv
= d
->v
; dvl
= d
->vl
;
239 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
241 /* --- Let's go to work --- */
245 mpw u
= MPW(*dv
* mi
);
246 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
251 /* --- Wrap everything up --- */
253 memmove(d
->v
, d
->v
+ n
, MPWS(MP_LEN(d
) - n
));
255 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
256 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
258 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
267 /* --- @mpmont_mul@ --- *
269 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
270 * @mp *d@ = destination
271 * @mp *a, *b@ = sources, assumed positive
273 * Returns: Result, %$a b R^{-1} \bmod m$%.
276 #ifdef MPMONT_DISABLE
278 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
281 mp_div(0, &d
, d
, mm
->m
);
287 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
289 if (mm
->n
> MPK_THRESH
* 3) {
291 d
= mpmont_reduce(mm
, d
, d
);
301 /* --- Initial conditioning of the arguments --- */
303 if (MP_LEN(a
) > MP_LEN(b
)) {
304 mp
*t
= a
; a
= b
; b
= t
;
310 MP_DEST(d
, 2 * n
+ 1, a
->f
| b
->f
| MP_UNDEF
);
311 dv
= d
->v
; dvl
= d
->vl
;
313 av
= a
->v
; avl
= a
->vl
;
314 bv
= b
->v
; bvl
= b
->vl
;
315 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
318 /* --- Montgomery multiplication phase --- */
322 while (i
< n
&& av
< avl
) {
324 mpw u
= MPW((*dv
+ x
* y
) * mi
);
325 MPX_UMLAN(dv
, dvl
, bv
, bvl
, x
);
326 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
331 /* --- Simpler Montgomery reduction phase --- */
334 mpw u
= MPW(*dv
* mi
);
335 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
342 memmove(d
->v
, dv
, MPWS(dvl
- dv
));
344 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
345 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
346 if ((a
->f
^ b
->f
) & MP_NEG
)
347 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
349 d
->f
= (a
->f
| b
->f
) & MP_BURN
;
359 /* --- @mpmont_expr@ --- *
361 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
362 * @mp *d@ = fake destination
366 * Returns: Result, %$a^e R \bmod m$%.
370 #define TABSZ (1 << (WINSZ - 1))
372 #define THRESH (((MPW_BITS / WINSZ) << 2) + 1)
374 static mp
*exp_simple(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
378 mp
*x
= MP_COPY(mm
->r
);
379 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
385 while (!MP_RBIT(&sc
))
388 /* --- Do the main body of the work --- */
390 ar
= mpmont_mul(mm
, MP_NEW
, a
, mm
->r2
);
395 y
= mp_sqr(spare
, x
);
396 y
= mpmont_reduce(mm
, y
, y
);
400 { mp
*y
= mpmont_mul(mm
, spare
, x
, ar
); spare
= x
; x
= y
; }
411 /* --- Do a final round of squaring --- */
416 y
= mp_sqr(spare
, x
);
417 y
= mpmont_reduce(mm
, y
, y
);
433 mp
*mpmont_expr(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
437 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
438 mp
*x
= MP_COPY(mm
->r
);
442 /* --- Do we bother? --- */
447 if (MP_LEN(e
) < THRESH
) {
449 return (exp_simple(mm
, d
, a
, e
));
452 /* --- Do the precomputation --- */
454 ar
= mpmont_mul(mm
, MP_NEW
, a
, mm
->r2
);
455 a2
= mp_sqr(MP_NEW
, ar
);
456 a2
= mpmont_reduce(mm
, a2
, a2
);
457 tab
= xmalloc(TABSZ
* sizeof(mp
*));
459 for (i
= 1; i
< TABSZ
; i
++)
460 tab
[i
] = mpmont_mul(mm
, MP_NEW
, tab
[i
- 1], a2
);
464 /* --- Skip top-end zero bits --- *
466 * If the initial step worked, there must be a set bit somewhere, so keep
467 * stepping until I find it.
471 while (!MP_RBIT(&sc
)) {
475 /* --- Now for the main work --- */
481 /* --- The next bit is set, so read a window index --- *
483 * Reset @i@ to zero and increment @sq@. Then, until either I read
484 * @WINSZ@ bits or I run out of bits, scan in a bit: if it's clear, bump
485 * the @z@ counter; if it's set, push a set bit into @i@, shift it over
486 * by @z@ bits, bump @sq@ by @z + 1@ and clear @z@. By the end of this
487 * palaver, @i@ is an index to the precomputed value in @tab@.
494 if (l
>= WINSZ
|| !MP_RSTEP(&sc
))
499 i
= ((i
<< 1) | 1) << z
;
505 /* --- Do the squaring --- *
507 * Remember that @sq@ carries over from the zero-skipping stuff below.
512 y
= mp_sqr(spare
, x
);
513 y
= mpmont_reduce(mm
, y
, y
);
518 /* --- Do the multiply --- */
520 { mp
*y
= mpmont_mul(mm
, spare
, x
, tab
[i
]); spare
= x
; x
= y
; }
522 /* --- Now grind along through the rest of the bits --- */
534 /* --- Do a final round of squaring --- */
539 y
= mp_sqr(spare
, x
);
540 y
= mpmont_reduce(mm
, y
, y
);
547 for (i
= 0; i
< TABSZ
; i
++)
558 /* --- @mpmont_exp@ --- *
560 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
561 * @mp *d@ = fake destination
565 * Returns: Result, %$a^e \bmod m$%.
568 mp
*mpmont_exp(mpmont
*mm
, mp
*d
, mp
*a
, mp
*e
)
570 d
= mpmont_expr(mm
, d
, a
, e
);
571 d
= mpmont_reduce(mm
, d
, d
);
575 /*----- Test rig ----------------------------------------------------------*/
579 static int tcreate(dstr
*v
)
581 mp
*m
= *(mp
**)v
[0].buf
;
582 mp
*mi
= *(mp
**)v
[1].buf
;
583 mp
*r
= *(mp
**)v
[2].buf
;
584 mp
*r2
= *(mp
**)v
[3].buf
;
589 mpmont_create(&mm
, m
);
591 if (mm
.mi
->v
[0] != mi
->v
[0]) {
592 fprintf(stderr
, "\n*** bad mi: found %lu, expected %lu",
593 (unsigned long)mm
.mi
->v
[0], (unsigned long)mi
->v
[0]);
594 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
599 if (!MP_EQ(mm
.r
, r
)) {
600 fputs("\n*** bad r", stderr
);
601 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
602 fputs("\nexpected ", stderr
); mp_writefile(r
, stderr
, 10);
603 fputs("\n found ", stderr
); mp_writefile(mm
.r
, stderr
, 10);
608 if (!MP_EQ(mm
.r2
, r2
)) {
609 fputs("\n*** bad r2", stderr
);
610 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
611 fputs("\nexpected ", stderr
); mp_writefile(r2
, stderr
, 10);
612 fputs("\n found ", stderr
); mp_writefile(mm
.r2
, stderr
, 10);
622 assert(mparena_count(MPARENA_GLOBAL
) == 0);
626 static int tmul(dstr
*v
)
628 mp
*m
= *(mp
**)v
[0].buf
;
629 mp
*a
= *(mp
**)v
[1].buf
;
630 mp
*b
= *(mp
**)v
[2].buf
;
631 mp
*r
= *(mp
**)v
[3].buf
;
635 mpmont_create(&mm
, m
);
638 mp
*qr
= mp_mul(MP_NEW
, a
, b
);
639 mp_div(0, &qr
, qr
, m
);
642 fputs("\n*** classical modmul failed", stderr
);
643 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
644 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
645 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
646 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
647 fputs("\nqr = ", stderr
); mp_writefile(qr
, stderr
, 10);
656 mp
*ar
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
657 mp
*br
= mpmont_mul(&mm
, MP_NEW
, b
, mm
.r2
);
658 mp
*mr
= mpmont_mul(&mm
, MP_NEW
, ar
, br
);
659 mr
= mpmont_reduce(&mm
, mr
, mr
);
661 fputs("\n*** montgomery modmul failed", stderr
);
662 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
663 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
664 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
665 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
666 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
670 MP_DROP(ar
); MP_DROP(br
);
680 assert(mparena_count(MPARENA_GLOBAL
) == 0);
684 static int texp(dstr
*v
)
686 mp
*m
= *(mp
**)v
[0].buf
;
687 mp
*a
= *(mp
**)v
[1].buf
;
688 mp
*b
= *(mp
**)v
[2].buf
;
689 mp
*r
= *(mp
**)v
[3].buf
;
694 mpmont_create(&mm
, m
);
696 mr
= mpmont_exp(&mm
, MP_NEW
, a
, b
);
699 fputs("\n*** montgomery modexp failed", stderr
);
700 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
701 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
702 fputs("\n e = ", stderr
); mp_writefile(b
, stderr
, 10);
703 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
704 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
715 assert(mparena_count(MPARENA_GLOBAL
) == 0);
720 static test_chunk tests
[] = {
721 { "create", tcreate
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
722 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
723 { "exp", texp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
727 int main(int argc
, char *argv
[])
730 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
736 /*----- That's all, folks -------------------------------------------------*/