From ef99ce6042aede3fcba7a708c02b7d669ba8388d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 11 Aug 2026 00:23:52 +0200 Subject: [PATCH] crypto: Multiply Fq2 with the Karatsuba method multiply(Fq2) was schoolbook, 4 base field multiplications, while every other level of the tower already used Karatsuba. Trading one multiplication for three additions pays here because a Montgomery multiplication costs several times an addition: about 6% off the ECPAIRING instruction count. --- lib/evmone_precompiles/pairing/bn254/fields.hpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/evmone_precompiles/pairing/bn254/fields.hpp b/lib/evmone_precompiles/pairing/bn254/fields.hpp index 9449a6221b..99406e68c3 100644 --- a/lib/evmone_precompiles/pairing/bn254/fields.hpp +++ b/lib/evmone_precompiles/pairing/bn254/fields.hpp @@ -42,7 +42,13 @@ constexpr Fq2 multiply(const Fq2& a, const Fq2& b) noexcept { const auto& [a0, a1] = a.coeffs; const auto& [b0, b1] = b.coeffs; - return Fq2({a0 * b0 - a1 * b1, a1 * b0 + a0 * b1}); + + // Karatsuba: for u^2 == -1 the product is (t0 - t1) + ((a0+a1)(b0+b1) - t0 - t1)*u, + // trading one of the schoolbook's 4 base field multiplications for 3 additions. + // TODO: Lazy reduction (https://eprint.iacr.org/2010/526) needs wide mul and redc in ModArith. + const auto t0 = a0 * b0; + const auto t1 = a1 * b1; + return Fq2({t0 - t1, (a0 + a1) * (b0 + b1) - t0 - t1}); } /// Squares an Fq^2 field element.