Skip to content
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

Squashed commit of the following: #227

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
object "EcAdd" {
code { }
code {
return(0, 0)
}
object "EcAdd_deployed" {
code {
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -43,18 +45,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 @@ -245,7 +247,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 addition.
/// @return ret The result of the Montgomery subtraction.
function montgomerySub(minuend, subtrahend) -> ret {
ret := montgomeryAdd(minuend, sub(P(), subtrahend))
}
Expand Down Expand Up @@ -298,7 +300,7 @@ object "EcAdd" {
mstore(32, 0)
return(0, 64)
}
if p1IsInfinity {
if and(p1IsInfinity, iszero(p2IsInfinity)) {
// Infinity + P = P

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

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

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

Expand All @@ -378,6 +380,10 @@ 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 @@ -391,11 +397,11 @@ object "EcAdd" {

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

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

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

x3 := outOfMontgomeryForm(x3)
y3 := outOfMontgomeryForm(y3)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
object "EcMul" {
code { }
code {
return(0, 0)
}
object "EcMul_deployed" {
code {
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -57,18 +59,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 @@ -223,7 +225,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 addition.
/// @return ret The result of the Montgomery subtraction.
function montgomerySub(minuend, subtrahend) -> ret {
ret := montgomeryAdd(minuend, sub(P(), subtrahend))
}
Expand Down Expand Up @@ -387,8 +389,6 @@ object "EcMul" {

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

Expand All @@ -402,8 +402,6 @@ 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" "EcAddG2" "EcMulG2")
precompiles=("EcAdd" "EcMul" "EcPairing" "Ecrecover" "Keccak256" "ModExp" "P256VERIFY" "SHA256" "secp256k1VERIFY")

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 modified .test-node-subtree/src/deps/contracts/secp256k1VERIFY.yul.zbin
Binary file not shown.
22 changes: 0 additions & 22 deletions .test-node-subtree/src/deps/system_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@ 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 @@ -162,21 +150,11 @@ 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
Loading