3 * $Id: f-niceprime.c,v 1.6 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
45 /* --- Field operations --- */
47 static void fdestroy(field *ff)
48 { fctx *f = (fctx *)ff; mpreduce_destroy(&f->r); DESTROY(f); }
50 static mp *frand(field *ff, mp *d, grand *r)
51 { fctx *f = (fctx *)ff; return (mprand_range(d, f->r.p, r, 0)); }
53 static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); }
55 static mp *fneg(field *ff, mp *d, mp *x)
56 { fctx *f = (fctx *)ff; return (mp_sub(d, f->r.p, x)); }
58 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
59 fctx *f = (fctx *)ff; d = mp_add(d, x, y);
60 if (d->f & MP_NEG) d = mp_add(d, d, f->r.p);
61 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
65 static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
66 fctx *f = (fctx *)ff; d = mp_sub(d, x, y);
67 if (d->f & MP_NEG) d = mp_add(d, d, f->r.p);
68 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
72 static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
73 fctx *f = (fctx *)ff; d = mp_mul(d, x, y);
74 return (mpreduce_do(&f->r, d, d));
77 static mp *fsqr(field *ff, mp *d, mp *x) {
78 fctx *f = (fctx *)ff; d = mp_sqr(d, x);
79 return (mpreduce_do(&f->r, d, d));
82 static mp *finv(field *ff, mp *d, mp *x)
83 { fctx *f = (fctx *)ff; d = mp_modinv(d, x, f->r.p); return (d); }
85 static mp *freduce(field *ff, mp *d, mp *x)
86 { fctx *f = (fctx *)ff; return (mpreduce_do(&f->r, d, x)); }
88 static mp *fsqrt(field *ff, mp *d, mp *x)
89 { fctx *f = (fctx *)ff; return (mp_modsqrt(d, x, f->r.p)); }
91 static mp *fdbl(field *ff, mp *d, mp *x) {
92 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 1);
93 if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
97 static mp *ftpl(field *ff, mp *d, mp *x) {
98 fctx *f = (fctx *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
99 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
100 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
104 static mp *fqdl(field *ff, mp *d, mp *x) {
105 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 2);
106 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
110 static mp *fhlv(field *ff, mp *d, mp *x) {
111 fctx *f = (fctx *)ff;
112 if (!MP_LEN(x)) { MP_COPY(x); MP_DROP(d); return (x); }
113 if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; }
114 return (mp_lsr(d, x, 1));
117 /* --- Field operations table --- */
119 static const field_ops fops = {
120 FTY_PRIME, "niceprime",
121 fdestroy, frand, field_stdsamep,
123 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
125 fdbl, ftpl, fqdl, fhlv
128 /* --- @field_niceprime@ --- *
130 * Arguments: @mp *p@ = the characteristic of the field
132 * Returns: A pointer to the field.
134 * Use: Creates a field structure for a prime field of size %$p$%,
135 * using efficient reduction for nice primes.
138 field *field_niceprime(mp *p)
140 fctx *f = CREATE(fctx);
144 f->f.nbits = mp_bits(p);
145 f->f.noctets = (f->f.nbits + 7) >> 3;
146 mpreduce_create(&f->r, p);
151 /*----- That's all, folks -------------------------------------------------*/