Repository navigation
fix(mock-contracts): store 0 or 1 for a random ebool - #463
erkancamli wants to merge 1 commit into
Conversation
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 detectedLatest commit: 2a64f34 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
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
left a comment
There was a problem hiding this comment.
Reviewed the mock ebool random path:
- Root cause checks out:
_set+ 8-bit ebool mask left a random byte (e.g. 66), soFHE.select(needs exact1) disagreed with decrypt/getPlaintext(nonzero → true) andpublishDecryptResult(ebool, bool, …)signed the raw byte →InvalidSigner. - Fix is the right shape:
if (returnType == Utils.EBOOL_TFHE) value &= 1before_set; other types untouched.
Local checks on this branch:
packages/mock-contracts:forge test→ 4/4 passpackages/foundry-plugin:pnpm test→ 96 pass / 14 fail — failures are mostlyACLNotAllowed/SenderNotAllowedacross 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 newMockRandomtests in this environment.
If CI on the PR is green for foundry-plugin, I’d treat this as ready from the mock-contracts side.
|
Thanks @erkancamli , holding this for now. Production doesn't support random |
|
Makes sense, thanks. Once the protocol team decides, I'm happy to update the mock to match whichever behaviour they pick. |
What is wrong
MockTaskManager.createRandomTaskstoreskeccak256(ctHash)through_set, which masks the value to the type width. Foreboolthat mask keeps 8 bits (getUtypeBitsreturns 8 forEBOOL_TFHE), so a randomeboolholds a value from 0 to 255 instead of 0 or 1. The real TaskManager acceptsEBOOL_TFHEas 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)takesaonly when the stored value is exactly 1, whilegetPlaintext(ebool)inCofheTestanddecryptForViewin the SDK treat any non zero value astrue. Sorreads astrueandselectstill returnsb.decryptForTxsigns the raw stored byte, butFHE.publishDecryptResult(ebool, bool, signature)publishes1, so the signature check reverts withInvalidSigner.How to reproduce
The two new tests in
packages/foundry-plugin/test/MockRandom.t.solfail on master:What the fix does
When the return type is
ebool,createRandomTaskkeeps one bit of the derived value before storing it. Other types are unchanged.Tests
testRandomEboolIsTrueOrFalse: eight randomebools are each 0 or 1, andFHE.selectpicks the branchgetPlaintextreports.testRandomEboolDecryptResultCanBePublished: a randomebooldecrypted throughMockThresholdNetwork.decryptForTxWithoutACPand signed like the SDK andCofheClientdo can be published withFHE.publishDecryptResult(ebool, bool, signature).forge testinpackages/foundry-plugin(110 tests) andpackages/mock-contracts(4 tests) pass.