Initialize the register dumping machinery while testing assembler code.
[catacomb] / math / mpmont-exp.c
CommitLineData
34e4f738 1/* -*-c-*-
2 *
34e4f738 3 * Modular exponentiation with Montgomery 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 "mpmont.h"
32#include "mpmont-exp.h"
33
34/*----- Exponentiation ----------------------------------------------------*/
35
36/* --- @mpmont_expr@ --- *
37 *
295f4f90 38 * Arguments: @const mpmont *mm@ = pointer to Montgomery reduction context
34e4f738 39 * @mp *d@ = fake destination
40 * @mp *a@ = base
41 * @mp *e@ = exponent
42 *
43 * Returns: Result, %$(a R^{-1})^e R \bmod m$%.
44 */
45
295f4f90 46mp *mpmont_expr(const mpmont *mm, mp *d, mp *a, mp *e)
34e4f738 47{
48 mp *x = MP_COPY(mm->r);
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 = mpmont_reduce(mm, a, a);
58 a = mp_modinv(a, a, mm->m);
59 a = mpmont_mul(mm, a, a, mm->r2);
60 }
61 if (MP_LEN(e) < EXP_THRESH)
62 EXP_SIMPLE(x, a, e);
63 else
64 EXP_WINDOW(x, a, e);
65 }
34e4f738 66 mp_drop(d);
67 mp_drop(spare);
68 mp_drop(a);
69 return (x);
70}
71
72/* --- @mpmont_exp@ --- *
73 *
295f4f90 74 * Arguments: @const mpmont *mm@ = pointer to Montgomery reduction context
34e4f738 75 * @mp *d@ = fake destination
76 * @mp *a@ = base
77 * @mp *e@ = exponent
78 *
79 * Returns: Result, %$a^e \bmod m$%.
80 */
81
295f4f90 82mp *mpmont_exp(const mpmont *mm, mp *d, mp *a, mp *e)
34e4f738 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
416b8869
MW
96#ifdef ENABLE_ASM_DEBUG
97# include "regdump.h"
98#endif
99
34e4f738 100static int texp(dstr *v)
101{
102 mp *m = *(mp **)v[0].buf;
103 mp *a = *(mp **)v[1].buf;
104 mp *b = *(mp **)v[2].buf;
105 mp *r = *(mp **)v[3].buf;
106 mp *mr;
107 int ok = 1;
108
109 mpmont mm;
110 mpmont_create(&mm, m);
111
112 mr = mpmont_exp(&mm, MP_NEW, a, b);
113
114 if (!MP_EQ(mr, r)) {
115 fputs("\n*** montgomery modexp failed", stderr);
116 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
117 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
118 fputs("\n e = ", stderr); mp_writefile(b, stderr, 10);
119 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
120 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
121 fputc('\n', stderr);
122 ok = 0;
123 }
124
125 MP_DROP(m);
126 MP_DROP(a);
127 MP_DROP(b);
128 MP_DROP(r);
129 MP_DROP(mr);
130 mpmont_destroy(&mm);
131 assert(mparena_count(MPARENA_GLOBAL) == 0);
132 return ok;
133}
134
34e4f738 135static test_chunk tests[] = {
136 { "exp", texp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
137 { 0, 0, { 0 } },
138};
139
140int main(int argc, char *argv[])
141{
142 sub_init();
416b8869
MW
143#ifdef ENABLE_ASM_DEBUG
144 regdump_init();
145#endif
0f00dc4c 146 test_run(argc, argv, tests, SRCDIR "/t/mpmont");
34e4f738 147 return (0);
148}
149
150#endif
151
152/*----- That's all, folks -------------------------------------------------*/