PublicDecryptMultipleValues

Category: Basic | Difficulty: Intermediate | Chapters: Decryption Public, Relayer | Concept: Public decryption flow for multiple encrypted values

Publish multiple encrypted results for public decryption.

Why this example

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

Quick start

npm install
npm run test:mocked -- test/basic/PublicDecryptMultipleValues.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 PublicDecryptMultipleValues
 * @author Gustavo Valverde
 * @notice Publish multiple encrypted results for public decryption.
 * @dev Example for fhEVM Examples - Basic Category
 *
 * @custom:category basic
 * @custom:chapter decryption-public, relayer
 * @custom:concept Public decryption flow for multiple encrypted values
 * @custom:difficulty intermediate
 */
contract PublicDecryptMultipleValues is ZamaEthereumConfig {
    euint64 private lastFirst;
    euint64 private lastSecond;

    /// @notice Store two encrypted values.
    /// @param encFirst First encrypted value handle
    /// @param encSecond Second encrypted value handle
    /// @param inputProof Proof for the encrypted inputs
    function storeValues(
        externalEuint64 encFirst,
        externalEuint64 encSecond,
        bytes calldata inputProof
    ) external {
        lastFirst = FHE.fromExternal(encFirst, inputProof);
        lastSecond = FHE.fromExternal(encSecond, inputProof);

        FHE.allowThis(lastFirst);
        FHE.allowThis(lastSecond);
        FHE.allow(lastFirst, msg.sender);
        FHE.allow(lastSecond, msg.sender);
    }

    /// @notice Publish both values for public decryption.
    function publishValues() external {
        FHE.makePubliclyDecryptable(lastFirst);
        FHE.makePubliclyDecryptable(lastSecond);
    }

    /// @notice Returns the handles for public decryption.
    /// @return Handle for the first encrypted value
    /// @return Handle for the second encrypted value
    function getValueHandles() external view returns (bytes32, bytes32) {
        return (FHE.toBytes32(lastFirst), FHE.toBytes32(lastSecond));
    }
}

Pitfalls to avoid

No pitfalls are highlighted in the tests for this example.

API Reference

Overview

Publish multiple encrypted results for public decryption.

Developer Notes

Example for fhEVM Examples - Basic Category

storeValues

Store two encrypted values.

Parameters

Name
Type
Description

encFirst

externalEuint64

First encrypted value handle

encSecond

externalEuint64

Second encrypted value handle

inputProof

bytes

Proof for the encrypted inputs

publishValues

Publish both values for public decryption.

getValueHandles

Returns the handles for public decryption.

Return Values

Name
Type
Description

[0]

bytes32

Handle for the first encrypted value

[1]

bytes32

Handle for the second encrypted value

Last updated