Skip to main content

Decryption

After an FHE program executes on encrypted inputs, the plaintext result can be retrieved through threshold decryption. This section covers requesting decryption and receiving results via callback functions in Solidity smart contracts.

How the callback pattern works

When you request decryption, you specify which function in your contract should receive the result. SPF processes the decryption through the threshold decryption committee, then calls your specified function with the plaintext value. This allows your contract to store the decrypted result or trigger further logic based on the value.

Requesting threshold decryption

To request threshold decryption of a ciphertext, use the requestDecryptionAsContract() function:

function requestDecryptionAsContract(bytes4 functionSelector, bytes32 ciphertextId)

This submits a decryption request to the SPF threshold committee. When decryption completes, SPF calls the specified function in your contract with the plaintext result. This enables seamless integration of FHE decryption into your smart contract logic.

Parameters:

  • functionSelector: bytes4 - The function the SPF calls with the decrypted result. Use functionName.selector to get the bytes4 selector.
  • ciphertextId: bytes32 - The ciphertext identifier to decrypt (obtained from getOutputHandle() and converted with passToDecryption()).

Implementing the callback function

The callback function receives the decrypted plaintext value from SPF. The function signature must match (bytes32, uint256):

function decryptionCallback(bytes32 identifier, uint256 result) onlyThresholdDecryption {
// Process the decrypted result
plaintext_field_in_contract = result;
}

Important: Use the onlyThresholdDecryption modifier to ensure only SPF can call this function.

To pass this example function to requestDecryptionAsContract(), use decryptionCallback.selector.

Converting output parameters

Program outputs from getOutputHandle() return SpfParameter type. To convert to the bytes32 ciphertext identifier required by requestDecryptionAsContract(), use the passToDecryption() helper:

function passToDecryption(SpfParameter memory param) returns (bytes32)

This converts the parameter format to the ciphertext identifier format expected by the decryption function.