|
| 1 | +# Integrating Aligned into your Application |
| 2 | + |
| 3 | +Aligned can be integrated into your applications in a few simple steps to provide a way to verify ZK proofs generated inside your system. |
| 4 | + |
| 5 | +You can find an example of the full flow of using Aligned in your app in the [ZKQuiz example](../../examples/zkquiz). |
| 6 | + |
| 7 | +This example shows a sample app that generates an SP1 proof that a user knows the answers to a quiz and then submits the proof to Aligned for verification. Finally, it includes a smart contract that verifies that a proof was verified in Aligned and mints an NFT. |
| 8 | + |
| 9 | +## 1. Generate your ZK Proof |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +You can find examples on how to generate proofs in the [generating proofs guide](3_generating_proofs.md). |
| 14 | + |
| 15 | +Also, you can find an example of the ZKQuiz proof [program](../../examples/zkquiz/quiz/program/src/main.rs) as well as the [script](../../examples/zkquiz/quiz/script/src/main.rs) that generates it in the [ZKQuiz example directory](../../examples/zkquiz). |
| 16 | + |
| 17 | +## 2. Write your smart contract |
| 18 | + |
| 19 | +To check if a proof was verified in Aligned, you need to make a call to the `AlignedServiceManager` contract inside your smart contract. |
| 20 | + |
| 21 | +Also, you will need a way to check that the proven program is the correct one. |
| 22 | + |
| 23 | +The Aligned CLI provides a way for you to get the verification key commitment without actually generating and submitting a proof. |
| 24 | + |
| 25 | +You can do this by running the following command: |
| 26 | + |
| 27 | +```bash |
| 28 | +aligned get-commitment --input <path_to_input_file> |
| 29 | +``` |
| 30 | + |
| 31 | +The following is an example of how to call the `verifyBatchInclusionMethod` from the `AlignedServiceManager` contract in your smart contract. |
| 32 | + |
| 33 | +```solidity |
| 34 | +contract YourContract { |
| 35 | + // Your contract variables ... |
| 36 | + address public alignedServiceManager; |
| 37 | + bytes32 public elfCommitment = <elf_commitment>; |
| 38 | +
|
| 39 | + constructor(address _alignedServiceManager) { |
| 40 | + //... Your contract constructor ... |
| 41 | + alignedServiceManager = _alignedServiceManager; |
| 42 | + } |
| 43 | + |
| 44 | + // Your contract code ... |
| 45 | + |
| 46 | + function yourContractMethod( |
| 47 | + //... Your function variables, ... |
| 48 | + bytes32 proofCommitment, |
| 49 | + bytes32 pubInputCommitment, |
| 50 | + bytes32 provingSystemAuxDataCommitment, |
| 51 | + bytes20 proofGeneratorAddr, |
| 52 | + bytes32 batchMerkleRoot, |
| 53 | + bytes memory merkleProof, |
| 54 | + uint256 verificationDataBatchIndex |
| 55 | + ) { |
| 56 | + // ... Your function code |
| 57 | + |
| 58 | + require(elfCommitment == provingSystemAuxDataCommitment, "ELF does not match"); |
| 59 | + |
| 60 | + (bool callWasSuccessful, bytes memory proofIsIncluded) = alignedServiceManager.staticcall( |
| 61 | + abi.encodeWithSignature( |
| 62 | + "verifyBatchInclusion(bytes32,bytes32,bytes32,bytes20,bytes32,bytes,uint256)", |
| 63 | + proofCommitment, |
| 64 | + pubInputCommitment, |
| 65 | + provingSystemAuxDataCommitment, |
| 66 | + proofGeneratorAddr, |
| 67 | + batchMerkleRoot, |
| 68 | + merkleProof, |
| 69 | + verificationDataBatchIndex |
| 70 | + ) |
| 71 | + ); |
| 72 | +
|
| 73 | + require(callWasSuccessful, "static_call failed"); |
| 74 | + |
| 75 | + bool proofIsIncludedBool = abi.decode(proofIsIncluded, (bool)); |
| 76 | + require(proofIsIncludedBool, "proof not included in batch"); |
| 77 | + |
| 78 | + // Your function code ... |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +You can find an example of the smart contract that checks if the proof was verified in Aligned in the [Quiz Verifier Contract](../../examples/zkquiz/contracts/src/VerifierContract.sol). |
| 84 | + |
| 85 | +Note that the contract checks if the verification key commitment is the same as the program ELF commitment. |
| 86 | + |
| 87 | +```solidity |
| 88 | +require(elfCommitment == provingSystemAuxDataCommitment, "ELF does not match"); |
| 89 | +``` |
| 90 | + |
| 91 | +This contract also includes a static call to the `AlignedServiceManager` contract to check if the proof was verified in Aligned. |
| 92 | + |
| 93 | +```solidity |
| 94 | +(bool callWasSuccessfull, bytes memory proofIsIncluded) = alignedServiceManager.staticcall( |
| 95 | + abi.encodeWithSignature( |
| 96 | + "verifyBatchInclusion(bytes32,bytes32,bytes32,bytes20,bytes32,bytes,uint256)", |
| 97 | + proofCommitment, |
| 98 | + pubInputCommitment, |
| 99 | + provingSystemAuxDataCommitment, |
| 100 | + proofGeneratorAddr, |
| 101 | + batchMerkleRoot, |
| 102 | + merkleProof, |
| 103 | + verificationDataBatchIndex |
| 104 | + ) |
| 105 | +); |
| 106 | +
|
| 107 | +require(callWasSuccessfull, "static_call failed"); |
| 108 | +
|
| 109 | +bool proofIsIncludedBool = abi.decode(proofIsIncluded, (bool)); |
| 110 | +require(proofIsIncludedBool, "proof not included in batch"); |
| 111 | +``` |
| 112 | + |
| 113 | +## 3. Submit and verify the proof to Aligned |
| 114 | + |
| 115 | +The proof submission and verification can be done either with the SDK or by using the Aligned CLI. |
| 116 | + |
| 117 | +#### Using the SDK |
| 118 | + |
| 119 | +To submit a proof using the SDK, you can use the `submit` function, and then you can use the `verify_proof_onchain` function to check if the proof was correctly verified in Aligned. |
| 120 | + |
| 121 | +The following code is an example of how to submit a proof using the SDK: |
| 122 | + |
| 123 | +```rust |
| 124 | +use aligned_sdk::sdk::submit; |
| 125 | +use aligned_sdk::types::{ProvingSystemId, VerificationData}; |
| 126 | +use ethers::prelude::*; |
| 127 | + |
| 128 | +const BATCHER_URL: &str = "wss://batcher.alignedlayer.com"; |
| 129 | +const ELF: &[u8] = include_bytes!("../../program/elf/riscv32im-succinct-zkvm-elf"); |
| 130 | + |
| 131 | +async fn submit_proof_to_aligned( |
| 132 | + proof: Vec<u8>, |
| 133 | + wallet: Wallet<SigningKey> |
| 134 | +) -> Result<AlignedVerificationData, anyhow::Error> { |
| 135 | + let verification_data = VerificationData { |
| 136 | + proving_system: ProvingSystemId::SP1, |
| 137 | + proof, |
| 138 | + proof_generator_addr: wallet.address(), |
| 139 | + vm_program_code: Some(ELF.to_vec()), |
| 140 | + verification_key: None, |
| 141 | + pub_input: None, |
| 142 | + }; |
| 143 | + |
| 144 | + submit(BATCHER_URL, &verification_data, wallet).await |
| 145 | + .map_err(|e| anyhow::anyhow!("Failed to submit proof: {:?}", e)) |
| 146 | +} |
| 147 | + |
| 148 | +#[tokio::main] |
| 149 | +async fn main() { |
| 150 | + let wallet = // Initialize wallet |
| 151 | + let proof = // Generate or obtain proof |
| 152 | + |
| 153 | + match submit_proof_to_aligned(proof, wallet).await { |
| 154 | + Ok(aligned_verification_data) => println!("Proof submitted successfully"), |
| 155 | + Err(err) => println!("Error: {:?}", err), |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +The following code is an example of how to verify the proof was correctly verified in Aligned using the SDK: |
| 161 | + |
| 162 | +```rust |
| 163 | +use aligned_sdk::sdk::verify_proof_onchain; |
| 164 | +use aligned_sdk::types::{AlignedVerificationData, Chain}; |
| 165 | +use ethers::prelude::*; |
| 166 | +use tokio::time::{sleep, Duration}; |
| 167 | + |
| 168 | +async fn wait_for_proof_verification( |
| 169 | + aligned_verification_data: AlignedVerificationData, |
| 170 | + rpc_url: String, |
| 171 | +) -> Result<(), anyhow::Error> { |
| 172 | + for _ in 0..10 { |
| 173 | + if verify_proof_onchain(aligned_verification_data.clone(), Chain::Holesky, rpc_url.as_str()).await.is_ok_and(|r| r) { |
| 174 | + println!("Proof verified successfully."); |
| 175 | + return Ok(()); |
| 176 | + } |
| 177 | + println!("Proof not verified yet. Waiting 10 seconds before checking again..."); |
| 178 | + sleep(Duration::from_secs(10)).await; |
| 179 | + } |
| 180 | + anyhow::bail!("Proof verification failed") |
| 181 | +} |
| 182 | + |
| 183 | +#[tokio::main] |
| 184 | +async fn main() { |
| 185 | + let aligned_verification_data = // Obtain aligned verification data |
| 186 | + let rpc_url = "https://ethereum-holesky-rpc.publicnode.com".to_string(); |
| 187 | + |
| 188 | + match wait_for_proof_verification(aligned_verification_data, rpc_url).await { |
| 189 | + Ok(_) => println!("Proof verified"), |
| 190 | + Err(err) => println!("Error: {:?}", err), |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +You can find an example of the proof submission and verification in the [ZKQuiz Program](../../examples/zkquiz/quiz/script/src/main.rs). |
| 196 | + |
| 197 | +This example generates a proof, instantiates a wallet to submit the proof, and then submits the proof to Aligned for verification. It then waits for the proof to be verified in Aligned. |
| 198 | + |
| 199 | +#### Using the CLI |
| 200 | + |
| 201 | +You can find examples of how to submit a proof using the CLI in the [submitting proofs guide](0_submitting_proofs.md). |
0 commit comments