progs/perftest.c: Use from Glibc syscall numbers.
[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 *
fa17e5dc
MW
57 * * %$m' = -m^{-1} \bmod b$%, a useful number for the reduction step.
58 * (This means that the modulus mustn't be even. This shouldn't be a
59 * problem.)
d3409d5e 60 *
61 * * %$R = b^n > m > b^{n - 1}$%, or at least %$\log_2 R$%.
62 *
63 * * %$R \bmod m$% and %$R^2 \bmod m$%, which are useful when doing
64 * calculations such as exponentiation.
65 *
fa17e5dc
MW
66 * Suppose that %$0 \le a_i \le (b^n + b^i - 1) m$% with %$a_i \equiv {}$%
67 * %$0 \pmod{b^i}$%. Let %$w_i = m' a_i/b^i \bmod b$%, and set %$a_{i+1} =
68 * a_i + b^i w_i m$%. Then obviously %$a_{i+1} \equiv {} $% %$a_i
69 * \pmod{m}$%, and less obviously %$a_{i+1}/b^i \equiv a_i/b^i + {}$% %$m m'
70 * a_i/b^i \equiv 0 \pmod{b}$% so %$a_{i+1} \equiv 0 \pmod{b^{i+1}}$%.
71 * Finally, we can bound %$a_{i+1} \le {}$% %$a_i + b^i (b - 1) m = {}$%
72 * %$a_i + (b^{i+1} - b^i) m \le (b^n + b^{i+1} - 1) m$%. As a result, if
73 * we're given some %a_0%, we can calculate %$a_n \equiv 0 \pmod{R}$%, with
74 * $%a_n \equiv a_0 \pmod{n}$%, i.e., %$a_n/R \equiv a_0 R^{-1} \pmod{m}$%;
75 * furthermore, if %$0 \le a_0 < m + b^n%$ then %$0 \le a_n/R < 2 m$%, so a
76 * fully reduced result can be obtained with a single conditional
77 * subtraction.
78 *
79 * The result of reduing %$a$% is then %$a R^{-1}$% \bmod m$%. This is
80 * actually rather useful for reducing products, if we run an extra factor of
81 * %$R$% through the calculation: the result of reducing the product of
82 * %$(x R)(y R) = x y R^2$% is then %$x y R \bmod m$%, preserving the running
83 * factor. Thanks to distributivity, additions and subtractions can be
84 * performed on numbers in this form -- the extra factor of %$R$% just runs
85 * through all the calculations until it's finally stripped out by a final
86 * reduction operation.
d3409d5e 87 */
88
89/*----- Data structures ---------------------------------------------------*/
90
91/* --- A Montgomery reduction context --- */
92
93typedef struct mpmont {
94 mp *m; /* Modulus */
f5f35081 95 mp *mi; /* %$-m^{-1} \bmod R$% */
96 size_t n; /* %$\log_b R$% */
d3409d5e 97 mp *r, *r2; /* %$R \bmod m$%, %$R^2 \bmod m$% */
98} mpmont;
99
100/*----- Functions provided ------------------------------------------------*/
101
102/* --- @mpmont_create@ --- *
103 *
104 * Arguments: @mpmont *mm@ = pointer to Montgomery reduction context
105 * @mp *m@ = modulus to use
106 *
f4535c64 107 * Returns: Zero on success, nonzero on error.
d3409d5e 108 *
109 * Use: Initializes a Montgomery reduction context ready for use.
b3f05084 110 * The argument @m@ must be a positive odd integer.
d3409d5e 111 */
112
f4535c64 113extern int mpmont_create(mpmont */*mm*/, mp */*m*/);
d3409d5e 114
2af1930e 115/* --- @mpmont_destroy@ --- *
116 *
117 * Arguments: @mpmont *mm@ = pointer to a Montgomery reduction context
118 *
119 * Returns: ---
120 *
121 * Use: Disposes of a context when it's no longer of any use to
122 * anyone.
123 */
124
125extern void mpmont_destroy(mpmont */*mm*/);
126
127/* --- @mpmont_reduce@ --- *
128 *
295f4f90 129 * Arguments: @const mpmont *mm@ = pointer to Montgomery reduction context
2af1930e 130 * @mp *d@ = destination
b3f05084 131 * @mp *a@ = source, assumed positive
2af1930e 132 *
133 * Returns: Result, %$a R^{-1} \bmod m$%.
134 */
135
295f4f90 136extern mp *mpmont_reduce(const mpmont */*mm*/, mp */*d*/, mp */*a*/);
2af1930e 137
138/* --- @mpmont_mul@ --- *
139 *
295f4f90 140 * Arguments: @const mpmont *mm@ = pointer to Montgomery reduction context
2af1930e 141 * @mp *d@ = destination
b3f05084 142 * @mp *a, *b@ = sources, assumed positive
2af1930e 143 *
144 * Returns: Result, %$a b R^{-1} \bmod m$%.
145 */
146
295f4f90 147extern mp *mpmont_mul(const mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*b*/);
2af1930e 148
149/* --- @mpmont_expr@ --- *
150 *
295f4f90 151 * Arguments: @const mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 152 * @mp *d@ = fake destination
153 * @mp *a@ = base
154 * @mp *e@ = exponent
2af1930e 155 *
932f6ca7 156 * Returns: Result, %$(a R^{-1})^e R \bmod m$%. This is useful if
157 * further modular arithmetic is to be performed on the result.
2af1930e 158 */
159
295f4f90
MW
160extern mp *mpmont_expr(const mpmont */*mm*/, mp */*d*/,
161 mp */*a*/, mp */*e*/);
2af1930e 162
163/* --- @mpmont_exp@ --- *
164 *
295f4f90 165 * Arguments: @const mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 166 * @mp *d@ = fake destination
167 * @mp *a@ = base
168 * @mp *e@ = exponent
2af1930e 169 *
170 * Returns: Result, %$a^e \bmod m$%.
171 */
172
295f4f90 173extern mp *mpmont_exp(const mpmont */*mm*/, mp */*d*/, mp */*a*/, mp */*e*/);
2af1930e 174
175/* --- @mpmont_mexpr@ --- *
176 *
295f4f90 177 * Arguments: @const mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 178 * @mp *d@ = fake destination
34e4f738 179 * @const mp_expfactor *f@ = pointer to array of factors
2af1930e 180 * @size_t n@ = number of factors supplied
181 *
182 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
183 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
184 * is:
185 *
932f6ca7 186 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
187 *
188 *
189 * except that the %$g_i$% and result are in Montgomery form.
2af1930e 190 */
191
295f4f90 192extern mp *mpmont_mexpr(const mpmont */*mm*/, mp */*d*/,
34e4f738 193 const mp_expfactor */*f*/, size_t /*n*/);
2af1930e 194
195/* --- @mpmont_mexp@ --- *
196 *
295f4f90 197 * Arguments: @const mpmont *mm@ = pointer to Montgomery reduction context
b3f05084 198 * @mp *d@ = fake destination
3beded37 199 * @const mp_expfactor *f@ = pointer to array of factors
2af1930e 200 * @size_t n@ = number of factors supplied
201 *
202 * Returns: Product of bases raised to exponents, all mod @m@.
203 *
204 * Use: Convenient interface over @mpmont_mexpr@.
205 */
206
295f4f90 207extern mp *mpmont_mexp(const mpmont */*mm*/, mp */*d*/,
3beded37 208 const mp_expfactor */*f*/, size_t /*n*/);
2af1930e 209
d3409d5e 210/*----- That's all, folks -------------------------------------------------*/
211
212#ifdef __cplusplus
213 }
214#endif
215
216#endif