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