Build your first Aligned Application
In this guide you will learn how to build applications on top of Aligned. It provides a few simple steps to help you verify ZK proofs generated within your system.
First we will show you an example of a trivia application, called ZkQuiz. We'll show you the different components and how they interact with each other to be able to submit the proof to aligned and verify that was correctly included in a batch.
ZkQuiz
ZkQuiz is an application that leverages Aligned's ZK verification infrastructure to run a small trivia. The proof allows any party to check that the quiz was answered right or wrong. If answered correctly, the user receives an NFT.
Received NFTs from ZkQuiz do not have any value. It is just a test application
The process is as follows:
The user runs ZKQuiz and answers the questions.
ZKQuiz generates a ZK Proof of correct answers.
The proof is posted on Aligned.
Upon verification, ZKQuiz mints an NFT via a Smart Contract.
The NFT is only granted if the user's answers correctly. Incorrect answers or tampering with the ZKQuiz code will result in proof generation failure or mismatched checksums, preventing NFT minting.
Next, we will see how to execute ZKQuiz to get your own ZKQuiz NFT!
Requirements
ELF commitment was generated with Rust v1.80.1 and SP1 v3.0.0.
Usage
1. Clone the repository
2. Create a Keystore
You need a keystore to pay for the proof verification, you can use cast to create a local keystore. If you already have one, you can skip this step.
Then you can import your created keystore using:
The keystores are saved in ~/.foundry/keystores
. You can find more information about keystores in the cast documentation.
Then you need to get some funds to pay for gas and proof verification. You can do this by using one of the following faucets:
3. Answer Quiz
To answer quiz questions run:
This will:
Ask quiz questions
Generate ZK proof
Pay & submit proof to aligned for verification
Wait for proof to be verified in aligned
Claim NFT if proof is verified
Deep dive
The ZkQuiz source code is available here.
ZkQuiz has three main components:
App/script
Program
Verifier contract
The user interacts with ZkQuiz App to solve a trivia challenge answering questions. Then, the App generates a Zk Proof with the Program generated using SP1.
The ZkQuiz Program is built using SP1 following the quickstart guide. For your projects, you can user any of the prooving systems supported by Aligned.
Once the proof is generated, the App sends the proof to Aligned, and once it is verified, the App calls to the ZkQuiz Verifier Contract to check the proof verification and send an NFT to the user is the proof was verified in Aligned.
Now, lets build ZkQuiz from scratch.
Program
First you need to write the code you want to prove; in this case it looks like this:
The program takes the user answers as inputs and checks that the hash of the inputs matches with the expected output. This is the program that will be compiled generating a binary file that will be ran by the zkVm and used later in the application side. In our case this file is already generated and is located on /quiz/program/elf/riscv32im-succinct-zkvm-elf
.
Verifier Contract
To check if a proof was verified in Aligned, you can create your own smart contract in order to make a call to the AlignedServiceManager
contract.
ZkQuiz uses a Smart Contract to check if aligned verified the proof and gives an NFT to the user.
It is not mandatory to create an Smart Contract. You can make off-chain apps that interact with the Aligned contract directly.
Program Identifier Validation
The contract first checks that the commitment of the program matches with the one that we expect. In our zkquiz example, we get the following elf commitment:
You can generate the expected commitment without actually generating and submitting a proof using the Aligned CLI tool running:
where the path_to_input_file
is the path to the elf
file generated with the program compilation and the proving_system_id
the name of the proving system used for compilation, in this case SP1
.
Then, the contract validates if the provided commitment of the program identifier matches the expected one.
The contract makes a call to the AlignedServiceManager
contract to check if the proof was verified in Aligned.
Finally, if the proof was verified, the contract sends a NFT to the user
App
The first part of the app takes the answers of the user via CLI. Once the user answer the questions, we prepare them and initiate the prover, as follows:
Now we can send the generated proof to Aligned using the SDK.
Finally, if the proof was sent to Aligned correctly, we can interact with our verifier Smart Contract to verify that the proof was correctly posted in aligned and claim the NFT.
You can find the full code of the proof submission and verification in the ZKQuiz App.
Last updated