Skip to content

Commit

Permalink
refactor(contracts): specify gas cost in SemaphoreVerifier precompile…
Browse files Browse the repository at this point in the history
… calls (#883)

* chore(contracts): remove sub() from gas() in staticcall at SemaphoreVerifier

re #871

* refactor(contracts): using fixed gas cost on precompile calls

re #871

* refactor(contracts): increease fixed gas cost with safety buffer
  • Loading branch information
jimmychu0807 authored Oct 31, 2024
1 parent caebc1a commit 901d095
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/contracts/contracts/base/SemaphoreVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ contract SemaphoreVerifier {
mstore(add(mIn, 32), y)
mstore(add(mIn, 64), s)

success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
// ecMul gas cost is fixed at 6000. Add 33.3% gas for safety buffer.
// Last checked in 2024 Oct, evm codename Cancun
// ref: https://www.evm.codes/precompiled?fork=cancun#0x07
success := staticcall(8000, 7, mIn, 96, mIn, 64)

if iszero(success) {
mstore(0, 0)
Expand All @@ -69,7 +72,10 @@ contract SemaphoreVerifier {
mstore(add(mIn, 64), mload(pR))
mstore(add(mIn, 96), mload(add(pR, 32)))

success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
// ecAdd gas cost is fixed at 150. Add 33.3% gas for safety buffer.
// Last checked in 2024 Oct, evm codename Cancun
// ref: https://www.evm.codes/precompiled?fork=cancun#0x06
success := staticcall(200, 6, mIn, 128, pR, 64)

if iszero(success) {
mstore(0, 0)
Expand Down Expand Up @@ -149,7 +155,10 @@ contract SemaphoreVerifier {
mstore(add(_pPairing, 704), mload(add(vkPoints, 64)))
mstore(add(_pPairing, 736), mload(add(vkPoints, 96)))

let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
// ecPairing gas cost at 181000 given 768 bytes input. Add 33.3% gas for safety buffer.
// Last checked in 2024 Oct, evm codename Cancun
// ref: https://www.evm.codes/precompiled?fork=cancun#0x08
let success := staticcall(241333, 8, _pPairing, 768, _pPairing, 0x20)

isOk := and(success, mload(_pPairing))
}
Expand Down

0 comments on commit 901d095

Please sign in to comment.