fix(abi/mockEncrypt): fix no-op bitmask in generateMockCtHash string hashing#243
fix(abi/mockEncrypt): fix no-op bitmask in generateMockCtHash string hashing#243amathxbt wants to merge 1 commit intoFhenixProtocol:masterfrom
generateMockCtHash string hashing#243Conversation
`hash & hash` is mathematically equivalent to `hash` (x & x == x for any x) so the intended 32-bit truncation never occurred. On strings longer than a few characters the BigInt hash grows unboundedly via the (hash << 5n) shift, producing values that can exceed uint256 range and cause ctHash field overflow or unexpected collisions in mock tests. Fix: use the correct mask 0xFFFFFFFFn to truncate to 32 bits after each iteration, matching the documented intent of the comment.
|
@amathxbt is attempting to deploy a commit to the Fhenix Team on Vercel. A member of the Team first needs to authorize it. |
|
architect-dev
left a comment
There was a problem hiding this comment.
Please update so that the CI checks are passing. Also please remove the commented annotation.
Thanks for your contribution 👍
| // NOTE: The mask must be 0xFFFFFFFFn to truncate to 32 bits on every | ||
| // iteration. The previous code used `hash & hash` which is a no-op | ||
| // (x & x == x for all x), so the hash grew without bound and could | ||
| // produce values that overflow the uint256 ctHash field on very long | ||
| // strings. |
There was a problem hiding this comment.
Lets remove the annotation, better in the PR description instead of baked into the codebase.
There was a problem hiding this comment.
Lets remove the annotation, better in the PR description instead of baked into the codebase.
Working on this thanks for your feedback
Summary
generateMockCtHashinpackages/abi/src/mockEncrypt.tsuseshash & hashas a bitmask when hashing strings, with the comment "Convert to 32-bit integer". This is a no-op —x & x === xfor any value — so the intended 32-bit truncation never occurs.Bug
Consequences:
2^(5*N)bits. A 200-character string produces a hash with ~1000 bits, far exceeding theuint256(256-bit) size of actHashfield.bigintis passed asctHashin anEncryptedInputstruct and encoded viaabi.encode, Solidity silently truncates it to the lower 256 bits — potentially producing the same ctHash for two different strings (collision), breaking mock test isolation.Fix
Replace
hash & hashwith the correct 32-bit maskhash & 0xFFFFFFFFn, matching the stated intent of the comment.