Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Loop optimizations #1

Merged
merged 1 commit into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
url = https://github.com/Uniswap/v4-core
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "lib/v4-periphery"]
path = lib/v4-periphery
url = https://github.com/Uniswap/v4-periphery
url = https://github.com/openzeppelin/openzeppelin-contracts
[submodule "lib/periphery-next"]
path = lib/periphery-next
url = https://github.com/Uniswap/periphery-next
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at a7a94c
1 change: 1 addition & 0 deletions lib/periphery-next
Submodule periphery-next added at e9ea3c
1 change: 0 additions & 1 deletion lib/v4-periphery
Submodule v4-periphery deleted from bb589f
2 changes: 1 addition & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
@uniswap/v4-periphery/=lib/v4-periphery/
@uniswap/v4-periphery/=lib/periphery-next/
@uniswap/v4-core/=lib/v4-core/
forge-std/=lib/forge-std/src/
67 changes: 25 additions & 42 deletions src/SaltMiner.huff
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
/// @author devtooligan

/* Interface */
#define function mineSalt(bytes32,uint256,address) view returns (uint256,address)
#define function mineSalt(address,uint256,bytes32) view returns (uint256,address)

/* Constants */
#define constant CREATE2_MARKER = 0xff
#define constant PREFIX_MASK = 0x00ff00000000000000000000000000000000000000
#define constant ADDRESS_MASK = 0x000000000000000000000000FFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF

/* Methods */
#define macro _MINE_SALT() = takes (3) returns (2) {
// takes: [deployerAddress, initCodeHash, prefix]
#define macro MINE_SALT() = takes (0) returns (0) {
0x60 0x04 0x00 calldatacopy
0x20 mload // [prefix]

0x00 mstore // [initCodeHash, prefix]
[CREATE2_MARKER] // [0xff,initCodeHash, prefix]
0x0b mstore8 // [initCodeHash, prefix]
// leave 0x20-0x3f untouched so initial salt is 0x00
0x40 mstore // [prefix]
[CREATE2_MARKER] // [0xff, prefix]
0x0b mstore8 // [prefix]

// Memory layout
// Hashing : 0xff(1) . deployerAddress(20) . salt(32) . initCodeHash(32)
Expand All @@ -38,41 +35,27 @@
// 0x00: 00 00 00 00 00 00 00 00 00 00 00 ff |-- address(this) -------------- |
// 0x20: |------------------------ salt ------------------------------------- |
// 0x40: |----------------- keccak256(initCode) ----------------------------- |

timestamp
begin_loop: // [salt, prefix]
0x20 mstore // [prefix]
0x55 0x0b sha3 // [hash, prefix]
dup1 // [hash, hash, prefix]
[PREFIX_MASK] and // [maskedHash, hash, prefix]
dup3 // [prefix, maskedHash, hash, prefix]
sub // [matchesPrefix, hash, prefix]
begin_loop
jumpi

begin_loop:
0x55 0x0b sha3 // [hash, prefix]
[ADDRESS_MASK] and // [newAddress, prefix]
dup1 // [newAddress, newAddress, prefix]
[PREFIX_MASK] and // [maskedAddress, newAddress, prefix]
dup3 // [prefix, maskedAddress, newAddress, prefix]
eq // [matchesPrefix, newAddress, prefix]
dup2 // [newAddress, matchesPrefix, newAddress, prefix]
extcodesize // [codeSize, matchesPrefix, newAddress, prefix]
iszero // [noCode, matchesPrefix, newAddress, prefix]
and // [matchesPrefix && noCode, newAddress, prefix]
found jumpi // [newAddress, prefix]

// use remaining gas as new salt, cheaper than maintining a counter
gas // [salt, newAddress, prefix]
// update salt in memory, not needed on stack
0x20 mstore // [newAddress, prefix]
pop // [prefix]
begin_loop jump // [prefix]
dup1 // [hash, hash, prefix]
extcodesize // [codeSize, hash, prefix]
begin_loop
jumpi

found: // [newAddress, prefix]
swap1 // [prefix, newAddress]
pop // [newAddress]
0x20 mload // [salt, newAddress]
}

#define macro MINE_SALT() = takes (0) returns (0) {
0x24 calldataload // [prefix]
0x04 calldataload // [initCodeHash, prefix]
0x44 calldataload // [deployerAddress, initCodeHash, prefix]
_MINE_SALT() // [salt, newAddress]
0x00 mstore // [newAddress]
0x20 mstore // []
0x40 0x00 return // []
[ADDRESS_MASK] and // [newAddress, prefix]
0x40 mstore // [prefix]
pop
0x40 0x20 return
}

#define macro MAIN() = takes (0) returns (0) {
Expand Down
8 changes: 4 additions & 4 deletions test/SaltMiner.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ contract SaltMinerTest is Test {
afterDonate: false
}));

uint start = gasleft();
(uint256 salt, address expectedAddress) = saltMiner.mineSalt(initCodeHash, prefix, address(this));
console.log("gas used: %s", salt);
// uint start = gasleft();
(uint256 salt, address expectedAddress) = saltMiner.mineSalt(address(this), prefix,initCodeHash );
console.log("salt: %s", salt);
address actualAddress = HookDeployer.deployHook(initCode, salt);
assertEq(actualAddress, expectedAddress);
}
}

interface ISaltMiner {
function mineSalt(bytes32 initCodeHash,uint256 prefix, address deployer) external view returns (uint256,address);
function mineSalt( address deployer,uint256 prefix,bytes32 initCodeHash) external view returns (uint256,address);
}