Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

EcAdd precompile implementation #45

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions SystemContractsHashes.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
"contractName": "EcAdd",
"bytecodePath": "contracts/precompiles/artifacts/EcAdd.yul/EcAdd.yul.zbin",
"sourceCodePath": "contracts/precompiles/EcAdd.yul",
"bytecodeHash": "0x010000c5a85a372f441ac693210a18e683b530bed875fdcab2f7e101b057d433",
"sourceCodeHash": "0x32645126b8765e4f7ced63c9508c70edc4ab734843d5f0f0f01d153c27206cee"
"bytecodeHash": "0x010000bb77ebc6b561e21e20b4075757ac005ccca2011f45c80dcfa5318a5cc5",
"sourceCodeHash": "0x0587d539fd7760c226c92c715c0ea9f8a1db9131015a2251dd449a83a96b9d2f"
},
{
"contractName": "EcMul",
Expand Down
34 changes: 12 additions & 22 deletions contracts/precompiles/EcAdd.yul
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ object "EcAdd" {
}
object "EcAdd_deployed" {
code {
////////////////////////////////////////////////////////////////
// CONSTANTS
////////////////////////////////////////////////////////////////
// CONSTANTS

/// @notice Constant function for value three in Montgomery form.
/// @dev This value was precomputed using Python.
Expand Down Expand Up @@ -40,9 +38,7 @@ object "EcAdd" {
ret := 111032442853175714102588374283752698368366046808579839647964533820976443843465
}

//////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS
//////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS

/// @dev Executes the `precompileCall` opcode.
function precompileCall(precompileParams, gasToBurn) -> ret {
Expand Down Expand Up @@ -281,9 +277,7 @@ object "EcAdd" {
quotient := montgomeryMul(dividend, montgomeryModularInverse(divisor))
}

////////////////////////////////////////////////////////////////
// FALLBACK
////////////////////////////////////////////////////////////////
// FALLBACK

// Retrieve the coordinates from the calldata
let x1 := calldataload(0)
Expand All @@ -300,7 +294,7 @@ object "EcAdd" {
mstore(32, 0)
return(0, 64)
}
if and(p1IsInfinity, iszero(p2IsInfinity)) {
if p1IsInfinity {
// Infinity + P = P

// Ensure that the coordinates are between 0 and the field order.
Expand All @@ -323,7 +317,7 @@ object "EcAdd" {
mstore(32, y2)
return(0, 64)
}
if and(iszero(p1IsInfinity), p2IsInfinity) {
if p2IsInfinity {
// P + Infinity = P

// Ensure that the coordinates are between 0 and the field order.
Expand Down Expand Up @@ -368,7 +362,7 @@ object "EcAdd" {
let m_y2 := intoMontgomeryForm(y2)

// Ensure that the points are in the curve (Y^2 = X^3 + 3).
if or(iszero(pointIsInCurve(m_x1, m_y1)), iszero(pointIsInCurve(m_x2, m_y2))) {
if iszero(pointIsInCurve(m_x1, m_y1)) {
burnGas()
}

Expand All @@ -380,10 +374,6 @@ object "EcAdd" {
return(0, 64)
}

if and(eq(x1, x2), and(iszero(eq(y1, y2)), iszero(eq(y1, submod(0, y2, P()))))) {
burnGas()
}

if and(eq(x1, x2), eq(y1, y2)) {
// P + P = 2P

Expand All @@ -397,11 +387,11 @@ object "EcAdd" {

// (3 * x1^2 + a) / (2 * y1)
let x1_squared := montgomeryMul(x, x)
let slope := montgomeryDiv(addmod(x1_squared, addmod(x1_squared, x1_squared, P()), P()), addmod(y, y, P()))
let slope := montgomeryDiv(montgomeryAdd(x1_squared, montgomeryAdd(x1_squared, x1_squared)), montgomeryAdd(y, y))
// x3 = slope^2 - 2 * x1
let x3 := submod(montgomeryMul(slope, slope), addmod(x, x, P()), P())
let x3 := montgomerySub(montgomeryMul(slope, slope), montgomeryAdd(x, x))
// y3 = slope * (x1 - x3) - y1
let y3 := submod(montgomeryMul(slope, submod(x, x3, P())), y, P())
let y3 := montgomerySub(montgomeryMul(slope, montgomerySub(x, x3)), y)

x3 := outOfMontgomeryForm(x3)
y3 := outOfMontgomeryForm(y3)
Expand All @@ -424,11 +414,11 @@ object "EcAdd" {
}

// (y2 - y1) / (x2 - x1)
let slope := montgomeryDiv(submod(y2, y1, P()), submod(x2, x1, P()))
let slope := montgomeryDiv(montgomerySub(y2, y1), montgomerySub(x2, x1))
// x3 = slope^2 - x1 - x2
let x3 := submod(montgomeryMul(slope, slope), addmod(x1, x2, P()), P())
let x3 := montgomerySub(montgomeryMul(slope, slope), montgomeryAdd(x1, x2))
// y3 = slope * (x1 - x3) - y1
let y3 := submod(montgomeryMul(slope, submod(x1, x3, P())), y1, P())
let y3 := montgomerySub(montgomeryMul(slope, montgomerySub(x1, x3)), y1)

x3 := outOfMontgomeryForm(x3)
y3 := outOfMontgomeryForm(y3)
Expand Down
Loading