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 /*----- Header files ------------------------------------------------------*/
35 /*----- Tweakables --------------------------------------------------------*/
37 /* --- @MPMONT_DISABLE@ --- *
39 * Replace all the clever Montgomery reduction with good old-fashioned long
43 /* #define MPMONT_DISABLE */
45 /*----- Reduction and multiplication --------------------------------------*/
47 /* --- @mpmont_create@ --- *
49 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
50 * @mp *m@ = modulus to use
54 * Use: Initializes a Montgomery reduction context ready for use.
55 * The argument @m@ must be a positive odd integer.
60 void mpmont_create(mpmont
*mm
, mp
*m
)
71 void mpmont_create(mpmont
*mm
, mp
*m
)
74 mp
*r2
= mp_new(2 * n
+ 1, 0);
77 /* --- Take a copy of the modulus --- */
79 assert(MP_POSP(m
) && MP_ODDP(m
));
82 /* --- Determine %$R^2$% --- */
85 MPX_ZERO(r2
->v
, r2
->vl
- 1);
88 /* --- Find the magic value @mi@ --- */
90 mp_build(&r
, r2
->v
+ n
, r2
->vl
);
91 mm
->mi
= mp_modinv(MP_NEW
, m
, &r
);
92 mm
->mi
= mp_sub(mm
->mi
, &r
, mm
->mi
);
94 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
97 mp_div(0, &mm
->r2
, r2
, m
);
98 mm
->r
= mpmont_reduce(mm
, MP_NEW
, mm
->r2
);
104 /* --- @mpmont_destroy@ --- *
106 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
110 * Use: Disposes of a context when it's no longer of any use to
114 void mpmont_destroy(mpmont
*mm
)
122 /* --- @mpmont_reduce@ --- *
124 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
125 * @mp *d@ = destination
126 * @mp *a@ = source, assumed positive
128 * Returns: Result, %$a R^{-1} \bmod m$%.
131 #ifdef MPMONT_DISABLE
133 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
135 mp_div(0, &d
, a
, mm
->m
);
141 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
145 /* --- Check for serious Karatsuba reduction --- */
147 if (n
> MPK_THRESH
* 3) {
156 mp_build(&al
, a
->v
, vl
);
157 u
= mp_mul(MP_NEW
, &al
, mm
->mi
);
160 u
= mp_mul(u
, u
, mm
->m
);
165 /* --- Otherwise do it the hard way --- */
173 /* --- Initial conditioning of the arguments --- */
179 MP_DEST(d
, 2 * n
+ 1, a
->f
);
181 dv
= d
->v
; dvl
= d
->vl
;
182 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
184 /* --- Let's go to work --- */
188 mpw u
= MPW(*dv
* mi
);
189 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
194 /* --- Wrap everything up --- */
196 memmove(d
->v
, d
->v
+ n
, MPWS(MP_LEN(d
) - n
));
198 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
199 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
201 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
210 /* --- @mpmont_mul@ --- *
212 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
213 * @mp *d@ = destination
214 * @mp *a, *b@ = sources, assumed positive
216 * Returns: Result, %$a b R^{-1} \bmod m$%.
219 #ifdef MPMONT_DISABLE
221 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
224 mp_div(0, &d
, d
, mm
->m
);
230 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
232 if (mm
->n
> MPK_THRESH
* 3) {
234 d
= mpmont_reduce(mm
, d
, d
);
244 /* --- Initial conditioning of the arguments --- */
246 if (MP_LEN(a
) > MP_LEN(b
)) {
247 mp
*t
= a
; a
= b
; b
= t
;
253 MP_DEST(d
, 2 * n
+ 1, a
->f
| b
->f
| MP_UNDEF
);
254 dv
= d
->v
; dvl
= d
->vl
;
256 av
= a
->v
; avl
= a
->vl
;
257 bv
= b
->v
; bvl
= b
->vl
;
258 mv
= mm
->m
->v
; mvl
= mm
->m
->vl
;
261 /* --- Montgomery multiplication phase --- */
265 while (i
< n
&& av
< avl
) {
267 mpw u
= MPW((*dv
+ x
* y
) * mi
);
268 MPX_UMLAN(dv
, dvl
, bv
, bvl
, x
);
269 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
274 /* --- Simpler Montgomery reduction phase --- */
277 mpw u
= MPW(*dv
* mi
);
278 MPX_UMLAN(dv
, dvl
, mv
, mvl
, u
);
285 memmove(d
->v
, dv
, MPWS(dvl
- dv
));
287 if (MPX_UCMP(d
->v
, d
->vl
, >=, mm
->m
->v
, mm
->m
->vl
))
288 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
);
289 if ((a
->f
^ b
->f
) & MP_NEG
)
290 mpx_usub(d
->v
, d
->vl
, mm
->m
->v
, mm
->m
->vl
, d
->v
, d
->vl
);
292 d
->f
= (a
->f
| b
->f
) & MP_BURN
;
302 /*----- Test rig ----------------------------------------------------------*/
306 static int tcreate(dstr
*v
)
308 mp
*m
= *(mp
**)v
[0].buf
;
309 mp
*mi
= *(mp
**)v
[1].buf
;
310 mp
*r
= *(mp
**)v
[2].buf
;
311 mp
*r2
= *(mp
**)v
[3].buf
;
316 mpmont_create(&mm
, m
);
318 if (mm
.mi
->v
[0] != mi
->v
[0]) {
319 fprintf(stderr
, "\n*** bad mi: found %lu, expected %lu",
320 (unsigned long)mm
.mi
->v
[0], (unsigned long)mi
->v
[0]);
321 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
326 if (!MP_EQ(mm
.r
, r
)) {
327 fputs("\n*** bad r", stderr
);
328 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
329 fputs("\nexpected ", stderr
); mp_writefile(r
, stderr
, 10);
330 fputs("\n found ", stderr
); mp_writefile(mm
.r
, stderr
, 10);
335 if (!MP_EQ(mm
.r2
, r2
)) {
336 fputs("\n*** bad r2", stderr
);
337 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
338 fputs("\nexpected ", stderr
); mp_writefile(r2
, stderr
, 10);
339 fputs("\n found ", stderr
); mp_writefile(mm
.r2
, stderr
, 10);
349 assert(mparena_count(MPARENA_GLOBAL
) == 0);
353 static int tmul(dstr
*v
)
355 mp
*m
= *(mp
**)v
[0].buf
;
356 mp
*a
= *(mp
**)v
[1].buf
;
357 mp
*b
= *(mp
**)v
[2].buf
;
358 mp
*r
= *(mp
**)v
[3].buf
;
362 mpmont_create(&mm
, m
);
365 mp
*qr
= mp_mul(MP_NEW
, a
, b
);
366 mp_div(0, &qr
, qr
, m
);
369 fputs("\n*** classical modmul failed", stderr
);
370 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
371 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
372 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
373 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
374 fputs("\nqr = ", stderr
); mp_writefile(qr
, stderr
, 10);
383 mp
*ar
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
384 mp
*br
= mpmont_mul(&mm
, MP_NEW
, b
, mm
.r2
);
385 mp
*mr
= mpmont_mul(&mm
, MP_NEW
, ar
, br
);
386 mr
= mpmont_reduce(&mm
, mr
, mr
);
388 fputs("\n*** montgomery modmul failed", stderr
);
389 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
390 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
391 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
392 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
393 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
397 MP_DROP(ar
); MP_DROP(br
);
407 assert(mparena_count(MPARENA_GLOBAL
) == 0);
411 static test_chunk tests
[] = {
412 { "create", tcreate
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
413 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
417 int main(int argc
, char *argv
[])
420 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpmont");
426 /*----- That's all, folks -------------------------------------------------*/