chiark / gitweb /
Normal basis support (translates to poly basis internally). Rewrite
[catacomb] / f-niceprime.c
1 /* -*-c-*-
2  *
3  * $Id: f-niceprime.c,v 1.4 2004/04/01 21:28:41 mdw Exp $
4  *
5  * Prime fields with efficient reduction for special-form primes
6  *
7  * (c) 2004 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Catacomb.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
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.
37  *
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.)
44  *
45  * Revision 1.2  2004/03/27 17:54:11  mdw
46  * Standard curves and curve checking.
47  *
48  * Revision 1.1  2004/03/27 00:04:46  mdw
49  * Implement efficient reduction for pleasant-looking primes.
50  *
51  */
52
53 /*----- Header files ------------------------------------------------------*/
54
55 #include <mLib/sub.h>
56
57 #include "field.h"
58 #include "mpreduce.h"
59 #include "mprand.h"
60
61 /*----- Main code ---------------------------------------------------------*/
62
63 typedef struct fctx {
64   field f;
65   mpreduce r;
66 } fctx;
67
68 /* --- Field operations --- */
69
70 static void fdestroy(field *ff)
71   { fctx *f = (fctx *)ff; mpreduce_destroy(&f->r); DESTROY(f); }
72
73 static mp *frand(field *ff, mp *d, grand *r)
74   { fctx *f = (fctx *)ff; return (mprand_range(d, f->r.p, r, 0)); }
75
76 static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); }
77
78 static mp *fneg(field *ff, mp *d, mp *x)
79   { fctx *f = (fctx *)ff; return (mp_sub(d, f->r.p, x)); }
80
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);
85   return (d);
86 }
87
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);
92   return (d);
93 }
94
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));
98 }
99
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));
103 }
104
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); }
107
108 static mp *freduce(field *ff, mp *d, mp *x)
109   { fctx *f = (fctx *)ff; return (mpreduce_do(&f->r, d, x)); }
110
111 static mp *fsqrt(field *ff, mp *d, mp *x)
112   { fctx *f = (fctx *)ff; return (mp_modsqrt(d, x, f->r.p)); }
113
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);
117   return (d);
118 }
119
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);
124   return (d);
125 }
126
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);
130   return (d);
131 }
132
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));
138 }
139
140 /* --- Field operations table --- */
141
142 static field_ops fops = {
143   FTY_PRIME, "niceprime",
144   fdestroy, frand, field_stdsamep,
145   freduce, field_id,
146   fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
147   0,
148   fdbl, ftpl, fqdl, fhlv
149 };
150
151 /* --- @field_niceprime@ --- *
152  *
153  * Arguments:   @mp *p@ = the characteristic of the field
154  *
155  * Returns:     A pointer to the field.
156  *
157  * Use:         Creates a field structure for a prime field of size %$p$%,
158  *              using efficient reduction for nice primes.
159  */
160
161 field *field_niceprime(mp *p)
162 {
163   fctx *f = CREATE(fctx);
164   f->f.ops = &fops;
165   f->f.zero = MP_ZERO;
166   f->f.one = MP_ONE;
167   f->f.nbits = mp_bits(p);
168   f->f.noctets = (f->f.nbits + 7) >> 3;
169   mpreduce_create(&f->r, p);
170   f->f.m = f->r.p;
171   return (&f->f);
172 }
173
174 /*----- That's all, folks -------------------------------------------------*/