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