3 * $Id: f-niceprime.c,v 1.1 2004/03/27 00:04:46 mdw Exp $
5 * Prime fields with efficient reduction for special-form 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: f-niceprime.c,v $
33 * Revision 1.1 2004/03/27 00:04:46 mdw
34 * Implement efficient reduction for pleasant-looking primes.
38 /*----- Header files ------------------------------------------------------*/
46 /*----- Data structures ---------------------------------------------------*/
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- Field operations --- */
57 static void fdestroy(field *ff)
60 mpreduce_destroy(&f->r);
64 static mp *frand(field *ff, mp *d, grand *r)
67 return (mprand_range(d, f->r.p, r, 0));
70 static int fzerop(field *ff, mp *x)
75 static mp *fneg(field *ff, mp *d, mp *x)
78 return (mp_sub(d, f->r.p, x));
81 static mp *fadd(field *ff, mp *d, mp *x, mp *y)
86 d = mp_add(d, d, f->r.p);
87 else if (MP_CMP(d, >, f->r.p))
88 d = mp_sub(d, d, f->r.p);
92 static mp *fsub(field *ff, mp *d, mp *x, mp *y)
97 d = mp_add(d, d, f->r.p);
98 else if (MP_CMP(d, >, f->r.p))
99 d = mp_sub(d, d, f->r.p);
103 static mp *fmul(field *ff, mp *d, mp *x, mp *y)
105 fctx *f = (fctx *)ff;
107 return (mpreduce_do(&f->r, d, d));
110 static mp *fsqr(field *ff, mp *d, mp *x)
112 fctx *f = (fctx *)ff;
114 return (mpreduce_do(&f->r, d, d));
117 static mp *finv(field *ff, mp *d, mp *x)
119 fctx *f = (fctx *)ff;
120 mp_gcd(0, 0, &d, f->r.p, x);
124 static mp *freduce(field *ff, mp *d, mp *x)
126 fctx *f = (fctx *)ff;
127 return (mpreduce_do(&f->r, d, x));
130 static mp *fsqrt(field *ff, mp *d, mp *x)
132 fctx *f = (fctx *)ff;
133 return (mp_modsqrt(d, x, f->r.p));
136 static mp *fdbl(field *ff, mp *d, mp *x)
138 fctx *f = (fctx *)ff;
140 if (MP_CMP(d, >, f->r.p))
141 d = mp_sub(d, d, f->r.p);
145 static mp *ftpl(field *ff, mp *d, mp *x)
147 fctx *f = (fctx *)ff;
148 MP_DEST(d, MP_LEN(x) + 1, x->f);
149 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
150 while (MP_CMP(d, >, f->r.p))
151 d = mp_sub(d, d, f->r.p);
155 static mp *fqdl(field *ff, mp *d, mp *x)
157 fctx *f = (fctx *)ff;
159 while (MP_CMP(d, >, f->r.p))
160 d = mp_sub(d, d, f->r.p);
164 static mp *fhlv(field *ff, mp *d, mp *x)
166 fctx *f = (fctx *)ff;
173 d = mp_add(d, x, f->r.p);
176 return (mp_lsr(d, x, 1));
179 /* --- Field operations table --- */
181 static field_ops fops = {
182 FTY_PRIME, "niceprime",
185 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
187 fdbl, ftpl, fqdl, fhlv
190 /* --- @field_niceprime@ --- *
192 * Arguments: @mp *p@ = the characteristic of the field
194 * Returns: A pointer to the field.
196 * Use: Creates a field structure for a prime field of size %$p$%,
197 * using efficient reduction for nice primes.
200 field *field_niceprime(mp *p)
202 fctx *f = CREATE(fctx);
206 mpreduce_create(&f->r, p);
210 /*----- That's all, folks -------------------------------------------------*/