UserDecryptSingleValue

Category: Basic | Difficulty: Beginner | Chapters: Decryption User | Concept: User decryption flow for a single encrypted result

Compute on encrypted input and allow the user to decrypt the result.

Why this example

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

Quick start

npm install
npm run test:mocked -- test/basic/UserDecryptSingleValue.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 UserDecryptSingleValue
 * @author Gustavo Valverde
 * @notice Compute on encrypted input and allow the user to decrypt the result.
 * @dev Example for fhEVM Examples - Basic Category
 *
 * @custom:category basic
 * @custom:chapter decryption-user
 * @custom:concept User decryption flow for a single encrypted result
 * @custom:difficulty beginner
 */
contract UserDecryptSingleValue is ZamaEthereumConfig {
    euint64 private lastResult;

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

    /// @notice Add 1 to the encrypted input and store the encrypted result.
    /// @param encValue Encrypted value handle
    /// @param inputProof Proof for the encrypted input
    function computePlusOne(externalEuint64 encValue, bytes calldata inputProof) external {
        euint64 value = FHE.fromExternal(encValue, inputProof);
        lastResult = FHE.add(value, FHE.asEuint64(1));
        FHE.allowThis(lastResult);
        FHE.allow(lastResult, msg.sender);
    }
}

Pitfalls to avoid

No pitfalls are highlighted in the tests for this example.

API Reference

Overview

Compute on encrypted input and allow the user to decrypt the result.

Developer Notes

Example for fhEVM Examples - Basic Category

getLastResult

Get the encrypted result.

Return Values

Name
Type
Description

[0]

euint64

The encrypted result

computePlusOne

Add 1 to the encrypted input and store the encrypted result.

Parameters

Name
Type
Description

encValue

externalEuint64

Encrypted value handle

inputProof

bytes

Proof for the encrypted input

Last updated