3 * $Id: f-niceprime.c,v 1.2 2004/03/27 17:54:11 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.2 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
36 * Revision 1.1 2004/03/27 00:04:46 mdw
37 * Implement efficient reduction for pleasant-looking primes.
41 /*----- Header files ------------------------------------------------------*/
49 /*----- Data structures ---------------------------------------------------*/
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- Field operations --- */
60 static void fdestroy(field *ff)
63 mpreduce_destroy(&f->r);
67 static mp *frand(field *ff, mp *d, grand *r)
70 return (mprand_range(d, f->r.p, r, 0));
73 static int fzerop(field *ff, mp *x)
78 static mp *fneg(field *ff, mp *d, mp *x)
81 return (mp_sub(d, f->r.p, x));
84 static mp *fadd(field *ff, mp *d, mp *x, mp *y)
89 d = mp_add(d, d, f->r.p);
90 else if (MP_CMP(d, >, f->r.p))
91 d = mp_sub(d, d, f->r.p);
95 static mp *fsub(field *ff, mp *d, mp *x, mp *y)
100 d = mp_add(d, d, f->r.p);
101 else if (MP_CMP(d, >, f->r.p))
102 d = mp_sub(d, d, f->r.p);
106 static mp *fmul(field *ff, mp *d, mp *x, mp *y)
108 fctx *f = (fctx *)ff;
110 return (mpreduce_do(&f->r, d, d));
113 static mp *fsqr(field *ff, mp *d, mp *x)
115 fctx *f = (fctx *)ff;
117 return (mpreduce_do(&f->r, d, d));
120 static mp *finv(field *ff, mp *d, mp *x)
122 fctx *f = (fctx *)ff;
123 mp_gcd(0, 0, &d, f->r.p, x);
127 static mp *freduce(field *ff, mp *d, mp *x)
129 fctx *f = (fctx *)ff;
130 return (mpreduce_do(&f->r, d, x));
133 static mp *fsqrt(field *ff, mp *d, mp *x)
135 fctx *f = (fctx *)ff;
136 return (mp_modsqrt(d, x, f->r.p));
139 static mp *fdbl(field *ff, mp *d, mp *x)
141 fctx *f = (fctx *)ff;
143 if (MP_CMP(d, >, f->r.p))
144 d = mp_sub(d, d, f->r.p);
148 static mp *ftpl(field *ff, mp *d, mp *x)
150 fctx *f = (fctx *)ff;
151 MP_DEST(d, MP_LEN(x) + 1, x->f);
152 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
153 while (MP_CMP(d, >, f->r.p))
154 d = mp_sub(d, d, f->r.p);
158 static mp *fqdl(field *ff, mp *d, mp *x)
160 fctx *f = (fctx *)ff;
162 while (MP_CMP(d, >, f->r.p))
163 d = mp_sub(d, d, f->r.p);
167 static mp *fhlv(field *ff, mp *d, mp *x)
169 fctx *f = (fctx *)ff;
176 d = mp_add(d, x, f->r.p);
179 return (mp_lsr(d, x, 1));
182 /* --- Field operations table --- */
184 static field_ops fops = {
185 FTY_PRIME, "niceprime",
188 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
190 fdbl, ftpl, fqdl, fhlv
193 /* --- @field_niceprime@ --- *
195 * Arguments: @mp *p@ = the characteristic of the field
197 * Returns: A pointer to the field.
199 * Use: Creates a field structure for a prime field of size %$p$%,
200 * using efficient reduction for nice primes.
203 field *field_niceprime(mp *p)
205 fctx *f = CREATE(fctx);
209 f->f.nbits = mp_bits(p);
210 f->f.noctets = (f->f.nbits + 7) >> 3;
211 mpreduce_create(&f->r, p);
216 /*----- That's all, folks -------------------------------------------------*/