3 * $Id: ecp.cal,v 1.2 2004/03/21 22:52:06 mdw Exp $
5 * Testbed for elliptic curve arithmetic over prime fields
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
36 * Revision 1.1.4.2 2004/03/20 00:13:31 mdw
37 * Projective coordinates for prime curves
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.
42 * Revision 1.1 2000/10/08 16:01:37 mdw
43 * Prototypes of various bits of code.
47 /*----- Object types ------------------------------------------------------*/
49 obj ecp_curve { a, b, p };
50 obj ecp_pt { x, y, e };
51 obj ecpp_pt { x, y, z, e };
53 /*----- Main code ---------------------------------------------------------*/
55 define ecp_curve(a, b, p)
57 local obj ecp_curve e;
64 define ecp_pt(x, y, e)
89 if (istype(pp, 1) || pp.z == 0)
95 p.x = pp.x * z2 % e.p;
96 p.y = pp.y * z3 % e.p;
106 if (istype(a, 1) || a.y == 0)
109 if (e.a % e.p == e.p - 3) {
111 m = 3 * (a.x + t4) * (a.x - t4) % e.p;
113 m = (3 * a.x^2 - e.a * a.z^4) % e.p;
115 d.z = 2 * a.y * a.z % 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;
124 define ecpp_add(a, b)
130 else if (!istype(a, b))
131 quit "bad type arguments to ecp_pt_add";
133 quit "points from different curves in ecp_pt_add";
139 define ecp_pt_print(a)
141 print "(" : a.x : ", " : a.y : ")" :;
144 define ecp_pt_add(a, b)
153 else if (!istype(a, b))
154 quit "bad type arguments to ecp_pt_add";
156 quit "points from different curves in ecp_pt_add";
163 alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p;
165 alpha = (b.y - a.y) * minv(b.x - a.x, e.p) % e.p;
167 d.x = (alpha^2 - a.x - b.x) % e.p;
168 d.y = (-a.y + alpha * (a.x - d.x)) % e.p;
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;
198 define ecp_pt_check(a)
203 if (a.y^2 % e.p != (a.x^3 + e.a * a.x + e.b) % e.p)
204 quit "bad curve point";
207 define ecp_pt_mul(a, b)
215 } else if (istype(b, 1)) {
219 return (newerror("bad arguments to ecp_pt_mul"));
231 /*----- FIPS186-2 standard curves -----------------------------------------*/
233 p192 = ecp_curve(-3, 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1,
234 6277101735386680763835789423207666416083908700390324961279);
235 p192_r = 6277101735386680763835789423176059013767194773182842284081;
236 p192_g = ecp_pt(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012,
237 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811, p192);
239 /*----- That's all, folks -------------------------------------------------*/