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); \
96 MAYBE_REDC4(amd64_sse2)
99 static redccore__functype *pick_redccore(void)
102 DISPATCH_PICK_COND(mpmont_reduce, maybe_redc4_x86_sse2,
103 cpu_feature_p(CPUFEAT_X86_SSE2));
106 DISPATCH_PICK_COND(mpmont_reduce, maybe_redc4_amd64_sse2,
107 cpu_feature_p(CPUFEAT_X86_SSE2));
109 DISPATCH_PICK_FALLBACK(mpmont_reduce, simple_redccore);
112 /* --- @redccore@ --- *
114 * Arguments: @mpw *dv, *dvl@ = base and limit of source/destination
115 * @const mpw *av, *avl@ = base and limit of first multiplicand
116 * @const mpw *bv, *bvl@ = base and limit of second multiplicand
117 * @const mpw *mv@ = base of modulus %$m$%
118 * @size_t n@ = length of modulus
119 * @const mpw *mi@ = base of REDC coefficient %$m'$%
123 * Use: Let %$a$% and %$b$% be the multiplicands. Let %$w = a b$%.
124 * Store in %$d$% the value %$a b + (m' a b \bmod R) m$%.
127 CPU_DISPATCH(static, (void), void, mulcore,
128 (mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
129 const mpw *bv, const mpw *bvl, const mpw *mv,
130 size_t n, const mpw *mi),
131 (dv, dvl, av, avl, bv, bvl, mv, n, mi),
132 pick_mulcore, simple_mulcore);
134 static void simple_mulcore(mpw *dv, mpw *dvl,
135 const mpw *av, const mpw *avl,
136 const mpw *bv, const mpw *bvl,
137 const mpw *mv, size_t n, const mpw *mi)
139 mpw ai, b0, y, mi0 = *mi;
141 const mpw *mvl = mv + n;
144 /* --- Initial setup --- */
147 if (avl - av > bvl - bv) {
148 tv = av; av = bv; bv = tv;
149 tvl = avl; avl = bvl; bvl = tvl;
153 /* --- Multiply, until we run out of multiplicand --- */
155 while (i < n && av < avl) {
157 y = MPW((*dv + ai*b0)*mi0);
158 MPX_UMLAN(dv, dvl, bv, bvl, ai);
159 MPX_UMLAN(dv, dvl, mv, mvl, y);
163 /* --- Continue reducing until we run out of modulus --- */
167 MPX_UMLAN(dv, dvl, mv, mvl, y);
172 #define MAYBE_MUL4(impl) \
173 extern void mpxmont_mul4_##impl(mpw *dv, \
174 const mpw *av, const mpw *bv, \
176 size_t n, const mpw *mi); \
177 static void maybe_mul4_##impl(mpw *dv, mpw *dvl, \
178 const mpw *av, const mpw *avl, \
179 const mpw *bv, const mpw *bvl, \
180 const mpw *mv, size_t n, const mpw *mi) \
182 size_t an = avl - av, bn = bvl - bv; \
183 if (n%4 || an != n || bn != n) \
184 simple_mulcore(dv, dvl, av, avl, bv, bvl, mv, n, mi); \
186 mpxmont_mul4_##impl(dv, av, bv, mv, n, mi); \
187 MPX_ZERO(dv + 2*n + 1, dvl); \
196 MAYBE_MUL4(amd64_sse2)
199 static mulcore__functype *pick_mulcore(void)
202 DISPATCH_PICK_COND(mpmont_mul, maybe_mul4_x86_sse2,
203 cpu_feature_p(CPUFEAT_X86_SSE2));
206 DISPATCH_PICK_COND(mpmont_mul, maybe_mul4_amd64_sse2,
207 cpu_feature_p(CPUFEAT_X86_SSE2));
209 DISPATCH_PICK_FALLBACK(mpmont_mul, simple_mulcore);
212 /* --- @finish@ --- *
214 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
215 * *mp *d@ = pointer to mostly-reduced operand
219 * Use: Applies the finishing touches to Montgomery reduction. The
220 * operand @d@ is a multiple of %$R%$ at this point, so it needs
221 * to be shifted down; the result might need a further
222 * subtraction to get it into the right interval; and we may
223 * need to do an additional subtraction if %$d$% is negative.
226 static void finish(mpmont *mm, mp *d)
228 mpw *dv = d->v, *dvl = d->vl;
231 memmove(dv, dv + n, MPWS(dvl - (dv + n)));
234 if (MPX_UCMP(dv, dvl, >=, mm->m->v, mm->m->vl))
235 mpx_usub(dv, dvl, dv, dvl, mm->m->v, mm->m->vl);
238 mpx_usub(dv, dvl, mm->m->v, mm->m->vl, dv, dvl);
248 /*----- Reduction and multiplication --------------------------------------*/
250 /* --- @mpmont_create@ --- *
252 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
253 * @mp *m@ = modulus to use
255 * Returns: Zero on success, nonzero on error.
257 * Use: Initializes a Montgomery reduction context ready for use.
258 * The argument @m@ must be a positive odd integer.
261 #ifdef MPMONT_DISABLE
263 int mpmont_create(mpmont *mm, mp *m)
275 int mpmont_create(mpmont *mm, mp *m)
277 size_t n = MP_LEN(m);
278 mp *r2 = mp_new(2 * n + 1, 0);
281 /* --- Take a copy of the modulus --- */
283 if (!MP_POSP(m) || !MP_ODDP(m))
287 /* --- Determine %$R^2$% --- */
290 MPX_ZERO(r2->v, r2->vl - 1);
293 /* --- Find the magic value @mi@ --- */
295 mp_build(&r, r2->v + n, r2->vl);
296 mm->mi = mp_modinv(MP_NEW, m, &r);
297 mm->mi = mp_sub(mm->mi, &r, mm->mi);
298 MP_ENSURE(mm->mi, n);
300 /* --- Discover the values %$R \bmod m$% and %$R^2 \bmod m$% --- */
303 mp_div(0, &mm->r2, r2, m);
304 mm->r = mpmont_reduce(mm, MP_NEW, mm->r2);
311 /* --- @mpmont_destroy@ --- *
313 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
317 * Use: Disposes of a context when it's no longer of any use to
321 void mpmont_destroy(mpmont *mm)
329 /* --- @mpmont_reduce@ --- *
331 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
332 * @mp *d@ = destination
333 * @mp *a@ = source, assumed positive
335 * Returns: Result, %$a R^{-1} \bmod m$%.
338 #ifdef MPMONT_DISABLE
340 mp *mpmont_reduce(mpmont *mm, mp *d, mp *a)
342 mp_div(0, &d, a, mm->m);
348 mp *mpmont_reduce(mpmont *mm, mp *d, mp *a)
352 /* --- Check for serious Karatsuba reduction --- */
354 if (n > MPMONT_KTHRESH) {
359 if (MP_LEN(a) >= n) vl = a->v + n;
361 mp_build(&al, a->v, vl);
362 u = mp_mul(MP_NEW, &al, mm->mi);
363 if (MP_LEN(u) > n) u->vl = u->v + n;
364 u = mp_mul(u, u, mm->m);
370 /* --- Otherwise do it the hard way --- */
376 MP_DEST(d, 2*mm->n + 1, a->f);
377 redccore(d->v, d->vl, mm->m->v, mm->n, mm->mi->v);
380 /* --- Wrap everything up --- */
388 /* --- @mpmont_mul@ --- *
390 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
391 * @mp *d@ = destination
392 * @mp *a, *b@ = sources, assumed positive
394 * Returns: Result, %$a b R^{-1} \bmod m$%.
397 #ifdef MPMONT_DISABLE
399 mp *mpmont_mul(mpmont *mm, mp *d, mp *a, mp *b)
402 mp_div(0, &d, d, mm->m);
408 mp *mpmont_mul(mpmont *mm, mp *d, mp *a, mp *b)
412 if (n > MPMONT_KTHRESH) {
414 d = mpmont_reduce(mm, d, d);
416 a = MP_COPY(a); b = MP_COPY(b);
417 MP_DEST(d, 2*n + 1, a->f | b->f | MP_UNDEF);
418 mulcore(d->v, d->vl, a->v, a->vl, b->v, b->vl,
419 mm->m->v, mm->n, mm->mi->v);
420 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
422 MP_DROP(a); MP_DROP(b);
430 /*----- Test rig ----------------------------------------------------------*/
434 static int tcreate(dstr *v)
436 mp *m = *(mp **)v[0].buf;
437 mp *mi = *(mp **)v[1].buf;
438 mp *r = *(mp **)v[2].buf;
439 mp *r2 = *(mp **)v[3].buf;
444 mpmont_create(&mm, m);
446 if (mm.mi->v[0] != mi->v[0]) {
447 fprintf(stderr, "\n*** bad mi: found %lu, expected %lu",
448 (unsigned long)mm.mi->v[0], (unsigned long)mi->v[0]);
449 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
454 if (!MP_EQ(mm.r, r)) {
455 fputs("\n*** bad r", stderr);
456 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
457 fputs("\nexpected ", stderr); mp_writefile(r, stderr, 10);
458 fputs("\n found ", stderr); mp_writefile(mm.r, stderr, 10);
463 if (!MP_EQ(mm.r2, r2)) {
464 fputs("\n*** bad r2", stderr);
465 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
466 fputs("\nexpected ", stderr); mp_writefile(r2, stderr, 10);
467 fputs("\n found ", stderr); mp_writefile(mm.r2, stderr, 10);
477 assert(mparena_count(MPARENA_GLOBAL) == 0);
481 static int tmul(dstr *v)
483 mp *m = *(mp **)v[0].buf;
484 mp *a = *(mp **)v[1].buf;
485 mp *b = *(mp **)v[2].buf;
486 mp *r = *(mp **)v[3].buf;
490 mpmont_create(&mm, m);
493 mp *qr = mp_mul(MP_NEW, a, b);
494 mp_div(0, &qr, qr, m);
497 fputs("\n*** classical modmul failed", stderr);
498 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
499 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
500 fputs("\n b = ", stderr); mp_writefile(b, stderr, 10);
501 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
502 fputs("\nqr = ", stderr); mp_writefile(qr, stderr, 10);
511 mp *ar = mpmont_mul(&mm, MP_NEW, a, mm.r2);
512 mp *br = mpmont_mul(&mm, MP_NEW, b, mm.r2);
513 mp *mr = mpmont_mul(&mm, MP_NEW, ar, br);
514 mr = mpmont_reduce(&mm, mr, mr);
516 fputs("\n*** montgomery modmul failed", stderr);
517 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
518 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
519 fputs("\n b = ", stderr); mp_writefile(b, stderr, 10);
520 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
521 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
525 MP_DROP(ar); MP_DROP(br);
535 assert(mparena_count(MPARENA_GLOBAL) == 0);
539 static test_chunk tests[] = {
540 { "create", tcreate, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
541 { "mul", tmul, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
545 int main(int argc, char *argv[])
548 test_run(argc, argv, tests, SRCDIR "/t/mpmont");
554 /*----- That's all, folks -------------------------------------------------*/