chiark / gitweb /
Implement efficient reduction for pleasant-looking primes.
[catacomb] / mpreduce.h
1 /* -*-c-*-
2  *
3  * $Id: mpreduce.h,v 1.1 2004/03/27 00:04:46 mdw Exp $
4  *
5  * Efficient reduction modulo nice primes
6  *
7  * (c) 2004 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: mpreduce.h,v $
33  * Revision 1.1  2004/03/27 00:04:46  mdw
34  * Implement efficient reduction for pleasant-looking primes.
35  *
36  */
37
38 #ifndef CATACOMB_MPREDUCE_H
39 #define CATACOMB_MPREDUCE_H
40
41 #ifdef __cplusplus
42   extern "C" {
43 #endif
44
45 /*----- Header files ------------------------------------------------------*/
46
47 /*----- Data structures ---------------------------------------------------*/
48
49 typedef struct mpreduce_instr {
50   unsigned op;                          /* Instruction opcode */
51   size_t argx, argy;                    /* Immediate arguments */
52 } mpreduce_instr;
53
54 enum {
55   MPRI_ADD,                             /* Add @p@ offset by @x@ words */
56   MPRI_ADDLSL,                          /* Add @p << y@ offset by @x@ */
57   MPRI_SUB,                             /* Sub @p@ offset by @x@ words */
58   MPRI_SUBLSL,                          /* Sub @p << y@ offset by @x@ */
59   MPRI_MAX
60 };
61
62 typedef struct mpreduce {
63   size_t lim;                           /* Word containing top bit */
64   unsigned s;                           /* Shift for top word */
65   mp *p;                                /* Copy of the modulus */
66   size_t in;                            /* Number of instruction words */
67   mpreduce_instr *iv;                   /* Vector of instructions */
68 } mpreduce;
69
70 /*----- Functions provided ------------------------------------------------*/
71
72 /* --- @mpreduce_create@ --- *
73  *
74  * Arguments:   @gfreduce *r@ = structure to fill in
75  *              @mp *x@ = an integer
76  *
77  * Returns:     ---
78  *
79  * Use:         Initializes a context structure for reduction.
80  */
81
82 extern void mpreduce_create(mpreduce */*r*/, mp */*p*/);
83
84 /* --- @mpreduce_destroy@ --- *
85  *
86  * Arguments:   @mpreduce *r@ = structure to free
87  *
88  * Returns:     ---
89  *
90  * Use:         Reclaims the resources from a reduction context.
91  */
92
93 extern void mpreduce_destroy(mpreduce */*r*/);
94
95 /* --- @mpreduce_dump@ --- *
96  *
97  * Arguments:   @mpreduce *r@ = structure to dump
98  *              @FILE *fp@ = file to dump on
99  *
100  * Returns:     ---
101  *
102  * Use:         Dumps a reduction context.
103  */
104
105 extern void mpreduce_dump(mpreduce */*r*/, FILE */*fp*/);
106
107 /* --- @mpreduce_do@ --- *
108  *
109  * Arguments:   @mpreduce *r@ = reduction context
110  *              @mp *d@ = destination
111  *              @mp *x@ = source
112  *
113  * Returns:     Destination, @x@ reduced modulo the reduction poly.
114  */
115
116 extern mp *mpreduce_do(mpreduce */*r*/, mp */*d*/, mp */*x*/);
117
118 /* --- @mpreduce_exp@ --- *
119  *
120  * Arguments:   @mpreduce *mr@ = pointer to reduction context
121  *              @mp *d@ = fake destination
122  *              @mp *a@ = base
123  *              @mp *e@ = exponent
124  *
125  * Returns:     Result, %$a^e \bmod m$%.
126  */
127
128 extern mp *mpreduce_exp(mpreduce */*mr*/, mp */*d*/, mp */*a*/, mp */*e*/);
129
130 /*----- That's all, folks -------------------------------------------------*/
131
132 #ifdef __cplusplus
133   }
134 #endif
135
136 #endif