Skip to content

Commit

Permalink
Replace invalid call to burnGas
Browse files Browse the repository at this point in the history
  • Loading branch information
IAvecilla committed Oct 2, 2023
1 parent b01b3c1 commit c872c9b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
32 changes: 23 additions & 9 deletions precompiles/EcAdd.yul
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ object "EcAdd" {
// HELPER FUNCTIONS
//////////////////////////////////////////////////////////////////

/// @dev Executes the `precompileCall` opcode.
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())
}

/// @notice Retrieves the highest half of the multiplication result.
/// @param multiplicand The value to multiply.
/// @param multiplier The multiplier.
Expand Down Expand Up @@ -289,15 +303,15 @@ object "EcAdd" {

// Ensure that the coordinates are between 0 and the field order.
if or(iszero(isOnFieldOrder(x2)), iszero(isOnFieldOrder(y2))) {
invalid()
burnGas()
}

let m_x2 := intoMontgomeryForm(x2)
let m_y2 := intoMontgomeryForm(y2)

// Ensure that the point is in the curve (Y^2 = X^3 + 3).
if iszero(pointIsInCurve(m_x2, m_y2)) {
invalid()
burnGas()
}

// We just need to go into the Montgomery form to perform the
Expand All @@ -312,15 +326,15 @@ object "EcAdd" {

// Ensure that the coordinates are between 0 and the field order.
if or(iszero(isOnFieldOrder(x1)), iszero(isOnFieldOrder(y1))) {
invalid()
burnGas()
}

let m_x1 := intoMontgomeryForm(x1)
let m_y1 := intoMontgomeryForm(y1)

// Ensure that the point is in the curve (Y^2 = X^3 + 3).
if iszero(pointIsInCurve(m_x1, m_y1)) {
invalid()
burnGas()
}

// We just need to go into the Montgomery form to perform the
Expand All @@ -333,12 +347,12 @@ object "EcAdd" {

// Ensure that the coordinates are between 0 and the field order.
if or(iszero(isOnFieldOrder(x1)), iszero(isOnFieldOrder(y1))) {
invalid()
burnGas()
}

// Ensure that the coordinates are between 0 and the field order.
if or(iszero(isOnFieldOrder(x2)), iszero(isOnFieldOrder(y2))) {
invalid()
burnGas()
}

// There's no need for transforming into Montgomery form
Expand All @@ -353,7 +367,7 @@ object "EcAdd" {

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

// We just need to go into the Montgomery form to perform the
Expand All @@ -372,7 +386,7 @@ object "EcAdd" {

// Ensure that the points are in the curve (Y^2 = X^3 + 3).
if iszero(pointIsInCurve(x, y)) {
invalid()
burnGas()
}

// (3 * x1^2 + a) / (2 * y1)
Expand Down Expand Up @@ -400,7 +414,7 @@ object "EcAdd" {

// Ensure that the points are in the curve (Y^2 = X^3 + 3).
if or(iszero(pointIsInCurve(x1, y1)), iszero(pointIsInCurve(x2, y2))) {
invalid()
burnGas()
}

// (y2 - y1) / (x2 - x1)
Expand Down
18 changes: 16 additions & 2 deletions precompiles/EcMul.yul
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ object "EcMul" {
// HELPER FUNCTIONS
// ////////////////////////////////////////////////////////////////

/// @dev Executes the `precompileCall` opcode.
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())
}

/// @notice Retrieves the highest half of the multiplication result.
/// @param multiplicand The value to multiply.
/// @param multiplier The multiplier.
Expand Down Expand Up @@ -367,7 +381,7 @@ object "EcMul" {
let x := calldataload(0)
let y := calldataload(32)
if iszero(affinePointCoordinatesAreOnFieldOrder(x, y)) {
invalid()
burnGas()
}
let scalar := calldataload(64)

Expand All @@ -383,7 +397,7 @@ object "EcMul" {

// Ensure that the point is in the curve (Y^2 = X^3 + 3).
if iszero(affinePointIsOnCurve(m_x, m_y)) {
invalid()
burnGas()
}

if eq(scalar, 0) {
Expand Down

0 comments on commit c872c9b

Please sign in to comment.