From 642d9e877926aa0d103151a159931fa539957742 Mon Sep 17 00:00:00 2001 From: Jimmy Chu <898091+jimmychu0807@users.noreply.github.com> Date: Sun, 27 Oct 2024 14:09:19 +0800 Subject: [PATCH] refactor(contracts): using fixed gas cost on precompile calls re #871 --- .../contracts/base/SemaphoreVerifier.sol | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/contracts/contracts/base/SemaphoreVerifier.sol b/packages/contracts/contracts/base/SemaphoreVerifier.sol index e721cb082..ab8d589d1 100644 --- a/packages/contracts/contracts/base/SemaphoreVerifier.sol +++ b/packages/contracts/contracts/base/SemaphoreVerifier.sol @@ -59,7 +59,11 @@ contract SemaphoreVerifier { mstore(add(mIn, 32), y) mstore(add(mIn, 64), s) - success := staticcall(gas(), 7, mIn, 96, mIn, 64) + // ref: https://www.evm.codes/precompiled 0x07 ecMul + // If inputs are valid, fixed gas cost 6000 is consumed. + // If inputs are invalid, all gas is consumed but is limited to 6000 in this case. + // `iszero(success)` will be true afterwards. + success := staticcall(6000, 7, mIn, 96, mIn, 64) if iszero(success) { mstore(0, 0) @@ -69,7 +73,11 @@ contract SemaphoreVerifier { mstore(add(mIn, 64), mload(pR)) mstore(add(mIn, 96), mload(add(pR, 32))) - success := staticcall(gas(), 6, mIn, 128, pR, 64) + // ref: https://www.evm.codes/precompiled 0x06 ecAdd + // If inputs are valid, fixed gas cost 150 is consumed. + // If inputs are invalid, all gas is consumed but is limited to 150 in this case. + // `iszero(success)` will be true afterwards. + success := staticcall(150, 6, mIn, 128, pR, 64) if iszero(success) { mstore(0, 0) @@ -149,7 +157,9 @@ contract SemaphoreVerifier { mstore(add(_pPairing, 704), mload(add(vkPoints, 64))) mstore(add(_pPairing, 736), mload(add(vkPoints, 96))) - let success := staticcall(gas(), 8, _pPairing, 768, _pPairing, 0x20) + // ref: https://www.evm.codes/precompiled 0x08 ecPairing + // Given the input size 768 bytes, gas cost is computed from the above web page. + let success := staticcall(181000, 8, _pPairing, 768, _pPairing, 0x20) isOk := and(success, mload(_pPairing)) }