/* -*-apcalc-*- * * Testbed for elliptic curve arithmetic over binary fields * * (c) 2004 Straylight/Edgeware */ /*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * * Catacomb is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * Catacomb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ /*----- Object types ------------------------------------------------------*/ obj ec2_curve { a, b, p }; obj ec2_pt { x, y, e }; obj ecpp_pt { x, y, z, e }; /*----- Main code ---------------------------------------------------------*/ define ec2_curve(a, b, p) { local obj ec2_curve e; e.a = a; e.b = b; e.p = p; return (e); } define ec2_pt(x, y, e) { local obj ec2_pt p; p.x = x % e.p; p.y = y % e.p; p.e = e; return (p); } define ec2_pt_print(a) { print "(" : a.x : ", " : a.y : ")" :; } define ec2_pt_add(a, b) { local e, alpha; local obj ec2_pt d; if (a == 0) d = b; else if (b == 0) d = a; else if (!istype(a, b)) quit "bad type arguments to ec2_pt_add"; else if (a.e != b.e) quit "points from different curves in ec2_pt_add"; else { e = a.e; if (a.x != b.x) { alpha = ((a.y + b.y) * gf_inv(a.x + b.x, e.p)) % e.p; d.x = (e.a + alpha^2 + alpha + a.x + b.x) % e.p; } else if (a.y != b.y || a.x == gf(0)) return 0; else { alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p; d.x = (e.a + alpha^2 + alpha) % e.p; } d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p; d.e = e; } return (d); } define ec2_pt_dbl(a) { local e, alpha; local obj ec2_pt d; if (istype(a, 1)) return (0); e = a.e; alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p; d.x = (e.a + alpha^2 + alpha) % e.p; d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p; d.e = e; return (d); } define ec2_pt_sub(a, b) { return ec2_pt_add(a, ec2_pt_neg(b)); } define ec2_pt_neg(a) { local obj ec2_pt d; d.x = a.x; d.y = a.x + a.y; d.e = a.e; return (d); } define ec2_pt_check(a) { local e; e = a.e; if ((a.y^2 + a.x * a.y) % e.p != (a.x^3 + e.a * a.x^2 + e.b) % e.p) quit "bad curve point"; } define ec2_pt_mul(a, b) { local p, n; local d; if (istype(a, 1)) { n = a; p = b; } else if (istype(b, 1)) { n = b; p = a; } else return (newerror("bad arguments to ec2_pt_mul")); d = 0; while (n) { if (n & 1) d += p; n >>= 1; p = ec2_pt_dbl(p); } return (d); } /*----- FIPS186-2 standard curves -----------------------------------------*/ b163 = ec2_curve(gf(1),gf(0x20a601907b8c953ca1481eb10512f78744a3205fd), gf(0x800000000000000000000000000000000000000c9)); b163_r = 5846006549323611672814742442876390689256843201587; b163_g = ec2_pt(0x3f0eba16286a2d57ea0991168d4994637e8343e36, 0x0d51fbc6c71a0094fa2cdd545b11c5c0c797324f1, b163); /*----- That's all, folks -------------------------------------------------*/