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 #ifndef CATACOMB_MPMONT_H
31 #define CATACOMB_MPMONT_H
37 /*----- Header files ------------------------------------------------------*/
43 /*----- Notes on Montgomery reduction -------------------------------------*
45 * Given a little bit of precomputation, Montgomery reduction enables modular
46 * reductions of products to be calculated rather rapidly, without recourse
47 * to annoying things like division.
49 * Before starting, you need to do a little work. In particular, the
50 * following things need to be worked out:
52 * * %$m$%, which is the modulus you'll be working with. This must be odd,
53 * otherwise the whole thing doesn't work. You're better off using
54 * Barrett reduction if your modulus might be even.
56 * * %$b$%, the radix of the number system you're in (here, it's
59 * * %$-m^{-1} \bmod b$%, a useful number for the reduction step. (This
60 * means that the modulus mustn't be even. This shouldn't be a problem.)
62 * * %$R = b^n > m > b^{n - 1}$%, or at least %$\log_2 R$%.
64 * * %$R \bmod m$% and %$R^2 \bmod m$%, which are useful when doing
65 * calculations such as exponentiation.
67 * The result of a Montgomery reduction of %$x$% is %$x R^{-1} \bmod m$%,
68 * which doesn't look ever-so useful. The trick is to initially apply a
69 * factor of %$R$% to all of your numbers so that when you multiply and
70 * perform a Montgomery reduction you get %$(x R \cdot y R) R^{-1} \bmod m$%,
71 * which is just %$x y R \bmod m$%. Thanks to distributivity, even additions
72 * and subtractions can be performed on numbers in this form -- the extra
73 * factor of %$R$% just runs through all the calculations until it's finally
74 * stripped out by a final reduction operation.
77 /*----- Data structures ---------------------------------------------------*/
79 /* --- A Montgomery reduction context --- */
81 typedef struct mpmont
{
83 mp
*mi
; /* %$-m^{-1} \bmod R$% */
84 size_t n
; /* %$\log_b R$% */
85 mp
*r
, *r2
; /* %$R \bmod m$%, %$R^2 \bmod m$% */
88 /*----- Functions provided ------------------------------------------------*/
90 /* --- @mpmont_create@ --- *
92 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
93 * @mp *m@ = modulus to use
95 * Returns: Zero on success, nonzero on error.
97 * Use: Initializes a Montgomery reduction context ready for use.
98 * The argument @m@ must be a positive odd integer.
101 extern int mpmont_create(mpmont */
*mm*/
, mp */
*m*/
);
103 /* --- @mpmont_destroy@ --- *
105 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
109 * Use: Disposes of a context when it's no longer of any use to
113 extern void mpmont_destroy(mpmont */
*mm*/
);
115 /* --- @mpmont_reduce@ --- *
117 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
118 * @mp *d@ = destination
119 * @mp *a@ = source, assumed positive
121 * Returns: Result, %$a R^{-1} \bmod m$%.
124 extern mp
*mpmont_reduce(mpmont */
*mm*/
, mp */
*d*/
, mp */
*a*/
);
126 /* --- @mpmont_mul@ --- *
128 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
129 * @mp *d@ = destination
130 * @mp *a, *b@ = sources, assumed positive
132 * Returns: Result, %$a b R^{-1} \bmod m$%.
135 extern mp
*mpmont_mul(mpmont */
*mm*/
, mp */
*d*/
, mp */
*a*/
, mp */
*b*/
);
137 /* --- @mpmont_expr@ --- *
139 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
140 * @mp *d@ = fake destination
144 * Returns: Result, %$(a R^{-1})^e R \bmod m$%. This is useful if
145 * further modular arithmetic is to be performed on the result.
148 extern mp
*mpmont_expr(mpmont */
*mm*/
, mp */
*d*/
, mp */
*a*/
, mp */
*e*/
);
150 /* --- @mpmont_exp@ --- *
152 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
153 * @mp *d@ = fake destination
157 * Returns: Result, %$a^e \bmod m$%.
160 extern mp
*mpmont_exp(mpmont */
*mm*/
, mp */
*d*/
, mp */
*a*/
, mp */
*e*/
);
162 /* --- @mpmont_mexpr@ --- *
164 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
165 * @mp *d@ = fake destination
166 * @const mp_expfactor *f@ = pointer to array of factors
167 * @size_t n@ = number of factors supplied
169 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
170 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
173 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
176 * except that the %$g_i$% and result are in Montgomery form.
179 extern mp
*mpmont_mexpr(mpmont */
*mm*/
, mp */
*d*/
,
180 const mp_expfactor */
*f*/
, size_t /*n*/);
182 /* --- @mpmont_mexp@ --- *
184 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
185 * @mp *d@ = fake destination
186 * @const mp_expfactor *f@ = pointer to array of factors
187 * @size_t n@ = number of factors supplied
189 * Returns: Product of bases raised to exponents, all mod @m@.
191 * Use: Convenient interface over @mpmont_mexpr@.
194 extern mp
*mpmont_mexp(mpmont */
*mm*/
, mp */
*d*/
,
195 const mp_expfactor */
*f*/
, size_t /*n*/);
197 /*----- That's all, folks -------------------------------------------------*/