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