FHEEq

Category: Basic | Difficulty: Beginner | Chapters: Comparisons | Concept: Compare two encrypted values using FHE.eq

Encrypted equality comparison with FHE.eq.

Why this example

This example focuses on Compare two encrypted values using FHE.eq. It is designed to be self-contained and easy to run locally.

Quick start

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

Dependencies

None

Contract and test

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

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

/**
 * @title FHEEq
 * @author Gustavo Valverde
 * @notice Encrypted equality comparison with FHE.eq.
 * @dev Example for fhEVM Examples - Basic Category
 *
 * @custom:category basic
 * @custom:chapter comparisons
 * @custom:concept Compare two encrypted values using FHE.eq
 * @custom:difficulty beginner
 */
contract FHEEq is ZamaEthereumConfig {
    ebool private lastResult;

    /// @notice Returns the last encrypted comparison result.
    /// @return The last encrypted comparison result
    function getLastResult() external view returns (ebool) {
        return lastResult;
    }

    /// @notice Compare two encrypted inputs and store the encrypted result.
    /// @param encA First encrypted value handle
    /// @param encB Second encrypted value handle
    /// @param inputProof Proof for the encrypted inputs
    function compare(
        externalEuint64 encA,
        externalEuint64 encB,
        bytes calldata inputProof
    ) external {
        euint64 a = FHE.fromExternal(encA, inputProof);
        euint64 b = FHE.fromExternal(encB, inputProof);
        lastResult = FHE.eq(a, b);
        FHE.allowThis(lastResult);
        FHE.allow(lastResult, msg.sender);
    }
}

Pitfalls to avoid

No pitfalls are highlighted in the tests for this example.

API Reference

Overview

Encrypted equality comparison with FHE.eq.

Developer Notes

Example for fhEVM Examples - Basic Category

getLastResult

Returns the last encrypted comparison result.

Return Values

Name
Type
Description

[0]

ebool

The last encrypted comparison result

compare

Compare two encrypted inputs and store the encrypted result.

Parameters

Name
Type
Description

encA

externalEuint64

First encrypted value handle

encB

externalEuint64

Second encrypted value handle

inputProof

bytes

Proof for the encrypted inputs

Last updated