InputProofsExplained

Category: Basic | Difficulty: Intermediate | Chapters: Input Proofs | Concept: Input proofs bind encrypted inputs to a contract and sender

Demonstrate input proof binding to contract and signer.

Why this example

This example focuses on Input proofs bind encrypted inputs to a contract and sender. It is designed to be self-contained and easy to run locally.

Quick start

npm install
npm run test:mocked -- test/basic/InputProofsExplained.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 InputProofsExplained
 * @author Gustavo Valverde
 * @notice Demonstrate input proof binding to contract and signer.
 * @dev Example for fhEVM Examples - Basic Category
 *
 * @custom:category basic
 * @custom:chapter input-proofs
 * @custom:concept Input proofs bind encrypted inputs to a contract and sender
 * @custom:difficulty intermediate
 */
contract InputProofsExplained is ZamaEthereumConfig {
    mapping(address user => euint64 secret) private secrets;

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

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

Pitfalls to avoid

  • rejects a proof bound to a different sender

  • rejects a proof bound to a different contract

API Reference

Overview

Demonstrate input proof binding to contract and signer.

Developer Notes

Example for fhEVM Examples - Basic Category

storeSecret

Store an encrypted secret for the sender.

Parameters

Name
Type
Description

encValue

externalEuint64

Encrypted value handle

inputProof

bytes

Proof for the encrypted input

getSecret

Retrieve the encrypted secret for a user.

Parameters

Name
Type
Description

user

address

Account holding the encrypted secret

Return Values

Name
Type
Description

[0]

euint64

The encrypted secret value

Last updated