From f492676a550f5689b8e85f7fc43e4e6152061e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 10 Aug 2026 21:01:27 +0200 Subject: [PATCH] crypto: Use the generic ecc::dbl for G2 point doubling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bn254::dbl duplicated the generic a=0 Jacobian doubling already in ecc::dbl. Both cost 18 Fq multiplications, but bn254::dbl reached the z coordinate via (y+z)² − y² − z², needing a z² that has no other use there, where ecc::dbl takes 2yz directly: same multiplication count, about a dozen fewer modular additions per doubling, and a G2 subgroup check does 63 doublings per pair. lin_func_and_dbl keeps its own copy of the formula, since it needs z² for the line coefficients anyway. Cuts about 0.3% off the ECPAIRING instruction count. --- lib/evmone_precompiles/ecc.hpp | 2 +- .../pairing/bn254/utils.hpp | 27 ------------------- 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/lib/evmone_precompiles/ecc.hpp b/lib/evmone_precompiles/ecc.hpp index e71d2b6241..968b91c925 100644 --- a/lib/evmone_precompiles/ecc.hpp +++ b/lib/evmone_precompiles/ecc.hpp @@ -371,7 +371,7 @@ ProjPoint add(const ProjPoint& p, const AffinePoint& q) noe } template -ProjPoint dbl(const ProjPoint& p) noexcept +constexpr ProjPoint dbl(const ProjPoint& p) noexcept { const auto& [x1, y1, z1] = p; diff --git a/lib/evmone_precompiles/pairing/bn254/utils.hpp b/lib/evmone_precompiles/pairing/bn254/utils.hpp index 0c5f8ced1e..3179ba5408 100644 --- a/lib/evmone_precompiles/pairing/bn254/utils.hpp +++ b/lib/evmone_precompiles/pairing/bn254/utils.hpp @@ -192,33 +192,6 @@ constexpr ecc::ProjPoint add( return {X3, Y3, Z3}; } -/// Computes `Q + Q` in Jacobian coordinates. -constexpr ecc::ProjPoint dbl(const ecc::ProjPoint& Q) noexcept -{ - const auto& x = Q.x; - const auto& y = Q.y; - const auto& z = Q.z; - - const auto y_squared = y * y; - const auto x_squared = x * x; - const auto z_squared = z * z; - const auto y_4 = y_squared * y_squared; - const auto _4y_4 = y_4 + y_4 + y_4 + y_4; - - const auto R = y_squared + y_squared; - const auto A = (x + R); - const auto S = A * A - x_squared - _4y_4; // 2xR = (x+R)^2 - x^2 - R^2 - const auto M = x_squared + x_squared + x_squared; - - const auto N = y + z; - - const auto Xp = M * M - (S + S); - const auto Yp = M * (S - Xp) - (_4y_4 + _4y_4); - const auto Zp = N * N - y_squared - z_squared; // 2yz = (y+z)^2 - y^2 - z^2 - - return {Xp, Yp, Zp}; -} - /// Computes `N` doubles of the point `a` in Jacobian coordinates. template constexpr ecc::ProjPoint n_dbl(const ecc::ProjPoint& a) noexcept