| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mpmont-mexp.c,v 1.6 2001/06/16 13:00:20 mdw Exp $ |
| 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 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: mpmont-mexp.c,v $ |
| 33 | * Revision 1.6 2001/06/16 13:00:20 mdw |
| 34 | * Use the generic exponentiation functions. |
| 35 | * |
| 36 | * Revision 1.5 2000/10/08 12:11:22 mdw |
| 37 | * Use @MP_EQ@ instead of @MP_CMP@. |
| 38 | * |
| 39 | * Revision 1.4 2000/06/17 11:45:09 mdw |
| 40 | * Major memory management overhaul. Added arena support. Use the secure |
| 41 | * arena for secret integers. Replace and improve the MP management macros |
| 42 | * (e.g., replace MP_MODIFY by MP_DEST). |
| 43 | * |
| 44 | * Revision 1.3 1999/12/10 23:18:39 mdw |
| 45 | * Change interface for suggested destinations. |
| 46 | * |
| 47 | * Revision 1.2 1999/11/21 11:35:10 mdw |
| 48 | * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of |
| 49 | * @mpmont_mul@ for squaring in exponentiation. |
| 50 | * |
| 51 | * Revision 1.1 1999/11/19 13:19:29 mdw |
| 52 | * Simultaneous exponentiation support. |
| 53 | * |
| 54 | */ |
| 55 | |
| 56 | /*----- Header files ------------------------------------------------------*/ |
| 57 | |
| 58 | #include "mp.h" |
| 59 | #include "mpmont.h" |
| 60 | |
| 61 | #define EXP_WINSZ 3 |
| 62 | #include "mpmont-exp.h" |
| 63 | |
| 64 | /*----- Main code ---------------------------------------------------------*/ |
| 65 | |
| 66 | /* --- @mpmont_mexpr@ --- * |
| 67 | * |
| 68 | * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context |
| 69 | * @mp *d@ = fake destination |
| 70 | * @mp_expfactor *f@ = pointer to array of factors |
| 71 | * @size_t n@ = number of factors supplied |
| 72 | * |
| 73 | * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the |
| 74 | * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result |
| 75 | * is: |
| 76 | * |
| 77 | * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$% |
| 78 | * |
| 79 | * except that the %$g_i$% and result are in Montgomery form. |
| 80 | */ |
| 81 | |
| 82 | mp *mpmont_mexpr(mpmont *mm, mp *d, mp_expfactor *f, size_t n) |
| 83 | { |
| 84 | mp *a = mp_copy(mm->r); |
| 85 | mp *spare; |
| 86 | size_t i; |
| 87 | |
| 88 | spare = MP_NEW; |
| 89 | for (i = 0; i < n; i++) { |
| 90 | if (f[i].exp->f & MP_BURN) { |
| 91 | spare = MP_NEWSEC; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | EXP_SIMUL(a, f, n); |
| 97 | mp_drop(d); |
| 98 | mp_drop(spare); |
| 99 | return (a); |
| 100 | } |
| 101 | |
| 102 | /* --- @mpmont_mexp@ --- * |
| 103 | * |
| 104 | * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context |
| 105 | * @mp *d@ = fake destination |
| 106 | * @mp_expfactor *f@ = pointer to array of factors |
| 107 | * @size_t n@ = number of factors supplied |
| 108 | * |
| 109 | * Returns: Product of bases raised to exponents, all mod @m@. |
| 110 | * |
| 111 | * Use: Convenient interface over @mpmont_mexpr@. |
| 112 | */ |
| 113 | |
| 114 | mp *mpmont_mexp(mpmont *mm, mp *d, mp_expfactor *f, size_t n) |
| 115 | { |
| 116 | mp_expfactor *v = xmalloc(sizeof(*v) * n); |
| 117 | size_t i; |
| 118 | |
| 119 | for (i = 0; i < n; i++) { |
| 120 | v[i].base = mpmont_mul(mm, MP_NEW, f[i].base, mm->r2); |
| 121 | v[i].exp = f[i].exp; |
| 122 | } |
| 123 | d = mpmont_mexpr(mm, d, v, n); |
| 124 | for (i = 0; i < n; i++) |
| 125 | MP_DROP(v[i].base); |
| 126 | xfree(v); |
| 127 | d = mpmont_reduce(mm, d, d); |
| 128 | return (d); |
| 129 | } |
| 130 | |
| 131 | /*----- Test rig ----------------------------------------------------------*/ |
| 132 | |
| 133 | #ifdef TEST_RIG |
| 134 | |
| 135 | #include <mLib/testrig.h> |
| 136 | |
| 137 | static int verify(size_t n, dstr *v) |
| 138 | { |
| 139 | mp *m = *(mp **)v[0].buf; |
| 140 | mp_expfactor *f = xmalloc(n * sizeof(*f)); |
| 141 | mp *r, *rr; |
| 142 | size_t i, j; |
| 143 | mpmont mm; |
| 144 | int ok = 1; |
| 145 | |
| 146 | j = 1; |
| 147 | for (i = 0; i < n; i++) { |
| 148 | f[i].base = *(mp **)v[j++].buf; |
| 149 | f[i].exp = *(mp **)v[j++].buf; |
| 150 | } |
| 151 | |
| 152 | rr = *(mp **)v[j].buf; |
| 153 | mpmont_create(&mm, m); |
| 154 | r = mpmont_mexp(&mm, MP_NEW, f, n); |
| 155 | if (!MP_EQ(r, rr)) { |
| 156 | fputs("\n*** mexp failed\n", stderr); |
| 157 | fputs("m = ", stderr); mp_writefile(m, stderr, 10); |
| 158 | for (i = 0; i < n; i++) { |
| 159 | fprintf(stderr, "\ng_%u = ", i); |
| 160 | mp_writefile(f[i].base, stderr, 10); |
| 161 | fprintf(stderr, "\ne_%u = ", i); |
| 162 | mp_writefile(f[i].exp, stderr, 10); |
| 163 | } |
| 164 | fputs("\nr = ", stderr); mp_writefile(r, stderr, 10); |
| 165 | fputs("\nR = ", stderr); mp_writefile(rr, stderr, 10); |
| 166 | fputc('\n', stderr); |
| 167 | ok = 0; |
| 168 | } |
| 169 | |
| 170 | for (i = 0; i < n; i++) { |
| 171 | MP_DROP(f[i].base); |
| 172 | MP_DROP(f[i].exp); |
| 173 | } |
| 174 | MP_DROP(m); |
| 175 | MP_DROP(r); |
| 176 | MP_DROP(rr); |
| 177 | mpmont_destroy(&mm); |
| 178 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 179 | return (ok); |
| 180 | } |
| 181 | |
| 182 | static int t1(dstr *v) { return verify(1, v); } |
| 183 | static int t2(dstr *v) { return verify(2, v); } |
| 184 | static int t3(dstr *v) { return verify(3, v); } |
| 185 | static int t4(dstr *v) { return verify(4, v); } |
| 186 | static int t5(dstr *v) { return verify(5, v); } |
| 187 | |
| 188 | static test_chunk tests[] = { |
| 189 | { "mexp-1", t1, { &type_mp, |
| 190 | &type_mp, &type_mp, |
| 191 | &type_mp, 0 } }, |
| 192 | { "mexp-2", t2, { &type_mp, |
| 193 | &type_mp, &type_mp, |
| 194 | &type_mp, &type_mp, |
| 195 | &type_mp, 0 } }, |
| 196 | { "mexp-3", t3, { &type_mp, |
| 197 | &type_mp, &type_mp, |
| 198 | &type_mp, &type_mp, |
| 199 | &type_mp, &type_mp, |
| 200 | &type_mp, 0 } }, |
| 201 | { "mexp-4", t4, { &type_mp, |
| 202 | &type_mp, &type_mp, |
| 203 | &type_mp, &type_mp, |
| 204 | &type_mp, &type_mp, |
| 205 | &type_mp, &type_mp, |
| 206 | &type_mp, 0 } }, |
| 207 | { "mexp-5", t5, { &type_mp, |
| 208 | &type_mp, &type_mp, |
| 209 | &type_mp, &type_mp, |
| 210 | &type_mp, &type_mp, |
| 211 | &type_mp, &type_mp, |
| 212 | &type_mp, &type_mp, |
| 213 | &type_mp, 0 } }, |
| 214 | { 0, 0, { 0 } } |
| 215 | }; |
| 216 | |
| 217 | int main(int argc, char *argv[]) |
| 218 | { |
| 219 | sub_init(); |
| 220 | test_run(argc, argv, tests, SRCDIR "/tests/mpmont"); |
| 221 | return (0); |
| 222 | } |
| 223 | |
| 224 | #endif |
| 225 | |
| 226 | /*----- That's all, folks -------------------------------------------------*/ |