Overview
This example demonstrates how to build a fully confidential auction system where bids remain encrypted throughout the bidding process. Only when the auction closes can the winner be revealed, ensuring that bidders cannot see or react to each other’s bids.What You’ll Learn
In this example, you’ll see practical implementations of:- Encrypted input handling - Processing encrypted bid amounts
- Encrypted comparisons - Finding the highest bid without revealing values
- Conditional logic with
select- Updating the highest bidder based on encrypted conditions - Access control management - Properly managing permissions for encrypted data
- Decrypt-with-proof pattern - Using
decryptForTxoff-chain andpublishDecryptResulton-chain to reveal the winner
How It Works
The auction follows this flow:Initialization
The auctioneer deploys the contract, which initializes the auction with zero bid and address values, both encrypted.
Bidding Phase
Participants submit bids by sending plaintext amounts that are immediately encrypted. Each bid is compared against the current highest bid using encrypted comparison (
FHE.gt), and the highest bid and bidder are updated accordingly.Close Auction
The auctioneer closes the auction and calls
FHE.allowPublic on the highest bid and bidder, making them eligible for public decryption.Decrypt Off-Chain
Anyone can call
decryptForTx off-chain to obtain the plaintext values and Threshold Network signatures for the winning bid and bidder.Key Concepts Demonstrated
1. Encrypted State Variables
The contract stores the highest bid and bidder as encrypted values:2. Encrypted Comparisons and Updates
When a new bid comes in, the contract uses encrypted operations to update the highest bid:3. Decrypt-with-Proof Pattern
The contract demonstrates the new decryption flow: Step 1: Close auction and allow public decryption (on-chain)Complete Contract Code
Here’s the full implementation of the encrypted auction contract:Code Walkthrough
Constructor
The constructor initializes the auction with encrypted zero values:The
FHE.allowThis() calls are crucial - they grant the contract permission to access these encrypted values in future transactions.Bidding Function
Thebid() function handles incoming bids:
Closing the Auction
The auctioneer closes the auction and allows public decryption of the winning bid and bidder:FHE.allowPublic is used, anyone can request decryption off-chain without needing a permit. The values are not revealed until someone submits the proof on-chain.
Revealing the Winner
TherevealWinner function accepts the decrypted values and their Threshold Network signatures, then publishes them on-chain:
FHE.publishDecryptResult verifies the Threshold Network signature before accepting the plaintext. If the signature is invalid, the transaction reverts.Usage Flow
1. Deploy the Contract
2. Place Bids
3. Close the Auction
4. Decrypt Off-Chain and Reveal the Winner
Key Takeaways
Privacy Throughout
Bids remain completely encrypted during the auction. No one can see the current highest bid or react to other bids.
Encrypted Comparisons
The contract uses
FHE.gt(), FHE.max(), and FHE.select() to update the highest bid without decrypting values.Access Control
Every encrypted value created must have permissions granted via
FHE.allowThis() for the contract to access it later. Use FHE.allowPublic() when values are ready to be revealed.Decrypt-with-Proof
Decryption happens off-chain via
decryptForTx, and results are verified on-chain via FHE.publishDecryptResult with a Threshold Network signature.Related Examples
- Learn about conditional logic in Conditions
- Understand decryption methods in Decryption Operations
- Explore access control in Access Control