chiark / gitweb /
math/gfreduce.[ch]: Fix out-of-bounds memory access.
[catacomb] / math / gfreduce.h
1 /* -*-c-*-
2  *
3  * Reduction modulo sparse binary polynomials
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_GFREDUCE_H
29 #define CATACOMB_GFREDUCE_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stdio.h>
38
39 #ifndef CATACOMB_GF_H
40 #  include "gf.h"
41 #endif
42
43 /*----- Data structures ---------------------------------------------------*/
44
45 typedef struct gfreduce_instr {
46   unsigned op;                          /* Instruction opcode */
47   size_t arg;                           /* Immediate argument */
48 } gfreduce_instr;
49
50 enum {
51   GFRI_LOAD,                            /* Load @p[arg]@ */
52   GFRI_LSL,                             /* XOR with @w << arg@ */
53   GFRI_LSR,                             /* XOR with @w >> arg@ */
54   GFRI_STORE,                           /* Store @p[arg]@ */
55   GFRI_MAX
56 };
57
58 typedef struct gfreduce {
59   size_t lim;                           /* Word of degree bit */
60   mpw mask;                             /* Mask for degree word */
61   mp *p;                                /* Copy of the polynomial */
62   size_t in;                            /* Number of instruction words */
63   gfreduce_instr *iv;                   /* Vector of instructions */
64   gfreduce_instr *fiv;                  /* Final-pass instruction suffix */
65 } gfreduce;
66
67 /*----- Functions provided ------------------------------------------------*/
68
69 /* --- @gfreduce_create@ --- *
70  *
71  * Arguments:   @gfreduce *r@ = structure to fill in
72  *              @mp *x@ = a (hopefully sparse) polynomial
73  *
74  * Returns:     ---
75  *
76  * Use:         Initializes a context structure for reduction.
77  */
78
79 extern void gfreduce_create(gfreduce */*r*/, mp */*p*/);
80
81 /* --- @gfreduce_destroy@ --- *
82  *
83  * Arguments:   @gfreduce *r@ = structure to free
84  *
85  * Returns:     ---
86  *
87  * Use:         Reclaims the resources from a reduction context.
88  */
89
90 extern void gfreduce_destroy(gfreduce */*r*/);
91
92 /* --- @gfreduce_dump@ --- *
93  *
94  * Arguments:   @gfreduce *r@ = structure to dump
95  *              @FILE *fp@ = file to dump on
96  *
97  * Returns:     ---
98  *
99  * Use:         Dumps a reduction context.
100  */
101
102 extern void gfreduce_dump(gfreduce */*r*/, FILE */*fp*/);
103
104 /* --- @gfreduce_do@ --- *
105  *
106  * Arguments:   @gfreduce *r@ = reduction context
107  *              @mp *d@ = destination
108  *              @mp *x@ = source
109  *
110  * Returns:     Destination, @x@ reduced modulo the reduction poly.
111  */
112
113 extern mp *gfreduce_do(gfreduce */*r*/, mp */*d*/, mp */*x*/);
114
115 /* --- @gfreduce_sqrt@ --- *
116  *
117  * Arguments:   @gfreduce *r@ = pointer to reduction context
118  *              @mp *d@ = destination
119  *              @mp *x@ = some polynomial
120  *
121  * Returns:     The square root of @x@ modulo @r->p@, or null.
122  */
123
124 extern mp *gfreduce_sqrt(gfreduce */*r*/, mp */*d*/, mp */*x*/);
125
126 /* --- @gfreduce_trace@ --- *
127  *
128  * Arguments:   @gfreduce *r@ = pointer to reduction context
129  *              @mp *x@ = some polynomial
130  *
131  * Returns:     The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
132  *              if %$x \in \gf{2^m}$%).
133  */
134
135 extern int gfreduce_trace(gfreduce */*r*/, mp */*x*/);
136
137 /* --- @gfreduce_halftrace@ --- *
138  *
139  * Arguments:   @gfreduce *r@ = pointer to reduction context
140  *              @mp *d@ = destination
141  *              @mp *x@ = some polynomial
142  *
143  * Returns:     The half-trace of @x@.
144  *              (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
145  *              if %$x \in \gf{2^m}$% with %$m$% odd).
146  */
147
148 extern mp *gfreduce_halftrace(gfreduce */*r*/, mp */*d*/, mp */*x*/);
149
150 /* --- @gfreduce_quadsolve@ --- *
151  *
152  * Arguments:   @gfreduce *r@ = pointer to reduction context
153  *              @mp *d@ = destination
154  *              @mp *x@ = some polynomial
155  *
156  * Returns:     A polynomial @y@ such that %$y^2 + y = x$%, or null.
157  */
158
159 extern mp *gfreduce_quadsolve(gfreduce */*r*/, mp */*d*/, mp */*x*/);
160
161 /* --- @gfreduce_exp@ --- *
162  *
163  * Arguments:   @gfreduce *gr@ = pointer to reduction context
164  *              @mp *d@ = fake destination
165  *              @mp *a@ = base
166  *              @mp *e@ = exponent
167  *
168  * Returns:     Result, %$a^e \bmod m$%.
169  */
170
171 extern mp *gfreduce_exp(gfreduce */*gr*/, mp */*d*/, mp */*a*/, mp */*e*/);
172
173 /*----- That's all, folks -------------------------------------------------*/
174
175 #ifdef __cplusplus
176   }
177 #endif
178
179 #endif