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