chiark / gitweb /
Miscellaneous constification.
[catacomb] / f-niceprime.c
1 /* -*-c-*-
2  *
3  * $Id: f-niceprime.c,v 1.5 2004/04/02 01:03:49 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.5  2004/04/02 01:03:49  mdw
34  * Miscellaneous constification.
35  *
36  * Revision 1.4  2004/04/01 21:28:41  mdw
37  * Normal basis support (translates to poly basis internally).  Rewrite
38  * EC and prime group table generators in awk, so that they can reuse data
39  * for repeated constants.
40  *
41  * Revision 1.3  2004/04/01 12:50:09  mdw
42  * Add cyclic group abstraction, with test code.  Separate off exponentation
43  * functions for better static linking.  Fix a buttload of bugs on the way.
44  * Generally ensure that negative exponents do inversion correctly.  Add
45  * table of standard prime-field subgroups.  (Binary field subgroups are
46  * currently unimplemented but easy to add if anyone ever finds a good one.)
47  *
48  * Revision 1.2  2004/03/27 17:54:11  mdw
49  * Standard curves and curve checking.
50  *
51  * Revision 1.1  2004/03/27 00:04:46  mdw
52  * Implement efficient reduction for pleasant-looking primes.
53  *
54  */
55
56 /*----- Header files ------------------------------------------------------*/
57
58 #include <mLib/sub.h>
59
60 #include "field.h"
61 #include "mpreduce.h"
62 #include "mprand.h"
63
64 /*----- Main code ---------------------------------------------------------*/
65
66 typedef struct fctx {
67   field f;
68   mpreduce r;
69 } fctx;
70
71 /* --- Field operations --- */
72
73 static void fdestroy(field *ff)
74   { fctx *f = (fctx *)ff; mpreduce_destroy(&f->r); DESTROY(f); }
75
76 static mp *frand(field *ff, mp *d, grand *r)
77   { fctx *f = (fctx *)ff; return (mprand_range(d, f->r.p, r, 0)); }
78
79 static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); }
80
81 static mp *fneg(field *ff, mp *d, mp *x)
82   { fctx *f = (fctx *)ff; return (mp_sub(d, f->r.p, x)); }
83
84 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
85   fctx *f = (fctx *)ff; d = mp_add(d, x, y);
86   if (d->f & MP_NEG) d = mp_add(d, d, f->r.p);
87   else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
88   return (d);
89 }
90
91 static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
92   fctx *f = (fctx *)ff; d = mp_sub(d, x, y);
93   if (d->f & MP_NEG) d = mp_add(d, d, f->r.p);
94   else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
95   return (d);
96 }
97
98 static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
99   fctx *f = (fctx *)ff; d = mp_mul(d, x, y);
100   return (mpreduce_do(&f->r, d, d));
101 }
102
103 static mp *fsqr(field *ff, mp *d, mp *x) {
104   fctx *f = (fctx *)ff; d = mp_sqr(d, x);
105   return (mpreduce_do(&f->r, d, d));
106 }
107
108 static mp *finv(field *ff, mp *d, mp *x)
109   { fctx *f = (fctx *)ff; mp_gcd(0, 0, &d, f->r.p, x); return (d); }
110
111 static mp *freduce(field *ff, mp *d, mp *x)
112   { fctx *f = (fctx *)ff; return (mpreduce_do(&f->r, d, x)); }
113
114 static mp *fsqrt(field *ff, mp *d, mp *x)
115   { fctx *f = (fctx *)ff; return (mp_modsqrt(d, x, f->r.p)); }
116
117 static mp *fdbl(field *ff, mp *d, mp *x) {
118   fctx *f = (fctx *)ff; d = mp_lsl(d, x, 1);
119   if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
120   return (d);
121 }
122
123 static mp *ftpl(field *ff, mp *d, mp *x) {
124   fctx *f = (fctx *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
125   MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
126   while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
127   return (d);
128 }
129
130 static mp *fqdl(field *ff, mp *d, mp *x) {
131   fctx *f = (fctx *)ff; d = mp_lsl(d, x, 2);
132   while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
133   return (d);
134 }
135
136 static mp *fhlv(field *ff, mp *d, mp *x) {
137   fctx *f = (fctx *)ff;
138   if (!MP_LEN(x)) { MP_COPY(x); MP_DROP(d); return (x); }
139   if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; }
140   return (mp_lsr(d, x, 1));
141 }
142
143 /* --- Field operations table --- */
144
145 static const field_ops fops = {
146   FTY_PRIME, "niceprime",
147   fdestroy, frand, field_stdsamep,
148   freduce, field_id,
149   fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
150   0,
151   fdbl, ftpl, fqdl, fhlv
152 };
153
154 /* --- @field_niceprime@ --- *
155  *
156  * Arguments:   @mp *p@ = the characteristic of the field
157  *
158  * Returns:     A pointer to the field.
159  *
160  * Use:         Creates a field structure for a prime field of size %$p$%,
161  *              using efficient reduction for nice primes.
162  */
163
164 field *field_niceprime(mp *p)
165 {
166   fctx *f = CREATE(fctx);
167   f->f.ops = &fops;
168   f->f.zero = MP_ZERO;
169   f->f.one = MP_ONE;
170   f->f.nbits = mp_bits(p);
171   f->f.noctets = (f->f.nbits + 7) >> 3;
172   mpreduce_create(&f->r, p);
173   f->f.m = f->r.p;
174   return (&f->f);
175 }
176
177 /*----- That's all, folks -------------------------------------------------*/