UserDecryptMultipleValues

Category: Basic | Difficulty: Beginner | Chapters: Decryption User | Concept: User decryption flow for multiple encrypted results

Produce multiple encrypted outputs and allow the user to decrypt both.

Why this example

This example focuses on User decryption flow for multiple encrypted results. It is designed to be self-contained and easy to run locally.

Quick start

npm install
npm run test:mocked -- test/basic/UserDecryptMultipleValues.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 UserDecryptMultipleValues
 * @author Gustavo Valverde
 * @notice Produce multiple encrypted outputs and allow the user to decrypt both.
 * @dev Example for fhEVM Examples - Basic Category
 *
 * @custom:category basic
 * @custom:chapter decryption-user
 * @custom:concept User decryption flow for multiple encrypted results
 * @custom:difficulty beginner
 */
contract UserDecryptMultipleValues is ZamaEthereumConfig {
    euint64 private lastSum;
    euint64 private lastDifference;

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

    /// @notice Returns the last encrypted difference.
    /// @return The last encrypted difference
    function getLastDifference() external view returns (euint64) {
        return lastDifference;
    }

    /// @notice Compute sum and difference for two encrypted inputs.
    /// @param encA First encrypted value handle
    /// @param encB Second encrypted value handle
    /// @param inputProof Proof for the encrypted inputs
    function computeSumAndDifference(
        externalEuint64 encA,
        externalEuint64 encB,
        bytes calldata inputProof
    ) external {
        euint64 a = FHE.fromExternal(encA, inputProof);
        euint64 b = FHE.fromExternal(encB, inputProof);

        lastSum = FHE.add(a, b);
        lastDifference = FHE.sub(a, b);

        FHE.allowThis(lastSum);
        FHE.allowThis(lastDifference);
        FHE.allow(lastSum, msg.sender);
        FHE.allow(lastDifference, msg.sender);
    }
}

Pitfalls to avoid

No pitfalls are highlighted in the tests for this example.

API Reference

Overview

Produce multiple encrypted outputs and allow the user to decrypt both.

Developer Notes

Example for fhEVM Examples - Basic Category

getLastSum

Returns the last encrypted sum.

Return Values

Name
Type
Description

[0]

euint64

The last encrypted sum

getLastDifference

Returns the last encrypted difference.

Return Values

Name
Type
Description

[0]

euint64

The last encrypted difference

computeSumAndDifference

Compute sum and difference for two encrypted inputs.

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