5 * Barrett modular reduction
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 ------------------------------------------------------*/
33 #include "mpbarrett.h"
35 /*----- Main code ---------------------------------------------------------*/
37 /* --- @mpbarrett_create@ --- *
39 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
40 * @mp *m@ = modulus to work to
43 * Returns: Zero on success, nonzero on error.
45 * Use: Initializes a Barrett reduction context ready for use.
48 int mpbarrett_create(mpbarrett *mb, mp *m)
52 /* --- Validate the arguments --- */
57 /* --- Compute %$\mu$% --- */
62 b = mp_new(2 * mb->k + 1, 0);
63 MPX_ZERO(b->v, b->vl - 1);
70 /* --- @mpbarrett_destroy@ --- *
72 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
76 * Use: Destroys a Barrett reduction context releasing any resources
80 void mpbarrett_destroy(mpbarrett *mb)
86 /* --- @mpbarrett_reduce@ --- *
88 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
89 * @mp *d@ = destination for result
90 * @mp *m@ = number to reduce
92 * Returns: The residue of @m@ modulo the number in the reduction
95 * Use: Performs an efficient modular reduction.
98 mp *mpbarrett_reduce(mpbarrett *mb, mp *d, mp *m)
103 /* --- Special case if @m@ is too small --- */
112 /* --- First stage --- */
116 mp_build(&qq, m->v + (k - 1), m->vl);
117 q = mp_mul(MP_NEW, &qq, mb->mu);
118 if (MP_LEN(q) <= k) {
127 /* --- Second stage --- */
134 if (MP_LEN(m) <= k + 1)
138 r = mp_new(k + 1, (q->f | mb->m->f) & MP_BURN);
139 mpx_umul(r->v, r->vl, q->v + k + 1, q->vl, mb->m->v, mb->m->vl);
140 MP_DEST(d, k + 1, r->f | MP_UNDEF);
141 mpx_usub(d->v, d->vl, m->v, mvl, r->v, r->vl);
142 d->f = (m->f | r->f) & (MP_BURN | MP_NEG);
148 /* --- Final stage --- */
151 while (MPX_UCMP(d->v, d->vl, >=, mb->m->v, mb->m->vl))
152 mpx_usub(d->v, d->vl, d->v, d->vl, mb->m->v, mb->m->vl);
154 /* --- Fix up the sign --- */
157 mpx_usub(d->v, d->vl, mb->m->v, mb->m->vl, d->v, d->vl);
165 /*----- Test rig ----------------------------------------------------------*/
169 static int vmod(dstr *v)
171 mp *x = *(mp **)v[0].buf;
172 mp *n = *(mp **)v[1].buf;
173 mp *r = *(mp **)v[2].buf;
178 mpbarrett_create(&mb, n);
179 s = mpbarrett_reduce(&mb, MP_NEW, x);
182 fputs("\n*** barrett reduction failure\n", stderr);
183 fputs("x = ", stderr); mp_writefile(x, stderr, 10); fputc('\n', stderr);
184 fputs("n = ", stderr); mp_writefile(n, stderr, 10); fputc('\n', stderr);
185 fputs("r = ", stderr); mp_writefile(r, stderr, 10); fputc('\n', stderr);
186 fputs("s = ", stderr); mp_writefile(s, stderr, 10); fputc('\n', stderr);
190 mpbarrett_destroy(&mb);
195 assert(mparena_count(MPARENA_GLOBAL) == 0);
199 static test_chunk tests[] = {
200 { "mpbarrett-reduce", vmod, { &type_mp, &type_mp, &type_mp, 0 } },
204 int main(int argc, char *argv[])
207 test_run(argc, argv, tests, SRCDIR "/tests/mpbarrett");
213 /*----- That's all, folks -------------------------------------------------*/