5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 /*----- Tweakables --------------------------------------------------------*/
35 /* --- @MPMONT_DISABLE@ --- *
37 * Replace all the clever Montgomery reduction with good old-fashioned long
41 /* #define MPMONT_DISABLE */
43 /*----- Low-level implementation ------------------------------------------*/
45 #ifndef MPMONT_DISABLE
47 /* --- @redccore@ --- *
49 * Arguments: @mpw *dv, *dvl@ = base and limit of source/destination
50 * @const mpw *mv@ = base of modulus %$m$%
51 * @size_t n@ = length of modulus
52 * @const mpw *mi@ = base of REDC coefficient %$m'$%
56 * Use: Let %$a$% be the input operand. Store in %$d$% the value
57 * %$a + (m' a \bmod R) m$%. The destination has space for at
58 * least %$2 n + 1$% words of result.
61 static void redccore(mpw
*dv
, mpw
*dvl
, const mpw
*mv
,
62 size_t n
, const mpw
*mi
)
67 for (i
= 0; i
< n
; i
++) {
68 MPX_UMLAN(dv
, dvl
, mv
, mv
+ n
, MPW(*dv
*mi0
));
73 /* --- @redccore@ --- *
75 * Arguments: @mpw *dv, *dvl@ = base and limit of source/destination
76 * @const mpw *av, *avl@ = base and limit of first multiplicand
77 * @const mpw *bv, *bvl@ = base and limit of second multiplicand
78 * @const mpw *mv@ = base of modulus %$m$%
79 * @size_t n@ = length of modulus
80 * @const mpw *mi@ = base of REDC coefficient %$m'$%
84 * Use: Let %$a$% and %$b$% be the multiplicands. Let %$w = a b$%.
85 * Store in %$d$% the value %$a b + (m' a b \bmod R) m$%.
88 static void mulcore(mpw
*dv
, mpw
*dvl
,
89 const mpw
*av
, const mpw
*avl
,
90 const mpw
*bv
, const mpw
*bvl
,
91 const mpw
*mv
, size_t n
, const mpw
*mi
)
93 mpw ai
, b0
, y
, mi0
= *mi
;
95 const mpw
*mvl
= mv
+ n
;
98 /* --- Initial setup --- */
101 if (avl
- av
> bvl
- bv
) {
102 tv
= av
; av
= bv
; bv
= tv
;
103 tvl
= avl
; avl
= bvl
; bvl
= tvl
;
107 /* --- Multiply, until we run out of multiplicand --- */
109 while (i
< n
&& av
< avl
) {
111 y
= MPW((*dv
+ ai
*b0
)*mi0
);
112 MPX_UMLAN(dv
, dvl
, bv
, bvl
, ai
);
113 MPX_UMLAN(dv
, dvl
, mv
, mvl
, y
);
117 /* --- Continue reducing until we run out of modulus --- */
121 MPX_UMLAN(dv
, dvl
, mv
, mvl
, y
);
126 /* --- @finish@ --- *
128 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
129 * *mp *d@ = pointer to mostly-reduced operand
133 * Use: Applies the finishing touches to Montgomery reduction. The
134 * operand @d@ is a multiple of %$R%$ at this point, so it needs
135 * to be shifted down; the result might need a further
136 * subtraction to get it into the right interval; and we may
137 * need to do an additional subtraction if %$d$% is negative.
140 static void finish(mpmont
*mm
, mp
*d
)
142 mpw
*dv
= d
->v
, *dvl
= d
->vl
;
145 memmove(dv
, dv
+ n
, MPWS(dvl
- (dv
+ n
)));
148 if (MPX_UCMP(dv
, dvl
, >=, mm
->m
->v
, mm
->m
->vl
))
149 mpx_usub(dv
, dvl
, dv
, dvl
, mm
->m
->v
, mm
->m
->vl
);
152 mpx_usub(dv
, dvl
, mm
->m
->v
, mm
->m
->vl
, dv
, dvl
);
162 /*----- Reduction and multiplication --------------------------------------*/
164 /* --- @mpmont_create@ --- *
166 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
167 * @mp *m@ = modulus to use
169 * Returns: Zero on success, nonzero on error.
171 * Use: Initializes a Montgomery reduction context ready for use.
172 * The argument @m@ must be a positive odd integer.
175 #ifdef MPMONT_DISABLE
177 int mpmont_create(mpmont
*mm
, mp
*m
)
189 int mpmont_create(mpmont
*mm
, mp
*m
)
191 size_t n
= MP_LEN(m
);
192 mp
*r2
= mp_new(2 * n
+ 1, 0);
195 /* --- Take a copy of the modulus --- */
197 if (!MP_POSP(m
) || !MP_ODDP(m
))
201 /* --- Determine %$R^2$% --- */
204 MPX_ZERO(r2
->v
, r2
->vl
- 1);
207 /* --- Find the magic value @mi@ --- */
209 mp_build(&r
, r2
->v
+ n
, r2
->vl
);
210 mm
->mi
= mp_modinv(MP_NEW
, m
, &r
);
211 mm
->mi
= mp_sub(mm
->mi
, &r
, mm
->mi
);
212 MP_ENSURE(mm
->mi
, n
);
214 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
217 mp_div(0, &mm
->r2
, r2
, m
);
218 mm
->r
= mpmont_reduce(mm
, MP_NEW
, mm
->r2
);
225 /* --- @mpmont_destroy@ --- *
227 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
231 * Use: Disposes of a context when it's no longer of any use to
235 void mpmont_destroy(mpmont
*mm
)
243 /* --- @mpmont_reduce@ --- *
245 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
246 * @mp *d@ = destination
247 * @mp *a@ = source, assumed positive
249 * Returns: Result, %$a R^{-1} \bmod m$%.
252 #ifdef MPMONT_DISABLE
254 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
256 mp_div(0, &d
, a
, mm
->m
);
262 mp
*mpmont_reduce(mpmont
*mm
, mp
*d
, mp
*a
)
266 /* --- Check for serious Karatsuba reduction --- */
268 if (n
> MPK_THRESH
* 3) {
273 if (MP_LEN(a
) >= n
) vl
= a
->v
+ n
;
275 mp_build(&al
, a
->v
, vl
);
276 u
= mp_mul(MP_NEW
, &al
, mm
->mi
);
277 if (MP_LEN(u
) > n
) u
->vl
= u
->v
+ n
;
278 u
= mp_mul(u
, u
, mm
->m
);
284 /* --- Otherwise do it the hard way --- */
290 MP_DEST(d
, 2*mm
->n
+ 1, a
->f
);
291 redccore(d
->v
, d
->vl
, mm
->m
->v
, mm
->n
, mm
->mi
->v
);
294 /* --- Wrap everything up --- */
302 /* --- @mpmont_mul@ --- *
304 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
305 * @mp *d@ = destination
306 * @mp *a, *b@ = sources, assumed positive
308 * Returns: Result, %$a b R^{-1} \bmod m$%.
311 #ifdef MPMONT_DISABLE
313 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
316 mp_div(0, &d
, d
, mm
->m
);
322 mp
*mpmont_mul(mpmont
*mm
, mp
*d
, mp
*a
, mp
*b
)
324 if (mm
->n
> MPK_THRESH
* 3) {
326 d
= mpmont_reduce(mm
, d
, d
);
330 MP_DEST(d
, 2*mm
->n
+ 1, a
->f
| b
->f
| MP_UNDEF
);
331 mulcore(d
->v
, d
->vl
, a
->v
, a
->vl
, b
->v
, b
->vl
,
332 mm
->m
->v
, mm
->n
, mm
->mi
->v
);
333 d
->f
= ((a
->f
| b
->f
) & MP_BURN
) | ((a
->f
^ b
->f
) & MP_NEG
);
335 MP_DROP(a
); MP_DROP(b
);
343 /*----- Test rig ----------------------------------------------------------*/
347 static int tcreate(dstr
*v
)
349 mp
*m
= *(mp
**)v
[0].buf
;
350 mp
*mi
= *(mp
**)v
[1].buf
;
351 mp
*r
= *(mp
**)v
[2].buf
;
352 mp
*r2
= *(mp
**)v
[3].buf
;
357 mpmont_create(&mm
, m
);
359 if (mm
.mi
->v
[0] != mi
->v
[0]) {
360 fprintf(stderr
, "\n*** bad mi: found %lu, expected %lu",
361 (unsigned long)mm
.mi
->v
[0], (unsigned long)mi
->v
[0]);
362 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
367 if (!MP_EQ(mm
.r
, r
)) {
368 fputs("\n*** bad r", stderr
);
369 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
370 fputs("\nexpected ", stderr
); mp_writefile(r
, stderr
, 10);
371 fputs("\n found ", stderr
); mp_writefile(mm
.r
, stderr
, 10);
376 if (!MP_EQ(mm
.r2
, r2
)) {
377 fputs("\n*** bad r2", stderr
);
378 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
379 fputs("\nexpected ", stderr
); mp_writefile(r2
, stderr
, 10);
380 fputs("\n found ", stderr
); mp_writefile(mm
.r2
, stderr
, 10);
390 assert(mparena_count(MPARENA_GLOBAL
) == 0);
394 static int tmul(dstr
*v
)
396 mp
*m
= *(mp
**)v
[0].buf
;
397 mp
*a
= *(mp
**)v
[1].buf
;
398 mp
*b
= *(mp
**)v
[2].buf
;
399 mp
*r
= *(mp
**)v
[3].buf
;
403 mpmont_create(&mm
, m
);
406 mp
*qr
= mp_mul(MP_NEW
, a
, b
);
407 mp_div(0, &qr
, qr
, m
);
410 fputs("\n*** classical modmul failed", stderr
);
411 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
412 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
413 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
414 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
415 fputs("\nqr = ", stderr
); mp_writefile(qr
, stderr
, 10);
424 mp
*ar
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
425 mp
*br
= mpmont_mul(&mm
, MP_NEW
, b
, mm
.r2
);
426 mp
*mr
= mpmont_mul(&mm
, MP_NEW
, ar
, br
);
427 mr
= mpmont_reduce(&mm
, mr
, mr
);
429 fputs("\n*** montgomery modmul failed", stderr
);
430 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
431 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
432 fputs("\n b = ", stderr
); mp_writefile(b
, stderr
, 10);
433 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
434 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
438 MP_DROP(ar
); MP_DROP(br
);
448 assert(mparena_count(MPARENA_GLOBAL
) == 0);
452 static test_chunk tests
[] = {
453 { "create", tcreate
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
454 { "mul", tmul
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
458 int main(int argc
, char *argv
[])
461 test_run(argc
, argv
, tests
, SRCDIR
"/t/mpmont");
467 /*----- That's all, folks -------------------------------------------------*/