Validating public input
In some applications, it is crucial to ensure that a third party has performed a computation correctly and to make use of the result of that computation. To achieve this, the third party must first interact with Aligned and obtain the AlignedVerificationData
, a receipt indicating that the proof of the computation was verified correctly. The application should then receive both the AlignedVerificationData
and the result of the computation. After confirming that the proof was verified by Aligned, it must check that the posted result matches the one committed in the AlignedVerificationData
.
This guide demonstrates how to validate a Risc0 proof using the Aligned SDK. The Risc0 program in this example is a Fibonacci sequence calculator. It generates a public input that corresponds to the last two Fibonacci numbers of the sequence, taken modulo 7919. Our goal is to validate, within a smart contract, that the public input commitments match these two numbers.
In this case, the Fibonacci number to be calculated is 500 and the last two numbers of the sequence modulo 7919 are 1268 and 1926.
This guide assumes you are in the examples/validating-public-input
directory.
Generate your ZK Proof
[!IMPORTANT] To generate the proof ensure you have docker installed and the docker daemon running. This is necessary to ensure deterministic builds of the binary we want to generate a proof of. If not used, builds may differ depending on the system you are running on. To know more about this, check this link from RiscZero docs.
To submit proofs to Aligned and get them verified, first you need to generate those proofs. Every proving system has its own way of generating proofs.
You can find examples on how to generate proofs in the generating proofs guide.
To generate the proof needed to try this example, run make generate_risc_zero_fibonacci_proof
.
Once finished, you will see the program id, the two last fibonacci numbers of the sequence and the result of the verification like so:
Write your smart contract
To check if a proof was verified in Aligned, you need to make a call to the AlignedServiceManager
contract inside your smart contract.
The following is an example of how to validate the public input of the Risc0 proof in your smart contract.
Explanation
Program Identifier Validation: The contract first validates if the provided commitment of the program identifier matches the expected one.
Public Input Validation: The contract then checks that the commitment of the public inputs matches the keccak 256 hash of the actual public inputs.
Static Call to AlignedServiceManager: The contract makes a static call to the
AlignedServiceManager
contract to check if the proof was verified in Aligned. It then extracts the last two Fibonacci numbers from the pubInputBytes and emits an event.
Bytes to two
uint32
conversion: A helper function to convert a byte array into twouint32
numbers, used for extracting the last two Fibonacci numbers from thepubInputBytes
.
To deploy the contract, first you will need to set up the .env
file in the contracts folder with the following variables:
Then, run make deploy_fibonacci_validator
.
To call the function in the contract with cast run:
Submit and verify the proof to Aligned
The proof submission and verification can be done either with the SDK or by using the Aligned CLI.
To submit the proof generated in this example, run make submit_fibonacci_proof
. This will output the AlignedVerificationData
needed to send to the verifyBatchInclusion
method of the contract in the batch_inclusion_data
directory inside aligned-integration
.
For more details on submitting proofs, refer to the submitting proofs guide.
Last updated