3 * $Id: mpmont.h,v 1.4 1999/12/11 01:51:14 mdw Exp $
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 1999/12/11 01:51:14 mdw
34 * Use a Karatsuba-based reduction for large moduli.
36 * Revision 1.3 1999/12/10 23:29:48 mdw
37 * Change header file guard names.
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.
43 * Revision 1.1 1999/11/17 18:02:16 mdw
44 * New multiprecision integer arithmetic suite.
48 #ifndef CATACOMB_MPMONT_H
49 #define CATACOMB_MPMONT_H
55 /*----- Header files ------------------------------------------------------*/
61 /*----- Notes on Montgomery reduction -------------------------------------*
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.
67 * Before starting, you need to do a little work. In particular, the
68 * following things need to be worked out:
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.
74 * * %$b$%, the radix of the number system you're in (here, it's
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.)
80 * * %$R = b^n > m > b^{n - 1}$%, or at least %$\log_2 R$%.
82 * * %$R \bmod m$% and %$R^2 \bmod m$%, which are useful when doing
83 * calculations such as exponentiation.
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.
95 /*----- Data structures ---------------------------------------------------*/
97 /* --- A Montgomery reduction context --- */
99 typedef struct mpmont
{
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$% */
106 /* --- A base/exponent pair for @mpmont_mexp@ --- */
108 typedef struct mpmont_factor
{
113 /*----- Functions provided ------------------------------------------------*/
115 /* --- @mpmont_create@ --- *
117 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
118 * @mp *m@ = modulus to use
122 * Use: Initializes a Montgomery reduction context ready for use.
123 * The argument @m@ must be a positive odd integer.
126 extern void mpmont_create(mpmont */
*mm*/
, mp */
*m*/
);
128 /* --- @mpmont_destroy@ --- *
130 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
134 * Use: Disposes of a context when it's no longer of any use to
138 extern void mpmont_destroy(mpmont */
*mm*/
);
140 /* --- @mpmont_reduce@ --- *
142 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
143 * @mp *d@ = destination
144 * @mp *a@ = source, assumed positive
146 * Returns: Result, %$a R^{-1} \bmod m$%.
149 extern mp
*mpmont_reduce(mpmont */
*mm*/
, mp */
*d*/
, mp */
*a*/
);
151 /* --- @mpmont_mul@ --- *
153 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
154 * @mp *d@ = destination
155 * @mp *a, *b@ = sources, assumed positive
157 * Returns: Result, %$a b R^{-1} \bmod m$%.
160 extern mp
*mpmont_mul(mpmont */
*mm*/
, mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
162 /* --- @mpmont_expr@ --- *
164 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
165 * @mp *d@ = fake destination
169 * Returns: Result, %$a^e R \bmod m$%. This is useful if further modular
170 * arithmetic is to be performed on the result.
173 extern mp
*mpmont_expr(mpmont */
*mm*/
, mp */
*d*/
, mp */
*a*/
, mp */
*e*/
);
175 /* --- @mpmont_exp@ --- *
177 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
178 * @mp *d@ = fake destination
182 * Returns: Result, %$a^e \bmod m$%.
185 extern mp
*mpmont_exp(mpmont */
*mm*/
, mp */
*d*/
, mp */
*a*/
, mp */
*e*/
);
187 /* --- @mpmont_mexpr@ --- *
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
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
198 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} R \bmod m$%
201 extern mp
*mpmont_mexpr(mpmont */
*mm*/
, mp */
*d*/
,
202 mpmont_factor */
*f*/
, size_t /*n*/);
204 /* --- @mpmont_mexp@ --- *
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
211 * Returns: Product of bases raised to exponents, all mod @m@.
213 * Use: Convenient interface over @mpmont_mexpr@.
216 extern mp
*mpmont_mexp(mpmont */
*mm*/
, mp */
*d*/
,
217 mpmont_factor */
*f*/
, size_t /*n*/);
219 /*----- That's all, folks -------------------------------------------------*/