AntiPatternMissingUserAllow

Category: Basic | Difficulty: Intermediate | Chapters: Anti Patterns | Concept: Missing FHE.allow(user) blocks user decryption

Forgetting to grant user access prevents decryption.

Why this example

This example focuses on Missing FHE.allow(user) blocks user decryption. It is designed to be self-contained and easy to run locally.

Quick start

npm install
npm run test:mocked -- test/basic/AntiPatternMissingUserAllow.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 AntiPatternMissingUserAllow
 * @author Gustavo Valverde
 * @notice Forgetting to grant user access prevents decryption.
 * @dev Example for fhEVM Examples - Basic Category
 *
 * @custom:category basic
 * @custom:chapter anti-patterns
 * @custom:concept Missing FHE.allow(user) blocks user decryption
 * @custom:difficulty intermediate
 */
contract AntiPatternMissingUserAllow is ZamaEthereumConfig {
    mapping(address user => euint64 value) private stored;

    /// @notice Store an encrypted value but forget to grant user access (pitfall).
    /// @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);
        stored[msg.sender] = value;
        FHE.allowThis(value);
    }

    /// @notice Return the stored encrypted value.
    /// @param user Account holding the encrypted value
    /// @return The encrypted stored value
    function getStoredValue(address user) external view returns (euint64) {
        return stored[user];
    }
}

Pitfalls to avoid

  • fails to decrypt when FHE.allow(user) is missing

API Reference

Overview

Forgetting to grant user access prevents decryption.

Developer Notes

Example for fhEVM Examples - Basic Category

storeValue

Store an encrypted value but forget to grant user access (pitfall).

Parameters

Name
Type
Description

encValue

externalEuint64

Encrypted value handle

inputProof

bytes

Proof for the encrypted input

getStoredValue

Return the stored encrypted value.

Parameters

Name
Type
Description

user

address

Account holding the encrypted value

Return Values

Name
Type
Description

[0]

euint64

The encrypted stored value

Last updated