chiark / gitweb /
Rearrange the file tree.
[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 /*----- Data structures ---------------------------------------------------*/
38
39 typedef struct mpreduce_instr {
40   unsigned op;                          /* Instruction opcode */
41   size_t argx, argy;                    /* Immediate arguments */
42 } mpreduce_instr;
43
44 enum {
45   MPRI_ADD,                             /* Add @p@ offset by @x@ words */
46   MPRI_ADDLSL,                          /* Add @p << y@ offset by @x@ */
47   MPRI_SUB,                             /* Sub @p@ offset by @x@ words */
48   MPRI_SUBLSL,                          /* Sub @p << y@ offset by @x@ */
49   MPRI_MAX
50 };
51
52 typedef struct mpreduce {
53   size_t lim;                           /* Word containing top bit */
54   unsigned s;                           /* Shift for top word */
55   mp *p;                                /* Copy of the modulus */
56   size_t in;                            /* Number of instruction words */
57   mpreduce_instr *iv;                   /* Vector of instructions */
58 } mpreduce;
59
60 /*----- Functions provided ------------------------------------------------*/
61
62 /* --- @mpreduce_create@ --- *
63  *
64  * Arguments:   @gfreduce *r@ = structure to fill in
65  *              @mp *x@ = an integer
66  *
67  * Returns:     Zero for success, nonzero on error.
68  *
69  * Use:         Initializes a context structure for reduction.
70  */
71
72 extern int mpreduce_create(mpreduce */*r*/, mp */*p*/);
73
74 /* --- @mpreduce_destroy@ --- *
75  *
76  * Arguments:   @mpreduce *r@ = structure to free
77  *
78  * Returns:     ---
79  *
80  * Use:         Reclaims the resources from a reduction context.
81  */
82
83 extern void mpreduce_destroy(mpreduce */*r*/);
84
85 /* --- @mpreduce_dump@ --- *
86  *
87  * Arguments:   @mpreduce *r@ = structure to dump
88  *              @FILE *fp@ = file to dump on
89  *
90  * Returns:     ---
91  *
92  * Use:         Dumps a reduction context.
93  */
94
95 extern void mpreduce_dump(mpreduce */*r*/, FILE */*fp*/);
96
97 /* --- @mpreduce_do@ --- *
98  *
99  * Arguments:   @mpreduce *r@ = reduction context
100  *              @mp *d@ = destination
101  *              @mp *x@ = source
102  *
103  * Returns:     Destination, @x@ reduced modulo the reduction poly.
104  */
105
106 extern mp *mpreduce_do(mpreduce */*r*/, mp */*d*/, mp */*x*/);
107
108 /* --- @mpreduce_exp@ --- *
109  *
110  * Arguments:   @mpreduce *mr@ = pointer to reduction context
111  *              @mp *d@ = fake destination
112  *              @mp *a@ = base
113  *              @mp *e@ = exponent
114  *
115  * Returns:     Result, %$a^e \bmod m$%.
116  */
117
118 extern mp *mpreduce_exp(mpreduce */*mr*/, mp */*d*/, mp */*a*/, mp */*e*/);
119
120 /*----- That's all, folks -------------------------------------------------*/
121
122 #ifdef __cplusplus
123   }
124 #endif
125
126 #endif