diff --git a/precompiles/EcAdd.yul b/precompiles/EcAdd.yul index 54592c39..550e19ab 100644 --- a/precompiles/EcAdd.yul +++ b/precompiles/EcAdd.yul @@ -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. @@ -289,7 +303,7 @@ 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) @@ -297,7 +311,7 @@ object "EcAdd" { // 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 @@ -312,7 +326,7 @@ 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) @@ -320,7 +334,7 @@ object "EcAdd" { // 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 @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/precompiles/EcMul.yul b/precompiles/EcMul.yul index 82a8c8a5..068cac01 100644 --- a/precompiles/EcMul.yul +++ b/precompiles/EcMul.yul @@ -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. @@ -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) @@ -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) {