Commit | Line | Data |
---|---|---|
34e4f738 | 1 | /* -*-c-*- |
34e4f738 | 2 | * |
3 | * Modular exponentiation using Barrett reduction | |
4 | * | |
5 | * (c) 2004 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
34e4f738 | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
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. | |
45c0fd36 | 16 | * |
34e4f738 | 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. | |
45c0fd36 | 21 | * |
34e4f738 | 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, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
34e4f738 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
30 | #include "mp.h" | |
31 | #include "mpbarrett.h" | |
32 | #include "mpbarrett-exp.h" | |
33 | ||
34 | /*----- Main code ---------------------------------------------------------*/ | |
35 | ||
36 | /* --- @mpbarrett_exp@ --- * | |
37 | * | |
38 | * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context | |
45c0fd36 MW |
39 | * @mp *d@ = fake destination |
40 | * @mp *a@ = base | |
41 | * @mp *e@ = exponent | |
34e4f738 | 42 | * |
45c0fd36 | 43 | * Returns: Result, %$a^e \bmod m$%. |
34e4f738 | 44 | */ |
45 | ||
46 | mp *mpbarrett_exp(mpbarrett *mb, mp *d, mp *a, mp *e) | |
47 | { | |
48 | mp *x = MP_ONE; | |
49 | mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW; | |
50 | ||
51 | MP_COPY(a); | |
52 | MP_SHRINK(e); | |
a69a3efd | 53 | if (MP_ZEROP(e)) |
34e4f738 | 54 | ; |
a69a3efd | 55 | else { |
56 | if (MP_NEGP(e)) | |
57 | a = mp_modinv(a, a, mb->m); | |
58 | if (MP_LEN(e) < EXP_THRESH) | |
59 | EXP_SIMPLE(x, a, e); | |
60 | else | |
61 | EXP_WINDOW(x, a, e); | |
62 | } | |
34e4f738 | 63 | mp_drop(d); |
64 | mp_drop(spare); | |
65 | mp_drop(a); | |
66 | return (x); | |
67 | } | |
68 | ||
69 | /*----- Test rig ----------------------------------------------------------*/ | |
70 | ||
71 | #ifdef TEST_RIG | |
72 | ||
73 | static int vexp(dstr *v) | |
74 | { | |
75 | mp *m = *(mp **)v[0].buf; | |
76 | mp *a = *(mp **)v[1].buf; | |
77 | mp *b = *(mp **)v[2].buf; | |
78 | mp *r = *(mp **)v[3].buf; | |
79 | mp *mr; | |
80 | int ok = 1; | |
81 | ||
82 | mpbarrett mb; | |
83 | mpbarrett_create(&mb, m); | |
84 | ||
85 | mr = mpbarrett_exp(&mb, MP_NEW, a, b); | |
86 | ||
87 | if (!MP_EQ(mr, r)) { | |
88 | fputs("\n*** barrett modexp failed", stderr); | |
89 | fputs("\n m = ", stderr); mp_writefile(m, stderr, 10); | |
90 | fputs("\n a = ", stderr); mp_writefile(a, stderr, 10); | |
91 | fputs("\n e = ", stderr); mp_writefile(b, stderr, 10); | |
92 | fputs("\n r = ", stderr); mp_writefile(r, stderr, 10); | |
93 | fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10); | |
94 | fputc('\n', stderr); | |
95 | ok = 0; | |
96 | } | |
97 | ||
98 | mp_drop(m); | |
99 | mp_drop(a); | |
100 | mp_drop(b); | |
101 | mp_drop(r); | |
102 | mp_drop(mr); | |
103 | mpbarrett_destroy(&mb); | |
104 | assert(mparena_count(MPARENA_GLOBAL) == 0); | |
105 | return ok; | |
106 | } | |
107 | ||
108 | static test_chunk tests[] = { | |
109 | { "mpbarrett-exp", vexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, | |
110 | { 0, 0, { 0 } } | |
111 | }; | |
112 | ||
113 | int main(int argc, char *argv[]) | |
114 | { | |
115 | sub_init(); | |
0f00dc4c | 116 | test_run(argc, argv, tests, SRCDIR "/t/mpbarrett"); |
34e4f738 | 117 | return (0); |
118 | } | |
119 | ||
120 | #endif | |
121 | ||
122 | /*----- That's all, folks -------------------------------------------------*/ |