34e4f738 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: mpmont-exp.c,v 1.1 2004/04/01 12:50:09 mdw Exp $ |
4 | * |
5 | * Modular exponentiation with Montgomery reduction |
6 | * |
7 | * (c) 2004 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-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.) |
39 | * |
40 | */ |
41 | |
42 | /*----- Header files ------------------------------------------------------*/ |
43 | |
44 | #include "mp.h" |
45 | #include "mpmont.h" |
46 | #include "mpmont-exp.h" |
47 | |
48 | /*----- Exponentiation ----------------------------------------------------*/ |
49 | |
50 | /* --- @mpmont_expr@ --- * |
51 | * |
52 | * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context |
53 | * @mp *d@ = fake destination |
54 | * @mp *a@ = base |
55 | * @mp *e@ = exponent |
56 | * |
57 | * Returns: Result, %$(a R^{-1})^e R \bmod m$%. |
58 | */ |
59 | |
60 | mp *mpmont_expr(mpmont *mm, mp *d, mp *a, mp *e) |
61 | { |
62 | mp *x = MP_COPY(mm->r); |
63 | mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW; |
64 | |
65 | MP_COPY(a); |
66 | MP_SHRINK(e); |
67 | if (e->f & MP_NEG) { |
68 | mp *g = MP_NEW; |
69 | a = mpmont_reduce(mm, a, a); |
70 | mp_gcd(&g, 0, &a, mm->m, a); |
71 | assert(MP_EQ(g, MP_ONE)); |
72 | a = mpmont_mul(mm, a, a, mm->r2); |
73 | mp_drop(g); |
74 | } |
75 | if (MP_LEN(e) == 0) |
76 | ; |
77 | else if (MP_LEN(e) < EXP_THRESH) |
78 | EXP_SIMPLE(x, a, e); |
79 | else |
80 | EXP_WINDOW(x, a, e); |
81 | mp_drop(d); |
82 | mp_drop(spare); |
83 | mp_drop(a); |
84 | return (x); |
85 | } |
86 | |
87 | /* --- @mpmont_exp@ --- * |
88 | * |
89 | * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context |
90 | * @mp *d@ = fake destination |
91 | * @mp *a@ = base |
92 | * @mp *e@ = exponent |
93 | * |
94 | * Returns: Result, %$a^e \bmod m$%. |
95 | */ |
96 | |
97 | mp *mpmont_exp(mpmont *mm, mp *d, mp *a, mp *e) |
98 | { |
99 | e = MP_COPY(e); |
100 | d = mpmont_mul(mm, d, a, mm->r2); |
101 | d = mpmont_expr(mm, d, d, e); |
102 | d = mpmont_reduce(mm, d, d); |
103 | MP_DROP(e); |
104 | return (d); |
105 | } |
106 | |
107 | /*----- Test rig ----------------------------------------------------------*/ |
108 | |
109 | #ifdef TEST_RIG |
110 | |
111 | static int texp(dstr *v) |
112 | { |
113 | mp *m = *(mp **)v[0].buf; |
114 | mp *a = *(mp **)v[1].buf; |
115 | mp *b = *(mp **)v[2].buf; |
116 | mp *r = *(mp **)v[3].buf; |
117 | mp *mr; |
118 | int ok = 1; |
119 | |
120 | mpmont mm; |
121 | mpmont_create(&mm, m); |
122 | |
123 | mr = mpmont_exp(&mm, MP_NEW, a, b); |
124 | |
125 | if (!MP_EQ(mr, r)) { |
126 | fputs("\n*** montgomery modexp failed", stderr); |
127 | fputs("\n m = ", stderr); mp_writefile(m, stderr, 10); |
128 | fputs("\n a = ", stderr); mp_writefile(a, stderr, 10); |
129 | fputs("\n e = ", stderr); mp_writefile(b, stderr, 10); |
130 | fputs("\n r = ", stderr); mp_writefile(r, stderr, 10); |
131 | fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10); |
132 | fputc('\n', stderr); |
133 | ok = 0; |
134 | } |
135 | |
136 | MP_DROP(m); |
137 | MP_DROP(a); |
138 | MP_DROP(b); |
139 | MP_DROP(r); |
140 | MP_DROP(mr); |
141 | mpmont_destroy(&mm); |
142 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
143 | return ok; |
144 | } |
145 | |
146 | |
147 | static test_chunk tests[] = { |
148 | { "exp", texp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, |
149 | { 0, 0, { 0 } }, |
150 | }; |
151 | |
152 | int main(int argc, char *argv[]) |
153 | { |
154 | sub_init(); |
155 | test_run(argc, argv, tests, SRCDIR "/tests/mpmont"); |
156 | return (0); |
157 | } |
158 | |
159 | #endif |
160 | |
161 | /*----- That's all, folks -------------------------------------------------*/ |