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 ------------------------------------------------------*/
35 /*----- Tweakables --------------------------------------------------------*/
37 /* --- @MPMONT_DISABLE@ --- *
39 * Replace all the clever Montgomery reduction with good old-fashioned long
43 /* #define MPMONT_DISABLE */
45 #define MPMONT_KTHRESH (16*MPK_THRESH)
47 /*----- Low-level implementation ------------------------------------------*/
49 #ifndef MPMONT_DISABLE
51 /* --- @redccore@ --- *
53 * Arguments: @mpw *dv, *dvl@ = base and limit of source/destination
54 * @const mpw *mv@ = base of modulus %$m$%
55 * @size_t n@ = length of modulus
56 * @const mpw *mi@ = base of REDC coefficient %$m'$%
60 * Use: Let %$a$% be the input operand. Store in %$d$% the value
61 * %$a + (m' a \bmod R) m$%. The destination has space for at
62 * least %$2 n + 1$% words of result.
65 CPU_DISPATCH(static, (void), void, redccore,
66 (mpw *dv, mpw *dvl, const mpw *mv, size_t n, const mpw *mi),
67 (dv, dvl, mv, n, mi), pick_redccore, simple_redccore);
69 static void simple_redccore(mpw *dv, mpw *dvl, const mpw *mv,
70 size_t n, const mpw *mi)
75 for (i = 0; i < n; i++) {
76 MPX_UMLAN(dv, dvl, mv, mv + n, MPW(*dv*mi0));
81 #define MAYBE_REDC4(impl) \
82 extern void mpxmont_redc4_##impl(mpw *dv, mpw *dvl, const mpw *mv, \
83 size_t n, const mpw *mi); \
84 static void maybe_redc4_##impl(mpw *dv, mpw *dvl, const mpw *mv, \
85 size_t n, const mpw *mi) \
87 if (n%4) simple_redccore(dv, dvl, mv, n, mi); \
88 else mpxmont_redc4_##impl(dv, dvl, mv, n, mi); \
95 static redccore__functype *pick_redccore(void)
98 DISPATCH_PICK_COND(mpmont_reduce, maybe_redc4_x86_sse2,
99 cpu_feature_p(CPUFEAT_X86_SSE2));
101 DISPATCH_PICK_FALLBACK(mpmont_reduce, simple_redccore);
104 /* --- @redccore@ --- *
106 * Arguments: @mpw *dv, *dvl@ = base and limit of source/destination
107 * @const mpw *av, *avl@ = base and limit of first multiplicand
108 * @const mpw *bv, *bvl@ = base and limit of second multiplicand
109 * @const mpw *mv@ = base of modulus %$m$%
110 * @size_t n@ = length of modulus
111 * @const mpw *mi@ = base of REDC coefficient %$m'$%
115 * Use: Let %$a$% and %$b$% be the multiplicands. Let %$w = a b$%.
116 * Store in %$d$% the value %$a b + (m' a b \bmod R) m$%.
119 CPU_DISPATCH(static, (void), void, mulcore,
120 (mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
121 const mpw *bv, const mpw *bvl, const mpw *mv,
122 size_t n, const mpw *mi),
123 (dv, dvl, av, avl, bv, bvl, mv, n, mi),
124 pick_mulcore, simple_mulcore);
126 static void simple_mulcore(mpw *dv, mpw *dvl,
127 const mpw *av, const mpw *avl,
128 const mpw *bv, const mpw *bvl,
129 const mpw *mv, size_t n, const mpw *mi)
131 mpw ai, b0, y, mi0 = *mi;
133 const mpw *mvl = mv + n;
136 /* --- Initial setup --- */
139 if (avl - av > bvl - bv) {
140 tv = av; av = bv; bv = tv;
141 tvl = avl; avl = bvl; bvl = tvl;
145 /* --- Multiply, until we run out of multiplicand --- */
147 while (i < n && av < avl) {
149 y = MPW((*dv + ai*b0)*mi0);
150 MPX_UMLAN(dv, dvl, bv, bvl, ai);
151 MPX_UMLAN(dv, dvl, mv, mvl, y);
155 /* --- Continue reducing until we run out of modulus --- */
159 MPX_UMLAN(dv, dvl, mv, mvl, y);
164 #define MAYBE_MUL4(impl) \
165 extern void mpxmont_mul4_##impl(mpw *dv, \
166 const mpw *av, const mpw *bv, \
168 size_t n, const mpw *mi); \
169 static void maybe_mul4_##impl(mpw *dv, mpw *dvl, \
170 const mpw *av, const mpw *avl, \
171 const mpw *bv, const mpw *bvl, \
172 const mpw *mv, size_t n, const mpw *mi) \
174 size_t an = avl - av, bn = bvl - bv; \
175 if (n%4 || an != n || bn != n) \
176 simple_mulcore(dv, dvl, av, avl, bv, bvl, mv, n, mi); \
178 mpxmont_mul4_##impl(dv, av, bv, mv, n, mi); \
179 MPX_ZERO(dv + 2*n + 1, dvl); \
187 static mulcore__functype *pick_mulcore(void)
190 DISPATCH_PICK_COND(mpmont_mul, maybe_mul4_x86_sse2,
191 cpu_feature_p(CPUFEAT_X86_SSE2));
193 DISPATCH_PICK_FALLBACK(mpmont_mul, simple_mulcore);
196 /* --- @finish@ --- *
198 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
199 * *mp *d@ = pointer to mostly-reduced operand
203 * Use: Applies the finishing touches to Montgomery reduction. The
204 * operand @d@ is a multiple of %$R%$ at this point, so it needs
205 * to be shifted down; the result might need a further
206 * subtraction to get it into the right interval; and we may
207 * need to do an additional subtraction if %$d$% is negative.
210 static void finish(mpmont *mm, mp *d)
212 mpw *dv = d->v, *dvl = d->vl;
215 memmove(dv, dv + n, MPWS(dvl - (dv + n)));
218 if (MPX_UCMP(dv, dvl, >=, mm->m->v, mm->m->vl))
219 mpx_usub(dv, dvl, dv, dvl, mm->m->v, mm->m->vl);
222 mpx_usub(dv, dvl, mm->m->v, mm->m->vl, dv, dvl);
232 /*----- Reduction and multiplication --------------------------------------*/
234 /* --- @mpmont_create@ --- *
236 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
237 * @mp *m@ = modulus to use
239 * Returns: Zero on success, nonzero on error.
241 * Use: Initializes a Montgomery reduction context ready for use.
242 * The argument @m@ must be a positive odd integer.
245 #ifdef MPMONT_DISABLE
247 int mpmont_create(mpmont *mm, mp *m)
259 int mpmont_create(mpmont *mm, mp *m)
261 size_t n = MP_LEN(m);
262 mp *r2 = mp_new(2 * n + 1, 0);
265 /* --- Take a copy of the modulus --- */
267 if (!MP_POSP(m) || !MP_ODDP(m))
271 /* --- Determine %$R^2$% --- */
274 MPX_ZERO(r2->v, r2->vl - 1);
277 /* --- Find the magic value @mi@ --- */
279 mp_build(&r, r2->v + n, r2->vl);
280 mm->mi = mp_modinv(MP_NEW, m, &r);
281 mm->mi = mp_sub(mm->mi, &r, mm->mi);
282 MP_ENSURE(mm->mi, n);
284 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
287 mp_div(0, &mm->r2, r2, m);
288 mm->r = mpmont_reduce(mm, MP_NEW, mm->r2);
295 /* --- @mpmont_destroy@ --- *
297 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
301 * Use: Disposes of a context when it's no longer of any use to
305 void mpmont_destroy(mpmont *mm)
313 /* --- @mpmont_reduce@ --- *
315 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
316 * @mp *d@ = destination
317 * @mp *a@ = source, assumed positive
319 * Returns: Result, %$a R^{-1} \bmod m$%.
322 #ifdef MPMONT_DISABLE
324 mp *mpmont_reduce(mpmont *mm, mp *d, mp *a)
326 mp_div(0, &d, a, mm->m);
332 mp *mpmont_reduce(mpmont *mm, mp *d, mp *a)
336 /* --- Check for serious Karatsuba reduction --- */
338 if (n > MPMONT_KTHRESH) {
343 if (MP_LEN(a) >= n) vl = a->v + n;
345 mp_build(&al, a->v, vl);
346 u = mp_mul(MP_NEW, &al, mm->mi);
347 if (MP_LEN(u) > n) u->vl = u->v + n;
348 u = mp_mul(u, u, mm->m);
354 /* --- Otherwise do it the hard way --- */
360 MP_DEST(d, 2*mm->n + 1, a->f);
361 redccore(d->v, d->vl, mm->m->v, mm->n, mm->mi->v);
364 /* --- Wrap everything up --- */
372 /* --- @mpmont_mul@ --- *
374 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
375 * @mp *d@ = destination
376 * @mp *a, *b@ = sources, assumed positive
378 * Returns: Result, %$a b R^{-1} \bmod m$%.
381 #ifdef MPMONT_DISABLE
383 mp *mpmont_mul(mpmont *mm, mp *d, mp *a, mp *b)
386 mp_div(0, &d, d, mm->m);
392 mp *mpmont_mul(mpmont *mm, mp *d, mp *a, mp *b)
396 if (n > MPMONT_KTHRESH) {
398 d = mpmont_reduce(mm, d, d);
400 a = MP_COPY(a); b = MP_COPY(b);
401 MP_DEST(d, 2*n + 1, a->f | b->f | MP_UNDEF);
402 mulcore(d->v, d->vl, a->v, a->vl, b->v, b->vl,
403 mm->m->v, mm->n, mm->mi->v);
404 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
406 MP_DROP(a); MP_DROP(b);
414 /*----- Test rig ----------------------------------------------------------*/
418 static int tcreate(dstr *v)
420 mp *m = *(mp **)v[0].buf;
421 mp *mi = *(mp **)v[1].buf;
422 mp *r = *(mp **)v[2].buf;
423 mp *r2 = *(mp **)v[3].buf;
428 mpmont_create(&mm, m);
430 if (mm.mi->v[0] != mi->v[0]) {
431 fprintf(stderr, "\n*** bad mi: found %lu, expected %lu",
432 (unsigned long)mm.mi->v[0], (unsigned long)mi->v[0]);
433 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
438 if (!MP_EQ(mm.r, r)) {
439 fputs("\n*** bad r", stderr);
440 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
441 fputs("\nexpected ", stderr); mp_writefile(r, stderr, 10);
442 fputs("\n found ", stderr); mp_writefile(mm.r, stderr, 10);
447 if (!MP_EQ(mm.r2, r2)) {
448 fputs("\n*** bad r2", stderr);
449 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
450 fputs("\nexpected ", stderr); mp_writefile(r2, stderr, 10);
451 fputs("\n found ", stderr); mp_writefile(mm.r2, stderr, 10);
461 assert(mparena_count(MPARENA_GLOBAL) == 0);
465 static int tmul(dstr *v)
467 mp *m = *(mp **)v[0].buf;
468 mp *a = *(mp **)v[1].buf;
469 mp *b = *(mp **)v[2].buf;
470 mp *r = *(mp **)v[3].buf;
474 mpmont_create(&mm, m);
477 mp *qr = mp_mul(MP_NEW, a, b);
478 mp_div(0, &qr, qr, m);
481 fputs("\n*** classical modmul failed", stderr);
482 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
483 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
484 fputs("\n b = ", stderr); mp_writefile(b, stderr, 10);
485 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
486 fputs("\nqr = ", stderr); mp_writefile(qr, stderr, 10);
495 mp *ar = mpmont_mul(&mm, MP_NEW, a, mm.r2);
496 mp *br = mpmont_mul(&mm, MP_NEW, b, mm.r2);
497 mp *mr = mpmont_mul(&mm, MP_NEW, ar, br);
498 mr = mpmont_reduce(&mm, mr, mr);
500 fputs("\n*** montgomery modmul failed", stderr);
501 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
502 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
503 fputs("\n b = ", stderr); mp_writefile(b, stderr, 10);
504 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
505 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
509 MP_DROP(ar); MP_DROP(br);
519 assert(mparena_count(MPARENA_GLOBAL) == 0);
523 static test_chunk tests[] = {
524 { "create", tcreate, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
525 { "mul", tmul, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
529 int main(int argc, char *argv[])
532 test_run(argc, argv, tests, SRCDIR "/t/mpmont");
538 /*----- That's all, folks -------------------------------------------------*/