3 * $Id: mpbarrett.c,v 1.9 2004/04/01 12:50:09 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
32 * $Log: mpbarrett.c,v $
33 * Revision 1.9 2004/04/01 12:50:09 mdw
34 * Add cyclic group abstraction, with test code. Separate off exponentation
35 * functions for better static linking. Fix a buttload of bugs on the way.
36 * Generally ensure that negative exponents do inversion correctly. Add
37 * table of standard prime-field subgroups. (Binary field subgroups are
38 * currently unimplemented but easy to add if anyone ever finds a good one.)
40 * Revision 1.8 2001/06/16 13:00:20 mdw
41 * Use the generic exponentiation functions.
43 * Revision 1.7 2001/04/19 18:25:26 mdw
44 * Use sliding-window exponentiation.
46 * Revision 1.6 2000/10/08 12:03:44 mdw
47 * (mpbarrett_reduce): Cope with negative numbers.
49 * Revision 1.5 2000/07/29 17:04:33 mdw
50 * Change to use left-to-right bitwise exponentiation. This will improve
51 * performance when the base is small.
53 * Revision 1.4 2000/06/17 11:45:09 mdw
54 * Major memory management overhaul. Added arena support. Use the secure
55 * arena for secret integers. Replace and improve the MP management macros
56 * (e.g., replace MP_MODIFY by MP_DEST).
58 * Revision 1.3 1999/12/12 15:08:52 mdw
59 * Don't bother shifting %$q$% in @mpbarrett_reduce@, just skip the least
62 * Revision 1.2 1999/12/11 01:50:56 mdw
63 * Improve initialization slightly.
65 * Revision 1.1 1999/12/10 23:21:59 mdw
66 * Barrett reduction support: works with even moduli.
70 /*----- Header files ------------------------------------------------------*/
73 #include "mpbarrett.h"
75 /*----- Main code ---------------------------------------------------------*/
77 /* --- @mpbarrett_create@ --- *
79 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
80 * @mp *m@ = modulus to work to
85 * Use: Initializes a Barrett reduction context ready for use.
88 void mpbarrett_create(mpbarrett
*mb
, mp
*m
)
92 /* --- Validate the arguments --- */
94 assert(((void)"Barrett modulus must be positive", (m
->f
& MP_NEG
) == 0));
96 /* --- Compute %$\mu$% --- */
101 b
= mp_new(2 * mb
->k
+ 1, 0);
102 MPX_ZERO(b
->v
, b
->vl
- 1);
108 /* --- @mpbarrett_destroy@ --- *
110 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
114 * Use: Destroys a Barrett reduction context releasing any resources
118 void mpbarrett_destroy(mpbarrett
*mb
)
124 /* --- @mpbarrett_reduce@ --- *
126 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
127 * @mp *d@ = destination for result
128 * @mp *m@ = number to reduce
130 * Returns: The residue of @m@ modulo the number in the reduction
133 * Use: Performs an efficient modular reduction.
136 mp
*mpbarrett_reduce(mpbarrett
*mb
, mp
*d
, mp
*m
)
141 /* --- Special case if @m@ is too small --- */
150 /* --- First stage --- */
154 mp_build(&qq
, m
->v
+ (k
- 1), m
->vl
);
155 q
= mp_mul(MP_NEW
, &qq
, mb
->mu
);
156 if (MP_LEN(q
) <= k
) {
164 /* --- Second stage --- */
171 if (MP_LEN(m
) <= k
+ 1)
175 r
= mp_new(k
+ 1, (q
->f
| mb
->m
->f
) & MP_BURN
);
176 mpx_umul(r
->v
, r
->vl
, q
->v
+ k
+ 1, q
->vl
, mb
->m
->v
, mb
->m
->vl
);
177 MP_DEST(d
, k
+ 1, r
->f
);
178 mpx_usub(d
->v
, d
->vl
, m
->v
, mvl
, r
->v
, r
->vl
);
179 d
->f
= (m
->f
| r
->f
) & (MP_BURN
| MP_NEG
);
185 /* --- Final stage --- */
188 while (MPX_UCMP(d
->v
, d
->vl
, >=, mb
->m
->v
, mb
->m
->vl
))
189 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mb
->m
->v
, mb
->m
->vl
);
191 /* --- Fix up the sign --- */
194 mpx_usub(d
->v
, d
->vl
, mb
->m
->v
, mb
->m
->vl
, d
->v
, d
->vl
);
202 /*----- Test rig ----------------------------------------------------------*/
206 static int vmod(dstr
*v
)
208 mp
*x
= *(mp
**)v
[0].buf
;
209 mp
*n
= *(mp
**)v
[1].buf
;
210 mp
*r
= *(mp
**)v
[2].buf
;
215 mpbarrett_create(&mb
, n
);
216 s
= mpbarrett_reduce(&mb
, MP_NEW
, x
);
219 fputs("\n*** barrett reduction failure\n", stderr
);
220 fputs("x = ", stderr
); mp_writefile(x
, stderr
, 10); fputc('\n', stderr
);
221 fputs("n = ", stderr
); mp_writefile(n
, stderr
, 10); fputc('\n', stderr
);
222 fputs("r = ", stderr
); mp_writefile(r
, stderr
, 10); fputc('\n', stderr
);
223 fputs("s = ", stderr
); mp_writefile(s
, stderr
, 10); fputc('\n', stderr
);
227 mpbarrett_destroy(&mb
);
232 assert(mparena_count(MPARENA_GLOBAL
) == 0);
236 static test_chunk tests
[] = {
237 { "mpbarrett-reduce", vmod
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
241 int main(int argc
, char *argv
[])
244 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpbarrett");
250 /*----- That's all, folks -------------------------------------------------*/