|
| 1 | +# Using Aligned on your app |
| 2 | + |
| 3 | +You can find an example of the full flow of using Aligned on your app |
| 4 | +in the [ZKQuiz example](../../examples/zkquiz). |
| 5 | + |
| 6 | +This example shows a sample app that generates a SP1 proof |
| 7 | +that a user knows the answers to a quiz, then submits the proof |
| 8 | +to Aligned for verification. |
| 9 | +Finally, it includes a smart contract that verifies that a proof |
| 10 | +was verified in Aligned and mints an NFT. |
| 11 | + |
| 12 | +## Steps |
| 13 | + |
| 14 | +### Step 1 - Write your ZK Proof |
| 15 | + |
| 16 | +Write your ZK proof using any of the proving systems supported by Aligned. |
| 17 | +For this example, we use the SP1 proving system. |
| 18 | + |
| 19 | +You can find the example of the quiz proof [program](../../examples/zkquiz/quiz/program/src/main.rs) |
| 20 | +as well as the [script](../../examples/zkquiz/quiz/script/src/main.rs) |
| 21 | +that generates it in the [ZKQuiz example](../../examples/zkquiz) folder. |
| 22 | + |
| 23 | +### Step 2 - Write your smart contract |
| 24 | + |
| 25 | +Write your smart contract that verifies the proof was verified in Aligned. |
| 26 | +For this, you will need a way to check that the proven program is your own. |
| 27 | + |
| 28 | +The aligned cli provides a way for you to get the verification key commitment |
| 29 | +without actually generating and submitting a proof. |
| 30 | + |
| 31 | +You can do this by running the following command: |
| 32 | + |
| 33 | +```bash |
| 34 | +aligned get-vk-commitment --input <path_to_input_file> |
| 35 | +``` |
| 36 | + |
| 37 | +For SP1 you would use the elf of the program as the input file. |
| 38 | + |
| 39 | +You can find the example of the smart contract that verifies the proof was verified in Aligned |
| 40 | +in the [Quiz Verifier Contract](../../examples/zkquiz/contracts/src/VerifierContract.sol). |
| 41 | + |
| 42 | +Note that the contract has and checks that the verification key commitment is the same as the program elf. |
| 43 | +```solidity |
| 44 | +require(elfCommitment == provingSystemAuxDataCommitment, "ELF does not match"); |
| 45 | +``` |
| 46 | + |
| 47 | +This contracts also includes a static call to the Aligned ServiceManager contract |
| 48 | +to check that the proof was verified in Aligned. For a full version of this, you can view, use as an example guide, or inherit the [Verify Batch Inclusion Caller](../../examples/verify/src/VerifyBatchInclusionCaller.sol) smart contract. |
| 49 | + |
| 50 | +```solidity |
| 51 | +(bool callWasSuccessfull, bytes memory proofIsIncluded) = alignedServiceManager.staticcall( |
| 52 | + abi.encodeWithSignature( |
| 53 | + "verifyBatchInclusion(bytes32,bytes32,bytes32,bytes20,bytes32,bytes,uint256)", |
| 54 | + proofCommitment, |
| 55 | + pubInputCommitment, |
| 56 | + provingSystemAuxDataCommitment, |
| 57 | + proofGeneratorAddr, |
| 58 | + batchMerkleRoot, |
| 59 | + merkleProof, |
| 60 | + verificationDataBatchIndex |
| 61 | + ) |
| 62 | +); |
| 63 | +
|
| 64 | +require(callWasSuccessfull, "static_call failed"); |
| 65 | +
|
| 66 | +bool proofIsIncludedBool = abi.decode(proofIsIncluded, (bool)); |
| 67 | +require(proofIsIncludedBool, "proof not included in batch"); |
| 68 | +``` |
| 69 | + |
| 70 | +### Step 3 - Have your app generate the proof and submit it to Aligned |
| 71 | + |
| 72 | +First, generate the proof. |
| 73 | +For SP1 this means having the [script](../../examples/zkquiz/quiz/script/src/main.rs) |
| 74 | +generate the proof. |
| 75 | + |
| 76 | +Then, submit the proof to Aligned for verification. |
| 77 | +This can be done either with the SDK or by using the Aligned CLI. |
| 78 | +You can find examples of how to submit a proof using the cli |
| 79 | +in the [README_SEND_PROOFS.md](../../README_SEND_PROOFS.md)). |
| 80 | + |
| 81 | +The call ZK Quiz uses is: |
| 82 | +```bash |
| 83 | +aligned submit \ |
| 84 | + --proving_system SP1 \ |
| 85 | + --proof quiz/script/proof-with-io.json \ |
| 86 | + --vm_program quiz/program/elf/riscv32im-succinct-zkvm-elf \ |
| 87 | + --proof_generator_addr <user_address> \ |
| 88 | + --conn wss://batcher.alignedlayer.com |
| 89 | +``` |
| 90 | + |
| 91 | +### Step 4 - Verify the proof was verified in Aligned |
| 92 | + |
| 93 | +Once the proof is verified in Aligned, |
| 94 | +you can verify that it was verified from your smart contract. |
| 95 | + |
| 96 | +The full example of this flow can be found on the [ZKQuiz Verifier Contract](../../examples/zkquiz/contracts/src/VerifierContract.sol). |
| 97 | + |
| 98 | +An example [python script](../../examples/zkquiz/encode_verification_data.py) can be found |
| 99 | +to encode the call data from the json output of the Aligned cli. |
| 100 | +This is then used to call the smart contract using cast: |
| 101 | + |
| 102 | +```bash |
| 103 | +cast send \ |
| 104 | + --rpc-url <rpc_url> \ |
| 105 | + --private-key <private_key> \ |
| 106 | + <contract_address> \ |
| 107 | + <encoded_calldata> |
| 108 | +``` |
| 109 | + |
| 110 | +This call can be done from any library that can interact with the Ethereum blockchain. |
| 111 | + |
0 commit comments