ab0350a6 |
1 | /* -*-c-*- |
2 | * |
a69a3efd |
3 | * $Id$ |
ab0350a6 |
4 | * |
5 | * Multiple simultaneous exponentiations |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
ab0350a6 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include "mp.h" |
33 | #include "mpbarrett.h" |
34 | |
35 | #define EXP_WINSZ 3 |
36 | #include "mpbarrett-exp.h" |
37 | |
38 | /*----- Main code ---------------------------------------------------------*/ |
39 | |
40 | /* --- @mpbarrett_mexp@ --- * |
41 | * |
42 | * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context |
43 | * @mp *d@ = fake destination |
34e4f738 |
44 | * @const mp_expfactor *f@ = pointer to array of factors |
ab0350a6 |
45 | * @size_t n@ = number of factors supplied |
46 | * |
47 | * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the |
48 | * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result |
49 | * is: |
50 | * |
51 | * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$% |
52 | */ |
53 | |
34e4f738 |
54 | mp *mpbarrett_mexp(mpbarrett *mb, mp *d, const mp_expfactor *f, size_t n) |
ab0350a6 |
55 | { |
34e4f738 |
56 | mp_expfactor *ff = xmalloc(n * sizeof(mp_expfactor)); |
ab0350a6 |
57 | mp *a = MP_ONE; |
58 | mp *spare; |
34e4f738 |
59 | mp *g = MP_NEW; |
ab0350a6 |
60 | size_t i; |
61 | |
62 | spare = MP_NEW; |
63 | for (i = 0; i < n; i++) { |
34e4f738 |
64 | if (f[i].exp->f & MP_BURN) |
ab0350a6 |
65 | spare = MP_NEWSEC; |
a69a3efd |
66 | if (MP_NEGP(f[i].exp)) |
b817bfc6 |
67 | ff[i].base = mp_modinv(MP_NEW, f[i].base, mb->m); |
a69a3efd |
68 | else |
69 | ff[i].base = MP_COPY(f[i].base); |
34e4f738 |
70 | ff[i].exp = f[i].exp; |
ab0350a6 |
71 | } |
34e4f738 |
72 | mp_drop(g); |
73 | EXP_SIMUL(a, ff, n); |
ab0350a6 |
74 | mp_drop(d); |
75 | mp_drop(spare); |
34e4f738 |
76 | for (i = 0; i < n; i++) |
77 | mp_drop(ff[i].base); |
78 | xfree(ff); |
ab0350a6 |
79 | return (a); |
80 | } |
81 | |
82 | /*----- Test rig ----------------------------------------------------------*/ |
83 | |
84 | #ifdef TEST_RIG |
85 | |
86 | #include <mLib/testrig.h> |
87 | |
88 | static int verify(size_t n, dstr *v) |
89 | { |
90 | mp *m = *(mp **)v[0].buf; |
91 | mp_expfactor *f = xmalloc(n * sizeof(*f)); |
92 | mp *r, *rr; |
93 | size_t i, j; |
94 | mpbarrett mb; |
95 | int ok = 1; |
96 | |
97 | j = 1; |
98 | for (i = 0; i < n; i++) { |
99 | f[i].base = *(mp **)v[j++].buf; |
100 | f[i].exp = *(mp **)v[j++].buf; |
101 | } |
102 | |
103 | rr = *(mp **)v[j].buf; |
104 | mpbarrett_create(&mb, m); |
105 | r = mpbarrett_mexp(&mb, MP_NEW, f, n); |
106 | if (!MP_EQ(r, rr)) { |
107 | fputs("\n*** mexp failed\n", stderr); |
108 | fputs("m = ", stderr); mp_writefile(m, stderr, 10); |
109 | for (i = 0; i < n; i++) { |
110 | fprintf(stderr, "\ng_%u = ", i); |
111 | mp_writefile(f[i].base, stderr, 10); |
112 | fprintf(stderr, "\ne_%u = ", i); |
113 | mp_writefile(f[i].exp, stderr, 10); |
114 | } |
115 | fputs("\nr = ", stderr); mp_writefile(r, stderr, 10); |
116 | fputs("\nR = ", stderr); mp_writefile(rr, stderr, 10); |
117 | fputc('\n', stderr); |
118 | ok = 0; |
119 | } |
120 | |
121 | for (i = 0; i < n; i++) { |
122 | MP_DROP(f[i].base); |
123 | MP_DROP(f[i].exp); |
124 | } |
125 | MP_DROP(m); |
126 | MP_DROP(r); |
127 | MP_DROP(rr); |
128 | mpbarrett_destroy(&mb); |
129 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
130 | return (ok); |
131 | } |
132 | |
133 | static int t1(dstr *v) { return verify(1, v); } |
134 | static int t2(dstr *v) { return verify(2, v); } |
135 | static int t3(dstr *v) { return verify(3, v); } |
136 | static int t4(dstr *v) { return verify(4, v); } |
137 | static int t5(dstr *v) { return verify(5, v); } |
138 | |
139 | static test_chunk tests[] = { |
140 | { "mexp-1", t1, { &type_mp, |
141 | &type_mp, &type_mp, |
142 | &type_mp, 0 } }, |
143 | { "mexp-2", t2, { &type_mp, |
144 | &type_mp, &type_mp, |
145 | &type_mp, &type_mp, |
146 | &type_mp, 0 } }, |
147 | { "mexp-3", t3, { &type_mp, |
148 | &type_mp, &type_mp, |
149 | &type_mp, &type_mp, |
150 | &type_mp, &type_mp, |
151 | &type_mp, 0 } }, |
152 | { "mexp-4", t4, { &type_mp, |
153 | &type_mp, &type_mp, |
154 | &type_mp, &type_mp, |
155 | &type_mp, &type_mp, |
156 | &type_mp, &type_mp, |
157 | &type_mp, 0 } }, |
158 | { "mexp-5", t5, { &type_mp, |
159 | &type_mp, &type_mp, |
160 | &type_mp, &type_mp, |
161 | &type_mp, &type_mp, |
162 | &type_mp, &type_mp, |
163 | &type_mp, &type_mp, |
164 | &type_mp, 0 } }, |
165 | { 0, 0, { 0 } } |
166 | }; |
167 | |
168 | int main(int argc, char *argv[]) |
169 | { |
170 | sub_init(); |
171 | test_run(argc, argv, tests, SRCDIR "/tests/mpbarrett"); |
172 | return (0); |
173 | } |
174 | |
175 | #endif |
176 | |
177 | /*----- That's all, folks -------------------------------------------------*/ |