diff options
Diffstat (limited to 'src/math/gfpmath/point_gfp.cpp')
-rw-r--r-- | src/math/gfpmath/point_gfp.cpp | 76 |
1 files changed, 29 insertions, 47 deletions
diff --git a/src/math/gfpmath/point_gfp.cpp b/src/math/gfpmath/point_gfp.cpp index fbe5d5406..cf1e8a882 100644 --- a/src/math/gfpmath/point_gfp.cpp +++ b/src/math/gfpmath/point_gfp.cpp @@ -154,32 +154,29 @@ PointGFp& PointGFp::operator-=(const PointGFp& rhs) PointGFp& PointGFp::operator*=(const BigInt& scalar) { - PointGFp H(this->curve); // create as zero - PointGFp P(*this); - BigInt m(scalar); - - if(m < BigInt(0)) + if(scalar == 0) { - m.flip_sign(); - P.negate(); + *this = PointGFp(curve); + return *this; } - - // Move upwards - if(P.is_zero() || (m == BigInt(0))) + else if(scalar == 1) + return *this; + else if(scalar == -1) { - *this = H; + this->negate(); return *this; } - // FIXME: *this != P if m was -1 ! - if(m == BigInt(1)) //*this == P already - return *this; + PointGFp H(this->curve); // create as zero + PointGFp P(*this); + + if(scalar.is_negative()) + P.negate(); - const int l = m.bits() - 1; - for(int i = l; i >= 0; --i) + for(int i = scalar.bits() - 1; i >= 0; --i) { H.mult2_in_place(); - if(m.get_bit(i)) + if(scalar.get_bit(i)) H += P; } @@ -210,47 +207,32 @@ PointGFp& PointGFp::mult2_in_place() return *this; } - GFpElement point_x(curve.get_p(), coord_x); - GFpElement point_y(curve.get_p(), coord_y); - GFpElement point_z(curve.get_p(), coord_z); - - GFpElement Y_squared = point_y*point_y; + Modular_Reducer mod_p(curve.get_p()); - GFpElement S = point_x * Y_squared; + BigInt y_2 = mod_p.square(coord_y); - GFpElement x = S + S; + BigInt S = mod_p.multiply(4, mod_p.multiply(coord_x, y_2)); - S = x + x; - - GFpElement a_z4(curve.get_p(), curve.get_a()); - - GFpElement z2 = point_z * point_z; - a_z4 *= z2; - a_z4 *= z2; + BigInt a_z4 = mod_p.multiply(curve.get_a(), + mod_p.square(mod_p.square(coord_z))); - GFpElement y(point_x * point_x); + BigInt M = mod_p.reduce(a_z4 + 3 * mod_p.square(coord_x)); - GFpElement M(y + y + y + a_z4); + BigInt x = mod_p.reduce(mod_p.square(M) - mod_p.multiply(2, S)); - x = M * M - (S+S); + BigInt y = mod_p.square(y_2); - y = Y_squared * Y_squared; + BigInt z = mod_p.multiply(2, mod_p.reduce(y + y)); - GFpElement U(y + y); + BigInt U = mod_p.reduce(z + z); - GFpElement z = U + U; + y = mod_p.reduce(mod_p.multiply(M, S - x) - U); - U = z + z; + z = mod_p.multiply(2, mod_p.multiply(coord_y, coord_z)); - y = M * (S - x) - U; - - z = point_y * point_z; - - z = z + z; - - coord_x = x.get_value(); - coord_y = y.get_value(); - coord_z = z.get_value(); + coord_x = x; + coord_y = y; + coord_z = z; return *this; } |