FHEAdd

Category: Basic | Difficulty: Beginner | Chapters: Arithmetic | Concept: Add two encrypted values with FHE.add

Encrypted addition example for two values.

Why this example

This example focuses on Add two encrypted values with FHE.add. It is designed to be self-contained and easy to run locally.

Quick start

npm install
npm run test:mocked -- test/basic/FHEAdd.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 FHEAdd
 * @author Gustavo Valverde
 * @notice Encrypted addition example for two values.
 * @dev Example for fhEVM Examples - Basic Category
 *
 * @custom:category basic
 * @custom:chapter arithmetic
 * @custom:concept Add two encrypted values with FHE.add
 * @custom:difficulty beginner
 */
contract FHEAdd is ZamaEthereumConfig {
    euint64 private lastResult;

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

    /// @notice Add 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 addValues(
        externalEuint64 encA,
        externalEuint64 encB,
        bytes calldata inputProof
    ) external {
        euint64 a = FHE.fromExternal(encA, inputProof);
        euint64 b = FHE.fromExternal(encB, inputProof);
        lastResult = FHE.add(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 addition example for two values.

Developer Notes

Example for fhEVM Examples - Basic Category

getLastResult

Returns the last encrypted sum.

Return Values

Name
Type
Description

[0]

euint64

The last encrypted sum

addValues

Add 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