Skip to main content

Encrypting Input Data

This step secures your data before sending it to the smart contract. Remember - all data sent to a smart contract on a blockchain is inherently public, which means that anyone can see it. However, Fhenix operates differently. To maintain user confidentiality and protect sensitive input data, Fhenix utilizes Cofhejs to provide built-in encryption methods that you must apply before sending any data to an FHE-enabled contract. Cofhejs provides an easy-to-use function to encrypt your inputs before sending them to the Fhenix Co-Processor.

Prerequisites

Before encrypting data, ensure you have:
Encryption in Fhenix is done using the global chain key. This key is loaded when you create a Cofhejs client automatically.
When you perform encryption, you specify the type of euint (Encrypted Integer) you want to create. This should match the expected type in the Solidity contract you are working with.

Setup

1

Initialize Cofhejs

First, initialize the library with your provider and signer:
await cofhejs.initializeWithEthers({
    ethersProvider: provider,
    ethersSigner: wallet,
    environment: "LOCAL",
});

Encryption Examples

Single Value Encryption

You can encrypt individual values by specifying the type:
import { cofhejs, Encryptable } from "cofhejs/node";

const logState = (state) => {
    console.log(`Log Encrypt State :: ${state}`);
};

let result: [CofheInBool] = await cofhejs.encrypt([Encryptable.bool(true)], logState);

Multiple Values Encryption

You can encrypt multiple values at once using the nested form:
import { cofhejs, Encryptable } from "cofhejs/node";

const logState = (state) => {
    console.log(`Log Encrypt State :: ${state}`);
};

let result = await cofhejs.encrypt([
    Encryptable.bool(true),
    Encryptable.uint8(10),
    Encryptable.uint16(10),
    Encryptable.uint32(10),
    Encryptable.uint64(10),
    Encryptable.uint128(10)
    Encryptable.address('0x1234567890123456789012345678901234567890'),
], logState);

Return Types

The returned types from the encrypt function will be an array of the type CoFheInBool, CoFheInUint8, CoFheInUint16, CoFheInUint32 (or 64/128) or CoFheInAddress depending on the type you specified. These encrypted types have the following structure:
export type CoFheInItem = {
  ctHash: bigint;
  securityZone: number;
  utype: FheTypes;
  signature: string;
};

export type CoFheInUint8 extends CoFheInItem {
  utype: FheTypes.Uint8;
}
These types exist in order to enable type checking when interacting with Solidity contracts, and to make it easier to work with encrypted data.

setState Callback

The setState function is used to monitor the state of the encryption process. Since the process is asynchronous, you can use this function to get the state of the encryption process for better UI experience.
const logState = (state) => {
    console.log(`Log Encrypt State :: ${state}`);
};
The available states are:
  • Extract - Getting all the data ready for encryption (values to encrypt, chain information, etc.).
  • Pack - Preparing the data for the encryption process.
  • Prove - Signing the data.
  • Verify - Verifies the user’s input, ensuring that it is safe to use (read more about this here).
  • Replace - Preparing the result and replacing the old values with encrypted ones.
  • Done - Process is finished.
By encrypting user data before sending it to a contract, Fhenix ensures that data remains private throughout its lifecycle in the blockchain environment.