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