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.
Example Usage
To initialize and use the FHEVM instance:
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:
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:
Creating and Using Encrypted Inputs
For encrypting inputs in a browser or React environment, follow these steps:
Use
createEncryptedInput()
to prepare an encrypted input associated with the contract.Use
input.add64()
to convert a plaintext value to ciphertext foreuint64.
Here’s an example of how to use encrypted inputs in a transaction:
For a React starter using fhevmjs
, check out the FHEVM.js React Example.
Available Methods for Encrypting Plaintext to Ciphertext
eInput Type
Method
Parameter Type
ebool
addBool
boolean
euint4
add4
number
or bigint
euint8
add8
number
or bigint
euint16
add16
number
or bigint
euint32
add32
number
or bigint
euint64
add64
number
or bigint
ebytes256
addBytes256
Uint8Array
eaddress
addAddress
string
Re-encryption
For re-encryption, refer to the guide below.
Last updated