3 * $Id: f-niceprime.c,v 1.4 2004/04/01 21:28:41 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.4 2004/04/01 21:28:41 mdw
34 * Normal basis support (translates to poly basis internally). Rewrite
35 * EC and prime group table generators in awk, so that they can reuse data
36 * for repeated constants.
38 * Revision 1.3 2004/04/01 12:50:09 mdw
39 * Add cyclic group abstraction, with test code. Separate off exponentation
40 * functions for better static linking. Fix a buttload of bugs on the way.
41 * Generally ensure that negative exponents do inversion correctly. Add
42 * table of standard prime-field subgroups. (Binary field subgroups are
43 * currently unimplemented but easy to add if anyone ever finds a good one.)
45 * Revision 1.2 2004/03/27 17:54:11 mdw
46 * Standard curves and curve checking.
48 * Revision 1.1 2004/03/27 00:04:46 mdw
49 * Implement efficient reduction for pleasant-looking primes.
53 /*----- Header files ------------------------------------------------------*/
61 /*----- Main code ---------------------------------------------------------*/
68 /* --- Field operations --- */
70 static void fdestroy(field *ff)
71 { fctx *f = (fctx *)ff; mpreduce_destroy(&f->r); DESTROY(f); }
73 static mp *frand(field *ff, mp *d, grand *r)
74 { fctx *f = (fctx *)ff; return (mprand_range(d, f->r.p, r, 0)); }
76 static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); }
78 static mp *fneg(field *ff, mp *d, mp *x)
79 { fctx *f = (fctx *)ff; return (mp_sub(d, f->r.p, x)); }
81 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
82 fctx *f = (fctx *)ff; d = mp_add(d, x, y);
83 if (d->f & MP_NEG) d = mp_add(d, d, f->r.p);
84 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
88 static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
89 fctx *f = (fctx *)ff; d = mp_sub(d, x, y);
90 if (d->f & MP_NEG) d = mp_add(d, d, f->r.p);
91 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
95 static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
96 fctx *f = (fctx *)ff; d = mp_mul(d, x, y);
97 return (mpreduce_do(&f->r, d, d));
100 static mp *fsqr(field *ff, mp *d, mp *x) {
101 fctx *f = (fctx *)ff; d = mp_sqr(d, x);
102 return (mpreduce_do(&f->r, d, d));
105 static mp *finv(field *ff, mp *d, mp *x)
106 { fctx *f = (fctx *)ff; mp_gcd(0, 0, &d, f->r.p, x); return (d); }
108 static mp *freduce(field *ff, mp *d, mp *x)
109 { fctx *f = (fctx *)ff; return (mpreduce_do(&f->r, d, x)); }
111 static mp *fsqrt(field *ff, mp *d, mp *x)
112 { fctx *f = (fctx *)ff; return (mp_modsqrt(d, x, f->r.p)); }
114 static mp *fdbl(field *ff, mp *d, mp *x) {
115 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 1);
116 if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
120 static mp *ftpl(field *ff, mp *d, mp *x) {
121 fctx *f = (fctx *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
122 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
123 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
127 static mp *fqdl(field *ff, mp *d, mp *x) {
128 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 2);
129 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
133 static mp *fhlv(field *ff, mp *d, mp *x) {
134 fctx *f = (fctx *)ff;
135 if (!MP_LEN(x)) { MP_COPY(x); MP_DROP(d); return (x); }
136 if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; }
137 return (mp_lsr(d, x, 1));
140 /* --- Field operations table --- */
142 static field_ops fops = {
143 FTY_PRIME, "niceprime",
144 fdestroy, frand, field_stdsamep,
146 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
148 fdbl, ftpl, fqdl, fhlv
151 /* --- @field_niceprime@ --- *
153 * Arguments: @mp *p@ = the characteristic of the field
155 * Returns: A pointer to the field.
157 * Use: Creates a field structure for a prime field of size %$p$%,
158 * using efficient reduction for nice primes.
161 field *field_niceprime(mp *p)
163 fctx *f = CREATE(fctx);
167 f->f.nbits = mp_bits(p);
168 f->f.noctets = (f->f.nbits + 7) >> 3;
169 mpreduce_create(&f->r, p);
174 /*----- That's all, folks -------------------------------------------------*/