New function and example program computes Fibonacci numbers fairly fast.
[u/mdw/catacomb] / mpmont.h
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
f4535c64 3 * $Id$
d3409d5e 4 *
5 * Montgomery reduction
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
d3409d5e 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.
45c0fd36 18 *
d3409d5e 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.
45c0fd36 23 *
d3409d5e 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
b3f05084 30#ifndef CATACOMB_MPMONT_H
31#define CATACOMB_MPMONT_H
d3409d5e 32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Header files ------------------------------------------------------*/
38
b3f05084 39#ifndef CATACOMB_MP_H
d3409d5e 40# include "mp.h"
41#endif
42
b3f05084 43/*----- Notes on Montgomery reduction -------------------------------------*
d3409d5e 44 *
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.
48 *
49 * Before starting, you need to do a little work. In particular, the
50 * following things need to be worked out:
51 *
b3f05084 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.
d3409d5e 55 *
56 * * %$b$%, the radix of the number system you're in (here, it's
57 * @MPW_MAX + 1@).
58 *
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.)
61 *
62 * * %$R = b^n > m > b^{n - 1}$%, or at least %$\log_2 R$%.
63 *
64 * * %$R \bmod m$% and %$R^2 \bmod m$%, which are useful when doing
65 * calculations such as exponentiation.
66 *
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
b3f05084 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
d3409d5e 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.
75 */
76
77/*----- Data structures ---------------------------------------------------*/
78
79/* --- A Montgomery reduction context --- */
80
81typedef struct mpmont {
82 mp *m; /* Modulus */
f5f35081 83 mp *mi; /* %$-m^{-1} \bmod R$% */
84 size_t n; /* %$\log_b R$% */
d3409d5e 85 mp *r, *r2; /* %$R \bmod m$%, %$R^2 \bmod m$% */
86} mpmont;
87
88/*----- Functions provided ------------------------------------------------*/
89
90/* --- @mpmont_create@ --- *
91 *
92 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
93 * @mp *m@ = modulus to use
94 *
f4535c64 95 * Returns: Zero on success, nonzero on error.
d3409d5e 96 *
97 * Use: Initializes a Montgomery reduction context ready for use.
b3f05084 98 * The argument @m@ must be a positive odd integer.
d3409d5e 99 */
100
f4535c64 101extern int mpmont_create(mpmont */*mm*/, mp */*m*/);
d3409d5e 102
2af1930e 103/* --- @mpmont_destroy@ --- *
104 *
105 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
106 *
107 * Returns: ---
108 *
109 * Use: Disposes of a context when it's no longer of any use to
110 * anyone.
111 */
112
113extern void mpmont_destroy(mpmont */*mm*/);
114
115/* --- @mpmont_reduce@ --- *
116 *
117 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
118 * @mp *d@ = destination
b3f05084 119 * @mp *a@ = source, assumed positive
2af1930e 120 *
121 * Returns: Result, %$a R^{-1} \bmod m$%.
122 */
123
b3f05084 124extern mp *mpmont_reduce(mpmont */*mm*/, mp */*d*/, mp */*a*/);
2af1930e 125
126/* --- @mpmont_mul@ --- *
127 *
128 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
129 * @mp *d@ = destination
b3f05084 130 * @mp *a, *b@ = sources, assumed positive
2af1930e 131 *
132 * Returns: Result, %$a b R^{-1} \bmod m$%.
133 */
134
b3f05084 135extern mp *mpmont_mul(mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*b*/);
2af1930e 136
137/* --- @mpmont_expr@ --- *
138 *
139 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 140 * @mp *d@ = fake destination
141 * @mp *a@ = base
142 * @mp *e@ = exponent
2af1930e 143 *
932f6ca7 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.
2af1930e 146 */
147
b3f05084 148extern mp *mpmont_expr(mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*e*/);
2af1930e 149
150/* --- @mpmont_exp@ --- *
151 *
152 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 153 * @mp *d@ = fake destination
154 * @mp *a@ = base
155 * @mp *e@ = exponent
2af1930e 156 *
157 * Returns: Result, %$a^e \bmod m$%.
158 */
159
b3f05084 160extern mp *mpmont_exp(mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*e*/);
2af1930e 161
162/* --- @mpmont_mexpr@ --- *
163 *
164 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 165 * @mp *d@ = fake destination
34e4f738 166 * @const mp_expfactor *f@ = pointer to array of factors
2af1930e 167 * @size_t n@ = number of factors supplied
168 *
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
171 * is:
172 *
932f6ca7 173 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
174 *
175 *
176 * except that the %$g_i$% and result are in Montgomery form.
2af1930e 177 */
178
b3f05084 179extern mp *mpmont_mexpr(mpmont */*mm*/, mp */*d*/,
34e4f738 180 const mp_expfactor */*f*/, size_t /*n*/);
2af1930e 181
182/* --- @mpmont_mexp@ --- *
183 *
184 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 185 * @mp *d@ = fake destination
3beded37 186 * @const mp_expfactor *f@ = pointer to array of factors
2af1930e 187 * @size_t n@ = number of factors supplied
188 *
189 * Returns: Product of bases raised to exponents, all mod @m@.
190 *
191 * Use: Convenient interface over @mpmont_mexpr@.
192 */
193
b3f05084 194extern mp *mpmont_mexp(mpmont */*mm*/, mp */*d*/,
3beded37 195 const mp_expfactor */*f*/, size_t /*n*/);
2af1930e 196
d3409d5e 197/*----- That's all, folks -------------------------------------------------*/
198
199#ifdef __cplusplus
200 }
201#endif
202
203#endif