Decrypt and reencrypt
How it's working
Validators of the blockchain do not own the blockchain's private key. Instead, the private key is owned by a Key Management Service (KMS). If the plaintext value is needed at some point, there are two ways to obtain it. Both methods are handled by a service called the Gateway.
If the plaintext is needed for some logic in a contract, the Gateway acts as an oracle service: it will listen to decryption request events and return the decrypted value through a callback function.
If the plaintext is needed by a dApp, the Gateway provides an API to reencrypt a ciphertext with the dApp's public key.
Decryption
We allow explicit decryption requests for any encrypted type. The values are decrypted with the network private key.
![](https://docs.zama.ai/~gitbook/image?url=https%3A%2F%2F4221220623-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FdjZLXIKWOxfECtlEOsO1%252Fuploads%252Fgit-blob-6658c0482a3f2f8329eb01b1938491ed402d35c5%252FasyncDecrypt.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=88438dae&sv=1)
You can read about an actual implemention in our decryption guide.
Reencrypt
Reencryption is performed on the client side by calling the gateway service using the fhevmjs library. To do this, you need to provide a view function that returns the ciphertext to be reencrypted.
The dApp retrieves the handle from the view function (e.g., balanceOf).
The dApp generates a keypair for the user and requests the user to sign the public key.
The dApp calls the gateway, providing the handle, public key, user address, contract address, and the user's signature.
The dApp decrypts the received value with the private key.
You can read our guide explaining how to use it.
Last updated