Skip to content

Commit bdda0d2

Browse files
uri-99taturosati
andauthored
fix: apply zkquiz fixes from v052 to v060 (#946)
Co-authored-by: taturosati <[email protected]>
1 parent dd52e7c commit bdda0d2

File tree

11 files changed

+161
-90
lines changed

11 files changed

+161
-90
lines changed

docs/guides/2_integrating_aligned_into_your_application.md

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ The proof submission and verification can be done either with the SDK or by usin
116116

117117
#### Using the SDK
118118

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-
119+
To submit a proof using the SDK, you can use the `submit_and_wait_verification` function.
120+
This function submits the proof to aligned and waits for it to be verified in Aligned.
121+
Alternatively you can call `submit` if you dont want to wait for proof verification.
121122
The following code is an example of how to submit a proof using the SDK:
122123

123124
```rust
124-
use aligned_sdk::sdk::{submit, get_next_nonce};
125+
use aligned_sdk::sdk::{submit_and_wait_verification, get_next_nonce};
125126
use aligned_sdk::types::{ProvingSystemId, VerificationData};
126127
use ethers::prelude::*;
127128

@@ -147,13 +148,33 @@ async fn submit_proof_to_aligned(
147148
.await
148149
.map_err(|e| anyhow::anyhow!("Failed to get next nonce: {:?}", e))?;
149150

150-
submit(BATCHER_URL, &verification_data, wallet, nonce).await
151-
.map_err(|e| anyhow::anyhow!("Failed to submit proof: {:?}", e))
151+
match submit_and_wait_verification(
152+
BATCHER_URL,
153+
&rpc_url,
154+
Chain::Holesky,
155+
&verification_data,
156+
wallet.clone(),
157+
nonce,
158+
BATCHER_PAYMENTS_ADDRESS
159+
)
160+
161+
submit_and_wait_verification(
162+
BATCHER_URL,
163+
RPC_URL,
164+
Chain::Holesky,
165+
&verification_data,
166+
wallet,
167+
nonce,
168+
BATCHER_CONTRACT_ADDRESS
169+
).await.map_err(|e| anyhow::anyhow!("Failed to submit proof: {:?}", e))
152170
}
153171

154172
#[tokio::main]
155173
async fn main() {
156174
let wallet = // Initialize wallet
175+
176+
let wallet = wallet.with_chain_id(17000u64)
177+
157178
let proof = // Generate or obtain proof
158179

159180
match submit_proof_to_aligned(proof, wallet).await {
@@ -163,41 +184,6 @@ async fn main() {
163184
}
164185
```
165186

166-
The following code is an example of how to verify the proof was correctly verified in Aligned using the SDK:
167-
168-
```rust
169-
use aligned_sdk::sdk::verify_proof_onchain;
170-
use aligned_sdk::types::{AlignedVerificationData, Chain};
171-
use ethers::prelude::*;
172-
use tokio::time::{sleep, Duration};
173-
174-
async fn wait_for_proof_verification(
175-
aligned_verification_data: AlignedVerificationData,
176-
rpc_url: String,
177-
) -> Result<(), anyhow::Error> {
178-
for _ in 0..10 {
179-
if verify_proof_onchain(aligned_verification_data.clone(), Chain::Holesky, rpc_url.as_str()).await.is_ok_and(|r| r) {
180-
println!("Proof verified successfully.");
181-
return Ok(());
182-
}
183-
println!("Proof not verified yet. Waiting 10 seconds before checking again...");
184-
sleep(Duration::from_secs(10)).await;
185-
}
186-
anyhow::bail!("Proof verification failed")
187-
}
188-
189-
#[tokio::main]
190-
async fn main() {
191-
let aligned_verification_data = // Obtain aligned verification data
192-
let rpc_url = "https://ethereum-holesky-rpc.publicnode.com".to_string();
193-
194-
match wait_for_proof_verification(aligned_verification_data, rpc_url).await {
195-
Ok(_) => println!("Proof verified"),
196-
Err(err) => println!("Error: {:?}", err),
197-
}
198-
}
199-
```
200-
201187
You can find an example of the proof submission and verification in the [ZKQuiz Program](../../examples/zkquiz/quiz/script/src/main.rs).
202188

203189
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.

examples/zkquiz/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
deploy_verifier:
22
@. ./contracts/.env && . ./contracts/deploy.sh
33

4-
CONTRACT_ADDRESS=0x8dB9e6f1393c3486F30181d606312ec632189621
4+
CONTRACT_ADDRESS=0xA828f1463074d26FB761c662F80194f5dB2d31D0
55
RPC_URL=https://ethereum-holesky-rpc.publicnode.com
66
VERIFICATION_DATA=./aligned_verification_data/0a1fab5df88a71e48633cbdeedc8d1a234b790d15a8a2fd04cd6a03c1e05b5ef_212.json
77

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
RPC_URL=<rpc_url>
22
PRIVATE_KEY=<private_key>
33
ALIGNED_SERVICE_MANAGER_ADDRESS=<aligned_service_manager_addr># 0x58F280BeBE9B34c9939C3C39e0890C81f163B623 for Holesky
4+
BATCHER_PAYMENT_SERVICE_ADDRESS=<batcher_payment_service_addr># 0x815aeCA64a974297942D2Bbf034ABEe22a38A003
45
ETHERSCAN_API_KEY=<etherscan_api_key>

examples/zkquiz/contracts/deploy.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ if [ -z "$ALIGNED_SERVICE_MANAGER_ADDRESS" ]; then
1111
exit 1
1212
fi
1313

14+
if [ -z "$BATCHER_PAYMENT_SERVICE_ADDRESS" ]; then
15+
echo "BATCHER_PAYMENT_SERVICE_ADDRESS is not set. Please set it in .env"
16+
exit 1
17+
fi
18+
1419
if [ -z "$RPC_URL" ]; then
1520
echo "RPC_URL is not set. Please set it in .env"
1621
exit 1
@@ -24,8 +29,8 @@ fi
2429
forge install
2530

2631
forge script script/Deployer.s.sol \
27-
"$ALIGNED_SERVICE_MANAGER_ADDRESS" \
32+
"$ALIGNED_SERVICE_MANAGER_ADDRESS" "$BATCHER_PAYMENT_SERVICE_ADDRESS" \
2833
--rpc-url "$RPC_URL" \
2934
--private-key "$PRIVATE_KEY" \
3035
--broadcast \
31-
--sig "run(address _alignedServiceManager)"
36+
--sig "run(address _alignedServiceManager, address _paymentService)"

examples/zkquiz/contracts/script/Deployer.s.sol

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ import {VerifierContract} from "../src/VerifierContract.sol";
77
contract CounterScript is Script {
88
function setUp() public {}
99

10-
function run(address _targetContract) external returns (address) {
10+
function run(
11+
address _alignedServiceManager,
12+
address _paymentService
13+
) external returns (address) {
1114
vm.startBroadcast();
1215

13-
VerifierContract verifyBatchInclusionCaller = new VerifierContract(_targetContract);
16+
VerifierContract verifyBatchInclusionCaller = new VerifierContract(
17+
_alignedServiceManager,
18+
_paymentService
19+
);
1420

1521
vm.stopBroadcast();
1622

examples/zkquiz/contracts/src/VerifierContract.sol

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ contract VerifierContract is ERC721URIStorage {
88
uint256 private _nextTokenId;
99

1010
address public alignedServiceManager;
11+
address public paymentServiceAddr;
1112

12-
bytes32 public elfCommitment = 0x35dd40ab04e180712996495caec915b8a7c488433acbb50c4d8d912cb55bf1f1;
13+
bytes32 public elfCommitment =
14+
0x35dd40ab04e180712996495caec915b8a7c488433acbb50c4d8d912cb55bf1f1;
1315

1416
// map to check if proof has already been submitted
1517
mapping(bytes32 => bool) public mintedProofs;
1618

17-
constructor(address _alignedServiceManager) ERC721("Aligned Layer ZK Quiz", "AZKQ") {
19+
constructor(
20+
address _alignedServiceManager,
21+
address _paymentServiceAddr
22+
) ERC721("Aligned Layer ZK Quiz", "AZKQ") {
1823
alignedServiceManager = _alignedServiceManager;
24+
paymentServiceAddr = _paymentServiceAddr;
1925
}
2026

2127
function verifyBatchInclusion(
@@ -27,25 +33,41 @@ contract VerifierContract is ERC721URIStorage {
2733
bytes memory merkleProof,
2834
uint256 verificationDataBatchIndex
2935
) external returns (uint256) {
30-
require(elfCommitment == provingSystemAuxDataCommitment, "ELF does not match");
31-
require(address(proofGeneratorAddr) == msg.sender, "proofGeneratorAddr does not match");
32-
33-
bytes32 fullHash = keccak256(abi.encodePacked(proofCommitment,
34-
pubInputCommitment, provingSystemAuxDataCommitment, proofGeneratorAddr));
35-
require(!mintedProofs[fullHash], "proof already minted");
36+
require(
37+
elfCommitment == provingSystemAuxDataCommitment,
38+
"ELF does not match"
39+
);
40+
require(
41+
address(proofGeneratorAddr) == msg.sender,
42+
"proofGeneratorAddr does not match"
43+
);
3644

37-
(bool callWasSuccessfull, bytes memory proofIsIncluded) = alignedServiceManager.staticcall(
38-
abi.encodeWithSignature(
39-
"verifyBatchInclusion(bytes32,bytes32,bytes32,bytes20,bytes32,bytes,uint256)",
45+
bytes32 fullHash = keccak256(
46+
abi.encodePacked(
4047
proofCommitment,
4148
pubInputCommitment,
4249
provingSystemAuxDataCommitment,
43-
proofGeneratorAddr,
44-
batchMerkleRoot,
45-
merkleProof,
46-
verificationDataBatchIndex
50+
proofGeneratorAddr
4751
)
4852
);
53+
require(!mintedProofs[fullHash], "proof already minted");
54+
55+
(
56+
bool callWasSuccessfull,
57+
bytes memory proofIsIncluded
58+
) = alignedServiceManager.staticcall(
59+
abi.encodeWithSignature(
60+
"verifyBatchInclusion(bytes32,bytes32,bytes32,bytes20,bytes32,bytes,uint256,address)",
61+
proofCommitment,
62+
pubInputCommitment,
63+
provingSystemAuxDataCommitment,
64+
proofGeneratorAddr,
65+
batchMerkleRoot,
66+
merkleProof,
67+
verificationDataBatchIndex,
68+
paymentServiceAddr
69+
)
70+
);
4971

5072
require(callWasSuccessfull, "static_call failed");
5173

@@ -56,15 +78,19 @@ contract VerifierContract is ERC721URIStorage {
5678

5779
uint256 tokenId = _nextTokenId++;
5880
_mint(msg.sender, tokenId);
59-
_setTokenURI(tokenId, "ipfs://QmUKviny9x2oQUegyJFFBAUU2q5rvu5CsPzrUaBSDukpHQ");
81+
_setTokenURI(
82+
tokenId,
83+
"ipfs://QmUKviny9x2oQUegyJFFBAUU2q5rvu5CsPzrUaBSDukpHQ"
84+
);
6085

6186
return tokenId;
6287
}
6388

64-
function tokenURI(uint256 tokenId) public override view virtual returns (string memory) {
89+
function tokenURI(
90+
uint256 tokenId
91+
) public view virtual override returns (string memory) {
6592
_requireOwned(tokenId);
6693

6794
return "ipfs://QmUKviny9x2oQUegyJFFBAUU2q5rvu5CsPzrUaBSDukpHQ";
6895
}
69-
7096
}

examples/zkquiz/quiz/program/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ edition = "2021"
66

77
[dependencies]
88
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git", rev = "v1.0.8-testnet" }
9-
tiny-keccak = { git = "https://github.com/sp1-patches/tiny-keccak", branch = "patch-v2.0.2", features = ["sha3"] }
9+
tiny-keccak = { git = "https://github.com/sp1-patches/tiny-keccak", branch = "patch-v2.0.2", features = [
10+
"sha3",
11+
] }

examples/zkquiz/quiz/script/Cargo.lock

Lines changed: 51 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/zkquiz/quiz/script/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66

77
[dependencies]
88
sp1-sdk = { git = "https://github.com/succinctlabs/sp1.git", rev = "v1.0.1" }
9-
aligned-sdk = { git = "https://github.com/yetanotherco/aligned_layer", tag = "v0.4.0" }
9+
aligned-sdk = { git = "https://github.com/yetanotherco/aligned_layer", rev = "6908110aaa04ec3107f15ab8a8bb2603e7bbead9" }
1010
ethers = { tag = "v2.0.15-fix-reconnections", features = [
1111
"ws",
1212
"rustls",

examples/zkquiz/quiz/script/VerifierContract.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)