chiark / gitweb /
utils/split-pieces (QfConvert): Construct an instance of the right class.
[catacomb] / calc / ec2.cal
CommitLineData
ceb3f0c0 1/* -*-apcalc-*-
ceb3f0c0 2 *
3 * Testbed for elliptic curve arithmetic over binary fields
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
ceb3f0c0 9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
45c0fd36 16 *
ceb3f0c0 17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
45c0fd36 21 *
ceb3f0c0 22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
ceb3f0c0 28/*----- Object types ------------------------------------------------------*/
29
30obj ec2_curve { a, b, p };
31obj ec2_pt { x, y, e };
32obj ecpp_pt { x, y, z, e };
33
34/*----- Main code ---------------------------------------------------------*/
35
36define ec2_curve(a, b, p)
37{
38 local obj ec2_curve e;
39 e.a = a;
40 e.b = b;
41 e.p = p;
42 return (e);
43}
44
45define ec2_pt(x, y, e)
46{
47 local obj ec2_pt p;
48 p.x = x % e.p;
49 p.y = y % e.p;
50 p.e = e;
51 return (p);
52}
53
54define ec2_pt_print(a)
55{
56 print "(" : a.x : ", " : a.y : ")" :;
57}
58
59define ec2_pt_add(a, b)
60{
61 local e, alpha;
62 local obj ec2_pt d;
63
ceb3f0c0 64 if (a == 0)
65 d = b;
66 else if (b == 0)
67 d = a;
68 else if (!istype(a, b))
69 quit "bad type arguments to ec2_pt_add";
70 else if (a.e != b.e)
71 quit "points from different curves in ec2_pt_add";
72 else {
73 e = a.e;
74 if (a.x != b.x) {
75 alpha = ((a.y + b.y) * gf_inv(a.x + b.x, e.p)) % e.p;
76 d.x = (e.a + alpha^2 + alpha + a.x + b.x) % e.p;
77 } else if (a.y != b.y || a.x == gf(0))
78 return 0;
79 else {
80 alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p;
81 d.x = (e.a + alpha^2 + alpha) % e.p;
82 }
83 d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p;
84 d.e = e;
85 }
86
ceb3f0c0 87 return (d);
88}
89
90define ec2_pt_dbl(a)
91{
92 local e, alpha;
93 local obj ec2_pt d;
ceb3f0c0 94 if (istype(a, 1))
95 return (0);
96 e = a.e;
97 alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p;
98 d.x = (e.a + alpha^2 + alpha) % e.p;
99 d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p;
100 d.e = e;
ceb3f0c0 101 return (d);
102}
103
104define ec2_pt_sub(a, b) { return ec2_pt_add(a, ec2_pt_neg(b)); }
105
106define ec2_pt_neg(a)
107{
108 local obj ec2_pt d;
109 d.x = a.x;
110 d.y = a.x + a.y;
111 d.e = a.e;
112 return (d);
113}
114
115define ec2_pt_check(a)
116{
117 local e;
118
119 e = a.e;
120 if ((a.y^2 + a.x * a.y) % e.p != (a.x^3 + e.a * a.x^2 + e.b) % e.p)
121 quit "bad curve point";
122}
123
124define ec2_pt_mul(a, b)
125{
126 local p, n;
127 local d;
128
129 if (istype(a, 1)) {
130 n = a;
131 p = b;
132 } else if (istype(b, 1)) {
133 n = b;
134 p = a;
135 } else
136 return (newerror("bad arguments to ec2_pt_mul"));
137
138 d = 0;
139 while (n) {
140 if (n & 1)
141 d += p;
142 n >>= 1;
143 p = ec2_pt_dbl(p);
144 }
145 return (d);
146}
147
148/*----- FIPS186-2 standard curves -----------------------------------------*/
149
150b163 = ec2_curve(gf(1),gf(0x20a601907b8c953ca1481eb10512f78744a3205fd),
151 gf(0x800000000000000000000000000000000000000c9));
152b163_r = 5846006549323611672814742442876390689256843201587;
153b163_g = ec2_pt(0x3f0eba16286a2d57ea0991168d4994637e8343e36,
154 0x0d51fbc6c71a0094fa2cdd545b11c5c0c797324f1, b163);
155
156/*----- That's all, folks -------------------------------------------------*/