FHECounter

Category: Basic | Difficulty: Beginner | Chapters: Basics | Concept: Encrypted counter using FHE.add and FHE.sub

Encrypted counter with increment and decrement operations.

Why this example

This example focuses on Encrypted counter using FHE.add and FHE.sub. It is designed to be self-contained and easy to run locally.

Quick start

npm install
npm run test:mocked -- test/basic/FHECounter.test.ts

Dependencies

None

Contract and test

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

import {FHE, euint64, externalEuint64} from "@fhevm/solidity/lib/FHE.sol";
import {ZamaEthereumConfig} from "@fhevm/solidity/config/ZamaConfig.sol";

/**
 * @title FHECounter
 * @author Gustavo Valverde
 * @notice Encrypted counter with increment and decrement operations.
 * @dev Example for fhEVM Examples - Basic Category
 *
 * @custom:category basic
 * @custom:chapter basics
 * @custom:concept Encrypted counter using FHE.add and FHE.sub
 * @custom:difficulty beginner
 */
contract FHECounter is ZamaEthereumConfig {
    euint64 private count;

    /// @notice Returns the encrypted counter value.
    /// @return The encrypted counter value
    function getCount() external view returns (euint64) {
        return count;
    }

    /// @notice Increment the counter by an encrypted amount.
    /// @param encAmount Encrypted amount handle
    /// @param inputProof Proof for the encrypted input
    function increment(externalEuint64 encAmount, bytes calldata inputProof) external {
        euint64 amount = FHE.fromExternal(encAmount, inputProof);
        count = FHE.add(count, amount);
        FHE.allowThis(count);
        FHE.allow(count, msg.sender);
    }

    /// @notice Decrement the counter by an encrypted amount.
    /// @param encAmount Encrypted amount handle
    /// @param inputProof Proof for the encrypted input
    function decrement(externalEuint64 encAmount, bytes calldata inputProof) external {
        euint64 amount = FHE.fromExternal(encAmount, inputProof);
        count = FHE.sub(count, amount);
        FHE.allowThis(count);
        FHE.allow(count, msg.sender);
    }
}

Pitfalls to avoid

No pitfalls are highlighted in the tests for this example.

API Reference

Overview

Encrypted counter with increment and decrement operations.

Developer Notes

Example for fhEVM Examples - Basic Category

getCount

Returns the encrypted counter value.

Return Values

Name
Type
Description

[0]

euint64

The encrypted counter value

increment

Increment the counter by an encrypted amount.

Parameters

Name
Type
Description

encAmount

externalEuint64

Encrypted amount handle

inputProof

bytes

Proof for the encrypted input

decrement

Decrement the counter by an encrypted amount.

Parameters

Name
Type
Description

encAmount

externalEuint64

Encrypted amount handle

inputProof

bytes

Proof for the encrypted input

Last updated