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