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