Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/evmone_precompiles/pairing/bn254/fields.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ constexpr Fq2 multiply(const Fq2& a, const Fq2& b)
return Fq2({a0 * b0 - a1 * b1, a1 * b0 + a0 * b1});
}

/// Squares an Fq^2 field element.
constexpr Fq2 sqr(const Fq2& a)
{
const auto& [a0, a1] = a.coeffs;

// (a0 + a1*u)^2 = (a0+a1)*(a0-a1) + 2a0a1*u.
const auto a0a1 = a0 * a1;
return Fq2({(a0 + a1) * (a0 - a1), a0a1 + a0a1});
}

/// Multiplies two Fq^6 field elements
constexpr Fq6 multiply(const Fq6& a, const Fq6& b)
{
Expand Down
8 changes: 7 additions & 1 deletion lib/evmone_precompiles/pairing/field_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ struct ExtFieldElem
return ExtFieldElem(ret);
}

friend constexpr ExtFieldElem operator*(const ExtFieldElem& e1, const ExtFieldElem& e2) noexcept
[[gnu::always_inline]] friend constexpr ExtFieldElem operator*(
const ExtFieldElem& e1, const ExtFieldElem& e2) noexcept
{
if constexpr (requires { sqr(e1); }) // Use sqr() if available.
{
if (&e1 == &e2)
return sqr(e1);
}
return multiply(e1, e2);
}

Expand Down