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