@cofhe/mock-contracts 0.0.0-alpha-20260409113701

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,86 @@
1
+ # @cofhe/mock-contracts Changelog
2
+
3
+ ## 0.0.0-alpha-20260409113701
4
+
5
+ ## 0.4.0
6
+
7
+ ## 0.3.2
8
+
9
+ ### Patch Changes
10
+
11
+ - d4e86ea: Aligns with CTA encrypted variables bytes32 representation.
12
+
13
+ - **@cofhe/hardhat-plugin**: `hre.cofhe.mocks.getTestBed()`, `getMockTaskManager()`, `getMockACL()`, `getMockThresholdNetwork()`, and `getMockZkVerifier()` now return typed contracts (typechain interfaces) instead of untyped `Contract`. `getPlaintext(ctHash)` and `expectPlaintext(ctHash, value)` now accept bytes32 ctHashes as `string` support cofhe-contracts 0.1.0 CTA changes.
14
+ - **@cofhe/mock-contracts**: Export typechain-generated contract types (`TestBed`, `MockACL`, `MockTaskManager`, `MockZkVerifier`, `MockThresholdNetwork`) for use with the hardhat plugin. Typechain is run from artifact ABIs only; factory files are not generated.
15
+ - **@cofhe/abi**: CTA-related types use `bytes32` (string) instead of `uint256`. Decryption and return-type helpers aligned with cofhe-contracts 0.1.0.
16
+ - **@cofhe/sdk**: Decryption APIs (`decryptForTx`, `decryptForView`, and related builders) now also accept `string` for ciphertext hashes (bytes32) as well as `bigint`.
17
+
18
+ ## 0.3.1
19
+
20
+ ### Patch Changes
21
+
22
+ - 370f0c7: no-op
23
+
24
+ ## 0.3.0
25
+
26
+ ### Minor Changes
27
+
28
+ - 35024b6: Remove `sdk` from function names and exported types. Rename:
29
+
30
+ - `createCofhesdkConfig` -> `createCofheConfig`
31
+ - `createCofhesdkClient` -> `createCofheClient`
32
+ - `hre.cofhesdk.*` -> `hre.cofhe.*`
33
+ - `hre.cofhesdk.createCofheConfig()` → `hre.cofhe.createConfig()`
34
+ - `hre.cofhesdk.createCofheClient()` → `hre.cofhe.createClient()`
35
+ - `hre.cofhesdk.createBatteriesIncludedCofheClient()` → `hre.cofhe.createClientWithBatteries()`
36
+
37
+ - 29c2401: implement decrypt-with-proof flows and related tests:
38
+
39
+ - Implement production `decryptForTx` backed by Threshold Network `POST /decrypt`, with explicit permit vs global-allowance selection.
40
+ - Rename mocks “Query Decrypter” -> “Threshold Network” and update SDK constants/contracts/artifacts accordingly.
41
+ - Extend mock contracts + hardhat plugin to publish & verify decryption results on-chain, and add end-to-end integration tests.
42
+
43
+ ## 0.2.1
44
+
45
+ ### Patch Changes
46
+
47
+ - ac47e2f: Add `PermitUtils.checkValidityOnChain` to validate permits against the on-chain deployed ACL (source of truth).
48
+ - 0000d5e: Mock contracts deployed to alternate fixed addresses to avoid collision with hardhat pre-compiles.
49
+
50
+ ## 0.2.0
51
+
52
+ ### Minor Changes
53
+
54
+ - 8fda09a: Removes `Promise<boolean>` return type from `client.connect(...)`, instead throws an error if the connection fails.
55
+ - e0caeca: Adds `environment: 'node' | 'web' | 'hardhat' | 'react'` option to config. Exposed via `client.config.enviroment`. Automatically populated appropriately within the various `createCofhesdkConfig` functions.
56
+
57
+ ### Patch Changes
58
+
59
+ - e121108: Fix ACL permission invalid issue. MockACL needs real deployment since etching doesn't call the constructor, so the EIP712 is uninitialized. Also adds additional utility functions to hardhat-plugin:
60
+
61
+ - `hre.cofhesdk.connectWithHardhatSigner(client, signer)` - Connect to client with hardhat ethers signer.
62
+ - `hre.cofhesdk.createBatteriesIncludedCofhesdkClient()` - Creates a batteries included client with signer connected.
63
+ - `hre.cofhesdk.mocks.getMockTaskManager()` - Gets deployed Mock Taskmanager
64
+ - `hre.cofhesdk.mocks.getMockACL()` - Gets deployed Mock ACL
65
+
66
+ ## 0.1.1
67
+
68
+ ### Patch Changes
69
+
70
+ - a1d1323: Add repository info to package.json of public packages to fix npm publish provenance issue.
71
+ - d232d11: Ensure publish includes correct src and dist files
72
+ - b6521fb: Update publish workflow to create versioning PR upon merge with changeset.
73
+
74
+ ## 0.1.0
75
+
76
+ ### Minor Changes
77
+
78
+ - 8d41cf2: Combine existing packages into more reasonable defaults. New package layout is @cofhe/sdk (includes all the core logic for configuring and creating a @cofhe/sdk client, encrypting values, and decrypting handles), mock-contracts, hardhat-plugin, and react.
79
+ - a83facb: Prepare for initial release. Rename scope from `@cofhesdk` to `@cofhe` and rename `cofhesdk` package to `@cofhe/sdk`. Create `publish.yml` to publish `beta` packages on merged PR, and `latest` on changeset PR.
80
+ - 58e93a8: Migrate cofhe-mock-contracts and cofhe-hardhat-plugin into @cofhe/sdk.
81
+
82
+ This changelog is maintained by Changesets and will be populated on each release.
83
+
84
+ - Do not edit this file by hand.
85
+ - Upcoming changes can be previewed with `pnpm changeset status --verbose`.
86
+ - Entries are generated when the Changesets "Version Packages" PR is created/merged.
package/README.md ADDED
@@ -0,0 +1,180 @@
1
+ # cofhe/mock-contracts [![NPM Package][npm-badge]][npm] [![License: MIT][license-badge]][license]
2
+
3
+ [npm]: https://www.npmjs.com/package/@fhenixprotocol/cofhe-mock-contracts
4
+ [npm-badge]: https://img.shields.io/npm/v/@fhenixprotocol/cofhe-mock-contracts.svg
5
+ [license]: https://opensource.org/licenses/MIT
6
+ [license-badge]: https://img.shields.io/badge/License-MIT-blue.svg
7
+
8
+ A mock smart contract library for testing CoFHE (Confidential Computing Framework for Homomorphic Encryption) with FHE primitives. This package provides mock implementations of core CoFHE contracts for development and testing purposes.
9
+
10
+ ## Features
11
+
12
+ - Mock implementations of core CoFHE contracts:
13
+ - MockTaskManager
14
+ - MockThresholdNetwork
15
+ - MockZkVerifier
16
+ - ACL (Access Control List)
17
+ - Synchronous operation simulation with mock delays
18
+ - On-chain access to unencrypted values for testing
19
+ - Compatible with the main `@fhenixprotocol/cofhe-contracts` package
20
+
21
+ ## Installation
22
+
23
+ npm
24
+
25
+ ```bash
26
+ npm install @fhenixprotocol/cofhe-mock-contracts
27
+ ```
28
+
29
+ foundry
30
+
31
+ ```bash
32
+ forge install fhenixprotocol/cofhe-mock-contracts
33
+ ```
34
+
35
+ ## Usages and Integrations
36
+
37
+ ### Who is this for?
38
+
39
+ This package is intended for **developers building and testing CoFHE-enabled applications and smart contracts**.
40
+
41
+ Use these mocks when you want to:
42
+
43
+ - Run **local tests** without depending on the real CoFHE coprocessor infrastructure.
44
+ - Debug flows end-to-end (encrypt → submit → operate → decrypt) with fast iteration.
45
+ - Assert on results deterministically in CI.
46
+
47
+ Do **not** use these mocks for production deployments: they intentionally make testing convenient (e.g. storing plaintext on-chain for inspection) and therefore **do not provide real confidentiality guarantees**.
48
+
49
+ ### Hardhat integration vs Foundry integration
50
+
51
+ Both integrations use the same underlying mock contracts, but they differ in **how mocks get deployed** and **how you interact with them**.
52
+
53
+ #### Hardhat (recommended for TS/SDK + Solidity tests)
54
+
55
+ Use this when you are already using **Hardhat** and/or want to run the **TypeScript SDK (`@cofhe/sdk`)** against a local chain.
56
+
57
+ - The `cofhesdk/hardhat-plugin` watches Hardhat `node` and `test` tasks.
58
+ - It automatically deploys the mocks to the Hardhat network at fixed addresses.
59
+ - The `cofheClient` (created with `createCofheClient(...)`) detects the mocks and routes CoFHE actions to them.
60
+
61
+ Minimal setup:
62
+
63
+ ```ts
64
+ // hardhat.config.ts
65
+ import 'cofhe-hardhat-plugin';
66
+
67
+ export default {
68
+ cofhe: {
69
+ logMocks: true, // optional
70
+ },
71
+ };
72
+ ```
73
+
74
+ Run:
75
+
76
+ ```bash
77
+ npx hardhat test
78
+ # or
79
+ npx hardhat node
80
+ ```
81
+
82
+ If you want to assert on plaintext values in Hardhat tests, the plugin exposes helpers like `mock_expectPlaintext(...)` (see the hardhat-plugin README).
83
+
84
+ #### Foundry (recommended for Solidity-only tests)
85
+
86
+ Use this when you are writing tests in **Solidity** and running them with `forge test`.
87
+
88
+ - You typically inherit from the abstract `CoFheTest` helper to deploy/setup the necessary FHE mock environment.
89
+ - You use helper methods to create encrypted inputs and assert their underlying values.
90
+
91
+ > **Important**: You must set `isolate = true` in your `foundry.toml`. Without this setting, some variables may be used without proper permission checks, which will cause failures on production chains.
92
+
93
+ `@cofhe/sdk` is designed to work with mock contracts in a testing / hardhat environment. `cofhesdk/hardhat-plugin` deploys the mock contracts in this repo, and the `cofheClient` detects a testnet chain and interacts correctly using the mocks rather than the true CoFHE coprocessor.
94
+
95
+ When installed and imported in the `hardhat.config.ts`, `cofhesdk/hardhat-plugin` will watch for Hardhat `node` and `test` tasks, and will deploy the mocks to the hardhat testnet chain at fixed addresses.
96
+
97
+ Once deployed, interaction with the mock contracts is handled by the `cofheClient` (created with `createCofheClient(...)`). The client checks for the existence of mock contracts at known addresses, and if they exist, marks the current connection as a testnet.
98
+
99
+ ## Logging
100
+
101
+ By default the mock CoFHE contracts log the internal "FHE" operations using `hardhat/console.sol`. Logs can be enabled or disabled using the `setLogOps()` function in `MockTaskManager.sol`.
102
+
103
+ ## Differences between Cofhe and Mocks
104
+
105
+ ### Symbolic Execution
106
+
107
+ The CoFHE coprocessor uses symbolic execution when performing operations on chain. Each ciphertext exists off-chain, and is represented by an on-chain ciphertext hash (`ctHash`).
108
+
109
+ FHE operations between one or more `ctHash`es returns a resultant `ctHash`, which is symbolically linked to the true `ciphertext` which includes the encrypted values.
110
+
111
+ In `cofhe-mock-contracts` the symbolic execution is preserved. In the case of the mocks, the `ciphertext` is not encrypted to be used in the FHE scheme, but is stored as a plaintext value. In this case, the `ctHash` associated with the `ciphertext` is pointing directly at the plaintext value instead.
112
+
113
+ During the execution of a mock FHE operation, say `FHE.add(euint8 ctHashA, euint8 ctHashB) -> euint8 ctHashC`, rather than being performed off-chain by the FHE computation engine, the input `ctHashes` are mapped to their plaintext value, and the operation performed as plaintext math on-chain. The result is inserted into the symbolic value position of `ctHashC`.
114
+
115
+ ### On-chain Decryption
116
+
117
+ CoFHE coprocessor handles on-chain decryption requests asynchronously. Once the decryption is requested with `FHE.decrypt(...)` the decryption will be performed off-chain by CoFHE, and the result posted on-chain in the `PlaintextStorage` module of `TaskManager`. The decryption result can then checked using either `FHE.getDecryptResult(...)` or `FHE.getDecryptResultSafe(...)`.
118
+
119
+ When a mock decryption is requested, a random number between 1 and 10 is generated to determine how many seconds the mock decryption async duration. Though the decryption result is available immediately within the mock contracts, the async duration is added to mimic the off-chain decryption and posting time.
120
+
121
+ ### ZkVerifying
122
+
123
+ A key component of CoFHE is the ability to pre-encrypt inputs in a secure and verifiable way. `cofhesdk` prepares these inputs automatically, and requests a verification signature from the coprocessor `ZkVerifier` module. The zkVerifier returns a signature indicating that the encrypted ciphertext is valid, and has been stored on the Fhenix L2 blockchain.
124
+
125
+ The mocks are then responsible for mocking two actions:
126
+
127
+ 1. Creating the signature.
128
+ 2. Storing the plaintext value on-chain.
129
+
130
+ The `MockZkVerifier` contract handles the on-chain storage of encrypted inputs. The signature creation is handled automatically within `cofheClient.encryptInputs` when executing against a testnet.
131
+
132
+ ### Off-chain Decryption / Sealing
133
+
134
+ Off-chain decryption is performed by calling the `cofheClient.decryptHandle` function with a valid `ctHash` and a valid `permit` [todo link].
135
+
136
+ When interacting with CoFHE this request is routed to the Threshold Network, which will perform the decryption operation, ultimately returning a decrypted result.
137
+
138
+ When working with the mocks, the `cofheClient` will instead query the `MockThresholdNetwork` contract, which will verify the request `permit`, and return the decrypted result.
139
+
140
+ ### Using Foundry
141
+
142
+ Use abstract CoFheTest contract to automatically deploy all necessary FHE contracts for testing.
143
+
144
+ CoFheTest also exposes useful test methods such as
145
+
146
+ - `assertHashValue(euint, uint)` - asserting an encrypted value is equal to an expected plaintext value
147
+ - `createInEuint..(number, user)` - for creating encrypted inputs (8-256bits) for a given user
148
+
149
+ see `contracts/TestBed.sol` for the original contract
150
+
151
+ ```solidity
152
+ import {Test} from "forge-std/Test.sol";
153
+ import {CoFheTest} from "@fhenixprotocol/cofhe-contracts/FHE.sol";
154
+ ...
155
+ contract TestBed is Test, CoFheTest {
156
+
157
+ TestBed private testbed;
158
+
159
+ address private user = makeAddr("user");
160
+
161
+ function setUp() public {
162
+ // optional ... enable verbose logging for fhe mocks
163
+ // setLog(true);
164
+
165
+ testbed = new TestBed();
166
+ }
167
+
168
+ function testSetNumber() public {
169
+ uint32 n = 10;
170
+ InEuint32 memory number = createInEuint32(n, user);
171
+
172
+ //must be the user who sends transaction
173
+ //or else invalid permissions from fhe allow
174
+ vm.prank(user);
175
+ testbed.setNumber(number);
176
+
177
+ assertHashValue(testbed.eNumber(), n);
178
+ }
179
+ }
180
+ ```
@@ -0,0 +1,99 @@
1
+ // SPDX-License-Identifier: UNLICENSED
2
+ pragma solidity ^0.8.13;
3
+
4
+ import '@fhenixprotocol/cofhe-contracts/FHE.sol';
5
+
6
+ contract ABITest {
7
+ euint32 public eNumber;
8
+ uint256 public numberHash;
9
+
10
+ euint8 public eUint8;
11
+ euint16 public eUint16;
12
+ euint32 public eUint32;
13
+ euint64 public eUint64;
14
+ euint128 public eUint128;
15
+ ebool public eBool;
16
+ eaddress public eAddress;
17
+
18
+ constructor() {
19
+ eUint8 = FHE.asEuint8(1);
20
+ eUint16 = FHE.asEuint16(1);
21
+ eUint32 = FHE.asEuint32(1);
22
+ eUint64 = FHE.asEuint64(1);
23
+ eUint128 = FHE.asEuint128(1);
24
+ eBool = FHE.asEbool(true);
25
+ eAddress = FHE.asEaddress(address(0));
26
+ }
27
+
28
+ struct ContainsEncryptedInput {
29
+ uint256 value;
30
+ InEuint32 encryptedInput;
31
+ }
32
+
33
+ struct ContainsEncryptedResult {
34
+ uint256 value;
35
+ euint32 encryptedResult;
36
+ }
37
+
38
+ // INPUTS
39
+
40
+ function fnNoEncryptedInputs(uint8 value) public {}
41
+
42
+ function fnEncryptedInput(InEuint32 memory inNumber) public {}
43
+
44
+ function fnBlendedInputsIncludingEncryptedInput(uint256 value, InEuint32 memory inNumber) public {}
45
+
46
+ function fnAllEncryptedInputs(
47
+ InEuint8 memory inEuint8,
48
+ InEuint16 memory inEuint16,
49
+ InEuint32 memory inEuint32,
50
+ InEuint64 memory inEuint64,
51
+ InEuint128 memory inEuint128,
52
+ InEbool memory inEbool,
53
+ InEaddress memory inEaddress
54
+ ) public {}
55
+
56
+ function fnStructContainsEncryptedInput(ContainsEncryptedInput memory containsEncryptedInput) public {}
57
+
58
+ function fnArrayContainsEncryptedInput(InEuint32[] memory inEuint32Array) public {}
59
+
60
+ function fnTupleContainsEncryptedInput(InEuint32[2] memory inEuint32Array) public {}
61
+
62
+ // OUTPUTS
63
+
64
+ function fnReturnNoEncrypted() public pure returns (uint256) {
65
+ return 1;
66
+ }
67
+
68
+ function fnReturnEncrypted() public view returns (euint32) {
69
+ return eUint32;
70
+ }
71
+
72
+ function fnReturnBlendedIncludingEncrypted() public view returns (uint256, euint32) {
73
+ return (1, eUint32);
74
+ }
75
+
76
+ function fnReturnEncryptedArray() public view returns (euint32[] memory) {
77
+ euint32[] memory encryptedArray = new euint32[](1);
78
+ encryptedArray[0] = eUint32;
79
+ return encryptedArray;
80
+ }
81
+
82
+ function fnReturnEncryptedStruct() public view returns (ContainsEncryptedResult memory) {
83
+ ContainsEncryptedResult memory encryptedResult = ContainsEncryptedResult({ value: 1, encryptedResult: eUint32 });
84
+ return encryptedResult;
85
+ }
86
+
87
+ function fnReturnAllEncrypted() public view returns (euint8, euint16, euint32, euint64, euint128, ebool, eaddress) {
88
+ return (eUint8, eUint16, eUint32, eUint64, eUint128, eBool, eAddress);
89
+ }
90
+
91
+ // EVENTS
92
+
93
+ event EventNoEncryptedInputs(uint8 value);
94
+ event EncryptedValue(euint32 value);
95
+ event BlendedValue(uint256 value, euint32 encryptedValue);
96
+ event EncryptedArray(euint32[] value);
97
+ event EncryptedStruct(ContainsEncryptedResult value);
98
+ event AllEncrypted(euint8, euint16, euint32, euint64, euint128, ebool, eaddress);
99
+ }