math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / math / mpmont.h
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
d3409d5e 3 * Montgomery reduction
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
d3409d5e 9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
45c0fd36 16 *
d3409d5e 17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
45c0fd36 21 *
d3409d5e 22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
b3f05084 28#ifndef CATACOMB_MPMONT_H
29#define CATACOMB_MPMONT_H
d3409d5e 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
b3f05084 37#ifndef CATACOMB_MP_H
d3409d5e 38# include "mp.h"
39#endif
40
b3f05084 41/*----- Notes on Montgomery reduction -------------------------------------*
d3409d5e 42 *
43 * Given a little bit of precomputation, Montgomery reduction enables modular
44 * reductions of products to be calculated rather rapidly, without recourse
45 * to annoying things like division.
46 *
47 * Before starting, you need to do a little work. In particular, the
48 * following things need to be worked out:
49 *
b3f05084 50 * * %$m$%, which is the modulus you'll be working with. This must be odd,
51 * otherwise the whole thing doesn't work. You're better off using
52 * Barrett reduction if your modulus might be even.
d3409d5e 53 *
54 * * %$b$%, the radix of the number system you're in (here, it's
55 * @MPW_MAX + 1@).
56 *
57 * * %$-m^{-1} \bmod b$%, a useful number for the reduction step. (This
58 * means that the modulus mustn't be even. This shouldn't be a problem.)
59 *
60 * * %$R = b^n > m > b^{n - 1}$%, or at least %$\log_2 R$%.
61 *
62 * * %$R \bmod m$% and %$R^2 \bmod m$%, which are useful when doing
63 * calculations such as exponentiation.
64 *
65 * The result of a Montgomery reduction of %$x$% is %$x R^{-1} \bmod m$%,
66 * which doesn't look ever-so useful. The trick is to initially apply a
67 * factor of %$R$% to all of your numbers so that when you multiply and
b3f05084 68 * perform a Montgomery reduction you get %$(x R \cdot y R) R^{-1} \bmod m$%,
69 * which is just %$x y R \bmod m$%. Thanks to distributivity, even additions
d3409d5e 70 * and subtractions can be performed on numbers in this form -- the extra
71 * factor of %$R$% just runs through all the calculations until it's finally
72 * stripped out by a final reduction operation.
73 */
74
75/*----- Data structures ---------------------------------------------------*/
76
77/* --- A Montgomery reduction context --- */
78
79typedef struct mpmont {
80 mp *m; /* Modulus */
f5f35081 81 mp *mi; /* %$-m^{-1} \bmod R$% */
82 size_t n; /* %$\log_b R$% */
d3409d5e 83 mp *r, *r2; /* %$R \bmod m$%, %$R^2 \bmod m$% */
84} mpmont;
85
86/*----- Functions provided ------------------------------------------------*/
87
88/* --- @mpmont_create@ --- *
89 *
90 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
91 * @mp *m@ = modulus to use
92 *
f4535c64 93 * Returns: Zero on success, nonzero on error.
d3409d5e 94 *
95 * Use: Initializes a Montgomery reduction context ready for use.
b3f05084 96 * The argument @m@ must be a positive odd integer.
d3409d5e 97 */
98
f4535c64 99extern int mpmont_create(mpmont */*mm*/, mp */*m*/);
d3409d5e 100
2af1930e 101/* --- @mpmont_destroy@ --- *
102 *
103 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
104 *
105 * Returns: ---
106 *
107 * Use: Disposes of a context when it's no longer of any use to
108 * anyone.
109 */
110
111extern void mpmont_destroy(mpmont */*mm*/);
112
113/* --- @mpmont_reduce@ --- *
114 *
115 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
116 * @mp *d@ = destination
b3f05084 117 * @mp *a@ = source, assumed positive
2af1930e 118 *
119 * Returns: Result, %$a R^{-1} \bmod m$%.
120 */
121
b3f05084 122extern mp *mpmont_reduce(mpmont */*mm*/, mp */*d*/, mp */*a*/);
2af1930e 123
124/* --- @mpmont_mul@ --- *
125 *
126 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
127 * @mp *d@ = destination
b3f05084 128 * @mp *a, *b@ = sources, assumed positive
2af1930e 129 *
130 * Returns: Result, %$a b R^{-1} \bmod m$%.
131 */
132
b3f05084 133extern mp *mpmont_mul(mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*b*/);
2af1930e 134
135/* --- @mpmont_expr@ --- *
136 *
137 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 138 * @mp *d@ = fake destination
139 * @mp *a@ = base
140 * @mp *e@ = exponent
2af1930e 141 *
932f6ca7 142 * Returns: Result, %$(a R^{-1})^e R \bmod m$%. This is useful if
143 * further modular arithmetic is to be performed on the result.
2af1930e 144 */
145
b3f05084 146extern mp *mpmont_expr(mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*e*/);
2af1930e 147
148/* --- @mpmont_exp@ --- *
149 *
150 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 151 * @mp *d@ = fake destination
152 * @mp *a@ = base
153 * @mp *e@ = exponent
2af1930e 154 *
155 * Returns: Result, %$a^e \bmod m$%.
156 */
157
b3f05084 158extern mp *mpmont_exp(mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*e*/);
2af1930e 159
160/* --- @mpmont_mexpr@ --- *
161 *
162 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 163 * @mp *d@ = fake destination
34e4f738 164 * @const mp_expfactor *f@ = pointer to array of factors
2af1930e 165 * @size_t n@ = number of factors supplied
166 *
167 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
168 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
169 * is:
170 *
932f6ca7 171 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
172 *
173 *
174 * except that the %$g_i$% and result are in Montgomery form.
2af1930e 175 */
176
b3f05084 177extern mp *mpmont_mexpr(mpmont */*mm*/, mp */*d*/,
34e4f738 178 const mp_expfactor */*f*/, size_t /*n*/);
2af1930e 179
180/* --- @mpmont_mexp@ --- *
181 *
182 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 183 * @mp *d@ = fake destination
3beded37 184 * @const mp_expfactor *f@ = pointer to array of factors
2af1930e 185 * @size_t n@ = number of factors supplied
186 *
187 * Returns: Product of bases raised to exponents, all mod @m@.
188 *
189 * Use: Convenient interface over @mpmont_mexpr@.
190 */
191
b3f05084 192extern mp *mpmont_mexp(mpmont */*mm*/, mp */*d*/,
3beded37 193 const mp_expfactor */*f*/, size_t /*n*/);
2af1930e 194
d3409d5e 195/*----- That's all, folks -------------------------------------------------*/
196
197#ifdef __cplusplus
198 }
199#endif
200
201#endif