3 * Prime fields with Montgomery arithmetic
5 * (c) 2001 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 ------------------------------------------------------*/
34 #include "field-guts.h"
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- Field operations --- */
40 static void fdestroy(field *ff) {
41 fctx_prime *f = (fctx_prime *)ff;
42 mpmont_destroy(&f->mm);
46 static mp *frand(field *ff, mp *d, grand *r) {
47 fctx_prime *f = (fctx_prime *)ff;
48 return (mprand_range(d, f->mm.m, r, 0));
51 static mp *fin(field *ff, mp *d, mp *x) {
52 fctx_prime *f = (fctx_prime *)ff;
53 mp_div(0, &d, x, f->mm.m);
54 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
57 static mp *fout(field *ff, mp *d, mp *x) {
58 fctx_prime *f = (fctx_prime *)ff;
59 return (mpmont_reduce(&f->mm, d, x));
62 static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); }
64 static mp *fneg(field *ff, mp *d, mp *x) {
65 fctx_prime *f = (fctx_prime *)ff;
66 if (MP_ZEROP(x)) { if (d != x) mp_drop(d); return (MP_COPY(x)); }
67 else return (mp_sub(d, f->mm.m, x));
70 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
71 fctx_prime *f = (fctx_prime *)ff; d = mp_add(d, x, y);
72 if (MP_NEGP(d)) d = mp_add(d, d, f->mm.m);
73 else if (MP_CMP(d, >=, f->mm.m)) d = mp_sub(d, d, f->mm.m);
77 static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
78 fctx_prime *f = (fctx_prime *)ff; d = mp_sub(d, x, y);
79 if (MP_NEGP(d)) d = mp_add(d, d, f->mm.m);
80 else if (MP_CMP(d, >=, f->mm.m)) d = mp_sub(d, d, f->mm.m);
84 static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
85 fctx_prime *f = (fctx_prime *)ff;
86 return (mpmont_mul(&f->mm, d, x, y));
89 static mp *fsqr(field *ff, mp *d, mp *x) {
90 fctx_prime *f = (fctx_prime *)ff; d = mp_sqr(d, x);
91 return (mpmont_reduce(&f->mm, d, d));
94 static mp *finv(field *ff, mp *d, mp *x) {
95 fctx_prime *f = (fctx_prime *)ff; d = mpmont_reduce(&f->mm, d, x);
96 d = mp_modinv(d, d, f->mm.m); return (mpmont_mul(&f->mm, d, d, f->mm.r2));
99 static mp *freduce(field *ff, mp *d, mp *x) {
100 fctx_prime *f = (fctx_prime *)ff;
101 mp_div(0, &d, x, f->mm.m);
105 static mp *fsqrt(field *ff, mp *d, mp *x) {
106 fctx_prime *f = (fctx_prime *)ff; d = mpmont_reduce(&f->mm, d, x);
107 d = mp_modsqrt(d, d, f->mm.m); if (!d) return (d);
108 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
111 static mp *fdbl(field *ff, mp *d, mp *x) {
112 fctx_prime *f = (fctx_prime *)ff; d = mp_lsl(d, x, 1);
113 if (MP_CMP(d, >=, f->mm.m)) d = mp_sub(d, d, f->mm.m);
117 static mp *ftpl(field *ff, mp *d, mp *x) {
118 fctx_prime *f = (fctx_prime *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
119 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3); d->f &= ~MP_UNDEF;
120 while (MP_CMP(d, >=, f->mm.m)) d = mp_sub(d, d, f->mm.m);
124 static mp *fqdl(field *ff, mp *d, mp *x) {
125 fctx_prime *f = (fctx_prime *)ff; d = mp_lsl(d, x, 2);
126 while (MP_CMP(d, >=, f->mm.m)) d = mp_sub(d, d, f->mm.m);
130 static mp *fhlv(field *ff, mp *d, mp *x) {
131 fctx_prime *f = (fctx_prime *)ff;
132 if (MP_ZEROP(x)) { MP_COPY(x); MP_DROP(d); return (x); }
133 if (x->v[0] & 1) { d = mp_add(d, x, f->mm.m); x = d; }
134 return (mp_lsr(d, x, 1));
137 /* --- Field operations table --- */
139 static const field_ops fops = {
141 fdestroy, frand, field_stdsamep,
143 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
145 fdbl, ftpl, fqdl, fhlv
148 /* --- @field_prime@ --- *
150 * Arguments: @mp *p@ = the characteristic of the field
152 * Returns: A pointer to the field or null.
154 * Use: Creates a field structure for a prime field of size %$p$%,
155 * using Montgomery reduction for arithmetic.
158 field *field_prime(mp *p)
162 f = CREATE(fctx_prime);
164 if (mpmont_create(&f->mm, p)) {
171 f->f.nbits = mp_bits(p);
172 f->f.noctets = (f->f.nbits + 7) >> 3;
177 /*----- That's all, folks -------------------------------------------------*/