3 * Reduction modulo sparse binary polynomials
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 #ifndef CATACOMB_GFREDUCE_H
29 #define CATACOMB_GFREDUCE_H
35 /*----- Header files ------------------------------------------------------*/
43 /*----- Data structures ---------------------------------------------------*/
45 typedef struct gfreduce_instr {
46 unsigned op; /* Instruction opcode */
47 size_t arg; /* Immediate argument */
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]@ */
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 */
67 /*----- Functions provided ------------------------------------------------*/
69 /* --- @gfreduce_create@ --- *
71 * Arguments: @gfreduce *r@ = structure to fill in
72 * @mp *x@ = a (hopefully sparse) polynomial
76 * Use: Initializes a context structure for reduction.
79 extern void gfreduce_create(gfreduce */*r*/, mp */*p*/);
81 /* --- @gfreduce_destroy@ --- *
83 * Arguments: @gfreduce *r@ = structure to free
87 * Use: Reclaims the resources from a reduction context.
90 extern void gfreduce_destroy(gfreduce */*r*/);
92 /* --- @gfreduce_dump@ --- *
94 * Arguments: @const gfreduce *r@ = structure to dump
95 * @FILE *fp@ = file to dump on
99 * Use: Dumps a reduction context.
102 extern void gfreduce_dump(const gfreduce */*r*/, FILE */*fp*/);
104 /* --- @gfreduce_do@ --- *
106 * Arguments: @const gfreduce *r@ = reduction context
107 * @mp *d@ = destination
110 * Returns: Destination, @x@ reduced modulo the reduction poly.
113 extern mp *gfreduce_do(const gfreduce */*r*/, mp */*d*/, mp */*x*/);
115 /* --- @gfreduce_sqrt@ --- *
117 * Arguments: @const gfreduce *r@ = pointer to reduction context
118 * @mp *d@ = destination
119 * @mp *x@ = some polynomial
121 * Returns: The square root of @x@ modulo @r->p@, or null.
124 extern mp *gfreduce_sqrt(const gfreduce */*r*/, mp */*d*/, mp */*x*/);
126 /* --- @gfreduce_trace@ --- *
128 * Arguments: @const gfreduce *r@ = pointer to reduction context
129 * @mp *x@ = some polynomial
131 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
132 * if %$x \in \gf{2^m}$%). Since the trace is invariant under
133 * the Frobenius automorphism (i.e., %$\Tr(x)^2 = \Tr(x)$%), it
134 * must be an element of the base field, i.e., %$\gf{2}$%, and
135 * we only need a single bit to represent it.
138 extern int gfreduce_trace(const gfreduce */*r*/, mp */*x*/);
140 /* --- @gfreduce_halftrace@ --- *
142 * Arguments: @const gfreduce *r@ = pointer to reduction context
143 * @mp *d@ = destination
144 * @mp *x@ = some polynomial
146 * Returns: The half-trace of @x@.
147 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
148 * if %$x \in \gf{2^m}$% with %$m$% odd).
151 extern mp *gfreduce_halftrace(const gfreduce */*r*/, mp */*d*/, mp */*x*/);
153 /* --- @gfreduce_quadsolve@ --- *
155 * Arguments: @const gfreduce *r@ = pointer to reduction context
156 * @mp *d@ = destination
157 * @mp *x@ = some polynomial
159 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
161 * Use: Solves quadratic equations in a field with characteristic 2.
162 * Suppose we have an equation %$y^2 + A y + B = 0$% where
163 * %$A \ne 0$%. (If %$A = 0$% then %$y = \sqrt{B}$% and you
164 * want @gfreduce_sqrt@ instead.) Use this function to solve
165 * %$z^2 + z = B/A^2$%; then set %$y = A z$%, since
166 * %$y^2 + y = A^2 z^2 + A^2 z = A^2 (z^2 + z) = B$% as
169 * The two roots are %$z$% and %$z + 1$%; this function always
170 * returns the one with zero scalar coefficient.
173 extern mp *gfreduce_quadsolve(const gfreduce */*r*/, mp */*d*/, mp */*x*/);
175 /* --- @gfreduce_exp@ --- *
177 * Arguments: @const gfreduce *gr@ = pointer to reduction context
178 * @mp *d@ = fake destination
182 * Returns: Result, %$a^e \bmod m$%.
185 extern mp *gfreduce_exp(const gfreduce */*gr*/, mp */*d*/,
186 mp */*a*/, mp */*e*/);
188 /*----- That's all, folks -------------------------------------------------*/