diff options
-rw-r--r-- | checks/ec_tests.cpp | 39 | ||||
-rw-r--r-- | src/math/gfpmath/point_gfp.cpp | 34 | ||||
-rw-r--r-- | src/math/gfpmath/point_gfp.h | 11 |
3 files changed, 42 insertions, 42 deletions
diff --git a/checks/ec_tests.cpp b/checks/ec_tests.cpp index 04840ad07..4115a84f1 100644 --- a/checks/ec_tests.cpp +++ b/checks/ec_tests.cpp @@ -75,14 +75,14 @@ void test_point_turn_on_sp_red_mul() PointGFp p_r1 = r1; PointGFp p_r2 = r2; - p_r1.mult2_in_place(); - p_r2.mult2_in_place(); + p_r1 *= 2; + p_r2 *= 2; CHECK_MESSAGE(p_r1.get_affine_x() == p_r2.get_affine_x(), "error with mult2 after extra turn on sp red mul"); CHECK(p_r1.get_affine_x() != BigInt("0")); CHECK(p_r2.get_affine_x() != BigInt("0")); - r1.mult2_in_place(); + r1 *= 2; - r2.mult2_in_place(); + r2 *= 2; CHECK_MESSAGE(r1 == r2, "error with mult2 after extra turn on sp red mul"); CHECK_MESSAGE(r1.get_affine_x() == r2.get_affine_x(), "error with mult2 after extra turn on sp red mul"); @@ -129,22 +129,17 @@ void test_coordinates() CurveGFp secp160r1 (bi_p_secp, bi_a_secp, bi_b_secp); PointGFp p_G = OS2ECP ( sv_G_secp_comp, secp160r1 ); PointGFp p0 = p_G; - PointGFp p1 = p_G.mult2_in_place(); + PointGFp p1 = p_G * 2; PointGFp point_exp(secp160r1, exp_affine_x, exp_affine_y); + point_exp.check_invariants(); - try - { - point_exp.check_invariants(); - } - catch (Illegal_Point e) - { - assert(false); - } + if(p1.get_jac_proj_x() != exp_x) + std::cout << p1.get_jac_proj_x() << " != " << exp_x << "\n"; + if(p1.get_jac_proj_y() != exp_y) + std::cout << p1.get_jac_proj_y() << " != " << exp_y << "\n"; + if(p1.get_jac_proj_z() != exp_z) + std::cout << p1.get_jac_proj_z() << " != " << exp_z << "\n"; - // testarea - CHECK( p1.get_jac_proj_x() == exp_x); - CHECK( p1.get_jac_proj_y() == exp_y); - CHECK( p1.get_jac_proj_z() == exp_z); CHECK_MESSAGE( p1.get_affine_x() == exp_affine_x, " p1_x = " << p1.get_affine_x() << "\n" << "exp_x = " << exp_affine_x << "\n"); CHECK_MESSAGE( p1.get_affine_y() == exp_affine_y, " p1_y = " << p1.get_affine_y() << "\n" << "exp_y = " << exp_affine_y << "\n"); } @@ -243,7 +238,7 @@ void test_point_negative() CurveGFp secp160r1(bi_p_secp, bi_a_secp, bi_b_secp); PointGFp p_G = OS2ECP ( sv_G_secp_comp, secp160r1 ); - PointGFp p1 = p_G.mult2_in_place(); + PointGFp p1 = p_G *= 2; CHECK( p1.get_jac_proj_x() == exp_p1_x); CHECK( p1.get_jac_proj_y() == exp_p1_y); @@ -368,7 +363,7 @@ void test_add_point() PointGFp p_G = OS2ECP ( sv_G_secp_comp, secp160r1 ); PointGFp p0 = p_G; - PointGFp p1 = p_G.mult2_in_place(); + PointGFp p1 = p_G *= 2; PointGFp expected(secp160r1, exp_add_x, exp_add_y, exp_add_z); @@ -401,7 +396,7 @@ void test_sub_point() PointGFp p_G = OS2ECP ( sv_G_secp_comp, secp160r1 ); PointGFp p0 = p_G; - PointGFp p1 = p_G.mult2_in_place(); + PointGFp p1 = p_G *= 2; p1 -= p0; @@ -433,7 +428,7 @@ void test_mult_point() PointGFp p_G = OS2ECP ( sv_G_secp_comp, secp160r1 ); PointGFp p0 = p_G; - PointGFp p1 = p_G.mult2_in_place(); + PointGFp p1 = p_G *= 2; p1 *= p0.get_jac_proj_x(); @@ -485,7 +480,7 @@ void test_basic_operations() PointGFp p_G = OS2ECP ( sv_G_secp_comp, secp160r1 ); PointGFp p0 = p_G; - PointGFp p1 = p_G.mult2_in_place(); + PointGFp p1 = p_G *= 2; // check that all points have correct values CHECK( p1.get_jac_proj_x() == exp_p1_x); diff --git a/src/math/gfpmath/point_gfp.cpp b/src/math/gfpmath/point_gfp.cpp index 1515e6759..e1992da70 100644 --- a/src/math/gfpmath/point_gfp.cpp +++ b/src/math/gfpmath/point_gfp.cpp @@ -123,16 +123,24 @@ PointGFp& PointGFp::operator-=(const PointGFp& rhs) PointGFp& PointGFp::operator*=(const BigInt& scalar) { - if(scalar == 0) + if(scalar.abs() <= 2) // special cases for small values { - *this = PointGFp(curve); - return *this; - } - else if(scalar == 1) - return *this; - else if(scalar == -1) - { - this->negate(); + u32bit value = scalar.abs().to_u32bit(); + + if(value == 0) + *this = PointGFp(curve); // set to zero point + else if(value == 1) + { + if(scalar.is_negative()) + this->negate(); + } + else if(value == 2) + { + this->mult2_in_place(); + if(scalar.is_negative()) + this->negate(); + } + return *this; } @@ -182,14 +190,14 @@ PointGFp& PointGFp::negate() } // *this *= 2 -PointGFp& PointGFp::mult2_in_place() +void PointGFp::mult2_in_place() { if(is_zero()) - return *this; + return; else if(coord_y.is_zero()) { *this = PointGFp(curve); // setting myself to zero - return *this; + return; } Modular_Reducer mod_p(curve.get_p()); @@ -218,8 +226,6 @@ PointGFp& PointGFp::mult2_in_place() coord_x = x; coord_y = y; coord_z = z; - - return *this; } BigInt PointGFp::get_affine_x() const diff --git a/src/math/gfpmath/point_gfp.h b/src/math/gfpmath/point_gfp.h index 40feb3fa8..e78188759 100644 --- a/src/math/gfpmath/point_gfp.h +++ b/src/math/gfpmath/point_gfp.h @@ -101,12 +101,6 @@ class BOTAN_DLL PointGFp PointGFp& negate(); /** - * Multiply the point by two - * @return *this - */ - PointGFp& mult2_in_place(); - - /** * Return base curve of this point * @result the curve over GF(p) of this point */ @@ -167,6 +161,11 @@ class BOTAN_DLL PointGFp */ bool operator==(const PointGFp& other) const; private: + /** + * Multiply the point by two + */ + void mult2_in_place(); + CurveGFp curve; BigInt coord_x, coord_y, coord_z; }; |