Velvet-Capital / Velvet-v4

V4 (thena+venus) on top of v3
Other
0 stars 0 forks source link

Fixed bitmap collision issue #51

Closed Havoc19 closed 4 days ago

Havoc19 commented 1 week ago

Update Summary for updateTokens and _verifyNewTokenList

Changes Made

How It Works

  1. Token Presence Tracking:

    • Each token’s address is hashed and mapped to a unique bit position within uint256[256].
    • The bitmap allows us to set, clear, and check each token’s presence efficiently by setting or clearing specific bits.
  2. Bitmap Array (uint256[256]):

    • Each uint256 element in the array provides 256 bits.
    • With 256 uint256 elements, we get a total of 65,536 bits, exactly matching the space needed for bit positions 0 to 65,535.
    • By dividing bitPos by 256, we determine the slot (index) in the array, and by taking bitPos % 256, we determine the specific bit within that slot (offset).
  3. Collision-Free Mapping:

    • keccak256 produces a unique 256-bit hash for each token address.
    • By taking bitPos = hash % 65536, we map the hash to one of the 65,536 unique bit positions in uint256[256].
    • This approach ensures that each token address has a unique bit in the bitmap, preventing any collisions.

Why We Use an Array

Benefits of This Approach

langnavina97 commented 1 week ago

The changes look good! I think the chance to have a collision is reduced but there is still a small risk, it might be small enough to accept this solution. Can we confirm with the auditors?