Re-encryption
Last updated
Last updated
This document explains how to perform re-encryption. Re-encryption is required when you want a user to access their private data without it being exposed to the blockchain.
Re-encryption in fhEVM enables the secure sharing or reuse of encrypted data under a new public key without exposing the plaintext. This feature is essential for scenarios where encrypted data must be transferred between contracts, dApps, or users while maintaining its confidentiality.
Before implementing re-encryption, ensure you are familiar with the foundational concepts of encryption, re-encryption and computation. Refer to .
Re-encryption is particularly useful for allowing individual users to securely access and decrypt their private data, such as balances or counters, while maintaining data confidentiality.
The re-encryption process involves retrieving ciphertext from the blockchain and performing re-encryption on the client-side. In other words we take the data that has been encrypted by the KMS, decrypt it and encrypt it with the users private key, so only he can access the information.
This ensures that the data remains encrypted under the blockchain’s FHE key but can be securely shared with a user by re-encrypting it under the user’s NaCl public key.
Re-encryption is facilitated by the Gateway and the Key Management System (KMS). The workflow consists of the following:
Retrieving the ciphertext from the blockchain using a contract’s view function.
Re-encrypting the ciphertext client-side with the user’s public key, ensuring only the user can decrypt it.
To retrieve the ciphertext that needs to be re-encrypted, you can implement a view function in your smart contract. Below is an example implementation:
Here, balanceOf
allows retrieval of the user’s encrypted balance stored on the blockchain.
This code retrieves the user’s encrypted balance, re-encrypts it with their public key, and decrypts it on the client-side using their private key.
instance.generateKeypair()
: Generates a public-private keypair for the user.
instance.createEIP712(publicKey, CONTRACT_ADDRESS)
: Creates an EIP712 object for signing the user’s public key.
instance.reencrypt()
: Facilitates the re-encryption process by contacting the Gateway and decrypting the data locally with the private key.
Here’s an enhanced Encrypted Counter example where each user maintains their own encrypted counter. Re-encryption is used to securely share counter values with individual users.
Here’s a sample test to verify re-encryption functionality:
setupReencryption():
Prepares the re-encryption process by generating keys and a signature for the user.
instance.reencrypt():
Facilitates re-encryption and local decryption of the data for testing purposes.
Validation: Confirms that the decrypted counter matches the expected value.
Re-encryption is performed client-side using the fhevmjs
library. to learn how to include fhevmjs
in your project. Below is an example of how to implement reencryption in a dApp: