Overview
Decryption is the process of converting encrypted data back into its original form. In the context of Fully Homomorphic Encryption (FHE), decryption allows for the retrieval of results after performing computations on encrypted data.Understanding Asynchronous Decryption
Like all other FHE operations in CoFHE, decryption is executed asynchronously. This means:- You request decryption - Call the decrypt function
- The operation takes time to complete - Processing happens off-chain
- Results are stored on-chain - Once ready, you can retrieve them
Decryption Methods: Query vs Transaction
Fhenix provides two primary ways to perform decryption, each suited for different use cases:1. Decryption via Solidity Contract Transaction
Decryption is requested in a smart contract transaction, storing the result on-chain for all to access. This ensures auditability but incurs higher gas costs and makes the result public.2. Decryption via RPC Query
Decryption is requested off-chain via an RPC query, returning the result only to the requester. This method keeps data private and reduces gas costs but prevents smart contract usage of the decrypted value.Read more about RPC query decryption and get examples in the CoFHEjs documentation.
Comparison Table
| Method | Visibility | Gas Cost | Smart Contract Usable | Best For |
|---|---|---|---|---|
| Transaction (on-chain) | Public (on-chain) | High | Yes | Public results, contract logic |
| Query (off-chain) | Private (off-chain) | None | No | Confidential data, external apps |
Asynchronous On-Chain Decryption
When decrypting data on-chain, you first request decryption usingFHE.decrypt(), then later retrieve the results. There are two ways to retrieve decryption results: the safe way (recommended) and the unsafe way.
Safe vs Unsafe Decryption
Safe Decryption - FHE.getDecryptResultSafe()
Safe Decryption - FHE.getDecryptResultSafe()
Recommended for most use casesUse Advantages:
FHE.getDecryptResultSafe(eParam) to get both the decrypted value and a plaintext boolean success indicator:- Returns a boolean indicating readiness
- Allows custom error handling
- Better user experience with informative messages
- More flexible control flow
- Production applications
- User-facing operations
- When you need graceful error handling
Unsafe Decryption - FHE.getDecryptResult()
Unsafe Decryption - FHE.getDecryptResult()
Use with cautionThe Advantages:
FHE.getDecryptResult(eParam) function doesn’t check readiness for you. If decryption is ready, you get the decrypted value; otherwise, the execution reverts.- Simpler, more concise code
- Fail-fast behavior
- Less code to write
- Internal operations where revert is acceptable
- When you want to fail fast
- Testing and development
Full Example Contract
Here’s a complete example showing both safe and unsafe decryption in an auction contract:Best Practices
Always Use Safe Decryption in Production
Prefer
FHE.getDecryptResultSafe() for production applications to provide better user feedback when decryption isn’t ready yet.Check Access Control Before Decrypting
Ensure only authorized parties can request decryption. Remember that decryption requires proper ACL permissions on the ciphertext handle.
Handle Decryption Delays Gracefully
Inform users that decryption is asynchronous and may take time. Provide clear feedback about when to check back for results.
Consider Gas Costs
On-chain decryption has higher gas costs. For confidential results, consider using off-chain RPC query decryption instead.
Common Patterns
Pattern 1: Two-Step Decryption
Pattern 2: Batch Decryption
Related Topics
- Learn about access control requirements in Access Control
- Understand asynchronous operations in Data Evaluation
- Explore off-chain decryption in CoFHEjs