3 * Barrett modular reduction
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 ------------------------------------------------------*/
31 #include "mpbarrett.h"
33 /*----- Main code ---------------------------------------------------------*/
35 /* --- @mpbarrett_create@ --- *
37 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
38 * @mp *m@ = modulus to work to
41 * Returns: Zero on success, nonzero on error.
43 * Use: Initializes a Barrett reduction context ready for use.
46 int mpbarrett_create(mpbarrett
*mb
, mp
*m
)
50 /* --- Validate the arguments --- */
55 /* --- Compute %$\mu$% --- */
60 b
= mp_new(2 * mb
->k
+ 1, 0);
61 MPX_ZERO(b
->v
, b
->vl
- 1);
68 /* --- @mpbarrett_destroy@ --- *
70 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
74 * Use: Destroys a Barrett reduction context releasing any resources
78 void mpbarrett_destroy(mpbarrett
*mb
)
84 /* --- @mpbarrett_reduce@ --- *
86 * Arguments: @const mpbarrett *mb@ = pointer to Barrett reduction context
87 * @mp *d@ = destination for result
88 * @mp *m@ = number to reduce
90 * Returns: The residue of @m@ modulo the number in the reduction
93 * Use: Performs an efficient modular reduction.
96 mp
*mpbarrett_reduce(const mpbarrett
*mb
, mp
*d
, mp
*m
)
101 /* --- Special case if @m@ is too small --- */
110 /* --- First stage --- */
114 mp_build(&qq
, m
->v
+ (k
- 1), m
->vl
);
115 q
= mp_mul(MP_NEW
, &qq
, mb
->mu
);
116 if (MP_LEN(q
) <= k
) {
125 /* --- Second stage --- */
132 if (MP_LEN(m
) <= k
+ 1)
136 r
= mp_new(k
+ 1, (q
->f
| mb
->m
->f
) & MP_BURN
);
137 mpx_umul(r
->v
, r
->vl
, q
->v
+ k
+ 1, q
->vl
, mb
->m
->v
, mb
->m
->vl
);
138 MP_DEST(d
, k
+ 1, r
->f
| MP_UNDEF
);
139 mpx_usub(d
->v
, d
->vl
, m
->v
, mvl
, r
->v
, r
->vl
);
140 d
->f
= (m
->f
| r
->f
) & (MP_BURN
| MP_NEG
);
146 /* --- Final stage --- */
149 while (MPX_UCMP(d
->v
, d
->vl
, >=, mb
->m
->v
, mb
->m
->vl
))
150 mpx_usub(d
->v
, d
->vl
, d
->v
, d
->vl
, mb
->m
->v
, mb
->m
->vl
);
152 /* --- Fix up the sign --- */
155 mpx_usub(d
->v
, d
->vl
, mb
->m
->v
, mb
->m
->vl
, d
->v
, d
->vl
);
163 /*----- Test rig ----------------------------------------------------------*/
167 static int vmod(dstr
*v
)
169 mp
*x
= *(mp
**)v
[0].buf
;
170 mp
*n
= *(mp
**)v
[1].buf
;
171 mp
*r
= *(mp
**)v
[2].buf
;
176 mpbarrett_create(&mb
, n
);
177 s
= mpbarrett_reduce(&mb
, MP_NEW
, x
);
180 fputs("\n*** barrett reduction failure\n", stderr
);
181 fputs("x = ", stderr
); mp_writefile(x
, stderr
, 10); fputc('\n', stderr
);
182 fputs("n = ", stderr
); mp_writefile(n
, stderr
, 10); fputc('\n', stderr
);
183 fputs("r = ", stderr
); mp_writefile(r
, stderr
, 10); fputc('\n', stderr
);
184 fputs("s = ", stderr
); mp_writefile(s
, stderr
, 10); fputc('\n', stderr
);
188 mpbarrett_destroy(&mb
);
193 assert(mparena_count(MPARENA_GLOBAL
) == 0);
197 static test_chunk tests
[] = {
198 { "mpbarrett-reduce", vmod
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
202 int main(int argc
, char *argv
[])
205 test_run(argc
, argv
, tests
, SRCDIR
"/t/mpbarrett");
211 /*----- That's all, folks -------------------------------------------------*/