a2a74efe |
1 | /* -*-apcalc-*- |
2 | * |
c3caa2fa |
3 | * $Id: ecp.cal,v 1.2 2004/03/21 22:52:06 mdw Exp $ |
a2a74efe |
4 | * |
5 | * Testbed for elliptic curve arithmetic over prime fields |
6 | * |
7 | * (c) 2000 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: ecp.cal,v $ |
c3caa2fa |
33 | * Revision 1.2 2004/03/21 22:52:06 mdw |
34 | * Merge and close elliptic curve branch. |
35 | * |
8823192f |
36 | * Revision 1.1.4.2 2004/03/20 00:13:31 mdw |
37 | * Projective coordinates for prime curves |
38 | * |
dbfee00a |
39 | * Revision 1.1.4.1 2003/06/10 13:43:53 mdw |
40 | * Simple (non-projective) curves over prime fields now seem to work. |
41 | * |
a2a74efe |
42 | * Revision 1.1 2000/10/08 16:01:37 mdw |
43 | * Prototypes of various bits of code. |
44 | * |
45 | */ |
46 | |
47 | /*----- Object types ------------------------------------------------------*/ |
48 | |
49 | obj ecp_curve { a, b, p }; |
50 | obj ecp_pt { x, y, e }; |
8823192f |
51 | obj ecpp_pt { x, y, z, e }; |
a2a74efe |
52 | |
53 | /*----- Main code ---------------------------------------------------------*/ |
54 | |
55 | define ecp_curve(a, b, p) |
56 | { |
57 | local obj ecp_curve e; |
58 | e.a = a; |
59 | e.b = b; |
60 | e.p = p; |
61 | return (e); |
62 | } |
63 | |
64 | define ecp_pt(x, y, e) |
65 | { |
66 | local obj ecp_pt p; |
67 | p.x = x % e.p; |
68 | p.y = y % e.p; |
69 | p.e = e; |
70 | return (p); |
71 | } |
72 | |
8823192f |
73 | define ecpp_pt(p) |
74 | { |
75 | local obj ecpp_pt pp; |
76 | if (istype(p, 1)) |
77 | return (0); |
78 | pp.x = p.x; |
79 | pp.y = p.y; |
80 | pp.z = 1; |
81 | pp.e = p.e; |
82 | return (pp); |
83 | } |
84 | |
85 | define ecpp_fix(pp) |
86 | { |
87 | local obj ecp_pt p; |
88 | local e, zi, z2, z3; |
89 | if (istype(pp, 1) || pp.z == 0) |
90 | return (0); |
91 | e = pp.e; |
92 | zi = minv(pp.z, e.p); |
93 | z2 = zi * zi; |
94 | z3 = zi * z2; |
95 | p.x = pp.x * z2 % e.p; |
96 | p.y = pp.y * z3 % e.p; |
97 | p.e = e; |
98 | return (p); |
99 | } |
100 | |
101 | define ecpp_dbl(a) |
102 | { |
103 | local m, s, t, y2; |
104 | local e; |
105 | local obj ecpp_pt d; |
106 | if (istype(a, 1) || a.y == 0) |
107 | return (0); |
108 | e = a.e; |
109 | if (e.a % e.p == e.p - 3) { |
110 | m = a.z^3 % e.p; |
111 | m = 3 * (a.x + t4) * (a.x - t4) % e.p; |
112 | } else { |
113 | m = (3 * a.x^2 - e.a * a.z^4) % e.p; |
114 | } |
115 | d.z = 2 * a.y * a.z % e.p; |
116 | y2 = a.y^2 % e.p; |
117 | s = 4 * a.x * a.y % e.p; |
118 | d.x = (m^2 - 2 * s) % e.p; |
119 | d.y = (m * (s - d.x) - y * y2^2) % e.p; |
120 | d.e = e; |
121 | return (d); |
122 | } |
123 | |
124 | define ecpp_add(a, b) |
125 | { |
126 | if (a == 0) |
127 | d = b; |
128 | else if (b == 0) |
129 | d = a; |
130 | else if (!istype(a, b)) |
131 | quit "bad type arguments to ecp_pt_add"; |
132 | else if (a.e != b.e) |
133 | quit "points from different curves in ecp_pt_add"; |
134 | else { |
135 | e = a.e; |
136 | |
137 | } |
138 | |
a2a74efe |
139 | define ecp_pt_print(a) |
140 | { |
141 | print "(" : a.x : ", " : a.y : ")" :; |
142 | } |
143 | |
144 | define ecp_pt_add(a, b) |
145 | { |
146 | local e, alpha; |
147 | local obj ecp_pt d; |
148 | |
149 | if (a == 0) |
150 | d = b; |
151 | else if (b == 0) |
152 | d = a; |
153 | else if (!istype(a, b)) |
154 | quit "bad type arguments to ecp_pt_add"; |
155 | else if (a.e != b.e) |
156 | quit "points from different curves in ecp_pt_add"; |
157 | else { |
158 | e = a.e; |
159 | if (a.x == b.x) { |
160 | if (a.y != b.y) { |
161 | return (0); |
162 | } |
163 | alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p; |
164 | } else |
165 | alpha = (b.y - a.y) * minv(b.x - a.x, e.p) % e.p; |
166 | |
167 | d.x = (alpha^2 - a.x - b.x) % e.p; |
168 | d.y = (-a.y + alpha * (a.x - d.x)) % e.p; |
169 | d.e = e; |
170 | } |
171 | |
172 | return (d); |
173 | } |
174 | |
dbfee00a |
175 | define ecp_pt_dbl(a) |
176 | { |
177 | local e, alpha; |
178 | local obj ecp_pt d; |
8823192f |
179 | if (istype(a, 1)) |
180 | return (0); |
dbfee00a |
181 | e = a.e; |
182 | alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p; |
183 | d.x = (alpha^2 - 2 * a.x) % e.p; |
184 | d.y = (-a.y + alpha * (a.x - d.x)) % e.p; |
185 | d.e = e; |
186 | return (d); |
187 | } |
188 | |
a2a74efe |
189 | define ecp_pt_neg(a) |
190 | { |
191 | local obj ecp_pt d; |
192 | d.x = a.x; |
193 | d.y = -a.y; |
194 | d.e = a.e; |
195 | return (d); |
196 | } |
197 | |
dbfee00a |
198 | define ecp_pt_check(a) |
199 | { |
200 | local e; |
201 | |
202 | e = a.e; |
203 | if (a.y^2 % e.p != (a.x^3 + e.a * a.x + e.b) % e.p) |
204 | quit "bad curve point"; |
205 | } |
206 | |
a2a74efe |
207 | define ecp_pt_mul(a, b) |
208 | { |
209 | local p, n; |
210 | local d; |
211 | |
212 | if (istype(a, 1)) { |
213 | n = a; |
214 | p = b; |
215 | } else if (istype(b, 1)) { |
216 | n = b; |
217 | p = a; |
218 | } else |
219 | return (newerror("bad arguments to ecp_pt_mul")); |
220 | |
221 | d = 0; |
222 | while (n) { |
223 | if (n & 1) |
224 | d += p; |
225 | n >>= 1; |
dbfee00a |
226 | p = ecp_pt_dbl(p); |
a2a74efe |
227 | } |
228 | return (d); |
229 | } |
230 | |
dbfee00a |
231 | /*----- FIPS186-2 standard curves -----------------------------------------*/ |
232 | |
233 | p192 = ecp_curve(-3, 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1, |
234 | 6277101735386680763835789423207666416083908700390324961279); |
235 | p192_r = 6277101735386680763835789423176059013767194773182842284081; |
236 | p192_g = ecp_pt(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012, |
237 | 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811, p192); |
238 | |
a2a74efe |
239 | /*----- That's all, folks -------------------------------------------------*/ |
240 | |