Skip to content

fix(mock-contracts): store 0 or 1 for a random ebool - #463

Open
erkancamli wants to merge 1 commit into
FhenixProtocol:masterfrom
erkancamli:fix/mock-random-ebool
Open

erkancamli wants to merge 1 commit into
FhenixProtocol:masterfrom
erkancamli:fix/mock-random-ebool

Conversation

@erkancamli

Copy link
Copy Markdown
Contributor

What is wrong

MockTaskManager.createRandomTask stores keccak256(ctHash) through _set, which masks the value to the type width. For ebool that mask keeps 8 bits (getUtypeBits returns 8 for EBOOL_TFHE), so a random ebool holds a value from 0 to 255 instead of 0 or 1. The real TaskManager accepts EBOOL_TFHE as a random return type and the coprocessor produces a real boolean.

With such a value the mocks disagree with themselves:

  • FHE.select(r, a, b) takes a only when the stored value is exactly 1, while getPlaintext(ebool) in CofheTest and decryptForView in the SDK treat any non zero value as true. So r reads as true and select still returns b.
  • decryptForTx signs the raw stored byte, but FHE.publishDecryptResult(ebool, bool, signature) publishes 1, so the signature check reverts with InvalidSigner.

How to reproduce

ebool r = ebool.wrap(Impl.random(Utils.EBOOL_TFHE));
mockTaskManager.mockStorage(uint256(ebool.unwrap(r))); // 66 on the first call in the test below

The two new tests in packages/foundry-plugin/test/MockRandom.t.sol fail on master:

[FAIL: InvalidSigner(0xcCb0..., 0x7099...)] testRandomEboolDecryptResultCanBePublished()
[FAIL: random ebool is neither false nor true: 66 > 1] testRandomEboolIsTrueOrFalse()

What the fix does

When the return type is ebool, createRandomTask keeps one bit of the derived value before storing it. Other types are unchanged.

Tests

  • testRandomEboolIsTrueOrFalse: eight random ebools are each 0 or 1, and FHE.select picks the branch getPlaintext reports.
  • testRandomEboolDecryptResultCanBePublished: a random ebool decrypted through MockThresholdNetwork.decryptForTxWithoutACP and signed like the SDK and CofheClient do can be published with FHE.publishDecryptResult(ebool, bool, signature).

forge test in packages/foundry-plugin (110 tests) and packages/mock-contracts (4 tests) pass.

MockTaskManager.createRandomTask stores keccak256(ctHash) through _set,
which masks the value to the type width. The ebool mask keeps 8 bits, so
a random ebool held a value from 0 to 255 instead of 0 or 1. The mocks
then disagreed with themselves: FHE.select only takes the true branch
for exactly 1, while getPlaintext and the SDK treat any non zero value
as true. Publishing the decrypt result through FHE.publishDecryptResult
(ebool, bool, signature) sends 1 while the mock signed the raw byte, so
it reverted with InvalidSigner.

Keep one bit of the value when the return type is ebool.
@changeset-bot

changeset-bot Bot commented Oct 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2a64f34

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@cofhe/mock-contracts Patch
@cofhe/foundry-plugin Patch
@cofhe/hardhat-3-plugin Patch
@cofhe/hardhat-plugin Patch
@cofhe/site Patch
@cofhe/abi Patch
@cofhe/react Patch
@cofhe/sdk Patch
@cofhe/example-react Patch

Not sure what this means? Click here to learn what changesets are.

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

@kutluhaneth46 kutluhaneth46 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed the mock ebool random path:

  • Root cause checks out: _set + 8-bit ebool mask left a random byte (e.g. 66), so FHE.select (needs exact 1) disagreed with decrypt/getPlaintext (nonzero → true) and publishDecryptResult(ebool, bool, …) signed the raw byte → InvalidSigner.
  • Fix is the right shape: if (returnType == Utils.EBOOL_TFHE) value &= 1 before _set; other types untouched.

Local checks on this branch:

  • packages/mock-contracts: forge test → 4/4 pass
  • packages/foundry-plugin: pnpm test → 96 pass / 14 fail — failures are mostly ACLNotAllowed / SenderNotAllowed across unrelated suites too (MockRem, SimpleTest, MockIsPubliclyAllowed), so this looks like env/deps drift here rather than the ebool change specifically. Couldn’t green-confirm the two new MockRandom tests in this environment.

If CI on the PR is green for foundry-plugin, I’d treat this as ready from the mock-contracts side.

@alexshchur

Copy link
Copy Markdown
Contributor

Thanks @erkancamli , holding this for now. Production doesn't support random ebool yet, and we're waiting on the protocol team to decide whether it will. The mock should match whatever they pick, so we'll come back to this once that's settled.

@erkancamli

Copy link
Copy Markdown
Contributor Author

Makes sense, thanks. Once the protocol team decides, I'm happy to update the mock to match whichever behaviour they pick.

This branch has not been deployed

No deployments
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.

3 participants