hashsum.c: Document `--progress' in the `--help' display.
[u/mdw/catacomb] / mpbarrett-exp.c
CommitLineData
34e4f738 1/* -*-c-*-
2 *
a69a3efd 3 * $Id$
34e4f738 4 *
5 * Modular exponentiation using Barrett reduction
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
34e4f738 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 *
34e4f738 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 *
34e4f738 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 "mpbarrett.h"
34#include "mpbarrett-exp.h"
35
36/*----- Main code ---------------------------------------------------------*/
37
38/* --- @mpbarrett_exp@ --- *
39 *
40 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
45c0fd36
MW
41 * @mp *d@ = fake destination
42 * @mp *a@ = base
43 * @mp *e@ = exponent
34e4f738 44 *
45c0fd36 45 * Returns: Result, %$a^e \bmod m$%.
34e4f738 46 */
47
48mp *mpbarrett_exp(mpbarrett *mb, mp *d, mp *a, mp *e)
49{
50 mp *x = MP_ONE;
51 mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW;
52
53 MP_COPY(a);
54 MP_SHRINK(e);
a69a3efd 55 if (MP_ZEROP(e))
34e4f738 56 ;
a69a3efd 57 else {
58 if (MP_NEGP(e))
59 a = mp_modinv(a, a, mb->m);
60 if (MP_LEN(e) < EXP_THRESH)
61 EXP_SIMPLE(x, a, e);
62 else
63 EXP_WINDOW(x, a, e);
64 }
34e4f738 65 mp_drop(d);
66 mp_drop(spare);
67 mp_drop(a);
68 return (x);
69}
70
71/*----- Test rig ----------------------------------------------------------*/
72
73#ifdef TEST_RIG
74
75static int vexp(dstr *v)
76{
77 mp *m = *(mp **)v[0].buf;
78 mp *a = *(mp **)v[1].buf;
79 mp *b = *(mp **)v[2].buf;
80 mp *r = *(mp **)v[3].buf;
81 mp *mr;
82 int ok = 1;
83
84 mpbarrett mb;
85 mpbarrett_create(&mb, m);
86
87 mr = mpbarrett_exp(&mb, MP_NEW, a, b);
88
89 if (!MP_EQ(mr, r)) {
90 fputs("\n*** barrett modexp failed", stderr);
91 fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
92 fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
93 fputs("\n e = ", stderr); mp_writefile(b, stderr, 10);
94 fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
95 fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
96 fputc('\n', stderr);
97 ok = 0;
98 }
99
100 mp_drop(m);
101 mp_drop(a);
102 mp_drop(b);
103 mp_drop(r);
104 mp_drop(mr);
105 mpbarrett_destroy(&mb);
106 assert(mparena_count(MPARENA_GLOBAL) == 0);
107 return ok;
108}
109
110static test_chunk tests[] = {
111 { "mpbarrett-exp", vexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
112 { 0, 0, { 0 } }
113};
114
115int main(int argc, char *argv[])
116{
117 sub_init();
118 test_run(argc, argv, tests, SRCDIR "/tests/mpbarrett");
119 return (0);
120}
121
122#endif
123
124/*----- That's all, folks -------------------------------------------------*/