Skip to content

Commit

Permalink
refactor(contracts): using fixed gas cost on precompile calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmychu0807 committed Oct 27, 2024
1 parent 31f891b commit 926c76e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/contracts/contracts/base/SemaphoreVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit 926c76e

Please sign in to comment.