General utilities cleanup. Add signature support to catcrypt. Throw in
[u/mdw/catacomb] / mpmont-exp.c
CommitLineData
34e4f738 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: mpmont-exp.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
34e4f738 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
34e4f738 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
48mp *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 (e->f & MP_NEG) {
34e4f738 56 a = mpmont_reduce(mm, a, a);
b817bfc6 57 a = mp_modinv(a, a, mm->m);
34e4f738 58 a = mpmont_mul(mm, a, a, mm->r2);
34e4f738 59 }
60 if (MP_LEN(e) == 0)
61 ;
62 else if (MP_LEN(e) < EXP_THRESH)
63 EXP_SIMPLE(x, a, e);
64 else
65 EXP_WINDOW(x, a, e);
66 mp_drop(d);
67 mp_drop(spare);
68 mp_drop(a);
69 return (x);
70}
71
72/* --- @mpmont_exp@ --- *
73 *
74 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
75 * @mp *d@ = fake destination
76 * @mp *a@ = base
77 * @mp *e@ = exponent
78 *
79 * Returns: Result, %$a^e \bmod m$%.
80 */
81
82mp *mpmont_exp(mpmont *mm, mp *d, mp *a, mp *e)
83{
84 e = MP_COPY(e);
85 d = mpmont_mul(mm, d, a, mm->r2);
86 d = mpmont_expr(mm, d, d, e);
87 d = mpmont_reduce(mm, d, d);
88 MP_DROP(e);
89 return (d);
90}
91
92/*----- Test rig ----------------------------------------------------------*/
93
94#ifdef TEST_RIG
95
96static int texp(dstr *v)
97{
98 mp *m = *(mp **)v[0].buf;
99 mp *a = *(mp **)v[1].buf;
100 mp *b = *(mp **)v[2].buf;
101 mp *r = *(mp **)v[3].buf;
102 mp *mr;
103 int ok = 1;
104
105 mpmont mm;
106 mpmont_create(&mm, m);
107
108 mr = mpmont_exp(&mm, MP_NEW, a, b);
109
110 if (!MP_EQ(mr, r)) {
111 fputs("\n*** montgomery modexp failed", stderr);
112 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
113 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
114 fputs("\n e = ", stderr); mp_writefile(b, stderr, 10);
115 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
116 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
117 fputc('\n', stderr);
118 ok = 0;
119 }
120
121 MP_DROP(m);
122 MP_DROP(a);
123 MP_DROP(b);
124 MP_DROP(r);
125 MP_DROP(mr);
126 mpmont_destroy(&mm);
127 assert(mparena_count(MPARENA_GLOBAL) == 0);
128 return ok;
129}
130
131
132static test_chunk tests[] = {
133 { "exp", texp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
134 { 0, 0, { 0 } },
135};
136
137int main(int argc, char *argv[])
138{
139 sub_init();
140 test_run(argc, argv, tests, SRCDIR "/tests/mpmont");
141 return (0);
142}
143
144#endif
145
146/*----- That's all, folks -------------------------------------------------*/