3 * Prime fields with efficient reduction for special-form primes
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 #include "field-guts.h"
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- Field operations --- */
40 static void fdestroy(field *ff) {
41 fctx_niceprime *f = (fctx_niceprime *)ff;
42 mpreduce_destroy(&f->r);
46 static mp *frand(field *ff, mp *d, grand *r) {
47 fctx_niceprime *f = (fctx_niceprime *)ff;
48 return (mprand_range(d, f->r.p, r, 0));
51 static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); }
53 static mp *fneg(field *ff, mp *d, mp *x) {
54 fctx_niceprime *f = (fctx_niceprime *)ff;
55 if (MP_ZEROP(x)) { if (d != x) mp_drop(d); return (MP_COPY(x)); }
56 else return (mp_sub(d, f->r.p, x));
59 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
60 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_add(d, x, y);
61 if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
62 else if (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
66 static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
67 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_sub(d, x, y);
68 if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
69 else if (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
73 static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
74 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_mul(d, x, y);
75 return (mpreduce_do(&f->r, d, d));
78 static mp *fsqr(field *ff, mp *d, mp *x) {
79 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_sqr(d, x);
80 return (mpreduce_do(&f->r, d, d));
83 static mp *finv(field *ff, mp *d, mp *x) {
84 fctx_niceprime *f = (fctx_niceprime *)ff;
85 d = mp_modinv(d, x, f->r.p);
89 static mp *freduce(field *ff, mp *d, mp *x) {
90 fctx_niceprime *f = (fctx_niceprime *)ff;
91 return (mpreduce_do(&f->r, d, x));
94 static mp *fsqrt(field *ff, mp *d, mp *x) {
95 fctx_niceprime *f = (fctx_niceprime *)ff;
96 return (mp_modsqrt(d, x, f->r.p));
99 static mp *fdbl(field *ff, mp *d, mp *x) {
100 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_lsl(d, x, 1);
101 if (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
105 static mp *ftpl(field *ff, mp *d, mp *x) {
106 fctx_niceprime *f = (fctx_niceprime *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
107 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3); d->f &= ~MP_UNDEF;
108 while (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
112 static mp *fqdl(field *ff, mp *d, mp *x) {
113 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_lsl(d, x, 2);
114 while (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
118 static mp *fhlv(field *ff, mp *d, mp *x) {
119 fctx_niceprime *f = (fctx_niceprime *)ff;
120 if (MP_ZEROP(x)) { MP_COPY(x); MP_DROP(d); return (x); }
121 if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; }
122 return (mp_lsr(d, x, 1));
125 /* --- Field operations table --- */
127 static const field_ops fops = {
128 FTY_PRIME, "niceprime",
129 fdestroy, frand, field_stdsamep,
131 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
133 fdbl, ftpl, fqdl, fhlv
136 /* --- @field_niceprime@ --- *
138 * Arguments: @mp *p@ = the characteristic of the field
140 * Returns: A pointer to the field, or null.
142 * Use: Creates a field structure for a prime field of size %$p$%,
143 * using efficient reduction for nice primes.
146 field *field_niceprime(mp *p)
148 fctx_niceprime *f = CREATE(fctx_niceprime);
152 f->f.nbits = mp_bits(p);
153 f->f.noctets = (f->f.nbits + 7) >> 3;
154 if (mpreduce_create(&f->r, p)) {
163 /*----- That's all, folks -------------------------------------------------*/