EncryptSingleValue

Category: Basic | Difficulty: Beginner | Chapters: Encryption | Concept: Store one encrypted value and grant permissions

Store a single encrypted value for each user.

Why this example

This example focuses on Store one encrypted value and grant permissions. It is designed to be self-contained and easy to run locally.

Quick start

npm install
npm run test:mocked -- test/basic/EncryptSingleValue.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 EncryptSingleValue
 * @author Gustavo Valverde
 * @notice Store a single encrypted value for each user.
 * @dev Example for fhEVM Examples - Basic Category
 *
 * @custom:category basic
 * @custom:chapter encryption
 * @custom:concept Store one encrypted value and grant permissions
 * @custom:difficulty beginner
 */
contract EncryptSingleValue is ZamaEthereumConfig {
    mapping(address user => euint64 value) private values;

    /// @notice Store an encrypted value for the sender.
    /// @param encValue Encrypted value handle
    /// @param inputProof Proof for the encrypted input
    function storeValue(externalEuint64 encValue, bytes calldata inputProof) external {
        euint64 value = FHE.fromExternal(encValue, inputProof);
        values[msg.sender] = value;
        FHE.allowThis(value);
        FHE.allow(value, msg.sender);
    }

    /// @notice Retrieve the encrypted value for a user.
    /// @param user Account holding the encrypted value
    /// @return The encrypted stored value
    function getValue(address user) external view returns (euint64) {
        return values[user];
    }
}

Pitfalls to avoid

No pitfalls are highlighted in the tests for this example.

API Reference

Overview

Store a single encrypted value for each user.

Developer Notes

Example for fhEVM Examples - Basic Category

storeValue

Store an encrypted value for the sender.

Parameters

Name
Type
Description

encValue

externalEuint64

Encrypted value handle

inputProof

bytes

Proof for the encrypted input

getValue

Retrieve the encrypted value for a user.

Parameters

Name
Type
Description

user

address

Account holding the encrypted value

Return Values

Name
Type
Description

[0]

euint64

The encrypted stored value

Last updated