You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Verifier contracts specify memory addresses to load constants from the trusted setup.
It starts from 0x220 in GrandSumVerifier and from \0x200 in InclusionVerifier.
However, it can start from 0x180.
Run forge test --debug "test_verify_grandsum_proof" and you can see memory from 0x000 to 0x180 is used in function parameters to calculate pairing. It is the blue part.
I changed the memory location constants to start from 0x180 and compared the test results.
GrandSumVerifier
15 gas saved.
before
$ forge test --match-test test_verify_grandsum_proof --gas-report
Ran 1 testfor test/GrandSumVerifier.t.sol:GrandSumVerifierTest
[PASS] test_verify_grandsum_proof() (gas: 285231)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 13.47ms (8.14ms CPU time)
| src/GrandSumVerifier.sol:GrandSumVerifier contract |||||||----------------------------------------------------|-----------------|--------|--------|--------|---------|| Deployment Cost | Deployment Size |||||| 277055 | 1068 |||||| Function Name | min | avg | median | max |# calls || verifyProof | 271155 | 271155 | 271155 | 271155 | 1 |
The maximum memory size used in both verifiers is 0x180, so I recommend to save constants from memory 0x180.
The saved memory is 160 and 128 bytes respectively.
The gas cost for memory is increasing in a quadratic way.
participants: @qpzm, @rkdud007
Description
Verifier contracts specify memory addresses to load constants from the trusted setup.
It starts from 0x220 in GrandSumVerifier and from \0x200 in InclusionVerifier.
However, it can start from 0x180.
POC
I installed foundry to see memory and added two tests.
https://github.com/qpzm/summa-solvency/blob/v2-fix-verifier-memory/contracts/test/GrandSumVerifier.t.sol
https://github.com/qpzm/summa-solvency/blob/v2-fix-verifier-memory/contracts/test/InclusionVerifier.t.sol
Run
forge test --debug "test_verify_grandsum_proof"
and you can see memory from 0x000 to 0x180 is used in function parameters to calculate pairing. It is the blue part.I changed the memory location constants to start from 0x180 and compared the test results.
GrandSumVerifier
15 gas saved.
before
Memory expansion: 960 bytes
after
Memory expansion: 800 bytes
InclusionVerifier
12 gas saved.
before
Memory expansion: 896 bytes
(The source view of foundry debugger is unreliable)
after
Memory expansion: 768 bytes
Recommendation
The maximum memory size used in both verifiers is 0x180, so I recommend to save constants from memory 0x180.
The saved memory is 160 and 128 bytes respectively.
The gas cost for memory is increasing in a quadratic way.
Reference: https://www.evm.codes/about#memoryexpansion
Therefore if we reduce 64 memory bytes, we can make
new_mem_size_words ^ 2 // 512
in memory gas cost to 0.I will further think about the way.
GrandSumVerifier
https://github.com/summa-dev/summa-solvency/blob/fec83a747ead213261aecfaf4a01b43fff9731ee/contracts/src/GrandSumVerifier.sol#L11-L24InclusionVerifier
https://github.com/summa-dev/summa-solvency/blob/fec83a747ead213261aecfaf4a01b43fff9731ee/contracts/src/InclusionVerifier.sol#L10-L22The text was updated successfully, but these errors were encountered: