Random field-element selection.
[u/mdw/catacomb] / f-prime.c
CommitLineData
b0ab12e6 1/* -*-c-*-
2 *
9b8b6877 3 * $Id: f-prime.c,v 1.5 2004/03/23 12:08:26 mdw Exp $
b0ab12e6 4 *
5 * Prime fields with Montgomery arithmetic
6 *
7 * (c) 2001 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-prime.c,v $
9b8b6877 33 * Revision 1.5 2004/03/23 12:08:26 mdw
34 * Random field-element selection.
35 *
c3caa2fa 36 * Revision 1.4 2004/03/21 22:52:06 mdw
37 * Merge and close elliptic curve branch.
38 *
ceb3f0c0 39 * Revision 1.3.4.3 2004/03/21 22:39:46 mdw
40 * Elliptic curves on binary fields work.
41 *
8823192f 42 * Revision 1.3.4.2 2004/03/20 00:13:31 mdw
43 * Projective coordinates for prime curves
44 *
dbfee00a 45 * Revision 1.3.4.1 2003/06/10 13:43:53 mdw
46 * Simple (non-projective) curves over prime fields now seem to work.
47 *
41cb1beb 48 * Revision 1.3 2003/05/15 23:25:59 mdw
49 * Make elliptic curve stuff build.
50 *
b085fd91 51 * Revision 1.2 2002/01/13 13:48:44 mdw
52 * Further progress.
53 *
b0ab12e6 54 * Revision 1.1 2001/04/29 18:12:33 mdw
55 * Prototype version.
56 *
57 */
58
59/*----- Header files ------------------------------------------------------*/
60
61#include <mLib/sub.h>
62
63#include "field.h"
64#include "mpmont.h"
9b8b6877 65#include "mprand.h"
b0ab12e6 66
67/*----- Data structures ---------------------------------------------------*/
68
69typedef struct fctx {
70 field f;
71 mpmont mm;
72} fctx;
73
74/*----- Main code ---------------------------------------------------------*/
75
76/* --- Field operations --- */
77
78static void fdestroy(field *ff)
79{
80 fctx *f = (fctx *)ff;
81 mpmont_destroy(&f->mm);
82 DESTROY(f);
83}
84
9b8b6877 85static mp *frand(field *ff, mp *d, grand *r)
86{
87 fctx *f = (fctx *)ff;
88 return (mprand_range(d, f->mm.m, r, 0));
89}
90
b0ab12e6 91static mp *fin(field *ff, mp *d, mp *x)
92{
93 fctx *f = (fctx *)ff;
dbfee00a 94 mp_div(0, &d, x, f->mm.m);
95 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
b0ab12e6 96}
97
98static mp *fout(field *ff, mp *d, mp *x)
99{
100 fctx *f = (fctx *)ff;
101 return (mpmont_reduce(&f->mm, d, x));
102}
103
8823192f 104static int fzerop(field *ff, mp *x)
105{
106 return (!MP_LEN(x));
107}
108
b085fd91 109static mp *fneg(field *ff, mp *d, mp *x)
110{
111 fctx *f = (fctx *)ff;
112 return (mp_sub(d, f->mm.m, x));
113}
114
b0ab12e6 115static mp *fadd(field *ff, mp *d, mp *x, mp *y)
116{
dbfee00a 117 fctx *f = (fctx *)ff;
118 d = mp_add(d, x, y);
119 if (d->f & MP_NEG)
120 d = mp_add(d, d, f->mm.m);
121 else if (MP_CMP(d, >, f->mm.m))
122 d = mp_sub(d, d, f->mm.m);
123 return (d);
b0ab12e6 124}
125
126static mp *fsub(field *ff, mp *d, mp *x, mp *y)
127{
dbfee00a 128 fctx *f = (fctx *)ff;
129 d = mp_sub(d, x, y);
130 if (d->f & MP_NEG)
131 d = mp_add(d, d, f->mm.m);
132 else if (MP_CMP(d, >, f->mm.m))
133 d = mp_sub(d, d, f->mm.m);
134 return (d);
b0ab12e6 135}
136
137static mp *fmul(field *ff, mp *d, mp *x, mp *y)
138{
139 fctx *f = (fctx *)ff;
140 return (mpmont_mul(&f->mm, d, x, y));
141}
142
143static mp *fsqr(field *ff, mp *d, mp *x)
144{
145 fctx *f = (fctx *)ff;
146 d = mp_sqr(d, x);
147 return (mpmont_reduce(&f->mm, d, d));
148}
149
150static mp *finv(field *ff, mp *d, mp *x)
151{
152 fctx *f = (fctx *)ff;
41cb1beb 153 d = mpmont_reduce(&f->mm, d, x);
b0ab12e6 154 mp_gcd(0, 0, &d, f->mm.m, d);
155 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
156}
157
158static mp *freduce(field *ff, mp *d, mp *x)
159{
160 fctx *f = (fctx *)ff;
161 mp_div(0, &d, x, f->mm.m);
162 return (d);
163}
164
ceb3f0c0 165static mp *fsqrt(field *ff, mp *d, mp *x)
166{
167 fctx *f = (fctx *)ff;
168 d = mpmont_reduce(&f->mm, d, x);
169 d = mp_modsqrt(d, d, f->mm.m);
170 if (!d)
171 return (d);
172 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
173}
174
b0ab12e6 175static mp *fdbl(field *ff, mp *d, mp *x)
176{
dbfee00a 177 fctx *f = (fctx *)ff;
178 d = mp_lsl(d, x, 1);
179 if (MP_CMP(d, >, f->mm.m))
180 d = mp_sub(d, d, f->mm.m);
181 return (d);
b0ab12e6 182}
183
184static mp *ftpl(field *ff, mp *d, mp *x)
185{
dbfee00a 186 fctx *f = (fctx *)ff;
b0ab12e6 187 MP_DEST(d, MP_LEN(x) + 1, x->f);
188 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
dbfee00a 189 while (MP_CMP(d, >, f->mm.m))
190 d = mp_sub(d, d, f->mm.m);
b0ab12e6 191 return (d);
192}
193
8823192f 194static mp *fqdl(field *ff, mp *d, mp *x)
b0ab12e6 195{
196 fctx *f = (fctx *)ff;
8823192f 197 d = mp_lsl(d, x, 2);
198 while (MP_CMP(d, >, f->mm.m))
199 d = mp_sub(d, d, f->mm.m);
200 return (d);
201}
202
203static mp *fhlv(field *ff, mp *d, mp *x)
204{
205 fctx *f = (fctx *)ff;
206 if (!MP_LEN(x)) {
207 MP_COPY(x);
208 MP_DROP(d);
209 return (x);
210 }
211 if (x->v[0] & 1) {
212 d = mp_add(d, x, f->mm.m);
213 x = d;
214 }
215 return (mp_lsr(d, x, 1));
b0ab12e6 216}
217
218/* --- Field operations table --- */
219
220static field_ops fops = {
9b8b6877 221 fdestroy, frand,
b0ab12e6 222 fin, fout,
ceb3f0c0 223 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
224 0,
225 fdbl, ftpl, fqdl, fhlv
b0ab12e6 226};
227
228/* --- @field_prime@ --- *
229 *
230 * Arguments: @mp *p@ = the characteristic of the field
231 *
232 * Returns: A pointer to the field.
233 *
234 * Use: Creates a field structure for a prime field of size %$p$%,
235 * using Montgomery reduction for arithmetic.
236 */
237
238field *field_prime(mp *p)
239{
41cb1beb 240 fctx *f = CREATE(fctx);
b0ab12e6 241 f->f.ops = &fops;
242 mpmont_create(&f->mm, p);
41cb1beb 243 f->f.zero = MP_ZERO;
244 f->f.one = f->mm.r;
b0ab12e6 245 return (&f->f);
246}
247
248/*----- That's all, folks -------------------------------------------------*/