Skip to content

Commit

Permalink
Update subtree
Browse files Browse the repository at this point in the history
  • Loading branch information
MauroToscano committed Feb 28, 2024
1 parent 314c3c1 commit 61938be
Show file tree
Hide file tree
Showing 20 changed files with 58 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
object "EcAdd" {
code {
return(0, 0)
}
code { }
object "EcAdd_deployed" {
code {
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -45,18 +43,18 @@ object "EcAdd" {
//////////////////////////////////////////////////////////////////

/// @dev Executes the `precompileCall` opcode.
function precompileCall(precompileParams, gasToBurn) -> ret {
// Compiler simulation for calling `precompileCall` opcode
ret := verbatim_2i_1o("precompile", precompileParams, gasToBurn)
}
function precompileCall(precompileParams, gasToBurn) -> ret {
// Compiler simulation for calling `precompileCall` opcode
ret := verbatim_2i_1o("precompile", precompileParams, gasToBurn)
}

/// @notice Burns remaining gas until revert.
/// @dev This function is used to burn gas in the case of a failed precompile call.
function burnGas() {
// Precompiles that do not have a circuit counterpart
// will burn the provided gas by calling this function.
precompileCall(0, gas())
}
function burnGas() {
// Precompiles that do not have a circuit counterpart
// will burn the provided gas by calling this function.
precompileCall(0, gas())
}

/// @notice Retrieves the highest half of the multiplication result.
/// @param multiplicand The value to multiply.
Expand Down Expand Up @@ -247,7 +245,7 @@ object "EcAdd" {
/// @dev See https://en.wikipedia.org/wiki/Montgomery_modular_multiplication#The_REDC_algorithm for further details on the Montgomery multiplication.
/// @param minuend The minuend in Montgomery form.
/// @param subtrahend The subtrahend in Montgomery form.
/// @return ret The result of the Montgomery subtraction.
/// @return ret The result of the Montgomery addition.
function montgomerySub(minuend, subtrahend) -> ret {
ret := montgomeryAdd(minuend, sub(P(), subtrahend))
}
Expand Down Expand Up @@ -300,7 +298,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 +321,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 +366,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 +378,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 +391,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 +418,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
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
object "EcMul" {
code {
return(0, 0)
}
code { }
object "EcMul_deployed" {
code {
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -59,18 +57,18 @@ object "EcMul" {
// ////////////////////////////////////////////////////////////////

/// @dev Executes the `precompileCall` opcode.
function precompileCall(precompileParams, gasToBurn) -> ret {
// Compiler simulation for calling `precompileCall` opcode
ret := verbatim_2i_1o("precompile", precompileParams, gasToBurn)
}
function precompileCall(precompileParams, gasToBurn) -> ret {
// Compiler simulation for calling `precompileCall` opcode
ret := verbatim_2i_1o("precompile", precompileParams, gasToBurn)
}

/// @notice Burns remaining gas until revert.
/// @dev This function is used to burn gas in the case of a failed precompile call.
function burnGas() {
// Precompiles that do not have a circuit counterpart
// will burn the provided gas by calling this function.
precompileCall(0, gas())
}
function burnGas() {
// Precompiles that do not have a circuit counterpart
// will burn the provided gas by calling this function.
precompileCall(0, gas())
}

/// @notice Retrieves the highest half of the multiplication result.
/// @param multiplicand The value to multiply.
Expand Down Expand Up @@ -225,7 +223,7 @@ object "EcMul" {
/// @dev See https://en.wikipedia.org/wiki/Montgomery_modular_multiplication#The_The_REDC_algorithm for further details on the Montgomery multiplication.
/// @param minuend The minuend in Montgomery form.
/// @param subtrahend The subtrahend in Montgomery form.
/// @return ret The result of the Montgomery subtraction.
/// @return ret The result of the Montgomery addition.
function montgomerySub(minuend, subtrahend) -> ret {
ret := montgomeryAdd(minuend, sub(P(), subtrahend))
}
Expand Down Expand Up @@ -389,6 +387,8 @@ object "EcMul" {

if affinePointIsInfinity(x, y) {
// Infinity * scalar = Infinity
mstore(0x00, 0x00)
mstore(0x20, 0x00)
return(0x00, 0x40)
}

Expand All @@ -402,6 +402,8 @@ object "EcMul" {

if eq(scalar, 0) {
// P * 0 = Infinity
mstore(0x00, 0x00)
mstore(0x20, 0x00)
return(0x00, 0x40)
}
if eq(scalar, 1) {
Expand Down
2 changes: 1 addition & 1 deletion .test-node-subtree/scripts/refresh_contracts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ for contract in "${contracts[@]}"; do
cp $SRC_DIR/$contract.sol/$contract.json $DST_DIR
done

precompiles=("EcAdd" "EcMul" "EcPairing" "Ecrecover" "Keccak256" "ModExp" "P256VERIFY" "SHA256" "secp256k1VERIFY")
precompiles=("EcAdd" "EcMul" "EcPairing" "Ecrecover" "Keccak256" "ModExp" "P256VERIFY" "SHA256" "secp256k1VERIFY" "EcAddG2" "EcMulG2")

for precompile in "${precompiles[@]}"; do
cp etc/system-contracts/contracts/precompiles/artifacts/$precompile.yul/$precompile.yul.zbin $DST_DIR
Expand Down
Binary file modified .test-node-subtree/src/deps/contracts/EcAdd.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/EcMul.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/EcPairing.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/Ecrecover.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/EventWriter.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/Keccak256.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/ModExp.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/P256VERIFY.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/SHA256.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/fee_estimate.yul.zbin
Binary file not shown.
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/gas_test.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/playground_batch.yul.zbin
Binary file not shown.
Binary file modified .test-node-subtree/src/deps/contracts/proved_batch.yul.zbin
Binary file not shown.
Binary file not shown.
Binary file not shown.
22 changes: 22 additions & 0 deletions .test-node-subtree/src/deps/system_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ pub const ECADD_PRECOMPILE_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x06,
]);

/// The `ecAddG2` system contract address.
pub const ECADD_G2_PRECOMPILE_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0A,
]);

/// The `ecMul` system contract address.
pub const ECMUL_PRECOMPILE_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x07,
]);

/// The `ecMulG2` system contract address.
pub const ECMUL_G2_PRECOMPILE_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0B,
]);

/// The `ecPairing` system contract address.
pub const ECPAIRING_PRECOMPILE_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -150,11 +162,21 @@ pub static COMPILED_IN_SYSTEM_CONTRACTS: Lazy<Vec<DeployedContract>> = Lazy::new
ECADD_PRECOMPILE_ADDRESS,
include_bytes!("contracts/EcAdd.yul.zbin").to_vec(),
),
(
"ECADD_G2_PRECOMPILE_ADDRESS",
ECADD_G2_PRECOMPILE_ADDRESS,
include_bytes!("contracts/EcAddG2.yul.zbin").to_vec(),
),
(
"ECMUL_PRECOMPILE_ADDRESS",
ECMUL_PRECOMPILE_ADDRESS,
include_bytes!("contracts/EcMul.yul.zbin").to_vec(),
),
(
"ECMUL_G2_PRECOMPILE_ADDRESS",
ECMUL_G2_PRECOMPILE_ADDRESS,
include_bytes!("contracts/EcMulG2.yul.zbin").to_vec(),
),
(
"ECPAIRING_PRECOMPILE_ADDRESS",
ECPAIRING_PRECOMPILE_ADDRESS,
Expand Down

0 comments on commit 61938be

Please sign in to comment.