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
Estimate the rows needed for a block that tries to max out the blake and sha256 precompiles in a block with N gas.
Sha256 is not integrated but we've had a PR for a long time with an implementation here #756
Blake is not implemented in halo2, but we can use the Chiquito implementation for reference.
Since these two precompiles are not integrated getting benchmarks would need a lot of work, so instead the idea is to just estimate the row usage of a maxed out block. Then we can compare that value against the average block from #1798 and get an estimation on the cost increase (by comparing the required k of average VS maxed out)
The text was updated successfully, but these errors were encountered:
Since SHA256 follows Merkle–Damgård construction, the words are chained through the compression function, goal is to max out the SHA256's compression function invocations that could be done within 30M gas.
The following approach is used:
Allocate some words on memory.
Call to precompile using the memory slice as input.
Change the first word in memory.
Go to step 2.
JUMPDESTJUMPDEST# return size and offsetPUSH10x20PUSH0# input size and offsetPUSH30x025120PUSH0# precompile addressPUSH12GASSTATICCALL# above also writes to first mem word# jump to PC = 1JUMP
The above evm code 5b5b60205f620251205f60025afa56 is sent to the NULL address which in this case keeps making SHA256 precompile calls until it runs into OOG. Even though this is a failed transaction, to prove it was indeed failed/OOG, zkEVM must perform SHA256 in the circuit.
Using evm-run, it is found that if we use hash length of 4746 words (0x025120 bytes) then it makes 525 successful unique SHA256 precompile calls (526 but last one that fails by OOG), this means 2,491,650 compressions.
This script is used to estimate that 4746 words x 525 calls maxes out sha256 usage within 30M gas (click to expand).
// gas cost for allocating num_words on EVM memoryfunctioninit_cost(num_words: number){letmem_cost=num_words*3+Math.floor(num_words**2/512);letextra_gas=1+2500;// jumpdest + first staticcallreturnmem_cost+extra_gas;}// gas cost for executing SHA256 precompilefunctionexec_cost(num_words: number){letextra_gas=24;return100+60+num_words*12+extra_gas;}functionfind_num_batches(num_words: number,target_gas: number){target_gas-=init_cost(num_words);letsingle_exec_cost=exec_cost(num_words);// floor because last batch will failletnum_batches=Math.floor(target_gas/single_exec_cost);returnnum_batches;}functionquantity(num_words: number,num_batches: number){returnnum_words*num_batches;}functionfind_maxima(target_gas: number){letbest_case={num_words: 1,num_batches: find_num_batches(1,target_gas),getquantity(){returnquantity(this.num_words,this.num_batches);},};letnew_num_words=1;while(1){new_num_words++;letnew_num_batches=find_num_batches(new_num_words,target_gas);letnew_quantity=quantity(new_num_words,new_num_batches);if(new_quantity>best_case.quantity){best_case.num_batches=new_num_batches;best_case.num_words=new_num_words;console.log("best case update",best_case);}// stop once doneif(new_num_batches===0){returnbest_case;}}returnbest_case;}console.log(find_maxima(30_000_000));
Similarly, it is found that for 300k gas maxes out to 24024 sha256 compression functions (27456 bytes memory x 29 precompile calls).
Using PR 756, it is found to take 897848 rows. That's $log_2{897848}=19.77$ hence $k=20$. I was not able to calculate rows for 30M gas because it took a lot of time on my system (> 2 hours).
Follow up from #1798
Estimate the rows needed for a block that tries to max out the blake and sha256 precompiles in a block with N gas.
Sha256 is not integrated but we've had a PR for a long time with an implementation here #756
Blake is not implemented in halo2, but we can use the Chiquito implementation for reference.
Since these two precompiles are not integrated getting benchmarks would need a lot of work, so instead the idea is to just estimate the row usage of a maxed out block. Then we can compare that value against the average block from #1798 and get an estimation on the cost increase (by comparing the required
k
of average VS maxed out)The text was updated successfully, but these errors were encountered: