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 #ifndef CATACOMB_MPREDUCE_H
31 #define CATACOMB_MPREDUCE_H
37 /*----- Header files ------------------------------------------------------*/
39 /*----- Data structures ---------------------------------------------------*/
41 typedef struct mpreduce_instr
{
42 unsigned op
; /* Instruction opcode */
43 size_t argx
, argy
; /* Immediate arguments */
47 MPRI_ADD
, /* Add @p@ offset by @x@ words */
48 MPRI_ADDLSL
, /* Add @p << y@ offset by @x@ */
49 MPRI_SUB
, /* Sub @p@ offset by @x@ words */
50 MPRI_SUBLSL
, /* Sub @p << y@ offset by @x@ */
54 typedef struct mpreduce
{
55 size_t lim
; /* Word containing top bit */
56 unsigned s
; /* Shift for top word */
57 mp
*p
; /* Copy of the modulus */
58 size_t in
; /* Number of instruction words */
59 mpreduce_instr
*iv
; /* Vector of instructions */
62 /*----- Functions provided ------------------------------------------------*/
64 /* --- @mpreduce_create@ --- *
66 * Arguments: @gfreduce *r@ = structure to fill in
67 * @mp *x@ = an integer
69 * Returns: Zero for success, nonzero on error.
71 * Use: Initializes a context structure for reduction.
74 extern int mpreduce_create(mpreduce */
*r*/
, mp */
*p*/
);
76 /* --- @mpreduce_destroy@ --- *
78 * Arguments: @mpreduce *r@ = structure to free
82 * Use: Reclaims the resources from a reduction context.
85 extern void mpreduce_destroy(mpreduce */
*r*/
);
87 /* --- @mpreduce_dump@ --- *
89 * Arguments: @mpreduce *r@ = structure to dump
90 * @FILE *fp@ = file to dump on
94 * Use: Dumps a reduction context.
97 extern void mpreduce_dump(mpreduce */
*r*/
, FILE */
*fp*/
);
99 /* --- @mpreduce_do@ --- *
101 * Arguments: @mpreduce *r@ = reduction context
102 * @mp *d@ = destination
105 * Returns: Destination, @x@ reduced modulo the reduction poly.
108 extern mp
*mpreduce_do(mpreduce */
*r*/
, mp */
*d*/
, mp */
*x*/
);
110 /* --- @mpreduce_exp@ --- *
112 * Arguments: @mpreduce *mr@ = pointer to reduction context
113 * @mp *d@ = fake destination
117 * Returns: Result, %$a^e \bmod m$%.
120 extern mp
*mpreduce_exp(mpreduce */
*mr*/
, mp */
*d*/
, mp */
*a*/
, mp */
*e*/
);
122 /*----- That's all, folks -------------------------------------------------*/