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