| 1 | /* -*-apcalc-*- |
| 2 | * |
| 3 | * $Id: ec2.cal,v 1.4 2004/04/08 01:36:15 mdw Exp $ |
| 4 | * |
| 5 | * Testbed for elliptic curve arithmetic over binary fields |
| 6 | * |
| 7 | * (c) 2004 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 | /*----- Object types ------------------------------------------------------*/ |
| 31 | |
| 32 | obj ec2_curve { a, b, p }; |
| 33 | obj ec2_pt { x, y, e }; |
| 34 | obj ecpp_pt { x, y, z, e }; |
| 35 | |
| 36 | /*----- Main code ---------------------------------------------------------*/ |
| 37 | |
| 38 | define ec2_curve(a, b, p) |
| 39 | { |
| 40 | local obj ec2_curve e; |
| 41 | e.a = a; |
| 42 | e.b = b; |
| 43 | e.p = p; |
| 44 | return (e); |
| 45 | } |
| 46 | |
| 47 | define ec2_pt(x, y, e) |
| 48 | { |
| 49 | local obj ec2_pt p; |
| 50 | p.x = x % e.p; |
| 51 | p.y = y % e.p; |
| 52 | p.e = e; |
| 53 | return (p); |
| 54 | } |
| 55 | |
| 56 | define ec2_pt_print(a) |
| 57 | { |
| 58 | print "(" : a.x : ", " : a.y : ")" :; |
| 59 | } |
| 60 | |
| 61 | define ec2_pt_add(a, b) |
| 62 | { |
| 63 | local e, alpha; |
| 64 | local obj ec2_pt d; |
| 65 | |
| 66 | if (a == 0) |
| 67 | d = b; |
| 68 | else if (b == 0) |
| 69 | d = a; |
| 70 | else if (!istype(a, b)) |
| 71 | quit "bad type arguments to ec2_pt_add"; |
| 72 | else if (a.e != b.e) |
| 73 | quit "points from different curves in ec2_pt_add"; |
| 74 | else { |
| 75 | e = a.e; |
| 76 | if (a.x != b.x) { |
| 77 | alpha = ((a.y + b.y) * gf_inv(a.x + b.x, e.p)) % e.p; |
| 78 | d.x = (e.a + alpha^2 + alpha + a.x + b.x) % e.p; |
| 79 | } else if (a.y != b.y || a.x == gf(0)) |
| 80 | return 0; |
| 81 | else { |
| 82 | alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p; |
| 83 | d.x = (e.a + alpha^2 + alpha) % e.p; |
| 84 | } |
| 85 | d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p; |
| 86 | d.e = e; |
| 87 | } |
| 88 | |
| 89 | return (d); |
| 90 | } |
| 91 | |
| 92 | define ec2_pt_dbl(a) |
| 93 | { |
| 94 | local e, alpha; |
| 95 | local obj ec2_pt d; |
| 96 | if (istype(a, 1)) |
| 97 | return (0); |
| 98 | e = a.e; |
| 99 | alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p; |
| 100 | d.x = (e.a + alpha^2 + alpha) % e.p; |
| 101 | d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p; |
| 102 | d.e = e; |
| 103 | return (d); |
| 104 | } |
| 105 | |
| 106 | define ec2_pt_sub(a, b) { return ec2_pt_add(a, ec2_pt_neg(b)); } |
| 107 | |
| 108 | define ec2_pt_neg(a) |
| 109 | { |
| 110 | local obj ec2_pt d; |
| 111 | d.x = a.x; |
| 112 | d.y = a.x + a.y; |
| 113 | d.e = a.e; |
| 114 | return (d); |
| 115 | } |
| 116 | |
| 117 | define ec2_pt_check(a) |
| 118 | { |
| 119 | local e; |
| 120 | |
| 121 | e = a.e; |
| 122 | if ((a.y^2 + a.x * a.y) % e.p != (a.x^3 + e.a * a.x^2 + e.b) % e.p) |
| 123 | quit "bad curve point"; |
| 124 | } |
| 125 | |
| 126 | define ec2_pt_mul(a, b) |
| 127 | { |
| 128 | local p, n; |
| 129 | local d; |
| 130 | |
| 131 | if (istype(a, 1)) { |
| 132 | n = a; |
| 133 | p = b; |
| 134 | } else if (istype(b, 1)) { |
| 135 | n = b; |
| 136 | p = a; |
| 137 | } else |
| 138 | return (newerror("bad arguments to ec2_pt_mul")); |
| 139 | |
| 140 | d = 0; |
| 141 | while (n) { |
| 142 | if (n & 1) |
| 143 | d += p; |
| 144 | n >>= 1; |
| 145 | p = ec2_pt_dbl(p); |
| 146 | } |
| 147 | return (d); |
| 148 | } |
| 149 | |
| 150 | /*----- FIPS186-2 standard curves -----------------------------------------*/ |
| 151 | |
| 152 | b163 = ec2_curve(gf(1),gf(0x20a601907b8c953ca1481eb10512f78744a3205fd), |
| 153 | gf(0x800000000000000000000000000000000000000c9)); |
| 154 | b163_r = 5846006549323611672814742442876390689256843201587; |
| 155 | b163_g = ec2_pt(0x3f0eba16286a2d57ea0991168d4994637e8343e36, |
| 156 | 0x0d51fbc6c71a0094fa2cdd545b11c5c0c797324f1, b163); |
| 157 | |
| 158 | /*----- That's all, folks -------------------------------------------------*/ |
| 159 | |