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