Skip to main content

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 decryptForTx off-chain and publishDecryptResult on-chain to reveal the winner

How It Works

The auction follows this flow:
1

Initialization

The auctioneer deploys the contract, which initializes the auction with zero bid and address values, both encrypted.
2

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.
3

Close Auction

The auctioneer closes the auction and calls FHE.allowPublic on the highest bid and bidder, making them eligible for public decryption.
4

Decrypt Off-Chain

Anyone can call decryptForTx off-chain to obtain the plaintext values and Threshold Network signatures for the winning bid and bidder.
5

Reveal Winner

The decrypted values and signatures are submitted on-chain via revealWinner, which calls FHE.publishDecryptResult to verify the proofs and store the results.

Key Concepts Demonstrated

1. Encrypted State Variables

The contract stores the highest bid and bidder as encrypted values:
These values remain encrypted throughout the entire auction, preventing anyone from seeing the current highest bid.

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)
Step 2: Request decryption off-chain (client-side)
Step 3: Publish results on-chain with proof

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

The bid() function handles incoming bids:
Notice how the contract never reveals the current highest bid to bidders. All comparisons and updates happen on encrypted data, maintaining complete confidentiality throughout the bidding process.

Closing the Auction

The auctioneer closes the auction and allows public decryption of the winning bid and bidder:
Since 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

The revealWinner 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.