Skip to content

Instantly share code, notes, and snippets.

@zobront
Last active September 28, 2023 04:04
Show Gist options
  • Save zobront/263f7cd6df079a48d9f96c83ef369f69 to your computer and use it in GitHub Desktop.
Save zobront/263f7cd6df079a48d9f96c83ef369f69 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { Test } from "forge-std/Test.sol";
import { console } from "forge-std/console.sol";
contract CollisionTest is Test {
bytes32 constant POOL_INIT_CODE_HASH = 0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
address constant factory = 0x1F98431c8aD98523631AE4a59f267346ea31F984;
address constant usdc = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
mapping(uint24 => bool) public pools;
function testCollisions() public {
uint seed;
// run this experiment 10 times
for (uint seedGen; seedGen < 10; seedGen++) {
// pick a starting number unique from the other tests
seed = 2 ** (seedGen * 8);
// set a random 256 addresses as true
for (uint token = seed; token < seed + 256; token++) {
pools[computeAddress(token)] = true;
}
// count up until we find a collision
uint24 counter;
while (true) {
if (pools[counter++]) {
console.log(counter);
break;
}
}
// reset all storage to false for the next test run
for (uint token = seed; token < seed + 256; token++) {
pools[computeAddress(token)] = false;
}
}
}
function computeAddress(uint token) internal pure returns (uint24 pool) {
pool = uint24(
uint256(
keccak256(
abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encode(usdc, token, 3000)),
POOL_INIT_CODE_HASH
)
)
)
);
}
}
Logs:
54697
45264
28723
72590
9763
65600
48479
45589
96942
188222
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment