Make @const@-correct.
[u/mdw/catacomb] / mpmont.h
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
3beded37 3 * $Id: mpmont.h,v 1.6 2002/01/13 13:49:25 mdw Exp $
d3409d5e 4 *
5 * Montgomery reduction
6 *
7 * (c) 1999 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/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mpmont.h,v $
3beded37 33 * Revision 1.6 2002/01/13 13:49:25 mdw
34 * Make @const@-correct.
35 *
932f6ca7 36 * Revision 1.5 2001/06/16 13:00:04 mdw
37 * Moved @mpmont_factor@ to <mp.h>. Documented interface change to
38 * @mpmont_expr@ and @mpmont_mexpr@ -- the arguments are now in Montgomery
39 * form.
40 *
f5f35081 41 * Revision 1.4 1999/12/11 01:51:14 mdw
42 * Use a Karatsuba-based reduction for large moduli.
43 *
b3f05084 44 * Revision 1.3 1999/12/10 23:29:48 mdw
45 * Change header file guard names.
46 *
2af1930e 47 * Revision 1.2 1999/11/19 13:17:43 mdw
48 * Add extra interface to exponentiation which returns a Montgomerized
49 * result. Add simultaneous exponentiation interface.
50 *
d3409d5e 51 * Revision 1.1 1999/11/17 18:02:16 mdw
52 * New multiprecision integer arithmetic suite.
53 *
54 */
55
b3f05084 56#ifndef CATACOMB_MPMONT_H
57#define CATACOMB_MPMONT_H
d3409d5e 58
59#ifdef __cplusplus
60 extern "C" {
61#endif
62
63/*----- Header files ------------------------------------------------------*/
64
b3f05084 65#ifndef CATACOMB_MP_H
d3409d5e 66# include "mp.h"
67#endif
68
b3f05084 69/*----- Notes on Montgomery reduction -------------------------------------*
d3409d5e 70 *
71 * Given a little bit of precomputation, Montgomery reduction enables modular
72 * reductions of products to be calculated rather rapidly, without recourse
73 * to annoying things like division.
74 *
75 * Before starting, you need to do a little work. In particular, the
76 * following things need to be worked out:
77 *
b3f05084 78 * * %$m$%, which is the modulus you'll be working with. This must be odd,
79 * otherwise the whole thing doesn't work. You're better off using
80 * Barrett reduction if your modulus might be even.
d3409d5e 81 *
82 * * %$b$%, the radix of the number system you're in (here, it's
83 * @MPW_MAX + 1@).
84 *
85 * * %$-m^{-1} \bmod b$%, a useful number for the reduction step. (This
86 * means that the modulus mustn't be even. This shouldn't be a problem.)
87 *
88 * * %$R = b^n > m > b^{n - 1}$%, or at least %$\log_2 R$%.
89 *
90 * * %$R \bmod m$% and %$R^2 \bmod m$%, which are useful when doing
91 * calculations such as exponentiation.
92 *
93 * The result of a Montgomery reduction of %$x$% is %$x R^{-1} \bmod m$%,
94 * which doesn't look ever-so useful. The trick is to initially apply a
95 * factor of %$R$% to all of your numbers so that when you multiply and
b3f05084 96 * perform a Montgomery reduction you get %$(x R \cdot y R) R^{-1} \bmod m$%,
97 * which is just %$x y R \bmod m$%. Thanks to distributivity, even additions
d3409d5e 98 * and subtractions can be performed on numbers in this form -- the extra
99 * factor of %$R$% just runs through all the calculations until it's finally
100 * stripped out by a final reduction operation.
101 */
102
103/*----- Data structures ---------------------------------------------------*/
104
105/* --- A Montgomery reduction context --- */
106
107typedef struct mpmont {
108 mp *m; /* Modulus */
f5f35081 109 mp *mi; /* %$-m^{-1} \bmod R$% */
110 size_t n; /* %$\log_b R$% */
d3409d5e 111 mp *r, *r2; /* %$R \bmod m$%, %$R^2 \bmod m$% */
112} mpmont;
113
114/*----- Functions provided ------------------------------------------------*/
115
116/* --- @mpmont_create@ --- *
117 *
118 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
119 * @mp *m@ = modulus to use
120 *
121 * Returns: ---
122 *
123 * Use: Initializes a Montgomery reduction context ready for use.
b3f05084 124 * The argument @m@ must be a positive odd integer.
d3409d5e 125 */
126
127extern void mpmont_create(mpmont */*mm*/, mp */*m*/);
128
2af1930e 129/* --- @mpmont_destroy@ --- *
130 *
131 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
132 *
133 * Returns: ---
134 *
135 * Use: Disposes of a context when it's no longer of any use to
136 * anyone.
137 */
138
139extern void mpmont_destroy(mpmont */*mm*/);
140
141/* --- @mpmont_reduce@ --- *
142 *
143 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
144 * @mp *d@ = destination
b3f05084 145 * @mp *a@ = source, assumed positive
2af1930e 146 *
147 * Returns: Result, %$a R^{-1} \bmod m$%.
148 */
149
b3f05084 150extern mp *mpmont_reduce(mpmont */*mm*/, mp */*d*/, mp */*a*/);
2af1930e 151
152/* --- @mpmont_mul@ --- *
153 *
154 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
155 * @mp *d@ = destination
b3f05084 156 * @mp *a, *b@ = sources, assumed positive
2af1930e 157 *
158 * Returns: Result, %$a b R^{-1} \bmod m$%.
159 */
160
b3f05084 161extern mp *mpmont_mul(mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*b*/);
2af1930e 162
163/* --- @mpmont_expr@ --- *
164 *
165 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 166 * @mp *d@ = fake destination
167 * @mp *a@ = base
168 * @mp *e@ = exponent
2af1930e 169 *
932f6ca7 170 * Returns: Result, %$(a R^{-1})^e R \bmod m$%. This is useful if
171 * further modular arithmetic is to be performed on the result.
2af1930e 172 */
173
b3f05084 174extern mp *mpmont_expr(mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*e*/);
2af1930e 175
176/* --- @mpmont_exp@ --- *
177 *
178 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 179 * @mp *d@ = fake destination
180 * @mp *a@ = base
181 * @mp *e@ = exponent
2af1930e 182 *
183 * Returns: Result, %$a^e \bmod m$%.
184 */
185
b3f05084 186extern mp *mpmont_exp(mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*e*/);
2af1930e 187
188/* --- @mpmont_mexpr@ --- *
189 *
190 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 191 * @mp *d@ = fake destination
932f6ca7 192 * @mp_expfactor *f@ = pointer to array of factors
2af1930e 193 * @size_t n@ = number of factors supplied
194 *
195 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
196 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
197 * is:
198 *
932f6ca7 199 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
200 *
201 *
202 * except that the %$g_i$% and result are in Montgomery form.
2af1930e 203 */
204
b3f05084 205extern mp *mpmont_mexpr(mpmont */*mm*/, mp */*d*/,
932f6ca7 206 mp_expfactor */*f*/, size_t /*n*/);
2af1930e 207
208/* --- @mpmont_mexp@ --- *
209 *
210 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 211 * @mp *d@ = fake destination
3beded37 212 * @const mp_expfactor *f@ = pointer to array of factors
2af1930e 213 * @size_t n@ = number of factors supplied
214 *
215 * Returns: Product of bases raised to exponents, all mod @m@.
216 *
217 * Use: Convenient interface over @mpmont_mexpr@.
218 */
219
b3f05084 220extern mp *mpmont_mexp(mpmont */*mm*/, mp */*d*/,
3beded37 221 const mp_expfactor */*f*/, size_t /*n*/);
2af1930e 222
d3409d5e 223/*----- That's all, folks -------------------------------------------------*/
224
225#ifdef __cplusplus
226 }
227#endif
228
229#endif