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
) {
126 /* --- Second stage --- */
133 if (MP_LEN(m
) <= k
+ 1)
137 r
= mp_new(k
+ 1, (q
->f
| mb
->m
->f
) & MP_BURN
);
138 mpx_umul(r
->v
, r
->vl
, q
->v
+ k
+ 1, q
->vl
, mb
->m
->v
, mb
->m
->vl
);
139 MP_DEST(d
, k
+ 1, r
->f
);
140 mpx_usub(d
->v
, d
->vl
, m
->v
, mvl
, r
->v
, r
->vl
);
141 d
->f
= (m
->f
| r
->f
) & (MP_BURN
| MP_NEG
);
147 /* --- Final stage --- */
150 while (MPX_UCMP(d
->v
, d
->vl
, >=, mb
->m
->v
, mb
->m
->vl
))
151 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mb
->m
->v
, mb
->m
->vl
);
153 /* --- Fix up the sign --- */
156 mpx_usub(d
->v
, d
->vl
, mb
->m
->v
, mb
->m
->vl
, d
->v
, d
->vl
);
164 /*----- Test rig ----------------------------------------------------------*/
168 static int vmod(dstr
*v
)
170 mp
*x
= *(mp
**)v
[0].buf
;
171 mp
*n
= *(mp
**)v
[1].buf
;
172 mp
*r
= *(mp
**)v
[2].buf
;
177 mpbarrett_create(&mb
, n
);
178 s
= mpbarrett_reduce(&mb
, MP_NEW
, x
);
181 fputs("\n*** barrett reduction failure\n", stderr
);
182 fputs("x = ", stderr
); mp_writefile(x
, stderr
, 10); fputc('\n', stderr
);
183 fputs("n = ", stderr
); mp_writefile(n
, stderr
, 10); fputc('\n', stderr
);
184 fputs("r = ", stderr
); mp_writefile(r
, stderr
, 10); fputc('\n', stderr
);
185 fputs("s = ", stderr
); mp_writefile(s
, stderr
, 10); fputc('\n', stderr
);
189 mpbarrett_destroy(&mb
);
194 assert(mparena_count(MPARENA_GLOBAL
) == 0);
198 static test_chunk tests
[] = {
199 { "mpbarrett-reduce", vmod
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
203 int main(int argc
, char *argv
[])
206 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpbarrett");
212 /*----- That's all, folks -------------------------------------------------*/