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