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