Re-encryption
The decryption of the confidential state can directly happen on-chain using TFHE.decrypt()
(see Decryption & Re-encryption), in which case the plain text is viewable by the public. In some cases, this might not be desirable, and we might want only a small number of users to be able to see the decrypted value. For example, in an EncryptedERC20 application, users should only be able to decrypt their own balance.
This can be done off-chain using reencryption. The authorized end user will create a local keypair, use TFHE.reencrypt()
on the smart contract side and instance.decrypt()
on the client side.
The publicKey
and privateKey
pair is generated using generatePublicKey()
from fhevmjs and the re-encrypted response can be decrypted using the instance.decrypt()
function on the client side.
Here's the fhevmjs code for a user to view their own balance for a private ERC-20 contract :
Please note that the balanceOf
function on the private ERC-20 contract (see Confidential ERC-20) is gated with an EIP 712 signature requirement. This is to prove that the users calling the balanceOf
function to view their balance is indeed the users themselves.
Last updated