3 * $Id: mpbarrett-exp.c,v 1.1 2004/04/01 12:50:09 mdw Exp $
5 * Modular exponentiation using Barrett reduction
7 * (c) 2004 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-exp.c,v $
33 * Revision 1.1 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.)
42 /*----- Header files ------------------------------------------------------*/
45 #include "mpbarrett.h"
46 #include "mpbarrett-exp.h"
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- @mpbarrett_exp@ --- *
52 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
53 * @mp *d@ = fake destination
57 * Returns: Result, %$a^e \bmod m$%.
60 mp
*mpbarrett_exp(mpbarrett
*mb
, mp
*d
, mp
*a
, mp
*e
)
63 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
69 mp_gcd(&g
, 0, &a
, mb
->m
, a
);
70 assert(MP_EQ(g
, MP_ONE
));
75 else if (MP_LEN(e
) < EXP_THRESH
)
85 /*----- Test rig ----------------------------------------------------------*/
89 static int vexp(dstr
*v
)
91 mp
*m
= *(mp
**)v
[0].buf
;
92 mp
*a
= *(mp
**)v
[1].buf
;
93 mp
*b
= *(mp
**)v
[2].buf
;
94 mp
*r
= *(mp
**)v
[3].buf
;
99 mpbarrett_create(&mb
, m
);
101 mr
= mpbarrett_exp(&mb
, MP_NEW
, a
, b
);
104 fputs("\n*** barrett modexp failed", stderr
);
105 fputs("\n m = ", stderr
); mp_writefile(m
, stderr
, 10);
106 fputs("\n a = ", stderr
); mp_writefile(a
, stderr
, 10);
107 fputs("\n e = ", stderr
); mp_writefile(b
, stderr
, 10);
108 fputs("\n r = ", stderr
); mp_writefile(r
, stderr
, 10);
109 fputs("\nmr = ", stderr
); mp_writefile(mr
, stderr
, 10);
119 mpbarrett_destroy(&mb
);
120 assert(mparena_count(MPARENA_GLOBAL
) == 0);
124 static test_chunk tests
[] = {
125 { "mpbarrett-exp", vexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
129 int main(int argc
, char *argv
[])
132 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpbarrett");
138 /*----- That's all, folks -------------------------------------------------*/