Encryption & Re-encryption

Encryption:

In order to store an encrypted values on-chain, a plain text can be converted into a cipher text on the client side using fhevmJs library.

NodeJs Setup

Creating an FHEVM Instance

To create a new FHEVM instance in Node.js, use the following code.

const { createInstance } = require("fhevmjs");

let fhevmInstance = null;

const createFhevmInstance = async () => {
  if (!fhevmInstance) {
    fhevmInstance = await createInstance({
      chainId: 21097,
      networkUrl: "https://validator.rivest.inco.org/",
      gatewayUrl: "https://gateway.rivest.inco.org/",
      aclAddress: "0x2Fb4341027eb1d2aD8B5D9708187df8633cAFA92",
    });
  }
  return fhevmInstance;
};

const getFhevmInstance = async () => {
  if (!fhevmInstance) {
    fhevmInstance = await createFhevmInstance();
  }
  return fhevmInstance;
};

Example Usage

To initialize and use the FHEVM instance:

const main = async () => {
  try {
    const instance = await getFhevmInstance();
    console.log("FHEVM Instance created:", instance);
  } catch (error) {
    console.error("Error initializing FHEVM:", error);
  }
};

main();

Encrypting Plain Text to Cipher Text

Once your instance is created, use input.add64() to convert a plaintext value to ciphertext for euint64. Here’s how to encrypt an input amount:

const signer = await provider.getSigner();
const address = await signer.getAddress();
const contractEncryptedERC20 = new Contract(CONTRACT_ADDRESS, erc20ABI, signer);
const input = instance.createEncryptedInput(await contractEncryptedERC20.getAddress(), signer);

// Encrypt input value
input.add64("YOUR_INPUT_AMOUNT_HERE");
const encryptedTransferAmount = input.encrypt();

Browser/React Setup

To use fhevmjs in the browser, load WebAssembly (WASM) with initFhevm. This step enables the cryptographic computations required by FHEVM.

Loading WASM for Browser Usage

Here’s how to initialize fhevmjs in a React environment:

import { createInstance, initFhevm } from "fhevmjs";

let fhevmInstance = null;

export const createFhevmInstance = async () => {
  if (!fhevmInstance) {
    await initFhevm(); // Load WASM for cryptographic computations
    fhevmInstance = await createInstance({
      chainId: 21097,
      networkUrl: "https://validator.rivest.inco.org/",
      gatewayUrl: "https://gateway.rivest.inco.org/",
      aclAddress: "0x2Fb4341027eb1d2aD8B5D9708187df8633cAFA92",
    });
  }
  return fhevmInstance;
};

export const getFhevmInstance = async () => {
  if (!fhevmInstance) {
    fhevmInstance = await createFhevmInstance();
  }
  return fhevmInstance;
};

Creating and Using Encrypted Inputs

For encrypting inputs in a browser or React environment, follow these steps:

  1. Use createEncryptedInput() to prepare an encrypted input associated with the contract.

  2. Use input.add64() to convert a plaintext value to ciphertext for euint64.

Here’s an example of how to use encrypted inputs in a transaction:

const signer = await provider.getSigner();
const contractEncryptedERC20 = new Contract(CONTRACT_ADDRESS, erc20ABI, signer);
const input = await fhevmInstance.createEncryptedInput(await contractEncryptedERC20.getAddress(), signer);

// Add a 64-bit encrypted input value
input.add64("YOUR_INPUT_AMOUNT_HERE");
const encryptedTransferAmount = input.encrypt();

For a React starter using fhevmjs, check out the FHEVM.js React Example.

Available Methods for Encrypting Plaintext to Ciphertext

Re-encryption

For re-encryption, refer to the guide below.

Last updated