Simple (non-projective) curves over prime fields now seem to work.
[u/mdw/catacomb] / calc / ecp.cal
CommitLineData
a2a74efe 1/* -*-apcalc-*-
2 *
dbfee00a 3 * $Id: ecp.cal,v 1.1.4.1 2003/06/10 13:43:53 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 $
dbfee00a 33 * Revision 1.1.4.1 2003/06/10 13:43:53 mdw
34 * Simple (non-projective) curves over prime fields now seem to work.
35 *
a2a74efe 36 * Revision 1.1 2000/10/08 16:01:37 mdw
37 * Prototypes of various bits of code.
38 *
39 */
40
41/*----- Object types ------------------------------------------------------*/
42
43obj ecp_curve { a, b, p };
44obj ecp_pt { x, y, e };
45
46/*----- Main code ---------------------------------------------------------*/
47
48define ecp_curve(a, b, p)
49{
50 local obj ecp_curve e;
51 e.a = a;
52 e.b = b;
53 e.p = p;
54 return (e);
55}
56
57define ecp_pt(x, y, e)
58{
59 local obj ecp_pt p;
60 p.x = x % e.p;
61 p.y = y % e.p;
62 p.e = e;
63 return (p);
64}
65
66define ecp_pt_print(a)
67{
68 print "(" : a.x : ", " : a.y : ")" :;
69}
70
71define ecp_pt_add(a, b)
72{
73 local e, alpha;
74 local obj ecp_pt d;
75
76 if (a == 0)
77 d = b;
78 else if (b == 0)
79 d = a;
80 else if (!istype(a, b))
81 quit "bad type arguments to ecp_pt_add";
82 else if (a.e != b.e)
83 quit "points from different curves in ecp_pt_add";
84 else {
85 e = a.e;
86 if (a.x == b.x) {
87 if (a.y != b.y) {
88 return (0);
89 }
90 alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p;
91 } else
92 alpha = (b.y - a.y) * minv(b.x - a.x, e.p) % e.p;
93
94 d.x = (alpha^2 - a.x - b.x) % e.p;
95 d.y = (-a.y + alpha * (a.x - d.x)) % e.p;
96 d.e = e;
97 }
98
99 return (d);
100}
101
dbfee00a 102define ecp_pt_dbl(a)
103{
104 local e, alpha;
105 local obj ecp_pt d;
106 e = a.e;
107 alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p;
108 d.x = (alpha^2 - 2 * a.x) % e.p;
109 d.y = (-a.y + alpha * (a.x - d.x)) % e.p;
110 d.e = e;
111 return (d);
112}
113
a2a74efe 114define ecp_pt_neg(a)
115{
116 local obj ecp_pt d;
117 d.x = a.x;
118 d.y = -a.y;
119 d.e = a.e;
120 return (d);
121}
122
dbfee00a 123define ecp_pt_check(a)
124{
125 local e;
126
127 e = a.e;
128 if (a.y^2 % e.p != (a.x^3 + e.a * a.x + e.b) % e.p)
129 quit "bad curve point";
130}
131
a2a74efe 132define ecp_pt_mul(a, b)
133{
134 local p, n;
135 local d;
136
137 if (istype(a, 1)) {
138 n = a;
139 p = b;
140 } else if (istype(b, 1)) {
141 n = b;
142 p = a;
143 } else
144 return (newerror("bad arguments to ecp_pt_mul"));
145
146 d = 0;
147 while (n) {
148 if (n & 1)
149 d += p;
150 n >>= 1;
dbfee00a 151 p = ecp_pt_dbl(p);
a2a74efe 152 }
153 return (d);
154}
155
dbfee00a 156/*----- FIPS186-2 standard curves -----------------------------------------*/
157
158p192 = ecp_curve(-3, 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1,
159 6277101735386680763835789423207666416083908700390324961279);
160p192_r = 6277101735386680763835789423176059013767194773182842284081;
161p192_g = ecp_pt(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012,
162 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811, p192);
163
a2a74efe 164/*----- That's all, folks -------------------------------------------------*/
165