Skip to content

Commit

Permalink
Merge pull request #1 from 0xdineshkumarsm/main
Browse files Browse the repository at this point in the history
⚡ Loop optimizations
  • Loading branch information
devtooligan committed Jul 8, 2023
2 parents 49d4a27 + 24008dd commit 9f8aeb3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 52 deletions.
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);
}

0 comments on commit 9f8aeb3

Please sign in to comment.