-
Notifications
You must be signed in to change notification settings - Fork 1
Repo of mock contracts that act as a 1-1 drop in replacement for CoFHE (but on-chain)
License
FhenixProtocol/cofhe-mock-contracts
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
@fhenixprotocol/cofhe-mock-contracts
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.
- Mock implementations of core CoFHE contracts:
- MockTaskManager
- MockQueryDecrypter
- MockZkVerifier
- ACL (Access Control List)
- Synchronous operation simulation with mock delays
- On-chain access to unencrypted values for testing
- Compatible with the main
@fhenixprotocol/cofhe-contracts
package
npm install @fhenixprotocol/cofhe-mock-contracts
Both cofhejs
and cofhe-hardhat-plugin
interact directly with the cofhe-mock-contracts.
When installed and imported in the hardhat.config.ts
, cofhe-hardhat-plugin
will watch for Hardhat node
and test
tasks, and will inject the necessary mock contracts into the hardhat testnet chain at fixed addresses.
Once deployed, interaction with the mock contracts is handled by cofhejs
. cofhejs
checks for the existence of mock contracts at known addresses, and if they exist, marks the current connection as a testnet. On testnet chains, cofhejs
will exit from the default flow when cofhejs.encrypt
or cofhejs.unseal
are used.
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
.
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
).
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.
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.
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
.
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(...)
.
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.
A key component of CoFHE is the ability to pre-encrypt inputs in a secure and verifiable way. cofhejs
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.
The mocks are then responsible for mocking two actions:
- Creating the signature.
- Storing the plaintext value on-chain.
The MockZkVerifier
contract handles the on-chain storage of encrypted inputs. The signature creation is handled in cofhejs
when executing against a testnet.
Off-chain decryption is performed by calling the cofhejs.unseal
function with a valid ctHash
and a valid permit
[todo link].
When interacting with CoFHE this request is routed to the Threshold Network, which will perform the decryption operation, ultimately returning a decrypted result.
When working with the mocks, cofhejs
will instead query the MockQueryDecrypter
contract, which will verify the request permit
, and return the decrypted result.
About
Repo of mock contracts that act as a 1-1 drop in replacement for CoFHE (but on-chain)