3 * Efficient reduction modulo nice primes
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_MPREDUCE_H
29 #define CATACOMB_MPREDUCE_H
35 /*----- Header files ------------------------------------------------------*/
43 /*----- Data structures ---------------------------------------------------*/
45 typedef struct mpreduce_instr
{
46 unsigned op
; /* Instruction opcode */
47 size_t argx
, argy
; /* Immediate arguments */
51 MPRI_ADD
, /* Add @p@ offset by @x@ words */
52 MPRI_ADDLSL
, /* Add @p << y@ offset by @x@ */
53 MPRI_SUB
, /* Sub @p@ offset by @x@ words */
54 MPRI_SUBLSL
, /* Sub @p << y@ offset by @x@ */
58 typedef struct mpreduce
{
59 size_t lim
; /* Word containing top bit */
60 unsigned s
; /* Shift for top word */
61 mp
*p
; /* Copy of the modulus */
62 size_t in
; /* Number of instruction words */
63 mpreduce_instr
*iv
; /* Vector of instructions */
66 /*----- Functions provided ------------------------------------------------*/
68 /* --- @mpreduce_create@ --- *
70 * Arguments: @gfreduce *r@ = structure to fill in
71 * @mp *x@ = an integer
73 * Returns: Zero for success, nonzero on error.
75 * Use: Initializes a context structure for reduction.
78 extern int mpreduce_create(mpreduce */
*r*/
, mp */
*p*/
);
80 /* --- @mpreduce_destroy@ --- *
82 * Arguments: @mpreduce *r@ = structure to free
86 * Use: Reclaims the resources from a reduction context.
89 extern void mpreduce_destroy(mpreduce */
*r*/
);
91 /* --- @mpreduce_dump@ --- *
93 * Arguments: @mpreduce *r@ = structure to dump
94 * @FILE *fp@ = file to dump on
98 * Use: Dumps a reduction context.
101 extern void mpreduce_dump(mpreduce */
*r*/
, FILE */
*fp*/
);
103 /* --- @mpreduce_do@ --- *
105 * Arguments: @mpreduce *r@ = reduction context
106 * @mp *d@ = destination
109 * Returns: Destination, @x@ reduced modulo the reduction poly.
112 extern mp
*mpreduce_do(mpreduce */
*r*/
, mp */
*d*/
, mp */
*x*/
);
114 /* --- @mpreduce_exp@ --- *
116 * Arguments: @mpreduce *mr@ = pointer to reduction context
117 * @mp *d@ = fake destination
121 * Returns: Result, %$a^e \bmod m$%.
124 extern mp
*mpreduce_exp(mpreduce */
*mr*/
, mp */
*d*/
, mp */
*a*/
, mp */
*e*/
);
126 /*----- That's all, folks -------------------------------------------------*/