3 * $Id: mpbarrett-mexp.c,v 1.2 2004/04/01 12:50:09 mdw Exp $
5 * Multiple simultaneous exponentiations
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-mexp.c,v $
33 * Revision 1.2 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.1 2001/06/16 12:58:34 mdw
41 * Added simultaneous exponentiation with Barrett reduction.
45 /*----- Header files ------------------------------------------------------*/
48 #include "mpbarrett.h"
51 #include "mpbarrett-exp.h"
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- @mpbarrett_mexp@ --- *
57 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
58 * @mp *d@ = fake destination
59 * @const mp_expfactor *f@ = pointer to array of factors
60 * @size_t n@ = number of factors supplied
62 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
63 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
66 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
69 mp
*mpbarrett_mexp(mpbarrett
*mb
, mp
*d
, const mp_expfactor
*f
, size_t n
)
71 mp_expfactor
*ff
= xmalloc(n
* sizeof(mp_expfactor
));
78 for (i
= 0; i
< n
; i
++) {
79 if (f
[i
].exp
->f
& MP_BURN
)
81 if (!(f
[i
].exp
->f
& MP_NEG
))
82 ff
[i
].base
= MP_COPY(f
[i
].base
);
85 mp_gcd(&g
, 0, &ff
[i
].base
, mb
->m
, f
[i
].base
);
86 assert(MP_EQ(g
, MP_ONE
));
94 for (i
= 0; i
< n
; i
++)
100 /*----- Test rig ----------------------------------------------------------*/
104 #include <mLib/testrig.h>
106 static int verify(size_t n
, dstr
*v
)
108 mp
*m
= *(mp
**)v
[0].buf
;
109 mp_expfactor
*f
= xmalloc(n
* sizeof(*f
));
116 for (i
= 0; i
< n
; i
++) {
117 f
[i
].base
= *(mp
**)v
[j
++].buf
;
118 f
[i
].exp
= *(mp
**)v
[j
++].buf
;
121 rr
= *(mp
**)v
[j
].buf
;
122 mpbarrett_create(&mb
, m
);
123 r
= mpbarrett_mexp(&mb
, MP_NEW
, f
, n
);
125 fputs("\n*** mexp failed\n", stderr
);
126 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
127 for (i
= 0; i
< n
; i
++) {
128 fprintf(stderr
, "\ng_%u = ", i
);
129 mp_writefile(f
[i
].base
, stderr
, 10);
130 fprintf(stderr
, "\ne_%u = ", i
);
131 mp_writefile(f
[i
].exp
, stderr
, 10);
133 fputs("\nr = ", stderr
); mp_writefile(r
, stderr
, 10);
134 fputs("\nR = ", stderr
); mp_writefile(rr
, stderr
, 10);
139 for (i
= 0; i
< n
; i
++) {
146 mpbarrett_destroy(&mb
);
147 assert(mparena_count(MPARENA_GLOBAL
) == 0);
151 static int t1(dstr
*v
) { return verify(1, v
); }
152 static int t2(dstr
*v
) { return verify(2, v
); }
153 static int t3(dstr
*v
) { return verify(3, v
); }
154 static int t4(dstr
*v
) { return verify(4, v
); }
155 static int t5(dstr
*v
) { return verify(5, v
); }
157 static test_chunk tests
[] = {
158 { "mexp-1", t1
, { &type_mp
,
161 { "mexp-2", t2
, { &type_mp
,
165 { "mexp-3", t3
, { &type_mp
,
170 { "mexp-4", t4
, { &type_mp
,
176 { "mexp-5", t5
, { &type_mp
,
186 int main(int argc
, char *argv
[])
189 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpbarrett");
195 /*----- That's all, folks -------------------------------------------------*/