Merge and close elliptic curve branch.
[u/mdw/catacomb] / calc / ec2.cal
1 /* -*-apcalc-*-
2 *
3 * $Id: ec2.cal,v 1.2 2004/03/21 22:52:06 mdw Exp $
4 *
5 * Testbed for elliptic curve arithmetic over binary fields
6 *
7 * (c) 2004 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: ec2.cal,v $
33 * Revision 1.2 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
35 *
36 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
37 * Elliptic curves on binary fields work.
38 *
39 * Revision 1.1.4.2 2004/03/20 00:13:31 mdw
40 * Projective coordinates for prime curves
41 *
42 * Revision 1.1.4.1 2003/06/10 13:43:53 mdw
43 * Simple (non-projective) curves over prime fields now seem to work.
44 *
45 * Revision 1.1 2000/10/08 16:01:37 mdw
46 * Prototypes of various bits of code.
47 *
48 */
49
50 /*----- Object types ------------------------------------------------------*/
51
52 obj ec2_curve { a, b, p };
53 obj ec2_pt { x, y, e };
54 obj ecpp_pt { x, y, z, e };
55
56 /*----- Main code ---------------------------------------------------------*/
57
58 define ec2_curve(a, b, p)
59 {
60 local obj ec2_curve e;
61 e.a = a;
62 e.b = b;
63 e.p = p;
64 return (e);
65 }
66
67 define ec2_pt(x, y, e)
68 {
69 local obj ec2_pt p;
70 p.x = x % e.p;
71 p.y = y % e.p;
72 p.e = e;
73 return (p);
74 }
75
76 define ec2_pt_print(a)
77 {
78 print "(" : a.x : ", " : a.y : ")" :;
79 }
80
81 define ec2_pt_add(a, b)
82 {
83 local e, alpha;
84 local obj ec2_pt d;
85
86 print "> ecadd: ", a, b;
87 if (a == 0)
88 d = b;
89 else if (b == 0)
90 d = a;
91 else if (!istype(a, b))
92 quit "bad type arguments to ec2_pt_add";
93 else if (a.e != b.e)
94 quit "points from different curves in ec2_pt_add";
95 else {
96 e = a.e;
97 if (a.x != b.x) {
98 alpha = ((a.y + b.y) * gf_inv(a.x + b.x, e.p)) % e.p;
99 d.x = (e.a + alpha^2 + alpha + a.x + b.x) % e.p;
100 } else if (a.y != b.y || a.x == gf(0))
101 return 0;
102 else {
103 alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p;
104 d.x = (e.a + alpha^2 + alpha) % e.p;
105 }
106 d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p;
107 d.e = e;
108 }
109
110 print "< ecadd: ", d;
111 return (d);
112 }
113
114 define ec2_pt_dbl(a)
115 {
116 local e, alpha;
117 local obj ec2_pt d;
118 print "> ecdbl: ", a;
119 if (istype(a, 1))
120 return (0);
121 e = a.e;
122 alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p;
123 d.x = (e.a + alpha^2 + alpha) % e.p;
124 d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p;
125 d.e = e;
126 print "< ecdbl: ", d;
127 return (d);
128 }
129
130 define ec2_pt_sub(a, b) { return ec2_pt_add(a, ec2_pt_neg(b)); }
131
132 define ec2_pt_neg(a)
133 {
134 local obj ec2_pt d;
135 d.x = a.x;
136 d.y = a.x + a.y;
137 d.e = a.e;
138 return (d);
139 }
140
141 define ec2_pt_check(a)
142 {
143 local e;
144
145 e = a.e;
146 if ((a.y^2 + a.x * a.y) % e.p != (a.x^3 + e.a * a.x^2 + e.b) % e.p)
147 quit "bad curve point";
148 }
149
150 define ec2_pt_mul(a, b)
151 {
152 local p, n;
153 local d;
154
155 if (istype(a, 1)) {
156 n = a;
157 p = b;
158 } else if (istype(b, 1)) {
159 n = b;
160 p = a;
161 } else
162 return (newerror("bad arguments to ec2_pt_mul"));
163
164 d = 0;
165 while (n) {
166 if (n & 1)
167 d += p;
168 n >>= 1;
169 p = ec2_pt_dbl(p);
170 }
171 return (d);
172 }
173
174 /*----- FIPS186-2 standard curves -----------------------------------------*/
175
176 b163 = ec2_curve(gf(1),gf(0x20a601907b8c953ca1481eb10512f78744a3205fd),
177 gf(0x800000000000000000000000000000000000000c9));
178 b163_r = 5846006549323611672814742442876390689256843201587;
179 b163_g = ec2_pt(0x3f0eba16286a2d57ea0991168d4994637e8343e36,
180 0x0d51fbc6c71a0094fa2cdd545b11c5c0c797324f1, b163);
181
182 /*----- That's all, folks -------------------------------------------------*/
183