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