From 2b5d0102fa12ef20904731b9165785be9999d126 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 7 Mar 2013 12:59:44 +0100 Subject: [PATCH] fix inversion, simplify, remove templates --- secp256k1.cpp | 222 ++++++++++++++++++++++++-------------------------- 1 file changed, 105 insertions(+), 117 deletions(-) diff --git a/secp256k1.cpp b/secp256k1.cpp index 78cd2e4d2e..5a6142d430 100644 --- a/secp256k1.cpp +++ b/secp256k1.cpp @@ -294,7 +294,7 @@ public: // calculate a^p, with p={45,63,1019,1023} FieldElem a2; a2.SetSquare(a); FieldElem a3; a3.SetMult(a2,a); - FieldElem a4; a4.SetSquare(a); + FieldElem a4; a4.SetSquare(a2); FieldElem a5; a5.SetMult(a4,a); FieldElem a10; a10.SetSquare(a5); FieldElem a11; a11.SetMult(a10,a); @@ -361,118 +361,114 @@ public: } }; -template class GroupElemJac; - -template class GroupElem { +class GroupElem { protected: bool fInfinity; - F x; - F y; + FieldElem x; + FieldElem y; public: - void SetXY(const F &xin, const F &yin) { - fInfinity = false; - this->x = xin; - this->y = yin; - } - /** Creates the point at infinity */ GroupElem() { - this->fInfinity = true; + fInfinity = true; } /** Creates the point with given affine coordinates */ - GroupElem(const F &xin, const F &yin) { - SetXY(xin,yin); + GroupElem(const FieldElem &xin, const FieldElem &yin) { + fInfinity = false; + x = xin; + y = yin; } /** Checks whether this is the point at infinity */ bool IsInfinity() const { - return this->fInfinity; + return fInfinity; } - void SetNeg(GroupElem &p) { - this->fInfinity = p.fInfinity; - this->x = p.x; + void SetNeg(GroupElem &p) { + fInfinity = p.fInfinity; + x = p.x; p.y.Normalize(); - this->y.SetNeg(p.y, 1); + y.SetNeg(p.y, 1); } std::string ToString() { - if (this->fInfinity) + if (fInfinity) return "(inf)"; - return "(" + this->x.ToString() + "," + this->y.ToString() + ")"; + return "(" + x.ToString() + "," + y.ToString() + ")"; } - friend class GroupElemJac; + friend class GroupElemJac; }; -template class GroupElemJac : public GroupElem { +class GroupElemJac : public GroupElem { protected: - F z; + FieldElem z; public: /** Creates the point at infinity */ - GroupElemJac() : GroupElem(), z(1) {} + GroupElemJac() : GroupElem(), z(1) {} /** Creates the point with given affine coordinates */ - GroupElemJac(const F &xin, const F &yin) : GroupElem(xin,yin), z(1) {} + GroupElemJac(const FieldElem &xin, const FieldElem &yin) : GroupElem(xin,yin), z(1) {} /** Checks whether this is a non-infinite point on the curve */ bool IsValid() { - if (this->IsInfinity()) + if (IsInfinity()) return false; // y^2 = x^3 + 7 // (Y/Z^3)^2 = (X/Z^2)^3 + 7 // Y^2 / Z^6 = X^3 / Z^6 + 7 // Y^2 = X^3 + 7*Z^6 - F y2; y2.SetSquare(this->y); - F x3; x3.SetSquare(this->x); x3.SetMult(x3,this->x); - F z2; z2.SetSquare(this->z); - F z6; z6.SetSquare(z2); z6.SetMult(z6,z2); + FieldElem y2; y2.SetSquare(y); + FieldElem x3; x3.SetSquare(x); x3.SetMult(x3,x); + FieldElem z2; z2.SetSquare(z); + FieldElem z6; z6.SetSquare(z2); z6.SetMult(z6,z2); z6 *= 7; x3 += z6; return y2 == x3; } /** Returns the affine coordinates of this point */ - void GetAffine(GroupElem &aff) { + void GetAffine(GroupElem &aff) { z.SetInverse(z); - F z2; + FieldElem z2; z2.SetSquare(z); - F z3; + FieldElem z3; z3.SetMult(z,z2); - this->x.SetMult(this->x,z2); - this->y.SetMult(this->y,z3); - this->z = F(1); - aff.SetXY(this->x,this->y); + x.SetMult(x,z2); + y.SetMult(y,z3); + z = FieldElem(1); + aff.fInfinity = false; + aff.x = x; + aff.y = y; } /** Sets this point to have a given X coordinate & given Y oddness */ - void SetCompressed(const F &xin, bool fOdd) { - this->x = xin; - F x2; x2.SetSquare(this->x); - F x3; x3.SetMult(this->x,x2); - this->fInfinity = false; - F c(7); + void SetCompressed(const FieldElem &xin, bool fOdd) { + x = xin; + FieldElem x2; x2.SetSquare(x); + FieldElem x3; x3.SetMult(x,x2); + fInfinity = false; + FieldElem c(7); c += x3; - this->y.SetSquareRoot(c); - this->z = F(1); - if (this->y.IsOdd() != fOdd) - this->y.SetNeg(this->y,1); + y.SetSquareRoot(c); + z = FieldElem(1); + if (y.IsOdd() != fOdd) + y.SetNeg(y,1); } /** Sets this point to be the EC double of another */ - void SetDouble(const GroupElemJac &p) { - if (p.fInfinity || this->y.IsZero()) { - this->fInfinity = true; + void SetDouble(const GroupElemJac &p) { + if (p.fInfinity || y.IsZero()) { + fInfinity = true; return; } - F t1,t2,t3,t4,t5; - this->z.SetMult(p.y,p.z); - this->z *= 2; // Z' = 2*Y*Z (2) + FieldElem t1,t2,t3,t4,t5; + z.SetMult(p.y,p.z); + z *= 2; // Z' = 2*Y*Z (2) t1.SetSquare(p.x); t1 *= 3; // T1 = 3*X^2 (3) t2.SetSquare(t1); // T2 = 9*X^4 (1) @@ -481,20 +477,20 @@ public: t4.SetSquare(t3); t4 *= 2; // T4 = 8*Y^4 (2) t3.SetMult(p.x,t3); // T3 = 2*X*Y^2 (1) - this->x = t3; - this->x *= 4; // X' = 8*X*Y^2 (4) - this->x.SetNeg(this->x,4); // X' = -8*X*Y^2 (5) - this->x += t2; // X' = 9*X^4 - 8*X*Y^2 (6) + x = t3; + x *= 4; // X' = 8*X*Y^2 (4) + x.SetNeg(x,4); // X' = -8*X*Y^2 (5) + x += t2; // X' = 9*X^4 - 8*X*Y^2 (6) t2.SetNeg(t2,1); // T2 = -9*X^4 (2) t3 *= 6; // T3 = 12*X*Y^2 (6) t3 += t2; // T3 = 12*X*Y^2 - 9*X^4 (8) - this->y.SetMult(t1,t3); // Y' = 36*X^3*Y^2 - 27*X^6 (1) + y.SetMult(t1,t3); // Y' = 36*X^3*Y^2 - 27*X^6 (1) t2.SetNeg(t4,2); // T2 = -8*Y^4 (3) - this->y += t2; // Y' = 36*X^3*Y^2 - 27*X^6 - 8*Y^4 (4) + y += t2; // Y' = 36*X^3*Y^2 - 27*X^6 - 8*Y^4 (4) } /** Sets this point to be the EC addition of two others */ - void SetAdd(const GroupElemJac &p, const GroupElemJac &q) { + void SetAdd(const GroupElemJac &p, const GroupElemJac &q) { if (p.fInfinity) { *this = q; return; @@ -503,79 +499,79 @@ public: *this = p; return; } - this->fInfinity = false; - const F &x1 = p.x, &y1 = p.y, &z1 = p.z, &x2 = q.x, &y2 = q.y, &z2 = q.z; - F z22; z22.SetSquare(z2); - F z12; z12.SetSquare(z1); - F u1; u1.SetMult(x1, z22); - F u2; u2.SetMult(x2, z12); - F s1; s1.SetMult(y1, z22); s1.SetMult(s1, z2); - F s2; s2.SetMult(y2, z12); s2.SetMult(s2, z1); + fInfinity = false; + const FieldElem &x1 = p.x, &y1 = p.y, &z1 = p.z, &x2 = q.x, &y2 = q.y, &z2 = q.z; + FieldElem z22; z22.SetSquare(z2); + FieldElem z12; z12.SetSquare(z1); + FieldElem u1; u1.SetMult(x1, z22); + FieldElem u2; u2.SetMult(x2, z12); + FieldElem s1; s1.SetMult(y1, z22); s1.SetMult(s1, z2); + FieldElem s2; s2.SetMult(y2, z12); s2.SetMult(s2, z1); if (u1 == u2) { if (s1 == s2) { SetDouble(p); } else { - this->fInfinity = true; + fInfinity = true; } return; } - F h; h.SetNeg(u1,1); h += u2; - F r; r.SetNeg(s1,1); r += s2; - F r2; r2.SetSquare(r); - F h2; h2.SetSquare(h); - F h3; h3.SetMult(h,h2); - this->z.SetMult(z1,z2); this->z.SetMult(z, h); - F t; t.SetMult(u1,h2); - this->x = t; this->x *= 2; this->x += h3; this->x.SetNeg(this->x,3); this->x += r2; - this->y.SetNeg(this->x,5); this->y += t; this->y.SetMult(this->y,r); + FieldElem h; h.SetNeg(u1,1); h += u2; + FieldElem r; r.SetNeg(s1,1); r += s2; + FieldElem r2; r2.SetSquare(r); + FieldElem h2; h2.SetSquare(h); + FieldElem h3; h3.SetMult(h,h2); + z.SetMult(z1,z2); z.SetMult(z, h); + FieldElem t; t.SetMult(u1,h2); + x = t; x *= 2; x += h3; x.SetNeg(x,3); x += r2; + y.SetNeg(x,5); y += t; y.SetMult(y,r); h3.SetMult(h3,s1); h3.SetNeg(h3,1); - this->y += h3; + y += h3; } /** Sets this point to be the EC addition of two others (one of which is in affine coordinates) */ - void SetAdd(const GroupElemJac &p, const GroupElem &q) { + void SetAdd(const GroupElemJac &p, const GroupElem &q) { if (p.fInfinity) { - this->x = q.x; - this->y = q.y; - this->fInfinity = q.fInfinity; - this->z = F(1); + x = q.x; + y = q.y; + fInfinity = q.fInfinity; + z = FieldElem(1); return; } if (q.fInfinity) { *this = p; return; } - this->fInfinity = false; - const F &x1 = p.x, &y1 = p.y, &z1 = p.z, &x2 = q.x, &y2 = q.y; - F z12; z12.SetSquare(z1); - F u1 = x1; u1.Normalize(); - F u2; u2.SetMult(x2, z12); - F s1 = y1; s1.Normalize(); - F s2; s2.SetMult(y2, z12); s2.SetMult(s2, z1); + fInfinity = false; + const FieldElem &x1 = p.x, &y1 = p.y, &z1 = p.z, &x2 = q.x, &y2 = q.y; + FieldElem z12; z12.SetSquare(z1); + FieldElem u1 = x1; u1.Normalize(); + FieldElem u2; u2.SetMult(x2, z12); + FieldElem s1 = y1; s1.Normalize(); + FieldElem s2; s2.SetMult(y2, z12); s2.SetMult(s2, z1); if (u1 == u2) { if (s1 == s2) { SetDouble(p); } else { - this->fInfinity = true; + fInfinity = true; } return; } - F h; h.SetNeg(u1,1); h += u2; - F r; r.SetNeg(s1,1); r += s2; - F r2; r2.SetSquare(r); - F h2; h2.SetSquare(h); - F h3; h3.SetMult(h,h2); - this->z = p.z; this->z.SetMult(z, h); - F t; t.SetMult(u1,h2); - this->x = t; this->x *= 2; this->x += h3; this->x.SetNeg(this->x,3); this->x += r2; - this->y.SetNeg(this->x,5); this->y += t; this->y.SetMult(this->y,r); + FieldElem h; h.SetNeg(u1,1); h += u2; + FieldElem r; r.SetNeg(s1,1); r += s2; + FieldElem r2; r2.SetSquare(r); + FieldElem h2; h2.SetSquare(h); + FieldElem h3; h3.SetMult(h,h2); + z = p.z; z.SetMult(z, h); + FieldElem t; t.SetMult(u1,h2); + x = t; x *= 2; x += h3; x.SetNeg(x,3); x += r2; + y.SetNeg(x,5); y += t; y.SetMult(y,r); h3.SetMult(h3,s1); h3.SetNeg(h3,1); - this->y += h3; + y += h3; } std::string ToString() { - GroupElem aff; - this->GetAffine(aff); + GroupElem aff; + GetAffine(aff); return aff.ToString(); } }; @@ -587,17 +583,9 @@ using namespace secp256k1; int main() { FieldElem f1,f2; f1.SetHex("8b30bbe9ae2a990696b22f670709dff3727fd8bc04d3362c6c7bf458e2846004"); - f2.SetHex("a357ae915c4a65281309edf20504740f1eb3333990216b4f81063cb65f2f7e0f"); - GroupElemJac g1; g1.SetCompressed(f1,false); - GroupElemJac g2; g2.SetCompressed(f2,false); - printf("g1: %s (%s)\n", g1.ToString().c_str(), g1.IsValid() ? "ok" : "fail"); - printf("g2: %s (%s)\n", g2.ToString().c_str(), g2.IsValid() ? "ok" : "fail"); - GroupElem g2a; g2.GetAffine(g2a); - printf("g2a:%s\n", g2a.ToString().c_str()); - GroupElemJac x1 = g1, x2 = g1; - for (int i=0; i<100000000; i++) { - x1.SetAdd(x1,g2a); + for (int i=0; i<1000000; i++) { + f1.SetInverse(f1); + f1 *= 2; } - printf("res:%s (%s)\n", x1.ToString().c_str(), x1.IsValid() ? "ok" : "fail"); return 0; }