a2a74efe |
1 | /* -*-apcalc-*- |
2 | * |
3 | * $Id: ecp.cal,v 1.1 2000/10/08 16:01:37 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.1 2000/10/08 16:01:37 mdw |
34 | * Prototypes of various bits of code. |
35 | * |
36 | */ |
37 | |
38 | /*----- Object types ------------------------------------------------------*/ |
39 | |
40 | obj ecp_curve { a, b, p }; |
41 | obj ecp_pt { x, y, e }; |
42 | |
43 | /*----- Main code ---------------------------------------------------------*/ |
44 | |
45 | define ecp_curve(a, b, p) |
46 | { |
47 | local obj ecp_curve e; |
48 | e.a = a; |
49 | e.b = b; |
50 | e.p = p; |
51 | return (e); |
52 | } |
53 | |
54 | define ecp_pt(x, y, e) |
55 | { |
56 | local obj ecp_pt p; |
57 | p.x = x % e.p; |
58 | p.y = y % e.p; |
59 | p.e = e; |
60 | return (p); |
61 | } |
62 | |
63 | define ecp_pt_print(a) |
64 | { |
65 | print "(" : a.x : ", " : a.y : ")" :; |
66 | } |
67 | |
68 | define ecp_pt_add(a, b) |
69 | { |
70 | local e, alpha; |
71 | local obj ecp_pt d; |
72 | |
73 | if (a == 0) |
74 | d = b; |
75 | else if (b == 0) |
76 | d = a; |
77 | else if (!istype(a, b)) |
78 | quit "bad type arguments to ecp_pt_add"; |
79 | else if (a.e != b.e) |
80 | quit "points from different curves in ecp_pt_add"; |
81 | else { |
82 | e = a.e; |
83 | if (a.x == b.x) { |
84 | if (a.y != b.y) { |
85 | return (0); |
86 | } |
87 | alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p; |
88 | } else |
89 | alpha = (b.y - a.y) * minv(b.x - a.x, e.p) % e.p; |
90 | |
91 | d.x = (alpha^2 - a.x - b.x) % e.p; |
92 | d.y = (-a.y + alpha * (a.x - d.x)) % e.p; |
93 | d.e = e; |
94 | } |
95 | |
96 | return (d); |
97 | } |
98 | |
99 | define ecp_pt_neg(a) |
100 | { |
101 | local obj ecp_pt d; |
102 | d.x = a.x; |
103 | d.y = -a.y; |
104 | d.e = a.e; |
105 | return (d); |
106 | } |
107 | |
108 | define ecp_pt_mul(a, b) |
109 | { |
110 | local p, n; |
111 | local d; |
112 | |
113 | if (istype(a, 1)) { |
114 | n = a; |
115 | p = b; |
116 | } else if (istype(b, 1)) { |
117 | n = b; |
118 | p = a; |
119 | } else |
120 | return (newerror("bad arguments to ecp_pt_mul")); |
121 | |
122 | d = 0; |
123 | while (n) { |
124 | if (n & 1) |
125 | d += p; |
126 | n >>= 1; |
127 | p += p; |
128 | } |
129 | return (d); |
130 | } |
131 | |
132 | /*----- That's all, folks -------------------------------------------------*/ |
133 | |