Skip to content

Commit 926c76e

Browse files
committed
refactor(contracts): using fixed gas cost on precompile calls
re semaphore-protocol#871
1 parent 31f891b commit 926c76e

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

packages/contracts/contracts/base/SemaphoreVerifier.sol

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ contract SemaphoreVerifier {
5959
mstore(add(mIn, 32), y)
6060
mstore(add(mIn, 64), s)
6161

62-
success := staticcall(gas(), 7, mIn, 96, mIn, 64)
62+
// ref: https://www.evm.codes/precompiled 0x07 ecMul
63+
// If inputs are valid, fixed gas cost 6000 is consumed.
64+
// If inputs are invalid, all gas is consumed but is limited to 6000 in this case.
65+
// `iszero(success)` will be true afterwards.
66+
success := staticcall(6000, 7, mIn, 96, mIn, 64)
6367

6468
if iszero(success) {
6569
mstore(0, 0)
@@ -69,7 +73,11 @@ contract SemaphoreVerifier {
6973
mstore(add(mIn, 64), mload(pR))
7074
mstore(add(mIn, 96), mload(add(pR, 32)))
7175

72-
success := staticcall(gas(), 6, mIn, 128, pR, 64)
76+
// ref: https://www.evm.codes/precompiled 0x06 ecAdd
77+
// If inputs are valid, fixed gas cost 150 is consumed.
78+
// If inputs are invalid, all gas is consumed but is limited to 150 in this case.
79+
// `iszero(success)` will be true afterwards.
80+
success := staticcall(150, 6, mIn, 128, pR, 64)
7381

7482
if iszero(success) {
7583
mstore(0, 0)
@@ -149,7 +157,9 @@ contract SemaphoreVerifier {
149157
mstore(add(_pPairing, 704), mload(add(vkPoints, 64)))
150158
mstore(add(_pPairing, 736), mload(add(vkPoints, 96)))
151159

152-
let success := staticcall(gas(), 8, _pPairing, 768, _pPairing, 0x20)
160+
// ref: https://www.evm.codes/precompiled 0x08 ecPairing
161+
// Given the input size 768 bytes, gas cost is computed from the above web page.
162+
let success := staticcall(181000, 8, _pPairing, 768, _pPairing, 0x20)
153163

154164
isOk := and(success, mload(_pPairing))
155165
}

0 commit comments

Comments
 (0)