X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/a2a74efe4b1276b1efec12f25fd65f0df060f38a..bc985cefafea2e1b02095a2ff2a9982c4c647d17:/calc/ecp.cal diff --git a/calc/ecp.cal b/calc/ecp.cal index 04971aa..3834359 100644 --- a/calc/ecp.cal +++ b/calc/ecp.cal @@ -1,6 +1,6 @@ /* -*-apcalc-*- * - * $Id: ecp.cal,v 1.1 2000/10/08 16:01:37 mdw Exp $ + * $Id: ecp.cal,v 1.3 2004/03/23 15:19:32 mdw Exp $ * * Testbed for elliptic curve arithmetic over prime fields * @@ -30,6 +30,18 @@ /*----- Revision history --------------------------------------------------* * * $Log: ecp.cal,v $ + * Revision 1.3 2004/03/23 15:19:32 mdw + * Test elliptic curves more thoroughly. + * + * Revision 1.2 2004/03/21 22:52:06 mdw + * Merge and close elliptic curve branch. + * + * Revision 1.1.4.2 2004/03/20 00:13:31 mdw + * Projective coordinates for prime curves + * + * Revision 1.1.4.1 2003/06/10 13:43:53 mdw + * Simple (non-projective) curves over prime fields now seem to work. + * * Revision 1.1 2000/10/08 16:01:37 mdw * Prototypes of various bits of code. * @@ -96,6 +108,20 @@ define ecp_pt_add(a, b) return (d); } +define ecp_pt_dbl(a) +{ + local e, alpha; + local obj ecp_pt d; + if (istype(a, 1)) + return (0); + e = a.e; + alpha = (3 * a.x^2 + e.a) * minv(2 * a.y, e.p) % e.p; + d.x = (alpha^2 - 2 * a.x) % e.p; + d.y = (-a.y + alpha * (a.x - d.x)) % e.p; + d.e = e; + return (d); +} + define ecp_pt_neg(a) { local obj ecp_pt d; @@ -105,6 +131,15 @@ define ecp_pt_neg(a) return (d); } +define ecp_pt_check(a) +{ + local e; + + e = a.e; + if (a.y^2 % e.p != (a.x^3 + e.a * a.x + e.b) % e.p) + quit "bad curve point"; +} + define ecp_pt_mul(a, b) { local p, n; @@ -124,10 +159,18 @@ define ecp_pt_mul(a, b) if (n & 1) d += p; n >>= 1; - p += p; + p = ecp_pt_dbl(p); } return (d); } +/*----- FIPS186-2 standard curves -----------------------------------------*/ + +p192 = ecp_curve(-3, 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1, + 6277101735386680763835789423207666416083908700390324961279); +p192_r = 6277101735386680763835789423176059013767194773182842284081; +p192_g = ecp_pt(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012, + 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811, p192); + /*----- That's all, folks -------------------------------------------------*/