a2a74efe |
1 | /* -*-apcalc-*- |
2 | * |
bc985cef |
3 | * $Id: ecp.cal,v 1.3 2004/03/23 15:19:32 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 $ |
bc985cef |
33 | * Revision 1.3 2004/03/23 15:19:32 mdw |
34 | * Test elliptic curves more thoroughly. |
35 | * |
c3caa2fa |
36 | * Revision 1.2 2004/03/21 22:52:06 mdw |
37 | * Merge and close elliptic curve branch. |
38 | * |
8823192f |
39 | * Revision 1.1.4.2 2004/03/20 00:13:31 mdw |
40 | * Projective coordinates for prime curves |
41 | * |
dbfee00a |
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 | * |
a2a74efe |
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 ecp_curve { a, b, p }; |
53 | obj ecp_pt { x, y, e }; |
54 | |
55 | /*----- Main code ---------------------------------------------------------*/ |
56 | |
57 | define ecp_curve(a, b, p) |
58 | { |
59 | local obj ecp_curve e; |
60 | e.a = a; |
61 | e.b = b; |
62 | e.p = p; |
63 | return (e); |
64 | } |
65 | |
66 | define ecp_pt(x, y, e) |
67 | { |
68 | local obj ecp_pt p; |
69 | p.x = x % e.p; |
70 | p.y = y % e.p; |
71 | p.e = e; |
72 | return (p); |
73 | } |
74 | |
75 | define ecp_pt_print(a) |
76 | { |
77 | print "(" : a.x : ", " : a.y : ")" :; |
78 | } |
79 | |
80 | define ecp_pt_add(a, b) |
81 | { |
82 | local e, alpha; |
83 | local obj ecp_pt d; |
84 | |
85 | if (a == 0) |
86 | d = b; |
87 | else if (b == 0) |
88 | d = a; |
89 | else if (!istype(a, b)) |
90 | quit "bad type arguments to ecp_pt_add"; |
91 | else if (a.e != b.e) |
92 | quit "points from different curves in ecp_pt_add"; |
93 | else { |
94 | e = a.e; |
95 | if (a.x == b.x) { |
96 | if (a.y != b.y) { |
97 | return (0); |
98 | } |
99 | alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p; |
100 | } else |
101 | alpha = (b.y - a.y) * minv(b.x - a.x, e.p) % e.p; |
102 | |
103 | d.x = (alpha^2 - a.x - b.x) % e.p; |
104 | d.y = (-a.y + alpha * (a.x - d.x)) % e.p; |
105 | d.e = e; |
106 | } |
107 | |
108 | return (d); |
109 | } |
110 | |
dbfee00a |
111 | define ecp_pt_dbl(a) |
112 | { |
113 | local e, alpha; |
114 | local obj ecp_pt d; |
8823192f |
115 | if (istype(a, 1)) |
116 | return (0); |
dbfee00a |
117 | e = a.e; |
118 | alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p; |
119 | d.x = (alpha^2 - 2 * a.x) % e.p; |
120 | d.y = (-a.y + alpha * (a.x - d.x)) % e.p; |
121 | d.e = e; |
122 | return (d); |
123 | } |
124 | |
a2a74efe |
125 | define ecp_pt_neg(a) |
126 | { |
127 | local obj ecp_pt d; |
128 | d.x = a.x; |
129 | d.y = -a.y; |
130 | d.e = a.e; |
131 | return (d); |
132 | } |
133 | |
dbfee00a |
134 | define ecp_pt_check(a) |
135 | { |
136 | local e; |
137 | |
138 | e = a.e; |
139 | if (a.y^2 % e.p != (a.x^3 + e.a * a.x + e.b) % e.p) |
140 | quit "bad curve point"; |
141 | } |
142 | |
a2a74efe |
143 | define ecp_pt_mul(a, b) |
144 | { |
145 | local p, n; |
146 | local d; |
147 | |
148 | if (istype(a, 1)) { |
149 | n = a; |
150 | p = b; |
151 | } else if (istype(b, 1)) { |
152 | n = b; |
153 | p = a; |
154 | } else |
155 | return (newerror("bad arguments to ecp_pt_mul")); |
156 | |
157 | d = 0; |
158 | while (n) { |
159 | if (n & 1) |
160 | d += p; |
161 | n >>= 1; |
dbfee00a |
162 | p = ecp_pt_dbl(p); |
a2a74efe |
163 | } |
164 | return (d); |
165 | } |
166 | |
dbfee00a |
167 | /*----- FIPS186-2 standard curves -----------------------------------------*/ |
168 | |
169 | p192 = ecp_curve(-3, 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1, |
170 | 6277101735386680763835789423207666416083908700390324961279); |
171 | p192_r = 6277101735386680763835789423176059013767194773182842284081; |
172 | p192_g = ecp_pt(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012, |
173 | 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811, p192); |
174 | |
a2a74efe |
175 | /*----- That's all, folks -------------------------------------------------*/ |
176 | |