Skip to content

fix(abi/mockEncrypt): fix no-op bitmask in generateMockCtHash string hashing#243

Open
amathxbt wants to merge 1 commit intoFhenixProtocol:masterfrom
amathxbt:fix/mock-ct-hash-bitmask-no-op
Open

fix(abi/mockEncrypt): fix no-op bitmask in generateMockCtHash string hashing#243
amathxbt wants to merge 1 commit intoFhenixProtocol:masterfrom
amathxbt:fix/mock-ct-hash-bitmask-no-op

Conversation

@amathxbt
Copy link
Copy Markdown

@amathxbt amathxbt commented May 3, 2026

Summary

generateMockCtHash in packages/abi/src/mockEncrypt.ts uses hash & hash as a bitmask when hashing strings, with the comment "Convert to 32-bit integer". This is a no-op — x & x === x for any value — so the intended 32-bit truncation never occurs.

Bug

if (typeof data === 'string') {
  let hash = 0n;
  for (let i = 0; i < data.length; i++) {
    const char = data.charCodeAt(i);
    hash = (hash << 5n) - hash + BigInt(char);
    hash = hash & hash;   // ← BUG: hash & hash == hash, this is a no-op!
    //                              Was intended to be: hash & 0xFFFFFFFFn
  }
  return hash < 0n ? -hash : hash;
}

Consequences:

  1. The hash accumulates a left-shift of 5 bits per character. For an input string of length N, the intermediate value can reach up to 2^(5*N) bits. A 200-character string produces a hash with ~1000 bits, far exceeding the uint256 (256-bit) size of a ctHash field.
  2. When this overflowing bigint is passed as ctHash in an EncryptedInput struct and encoded via abi.encode, Solidity silently truncates it to the lower 256 bits — potentially producing the same ctHash for two different strings (collision), breaking mock test isolation.
  3. Mock tests that encrypt string values (e.g., addresses as strings) may produce unexpected results that only manifest at long input lengths.

Fix

Replace hash & hash with the correct 32-bit mask hash & 0xFFFFFFFFn, matching the stated intent of the comment.

`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.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 3, 2026

@amathxbt is attempting to deploy a commit to the Fhenix Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 3, 2026

⚠️ No Changeset found

Latest commit: be3837a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Copy Markdown
Collaborator

@architect-dev architect-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update so that the CI checks are passing. Also please remove the commented annotation.

Thanks for your contribution 👍

Comment on lines +29 to +33
// 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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets remove the annotation, better in the PR description instead of baked into the codebase.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets remove the annotation, better in the PR description instead of baked into the codebase.

Working on this thanks for your feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants