8000 precompiles: Use classic EC point add formula by chfast · Pull Request #1165 · ethereum/evmone · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

precompiles: Use classic EC point add formula #1165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
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
12 changes: 2 additions & 10 deletions lib/evmone_precompiles/bn254.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,9 @@ bool validate(const Point& pt) noexcept
return y2 == x3_3;
}

Point add(const Point& pt1, const Point& pt2) noexcept
Point add(const Point& p, const Point& q) noexcept
{
if (pt1.is_inf())
return pt2;
if (pt2.is_inf())
return pt1;

// b3 == 9 for y^2 == x^3 + 3
const auto r = ecc::add(Fp, ecc::to_proj(Fp, pt1), ecc::to_proj(Fp, pt2), B3);

return ecc::to_affine(Fp, r);
return ecc::add(Fp, p, q);
}

Point mul(const Point& pt, const uint256& c) noexcept
Expand Down
2 changes: 1 addition & 1 deletion lib/evmone_precompiles/bn254.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool validate(const Point& pt) noexcept;
/// Addition in bn254 curve group.
///
/// Computes P ⊕ Q for two points in affine coordinates on the bn254 curve,
Point add(const Point& pt1, const Point& pt2) noexcept;
Point add(const Point& p, const Point& q) noexcept;

/// Scalar multiplication in bn254 curve group.
///
Expand Down
37 changes: 37 additions & 0 deletions lib/evmone_precompiles/ecc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,43 @@ inline Point<IntT> to_affine(const ModArith<IntT>& s, const ProjPoint<IntT>& p)
return {s.from_mont(s.mul(p.x, z_inv)), s.from_mont(s.mul(p.y, z_inv))};
}

/// Adds two elliptic curve points in affine coordinates
/// and returns the result in affine coordinates.
template <typename IntT>
Point<IntT> add(const ModArith<IntT>& m, const Point<IntT>& p, const Point<IntT>& q) noexcept
{
if (p.is_inf())
return q;
if (q.is_inf())
return p;

const auto x1 = m.to_mont(p.x);
const auto y1 = m.to_mont(p.y);
const auto x2 = m.to_mont(q.x);
const auto y2 = m.to_mont(q.y);

// Use classic formula for point addition.
// https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Point_operations

auto dx = m.sub(x2, x1);
auto dy = m.sub(y2, y1);
if (dx == 0)
{
if (dy != 0) // For opposite points
return {}; // return the point at infinity.

// For coincident points find the slope of the tangent line.
const auto xx = m.mul(x1, x1);
dy = m.add(m.add(xx, xx), xx);
dx = m.add(y1, y1);
}
const auto slope = m.mul(dy, m.inv(dx));

const auto xr = m.sub(m.sub(m.mul(slope, slope), x1), x2);
const auto yr = m.sub(m.mul(m.sub(x1, xr), slope), y1);
return {m.from_mont(xr), m.from_mont(yr)};
}

template <typename IntT, int A = 0>
ProjPoint<IntT> add(const evmmax::ModArith<IntT>& s, const ProjPoint<IntT>& p,
const ProjPoint<IntT>& q, const IntT& b3) noexcept
Comment on lines 131 to 133
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not drop it if we have general formula?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needed for multiplication where projective coordinates are used.

Expand Down
0