3 * Testbed for elliptic curve arithmetic over prime fields
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Object types ------------------------------------------------------*/
30 obj ecp_curve { a, b, p };
31 obj ecp_pt { x, y, e };
33 /*----- Main code ---------------------------------------------------------*/
35 define ecp_curve(a, b, p)
37 local obj ecp_curve e;
44 define ecp_pt(x, y, e)
53 define ecp_pt_print(a)
55 print "(" : a.x : ", " : a.y : ")" :;
58 define ecp_pt_add(a, b)
67 else if (!istype(a, b))
68 quit "bad type arguments to ecp_pt_add";
70 quit "points from different curves in ecp_pt_add";
77 alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p;
79 alpha = (b.y - a.y) * minv(b.x - a.x, e.p) % e.p;
81 d.x = (alpha^2 - a.x - b.x) % e.p;
82 d.y = (-a.y + alpha * (a.x - d.x)) % e.p;
96 alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p;
97 d.x = (alpha^2 - 2 * a.x) % e.p;
98 d.y = (-a.y + alpha * (a.x - d.x)) % e.p;
112 define ecp_pt_check(a)
117 if (a.y^2 % e.p != (a.x^3 + e.a * a.x + e.b) % e.p)
118 quit "bad curve point";
121 define ecp_pt_mul(a, b)
129 } else if (istype(b, 1)) {
133 return (newerror("bad arguments to ecp_pt_mul"));
145 /*----- FIPS186-2 standard curves -----------------------------------------*/
147 p192 = ecp_curve(-3, 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1,
148 6277101735386680763835789423207666416083908700390324961279);
149 p192_r = 6277101735386680763835789423176059013767194773182842284081;
150 p192_g = ecp_pt(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012,
151 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811, p192);
153 /*----- That's all, folks -------------------------------------------------*/