eaec41da134b5c23590e22432a64e4a1b583eb27
[u/mdw/catacomb] / mpbarrett.h
1 /* -*-c-*-
2 *
3 * $Id: mpbarrett.h,v 1.3 2001/06/16 12:58:47 mdw Exp $
4 *
5 * Barrett modular reduction
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
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.
18 *
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.
23 *
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
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mpbarrett.h,v $
33 * Revision 1.3 2001/06/16 12:58:47 mdw
34 * Added simultaneous exponentiation with Barrett reduction.
35 *
36 * Revision 1.2 2000/10/08 12:03:44 mdw
37 * (mpbarrett_reduce): Cope with negative numbers.
38 *
39 * Revision 1.1 1999/12/10 23:22:00 mdw
40 * Barrett reduction support: works with even moduli.
41 *
42 */
43
44 /*----- Notes on Barrett reduction ----------------------------------------*
45 *
46 * Barrett reduction is a technique for computing modular residues. Unlike
47 * Montgomery reduction, it doesn't have restrictions on the modulus (except
48 * that it be positive) and doesn't confuse matters by putting an extra
49 * factor all the way through your computation.
50 *
51 * It's useful for slightly less heavy-duty work than Montgomery reduction
52 * because the precomputation phase is rather simpler, involving a single
53 * division operation.
54 *
55 * Sometimes it's useful to exponentiate modulo an even number, so there's a
56 * modexp routine provided which uses Barrett reduction rather than
57 * Montgomery reduction. This is handy when you're working on indices in an
58 * even-order cyclic group or something.
59 */
60
61 #ifndef CATACOMB_MPBARRETT_H
62 #define CATACOMB_MPBARRETT_H
63
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67
68 /*----- Header files ------------------------------------------------------*/
69
70 #ifndef CATACOMB_MP_H
71 # include "mp.h"
72 #endif
73
74 /*----- Data structures ---------------------------------------------------*/
75
76 typedef struct mpbarrett {
77 mp *m;
78 mp *mu;
79 size_t k;
80 } mpbarrett;
81
82 /*----- Functions provided ------------------------------------------------*/
83
84 /* --- @mpbarrett_create@ --- *
85 *
86 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
87 * @mp *m@ = modulus to work to
88 *
89 *
90 * Returns: ---
91 *
92 * Use: Initializes a Barrett reduction context ready for use.
93 */
94
95 extern void mpbarrett_create(mpbarrett */*mb*/, mp */*m*/);
96
97 /* --- @mpbarrett_destroy@ --- *
98 *
99 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
100 *
101 * Returns: ---
102 *
103 * Use: Destroys a Barrett reduction context releasing any resources
104 * claimed.
105 */
106
107 extern void mpbarrett_destroy(mpbarrett */*mb*/);
108
109 /* --- @mpbarrett_reduce@ --- *
110 *
111 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
112 * @mp *d@ = destination for result
113 * @mp *m@ = number to reduce
114 *
115 * Returns: The residue of @m@ modulo the number in the reduction
116 * context.
117 *
118 * Use: Performs an efficient modular reduction.
119 */
120
121 extern mp *mpbarrett_reduce(mpbarrett */*mb*/, mp */*d*/, mp */*m*/);
122
123 /* --- @mpbarrett_exp@ --- *
124 *
125 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
126 * @mp *d@ = fake destination
127 * @mp *a@ = base
128 * @mp *e@ = exponent
129 *
130 * Returns: Result, %$a^e \bmod m$%.
131 */
132
133 extern mp *mpbarrett_exp(mpbarrett */*mb*/, mp */*d*/, mp */*a*/, mp */*e*/);
134
135 /* --- @mpbarrett_mexp@ --- *
136 *
137 * Arguments: @mpbarrett *mb@ = pointer to Barrett reduction context
138 * @mp *d@ = fake destination
139 * @mp_expfactor *f@ = pointer to array of factors
140 * @size_t n@ = number of factors supplied
141 *
142 * Returns: If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
143 * exponents are %$e_0, e_1, \ldots, e_{n-1}$% then the result
144 * is:
145 *
146 * %$g_0^{e_0} g_1^{e_1} \ldots g_{n-1}^{e_{n-1}} \bmod m$%
147 */
148
149 extern mp *mpbarrett_mexp(mpbarrett */*mb*/, mp */*d*/,
150 mp_expfactor */*f*/, size_t /*n*/);
151
152 /*----- That's all, folks -------------------------------------------------*/
153
154 #ifdef __cplusplus
155 }
156 #endif
157
158 #endif