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 ------------------------------------------------------*/
35 #include "field-guts.h"
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- Field operations --- */
42 static void fdestroy(field *ff) {
43 fctx_niceprime *f = (fctx_niceprime *)ff;
44 mpreduce_destroy(&f->r);
48 static mp *frand(field *ff, mp *d, grand *r) {
49 fctx_niceprime *f = (fctx_niceprime *)ff;
50 return (mprand_range(d, f->r.p, r, 0));
53 static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); }
55 static mp *fneg(field *ff, mp *d, mp *x) {
56 fctx_niceprime *f = (fctx_niceprime *)ff;
57 return (mp_sub(d, f->r.p, x));
60 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
61 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_add(d, x, y);
62 if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
63 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
67 static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
68 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_sub(d, x, y);
69 if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
70 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
74 static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
75 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_mul(d, x, y);
76 return (mpreduce_do(&f->r, d, d));
79 static mp *fsqr(field *ff, mp *d, mp *x) {
80 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_sqr(d, x);
81 return (mpreduce_do(&f->r, d, d));
84 static mp *finv(field *ff, mp *d, mp *x) {
85 fctx_niceprime *f = (fctx_niceprime *)ff;
86 d = mp_modinv(d, x, f->r.p);
90 static mp *freduce(field *ff, mp *d, mp *x) {
91 fctx_niceprime *f = (fctx_niceprime *)ff;
92 return (mpreduce_do(&f->r, d, x));
95 static mp *fsqrt(field *ff, mp *d, mp *x) {
96 fctx_niceprime *f = (fctx_niceprime *)ff;
97 return (mp_modsqrt(d, x, f->r.p));
100 static mp *fdbl(field *ff, mp *d, mp *x) {
101 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_lsl(d, x, 1);
102 if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
106 static mp *ftpl(field *ff, mp *d, mp *x) {
107 fctx_niceprime *f = (fctx_niceprime *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
108 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
109 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
113 static mp *fqdl(field *ff, mp *d, mp *x) {
114 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_lsl(d, x, 2);
115 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
119 static mp *fhlv(field *ff, mp *d, mp *x) {
120 fctx_niceprime *f = (fctx_niceprime *)ff;
121 if (MP_ZEROP(x)) { MP_COPY(x); MP_DROP(d); return (x); }
122 if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; }
123 return (mp_lsr(d, x, 1));
126 /* --- Field operations table --- */
128 static const field_ops fops = {
129 FTY_PRIME, "niceprime",
130 fdestroy, frand, field_stdsamep,
132 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
134 fdbl, ftpl, fqdl, fhlv
137 /* --- @field_niceprime@ --- *
139 * Arguments: @mp *p@ = the characteristic of the field
141 * Returns: A pointer to the field, or null.
143 * Use: Creates a field structure for a prime field of size %$p$%,
144 * using efficient reduction for nice primes.
147 field *field_niceprime(mp *p)
149 fctx_niceprime *f = CREATE(fctx_niceprime);
153 f->f.nbits = mp_bits(p);
154 f->f.noctets = (f->f.nbits + 7) >> 3;
155 if (mpreduce_create(&f->r, p)) {
163 /*----- That's all, folks -------------------------------------------------*/