Skip to content

fix(mock-contracts): make FHE.random* handles usable in mocks - #402

Merged
architect-dev merged 1 commit into
FhenixProtocol:masterfrom
erkancamli:fix/mock-random-task
Oct 14, 2026
Merged

architect-dev merged 1 commit into
FhenixProtocol:masterfrom
erkancamli:fix/mock-random-task

Conversation

@erkancamli

@erkancamli erkancamli commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

MockTaskManager.createRandomTask was a stub that returned keccak256(returnType, seed, securityZone, block.timestamp). That handle has no type or security zone metadata, is not in mock storage, and the caller gets no ACL access. So FHE.randomEuint*() cannot be used in Hardhat or Foundry tests: FHE.add(r, x) reverts with InvalidTypeOrSecurityZone, FHE.allowThis(r) usually reverts with SenderNotAllowed, and two calls in one transaction return the same handle.

This follows TaskManager.createRandomTask in cofhe-contracts: validate the security zone, derive the seed when it is 0 (with a counter), build the handle with calcPlaceholderKey over (seed, msg.sender), grant transient access to the caller and emit TaskCreated. As mock only work it stores a value derived from the handle, inside the existing gas tracking. The ABI does not change, and the new randomCounter is appended after the existing storage variables.

Tests: packages/foundry-plugin/test/MockRandom.t.sol checks the handle's type and zone bits and mock storage, allowThis plus add on a random handle, two random values in one transaction, and the zone check. All 4 fail on master (utype: 84 != 4, InvalidTypeOrSecurityZone("add"), same handle, no revert) and pass with this change. The full foundry plugin suite passes (77 tests). Prettier is clean. A changeset is included.


Note

Low Risk
Test-only mock behavior aligned with production random-task flow; no production contract or ABI changes.

Overview
MockTaskManager.createRandomTask is no longer a bare keccak256 stub. It now mirrors production behavior: invalid security zones revert, zero seeds are derived (with a new randomCounter), handles are built via calcPlaceholderKey with caller in the preimage, transient ACL is granted, TaskCreated is emitted, and a mock plaintext is stored under gas tracking.

That makes FHE.random* handles usable in Hardhat/Foundry tests—correct type/zone metadata, distinct handles per call, and support for allowThis / add without InvalidTypeOrSecurityZone or SenderNotAllowed.

Adds MockRandom.t.sol (metadata, usability, uniqueness, zone rejection) and a @cofhe/mock-contracts patch changeset. ABI unchanged.

Reviewed by Cursor Bugbot for commit 1deac44. Bugbot is set up for automated code reviews on this repo. Configure here.

MockTaskManager.createRandomTask was a stub returning
keccak256(returnType, seed, securityZone, block.timestamp). The handle
had no type or security zone metadata, no value in mock storage and no
ACL access for the caller, so FHE.random* could not be used in Hardhat
or Foundry tests: FHE.add(r, x) reverted with InvalidTypeOrSecurityZone
and two calls in one transaction returned the same handle.

Follow TaskManager.createRandomTask: validate the zone, derive the seed
when it is 0 with a counter, build the handle with calcPlaceholderKey
over (seed, msg.sender), grant the caller transient access and emit
TaskCreated. As mock only work, store a value derived from the handle.
@changeset-bot

changeset-bot Bot commented Oct 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1deac44

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

@architect-dev architect-dev left a comment

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.

Good catch and fix. Happy to have the correct random function in there now. Will merge after tests run (just approved workflow). Tyty 🙏

@architect-dev
architect-dev merged commit 010e1c9 into FhenixProtocol:master Oct 14, 2026
13 checks passed
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