cleanup: Big pile of whitespace fixes, all at once.
[u/mdw/catacomb] / mp-modexp.c
CommitLineData
ffec4880
MW
1/* -*-c-*-
2 *
3 * $Id$
4 *
5 * General-purpose modular exponentiation
6 *
7 * (c) 2006 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
ffec4880
MW
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.
45c0fd36 18 *
ffec4880
MW
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.
45c0fd36 23 *
ffec4880
MW
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 "mpbarrett.h"
34#include "mpmont.h"
35
36/*----- Main code ---------------------------------------------------------*/
37
38/* --- @mp_modexp@ --- *
39 *
40 * Arguments: @mp *d@ = fake destination
41 * @mp *x@ = base of exponentiation
42 * @mp *e@ = exponent
43 * @mp *n@ = modulus (must be positive)
44 *
45 * Returns: The value %$x^e \bmod n$%.
46 */
47
48mp *mp_modexp(mp *d, mp *x, mp *e, mp *n)
49{
50 if (MP_ODDP(n)) {
51 mpmont mm;
52 mpmont_create(&mm, n);
53 d = mpmont_exp(&mm, d, x, e);
54 mpmont_destroy(&mm);
55 } else {
56 mpbarrett mb;
57 mpbarrett_create(&mb, n);
58 d = mpbarrett_exp(&mb, d, x, e);
59 mpbarrett_destroy(&mb);
60 }
61 return (d);
62}
63
64/*----- Test rig ----------------------------------------------------------*/
65
66#ifdef TEST_RIG
67
68static int tmodexp(dstr *v)
69{
70 mp *a = *(mp **)v[0].buf;
71 mp *b = *(mp **)v[1].buf;
72 mp *m = *(mp **)v[2].buf;
73 mp *r = *(mp **)v[3].buf;
74 mp *mr;
75 int ok = 1;
76
77 mr = mp_modexp(MP_NEW, a, b, m);
78
79 if (!MP_EQ(mr, r)) {
80 fputs("\n*** modexp failed", stderr);
81 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
82 fputs("\n e = ", stderr); mp_writefile(b, stderr, 10);
83 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
84 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
85 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
86 fputc('\n', stderr);
87 ok = 0;
88 }
89
90 MP_DROP(m);
91 MP_DROP(a);
92 MP_DROP(b);
93 MP_DROP(r);
94 MP_DROP(mr);
95 assert(mparena_count(MPARENA_GLOBAL) == 0);
96 return ok;
97}
98
99static test_chunk tests[] = {
100 { "modexp", tmodexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
101 { 0, 0, { 0 } }
102};
103
104int main(int argc, char *argv[])
105{
106 sub_init();
107 test_run(argc, argv, tests, SRCDIR "/tests/mp");
108 return (0);
109}
110
111#endif
112
113/*----- That's all, folks -------------------------------------------------*/