3 * $Id: mpmont.h,v 1.2 1999/11/19 13:17:43 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.2 1999/11/19 13:17:43 mdw
34 * Add extra interface to exponentiation which returns a Montgomerized
35 * result. Add simultaneous exponentiation interface.
37 * Revision 1.1 1999/11/17 18:02:16 mdw
38 * New multiprecision integer arithmetic suite.
49 /*----- Header files ------------------------------------------------------*/
55 /*----- What's going on here? ---------------------------------------------*
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.
61 * Before starting, you need to do a little work. In particular, the
62 * following things need to be worked out:
64 * * %$m$%, which is the modulus you'll be working with.
66 * * %$b$%, the radix of the number system you're in (here, it's
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.)
72 * * %$R = b^n > m > b^{n - 1}$%, or at least %$\log_2 R$%.
74 * * %$R \bmod m$% and %$R^2 \bmod m$%, which are useful when doing
75 * calculations such as exponentiation.
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.
87 /*----- Data structures ---------------------------------------------------*/
89 /* --- A Montgomery reduction context --- */
91 typedef struct mpmont
{
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$% */
98 /* --- A base/exponent pair for @mpmont_mexp@ --- */
100 typedef struct mpmont_factor
{
105 /*----- Functions provided ------------------------------------------------*/
107 /* --- @mpmont_create@ --- *
109 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
110 * @mp *m@ = modulus to use
114 * Use: Initializes a Montgomery reduction context ready for use.
117 extern void mpmont_create(mpmont */
*mm*/
, mp */
*m*/
);
119 /* --- @mpmont_destroy@ --- *
121 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
125 * Use: Disposes of a context when it's no longer of any use to
129 extern void mpmont_destroy(mpmont */
*mm*/
);
131 /* --- @mpmont_reduce@ --- *
133 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
134 * @mp *d@ = destination
135 * @const mp *a@ = source, assumed positive
137 * Returns: Result, %$a R^{-1} \bmod m$%.
140 extern mp
*mpmont_reduce(mpmont */
*mm*/
, mp */
*d*/
, const mp */
*a*/
);
142 /* --- @mpmont_mul@ --- *
144 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
145 * @mp *d@ = destination
146 * @const mp *a, *b@ = sources, assumed positive
148 * Returns: Result, %$a b R^{-1} \bmod m$%.
151 extern mp
*mpmont_mul(mpmont */
*mm*/
, mp */
*d*/
,
152 const mp */
*a*/
, const mp */
*b*/
);
154 /* --- @mpmont_expr@ --- *
156 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
157 * @const mp *a@ = base
158 * @const mp *e@ = exponent
160 * Returns: Result, %$a^e R \bmod m$%. This is useful if further modular
161 * arithmetic is to be performed on the result.
164 extern mp
*mpmont_expr(mpmont */
*mm*/
, const mp */
*a*/
, const mp */
*e*/
);
166 /* --- @mpmont_exp@ --- *
168 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
169 * @const mp *a@ = base
170 * @const mp *e@ = exponent
172 * Returns: Result, %$a^e \bmod m$%.
175 extern mp
*mpmont_exp(mpmont */
*mm*/
, const mp */
*a*/
, const mp */
*e*/
);
177 /* --- @mpmont_mexpr@ --- *
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
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
187 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} R \bmod m$%
190 extern mp
*mpmont_mexpr(mpmont */
*mm*/
, mpmont_factor */
*f*/
, size_t /*n*/);
192 /* --- @mpmont_mexp@ --- *
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
198 * Returns: Product of bases raised to exponents, all mod @m@.
200 * Use: Convenient interface over @mpmont_mexpr@.
203 extern mp
*mpmont_mexp(mpmont */
*mm*/
, mpmont_factor */
*f*/
, size_t /*n*/);
205 /*----- That's all, folks -------------------------------------------------*/