diff options
author | Jack Lloyd <[email protected]> | 2018-12-01 08:54:24 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-12-01 12:12:58 -0500 |
commit | f780cde67afac7b6213c801fb0edcc2eccdffe59 (patch) | |
tree | 67c96decf93426ed995cba92af261e1c43287092 /src/lib/pubkey/ec_group | |
parent | 1e9e5d2f3bdac32838ad99b5718cad46cca693f3 (diff) |
Add BigInt::mod_mul
Diffstat (limited to 'src/lib/pubkey/ec_group')
-rw-r--r-- | src/lib/pubkey/ec_group/curve_gfp.cpp | 9 | ||||
-rw-r--r-- | src/lib/pubkey/ec_group/curve_gfp.h | 7 | ||||
-rw-r--r-- | src/lib/pubkey/ec_group/point_gfp.cpp | 21 |
3 files changed, 10 insertions, 27 deletions
diff --git a/src/lib/pubkey/ec_group/curve_gfp.cpp b/src/lib/pubkey/ec_group/curve_gfp.cpp index bd68a3ed7..f2f5607e1 100644 --- a/src/lib/pubkey/ec_group/curve_gfp.cpp +++ b/src/lib/pubkey/ec_group/curve_gfp.cpp @@ -60,8 +60,6 @@ class CurveGFp_Montgomery final : public CurveGFp_Repr size_t get_ws_size() const override { return 2*m_p_words + 4; } - void redc_mod_p(BigInt& z, secure_vector<word>& ws) const override; - BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const override; void to_curve_rep(BigInt& x, secure_vector<word>& ws) const override; @@ -93,11 +91,6 @@ class CurveGFp_Montgomery final : public CurveGFp_Repr bool m_a_is_minus_3; }; -void CurveGFp_Montgomery::redc_mod_p(BigInt& z, secure_vector<word>& ws) const - { - z.reduce_below(m_p, ws); - } - BigInt CurveGFp_Montgomery::invert_element(const BigInt& x, secure_vector<word>& ws) const { // Should we use Montgomery inverse instead? @@ -207,6 +200,8 @@ class CurveGFp_NIST : public CurveGFp_Repr void from_curve_rep(BigInt& x, secure_vector<word>& ws) const override { redc_mod_p(x, ws); } + virtual void redc_mod_p(BigInt& z, secure_vector<word>& ws) const = 0; + BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const override; void curve_mul_words(BigInt& z, diff --git a/src/lib/pubkey/ec_group/curve_gfp.h b/src/lib/pubkey/ec_group/curve_gfp.h index d03247244..fe7a0a54d 100644 --- a/src/lib/pubkey/ec_group/curve_gfp.h +++ b/src/lib/pubkey/ec_group/curve_gfp.h @@ -49,8 +49,6 @@ class BOTAN_UNSTABLE_API CurveGFp_Repr */ virtual const BigInt& get_1_rep() const = 0; - virtual void redc_mod_p(BigInt& z, secure_vector<word>& ws) const = 0; - virtual BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const = 0; virtual void to_curve_rep(BigInt& x, secure_vector<word>& ws) const = 0; @@ -171,11 +169,6 @@ class BOTAN_UNSTABLE_API CurveGFp final // TODO: from_rep taking && ref - void redc_mod_p(BigInt& z, secure_vector<word>& ws) const - { - m_repr->redc_mod_p(z, ws); - } - void mul(BigInt& z, const BigInt& x, const BigInt& y, secure_vector<word>& ws) const { m_repr->curve_mul(z, x, y, ws); diff --git a/src/lib/pubkey/ec_group/point_gfp.cpp b/src/lib/pubkey/ec_group/point_gfp.cpp index 7bc6c4975..b4b3871cb 100644 --- a/src/lib/pubkey/ec_group/point_gfp.cpp +++ b/src/lib/pubkey/ec_group/point_gfp.cpp @@ -2,7 +2,7 @@ * Point arithmetic on elliptic curves over GF(p) * * (C) 2007 Martin Doering, Christoph Ludwig, Falko Strenzke -* 2008-2011,2012,2014,2015 Jack Lloyd +* 2008-2011,2012,2014,2015,2018 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -152,6 +152,7 @@ void PointGFp::add_affine(const word x_words[], size_t x_size, m_curve.sqr(m_coord_x, T0, ws); m_coord_x.mod_sub(T1, p, sub_ws); + m_coord_x.mod_sub(T3, p, sub_ws); m_coord_x.mod_sub(T3, p, sub_ws); @@ -303,15 +304,13 @@ void PointGFp::mult2(std::vector<BigInt>& ws_bn) m_curve.sqr(T0, m_coord_y, ws); m_curve.mul(T1, m_coord_x, T0, ws); - T1 <<= 2; // * 4 - m_curve.redc_mod_p(T1, sub_ws); + T1.mod_mul(4, p, sub_ws); if(m_curve.a_is_zero()) { // if a == 0 then 3*x^2 + a*z^4 is just 3*x^2 m_curve.sqr(T4, m_coord_x, ws); // x^2 - T4 *= 3; // 3*x^2 - m_curve.redc_mod_p(T4, sub_ws); + T4.mod_mul(3, p, sub_ws); // 3*x^2 } else if(m_curve.a_is_minus_3()) { @@ -330,8 +329,7 @@ void PointGFp::mult2(std::vector<BigInt>& ws_bn) m_curve.mul(T4, T2, T3, ws); // (x-z^2)*(x+z^2) - T4 *= 3; // 3*(x-z^2)*(x+z^2) - m_curve.redc_mod_p(T4, sub_ws); + T4.mod_mul(3, p, sub_ws); // 3*(x-z^2)*(x+z^2) } else { @@ -340,8 +338,7 @@ void PointGFp::mult2(std::vector<BigInt>& ws_bn) m_curve.mul(T3, m_curve.get_a_rep(), T4, ws); // a*z^4 m_curve.sqr(T4, m_coord_x, ws); // x^2 - T4 *= 3; // 3*x^2 - T4.reduce_below(p, sub_ws); + T4.mod_mul(3, p, sub_ws); T4.mod_add(T3, p, sub_ws); // 3*x^2 + a*z^4 } @@ -350,8 +347,7 @@ void PointGFp::mult2(std::vector<BigInt>& ws_bn) T2.mod_sub(T1, p, sub_ws); m_curve.sqr(T3, T0, ws); - T3 <<= 3; - m_curve.redc_mod_p(T3, sub_ws); + T3.mod_mul(8, p, sub_ws); T1.mod_sub(T2, p, sub_ws); @@ -361,8 +357,7 @@ void PointGFp::mult2(std::vector<BigInt>& ws_bn) m_coord_x = T2; m_curve.mul(T2, m_coord_y, m_coord_z, ws); - T2 <<= 1; - m_curve.redc_mod_p(T2, sub_ws); + T2.mod_mul(2, p, sub_ws); m_coord_y = T0; m_coord_z = T2; |