math/mpreduce.[ch]: Extend the domain to all positive integers.
[u/mdw/catacomb] / math / mpreduce.h
1 /* -*-c-*-
2 *
3 * Efficient reduction modulo nice primes
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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
28 #ifndef CATACOMB_MPREDUCE_H
29 #define CATACOMB_MPREDUCE_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stdio.h>
38
39 #ifndef CATACOMB_MP_H
40 # include "mp.h"
41 #endif
42
43 /*----- Data structures ---------------------------------------------------*/
44
45 typedef struct mpreduce_instr {
46 unsigned op; /* Instruction opcode */
47 size_t argx, argy; /* Immediate arguments */
48 } mpreduce_instr;
49
50 enum {
51 MPRI_ADD, /* Add @p@ offset by @x@ words */
52 MPRI_ADDLSL, /* Add @p << y@ offset by @x@ */
53 MPRI_SUB, /* Sub @p@ offset by @x@ words */
54 MPRI_SUBLSL, /* Sub @p << y@ offset by @x@ */
55 MPRI_MAX
56 };
57
58 typedef struct mpreduce {
59 size_t lim; /* Word containing top bit */
60 unsigned s; /* Shift for top word */
61 mp *p; /* Copy of the modulus */
62 size_t in; /* Number of instruction words */
63 mpreduce_instr *iv; /* Vector of instructions */
64 } mpreduce;
65
66 /*----- Functions provided ------------------------------------------------*/
67
68 /* --- @mpreduce_create@ --- *
69 *
70 * Arguments: @gfreduce *r@ = structure to fill in
71 * @mp *x@ = an integer
72 *
73 * Returns: Zero if successful; nonzero on failure. The current
74 * algorithm always succeeds when given positive @x@. Earlier
75 * versions used to fail on particular kinds of integers, but
76 * this is guaranteed not to happen any more.
77 *
78 * Use: Initializes a context structure for reduction.
79 */
80
81 extern int mpreduce_create(mpreduce */*r*/, mp */*p*/);
82
83 /* --- @mpreduce_destroy@ --- *
84 *
85 * Arguments: @mpreduce *r@ = structure to free
86 *
87 * Returns: ---
88 *
89 * Use: Reclaims the resources from a reduction context.
90 */
91
92 extern void mpreduce_destroy(mpreduce */*r*/);
93
94 /* --- @mpreduce_dump@ --- *
95 *
96 * Arguments: @mpreduce *r@ = structure to dump
97 * @FILE *fp@ = file to dump on
98 *
99 * Returns: ---
100 *
101 * Use: Dumps a reduction context.
102 */
103
104 extern void mpreduce_dump(mpreduce */*r*/, FILE */*fp*/);
105
106 /* --- @mpreduce_do@ --- *
107 *
108 * Arguments: @mpreduce *r@ = reduction context
109 * @mp *d@ = destination
110 * @mp *x@ = source
111 *
112 * Returns: Destination, @x@ reduced modulo the reduction poly.
113 */
114
115 extern mp *mpreduce_do(mpreduce */*r*/, mp */*d*/, mp */*x*/);
116
117 /* --- @mpreduce_exp@ --- *
118 *
119 * Arguments: @mpreduce *mr@ = pointer to reduction context
120 * @mp *d@ = fake destination
121 * @mp *a@ = base
122 * @mp *e@ = exponent
123 *
124 * Returns: Result, %$a^e \bmod m$%.
125 */
126
127 extern mp *mpreduce_exp(mpreduce */*mr*/, mp */*d*/, mp */*a*/, mp */*e*/);
128
129 /*----- That's all, folks -------------------------------------------------*/
130
131 #ifdef __cplusplus
132 }
133 #endif
134
135 #endif