@cofhe/sdk fits into the Fhenix framework, you’ll explore a simple mental model using a Counter smart contract example. This will show you how data flows through FHE-enabled dApps—from encryption to computation to decryption.
The Counter Example
Imagine a smart contract called Counter where each user has their own private counter. Users can increment their counter and read its value with complete privacy—no one, including the smart contract itself, can see the actual counter values.Key Concepts
- Public Key = A lock that anyone can use to seal data
- Private Key = The unique key to unlock sealed data
- CoFHE Co-Processor = Fhenix’s off-chain service that handles FHE operations
- Ciphertext = Encrypted data that can be computed on without decryption
Encrypting Input Data
When a user wants to add
5 to their counter, the data must first be encrypted before being sent to the smart contract.What happens:- The user’s plaintext value
5is encrypted usingclient.encryptInputs([Encryptable.uint32(5n)]).execute() - The SDK generates a ZK proof and submits the encrypted value to the CoFHE verifier
- The returned
EncryptedItemInputis sent to the smart contract on-chain - The blockchain sees only encrypted data, never the actual value
5
The “Locked Box” Analogy
Think of this as placing the value5 in a box and locking it with the CoFHE co-processor’s public key. The locked box (ciphertext) can be sent to the smart contract, but no one can see what’s inside without the private key.Performing Computations on Encrypted Data
Once the encrypted data reaches the smart contract, FHE magic happens. The smart contract can perform arithmetic operations directly on the ciphertext without ever decrypting it.What happens:
- The smart contract receives the encrypted value
- It retrieves the user’s encrypted counter from storage (also encrypted)
- Using FHE operations, it adds the encrypted values together
- The result is stored as encrypted data
- Critical: At no point does the smart contract, blockchain, or anyone else see the actual numbers
FHE Computation Magic
This is where Fully Homomorphic Encryption shines. The CoFHE co-processor enables the smart contract to:- Add encrypted values together
- Compare encrypted values
- Perform other arithmetic operations
- All while the data remains encrypted and private
Retrieving Encrypted Output
When a user wants to read their counter value, they use one of the SDK’s two decryption methods depending on their goal.For UI display (
decryptForView):- The user reads the encrypted handle (
ctHash) from the contract - A permit authorizes decryption — created via
client.permits.getOrCreateSelfPermit() - The SDK requests re-encryption from the Threshold Network using the permit’s sealing key
- The plaintext is returned locally for display
decryptForTx):- The user reads the encrypted handle (
ctHash) from the contract - The SDK requests decryption from the Threshold Network
- The plaintext and a verifiable signature are returned
- The signature can be verified on-chain via
FHE.verifyDecryptResult(...)
The “Lock Exchange” Analogy
This is like exchanging locks on the box:- The box starts locked with the CoFHE co-processor’s lock
- The user sends their own lock to the co-processor (via the permit’s sealing key)
- The co-processor removes its lock and applies the user’s lock
- The box remains locked throughout, but now only the user can open it
- The data remains private at every step
The Complete Flow
Key Takeaways
- Encryption happens client-side: The SDK encrypts data with ZK proofs before it reaches the blockchain
- Computation happens on-chain: Smart contracts perform operations on encrypted data via
FHE.sol - FHE enables privacy-preserving computation: The blockchain never sees plaintext values
- Permits enable access control: EIP-712 signed permits authorize who can decrypt specific data
- Two decryption paths:
decryptForViewfor UI display,decryptForTxfor on-chain verification