3 * $Id: mpreduce.h,v 1.1 2004/03/27 00:04:46 mdw Exp $
5 * Efficient reduction modulo nice primes
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: mpreduce.h,v $
33 * Revision 1.1 2004/03/27 00:04:46 mdw
34 * Implement efficient reduction for pleasant-looking primes.
38 #ifndef CATACOMB_MPREDUCE_H
39 #define CATACOMB_MPREDUCE_H
45 /*----- Header files ------------------------------------------------------*/
47 /*----- Data structures ---------------------------------------------------*/
49 typedef struct mpreduce_instr {
50 unsigned op; /* Instruction opcode */
51 size_t argx, argy; /* Immediate arguments */
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@ */
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 */
70 /*----- Functions provided ------------------------------------------------*/
72 /* --- @mpreduce_create@ --- *
74 * Arguments: @gfreduce *r@ = structure to fill in
75 * @mp *x@ = an integer
79 * Use: Initializes a context structure for reduction.
82 extern void mpreduce_create(mpreduce */*r*/, mp */*p*/);
84 /* --- @mpreduce_destroy@ --- *
86 * Arguments: @mpreduce *r@ = structure to free
90 * Use: Reclaims the resources from a reduction context.
93 extern void mpreduce_destroy(mpreduce */*r*/);
95 /* --- @mpreduce_dump@ --- *
97 * Arguments: @mpreduce *r@ = structure to dump
98 * @FILE *fp@ = file to dump on
102 * Use: Dumps a reduction context.
105 extern void mpreduce_dump(mpreduce */*r*/, FILE */*fp*/);
107 /* --- @mpreduce_do@ --- *
109 * Arguments: @mpreduce *r@ = reduction context
110 * @mp *d@ = destination
113 * Returns: Destination, @x@ reduced modulo the reduction poly.
116 extern mp *mpreduce_do(mpreduce */*r*/, mp */*d*/, mp */*x*/);
118 /* --- @mpreduce_exp@ --- *
120 * Arguments: @mpreduce *mr@ = pointer to reduction context
121 * @mp *d@ = fake destination
125 * Returns: Result, %$a^e \bmod m$%.
128 extern mp *mpreduce_exp(mpreduce */*mr*/, mp */*d*/, mp */*a*/, mp */*e*/);
130 /*----- That's all, folks -------------------------------------------------*/