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