Logo
Back to Hub

FHE Counter

Simple encrypted counter demonstrating basic FHE operations

Basics
Documentation

FHE Counter

A simple encrypted counter contract demonstrating the basics of Fully Homomorphic Encryption (FHE) on Ethereum using FHEVM.

What You'll Learn

This example teaches the fundamental patterns for working with encrypted data in FHEVM:

  • Encrypted State: Store encrypted values (euint32) on-chain
  • External Inputs: Accept encrypted inputs from users with FHE.fromExternal()
  • FHE Operations: Perform arithmetic on encrypted values (FHE.add(), FHE.sub())
  • Permission Management: Control access to encrypted data with FHE.allow() and FHE.allowThis()
  • View Functions: Return encrypted handles for client-side decryption

Contract Overview

The FHECounter contract maintains an encrypted counter that can be incremented and decremented without revealing the actual count value on-chain.

Key Features

Loading...
  • increment(inputEuint32, inputProof) - Add encrypted value to counter
  • decrement(inputEuint32, inputProof) - Subtract encrypted value from counter
  • getCount() - Returns encrypted count handle for decryption

Quick Start

Prerequisites

  • Node.js >= 20
  • npm >= 7.0.0

Installation

Loading...

Compile

Loading...

Test

Loading...

Usage Example

Loading...

Key Patterns Demonstrated

1. Accepting Encrypted External Inputs

Loading...

The externalEuint32 type and inputProof work together to securely bring encrypted data from the client into the contract.

2. FHE Arithmetic Operations

Loading...

All arithmetic happens on encrypted values without decryption.

3. Permission Management

Loading...

Permissions control who can decrypt encrypted values.

4. Configuration Inheritance

The FHECounter contract inherits from ZamaEthereumConfig to access the FHEVM configuration.

Loading...

Important Notes

This example intentionally omits overflow/underflow checks for clarity and simplicity. In production:

Loading...

Testing

The test suite demonstrates:

  • Deploying FHE contracts
  • Creating encrypted inputs with fhevm.createEncryptedInput()
  • Calling contract functions with encrypted parameters
  • Decrypting results with fhevm.userDecryptEuint()
  • Verifying encrypted computation results

Run tests:

Loading...

Next Steps

After mastering this example, explore:

  • FHE Simple Vault - Learn access control with encrypted balances
  • FHE Voting - Confidential voting mechanisms
  • Confidential Bank - Gateway decryption for revealing results

Resources

License

BSD-3-Clause-Clear


Part of ZCraft FHEVM Examples | Built with Zama FHEVM

Loading...