docs/solidity-guides/inputs.md
This document introduces the concept of encrypted inputs in the FHEVM, explaining their role, structure, validation process, and how developers can integrate them into smart contracts and applications.
Encrypted inputs are a core feature of FHEVM, enabling users to push encrypted data onto the blockchain while ensuring data confidentiality and integrity.
Encrypted inputs are data values submitted by users in ciphertext form. These inputs allow sensitive information to remain confidential while still being processed by smart contracts. They are accompanied by Zero-Knowledge Proofs of Knowledge (ZKPoKs) to ensure the validity of the encrypted data without revealing the plaintext.
When a function in a smart contract is called, it may accept two types of parameters for encrypted inputs:
externalEbool, externalEaddress,externalEuintXX: Refers to the index of the encrypted parameter within the proof, representing a specific encrypted input handle.bytes: Contains the ciphertext and the associated zero-knowledge proof used for validation.Here’s an example of a Solidity function accepting multiple encrypted parameters:
function exampleFunction(
externalEbool param1,
externalEuint64 param2,
externalEuint8 param3,
bytes calldata inputProof
) public {
// Function logic here
}
In this example, param1, param2, and param3 are encrypted inputs for ebool, euint64, and euint8 while inputProof contains the corresponding ZKPoK to validate their authenticity.
In the below example, we use Alice's address to create the encrypted inputs and submits the transaction.
import { fhevm } from "hardhat";
const input = fhevm.createEncryptedInput(contract.address, signers.alice.address);
input.addBool(canTransfer); // at index 0
input.add64(transferAmount); // at index 1
input.add8(transferType); // at index 2
const encryptedInput = await input.encrypt();
const externalEboolParam1 = encryptedInput.handles[0];
const externalEuint64Param2 = encryptedInput.handles[1];
const externalEuint8Param3 = encryptedInput.handles[2];
const inputProof = encryptedInput.inputProof;
tx = await myContract
.connect(signers.alice)
[
"exampleFunction(bytes32,bytes32,bytes32,bytes)"
](signers.bob.address, externalEboolParam1, externalEuint64Param2, externalEuint8Param3, inputProof);
await tx.wait();
Developers are free to design the function parameters in any order. There is no required correspondence between the order in which encrypted inputs are constructed in TypeScript and the order of arguments in the Solidity function.
Smart contracts process encrypted inputs by verifying them against the associated zero-knowledge proof. This is done using the FHE.asEuintXX, FHE.asEbool, or FHE.asEaddress functions, which validate the input and convert it into the appropriate encrypted type.
This example demonstrates a function that performs multiple encrypted operations, such as updating a user's encrypted balance and toggling an encrypted boolean flag:
function myExample(externalEuint64 encryptedAmount, externalEbool encryptedToggle, bytes calldata inputProof) public {
// Validate and convert the encrypted inputs
euint64 amount = FHE.fromExternal(encryptedAmount, inputProof);
ebool toggleFlag = FHE.fromExternal(encryptedToggle, inputProof);
// Update the user's encrypted balance
balances[msg.sender] = FHE.add(balances[msg.sender], amount);
// Toggle the user's encrypted flag
userFlags[msg.sender] = FHE.not(toggleFlag);
// FHE permissions and function logic here
...
}
// Function to retrieve a user's encrypted balance
function getEncryptedBalance() public view returns (euint64) {
return balances[msg.sender];
}
// Function to retrieve a user's encrypted flag
function getEncryptedFlag() public view returns (ebool) {
return userFlags[msg.sender];
}
ConfidentialERC20.sol smart contractHere’s an example of a smart contract function that verifies an encrypted input before proceeding:
function transfer(
address to,
externalEuint64 encryptedAmount,
bytes calldata inputProof
) public {
// Verify the provided encrypted amount and convert it into an encrypted uint64
euint64 amount = FHE.fromExternal(encryptedAmount, inputProof);
// Function logic here, such as transferring funds
...
}
FHE.fromExternal function ensures that the input is a valid ciphertext with a corresponding ZKPoK.externalEbool, externalEaddress, externalEuintXX into the appropriate encrypted type (ebool, eaddress, euintXX) for further operations within the contract.Encrypted inputs and their validation form the backbone of secure and private interactions in the FHEVM. By leveraging these tools, developers can create robust, privacy-preserving smart contracts without compromising functionality or scalability.