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