Skip to content

0xArcturus/binarysearch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project proves that ordering functions by use and then setting their method ids in ascending order wont necessarily result in a gas optimization, which conflicted with the answer for question 8 for the RACE #17 of the Secureum Bootcamp, and has been accepted by the Secureum team and posted as a correction in Ventral Digital's solutions post.

In this compiler code we can see that in the function appendInternalSelector()the compiler checks if the number of methods is over 4. If there are more than 4 it selects the method id in the middle of the remaining methods as a pivot, sets a GT comparison with the pivot and calls itself recursively with the parameters for the new separated blocks of method ids, one block being higher method ids than the pivot and the other lower, therefore creating a binary search.

If the remaining method ids are <= 4, it just sets up a succession of EQ comparisons with the selectors in ascending order:

Block with under <=4 method ids

On the vault contract these are the method ids in ascending order:

"totalContributions()": "37c08923",
"TOTAL_CONTRIBUTION_CAP()": "4b03a338",
"requestAllowance(uint256)": "5b1164de",
"paused()": "5c975abb",
"deposit(uint72)": "626fb5b8",
"ALLOWANCE_CAP()": "84f9b1fc",
"withdraw(uint72)": "a92c4707"
"WETH()": "ad5c4648",
"controller()": "f77c4791",

When compiling the Vault contract with the default 200 optimizer runs and 100_000 optimizer runs I got the the Vault200Runs.asm and the Vault100_000Runs.asm files in this repo, where you can see the binary search algorithm in assembly..

In both of them you can see that the contract implements a binary search, where the 100_000 runs file has two pivots and is more optimized than the 200 runs file, that implements only one pivot.

The following flowchart describes the execution of the most optimized assembly code:

flowchart All in all, as far as Markus and I have understood, arriving to execution for method Id 0xa92c4707 consumes less gas than arriving to execution for method Id 0x5b1164de.

To our understanding, the gas cost is relative to the amount of pivots and the distance from each pivot.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published