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