From 70312ea26f4f0bc9ba1111984501126c468bc893 Mon Sep 17 00:00:00 2001 From: "Ramon \"9Tails\" Canales" Date: Thu, 8 Feb 2024 14:28:42 +0000 Subject: [PATCH 1/2] feat: adding protocol versioning to verification key --- .../protocol_version/18/scheduler_key.json | 402 ++++++++++++++++++ .../protocol_version/19/scheduler_key.json | 399 +++++++++++++++++ .../20}/scheduler_key.json | 0 src/main.rs | 11 +- src/requests.rs | 119 ++++++ src/utils.rs | 25 +- 6 files changed, 934 insertions(+), 22 deletions(-) create mode 100644 src/keys/protocol_version/18/scheduler_key.json create mode 100644 src/keys/protocol_version/19/scheduler_key.json rename src/keys/{ => protocol_version/20}/scheduler_key.json (100%) diff --git a/src/keys/protocol_version/18/scheduler_key.json b/src/keys/protocol_version/18/scheduler_key.json new file mode 100644 index 0000000..8e56f82 --- /dev/null +++ b/src/keys/protocol_version/18/scheduler_key.json @@ -0,0 +1,402 @@ +// 20240208142152 +// https://raw.githubusercontent.com/matter-labs/era-boojum-validator-cli/e18f7c80ba4f5f257483b364d430c255c7b0d744/src/keys/scheduler_key.json + +{ + "n": 16777215, + "num_inputs": 1, + "state_width": 4, + "num_witness_polys": 0, + "gate_setup_commitments": [ + { + "x": [ + 4563648229522529090, + 12621273650309129924, + 6198549568814142266, + 2552508369834820982 + ], + "y": [ + 6942986305285328922, + 13872369038900622115, + 16362071052994133467, + 804758705072609803 + ], + "infinity": false + }, + { + "x": [ + 11488992528554025682, + 12016824828223971094, + 11942004360057333370, + 316831626296641307 + ], + "y": [ + 304673622018339856, + 7139037552557818730, + 12475560967982555143, + 1055588351918295250 + ], + "infinity": false + }, + { + "x": [ + 2274984630539920017, + 5398167177582250136, + 16440396753384808945, + 1037682586893548769 + ], + "y": [ + 10168660308952593373, + 16526369642614237721, + 569062739734175056, + 155645558476901406 + ], + "infinity": false + }, + { + "x": [ + 14005362797509427677, + 2662603874351919260, + 14261489165672308143, + 1470528288349794782 + ], + "y": [ + 11144229651170108862, + 11439490264313454962, + 114993091474760680, + 1037267173208738614 + ], + "infinity": false + }, + { + "x": [ + 10726125240955612787, + 1916320162213728495, + 1058608086768277905, + 1651114031905829493 + ], + "y": [ + 13237242732587628574, + 4774776044666137690, + 14401013098807103799, + 2514139699916115771 + ], + "infinity": false + }, + { + "x": [ + 14434760601334248377, + 5316938318287831815, + 6221098547630910324, + 980422841280734466 + ], + "y": [ + 9201886393750447942, + 3840149540273146267, + 18179910191622136829, + 1563809864380914603 + ], + "infinity": false + }, + { + "x": [ + 17877966991893524598, + 5994085157062523222, + 1757001537059388699, + 624319710399696047 + ], + "y": [ + 4763287561833789230, + 2005258298669826833, + 16972767076042430273, + 143191352994868054 + ], + "infinity": false + }, + { + "x": [ + 11830690209042008764, + 11761396005838073769, + 18271188400274886574, + 2896734446482773484 + ], + "y": [ + 1890606551566554401, + 10220931290312275762, + 3256711195869515344, + 2466626485328709457 + ], + "infinity": false + } + ], + "gate_selectors_commitments": [ + { + "x": [ + 10865727529243127085, + 4083978853392244827, + 14303622309482785753, + 2263042021033673595 + ], + "y": [ + 3019601017411802529, + 880444282195426618, + 9998743525359587628, + 2891421025832200233 + ], + "infinity": false + }, + { + "x": [ + 5208608554346323426, + 8575970970223832576, + 2966209169082345602, + 239576408267301488 + ], + "y": [ + 17715084817752316452, + 2726293100894160682, + 17920596859559317135, + 3485576345363305439 + ], + "infinity": false + } + ], + "permutation_commitments": [ + { + "x": [ + 14761045450946573029, + 17157644513453531531, + 2555518804134782053, + 1415819224310783987 + ], + "y": [ + 17265629196749977462, + 4128711855633066822, + 8435602817910411328, + 1408116296902303196 + ], + "infinity": false + }, + { + "x": [ + 3307267823832528482, + 2406249680085831639, + 9091964031261402109, + 2846274000290842933 + ], + "y": [ + 17374905554931807856, + 6690578002079222163, + 11809376320193686210, + 2676076649992974574 + ], + "infinity": false + }, + { + "x": [ + 3159118708748226574, + 5508845413629697013, + 13350869305506486049, + 689297560178790472 + ], + "y": [ + 15696011303896469684, + 12551611148155235140, + 14438660833518031207, + 425021756161657108 + ], + "infinity": false + }, + { + "x": [ + 18349397811516917436, + 4473982696343317918, + 13070312540813307819, + 2109468484629113245 + ], + "y": [ + 13254534552549721008, + 17388411854346636521, + 17875890960520499518, + 1062184221180884481 + ], + "infinity": false + } + ], + "total_lookup_entries_length": 1787472, + "lookup_selector_commitment": { + "x": [ + 9324906502432882695, + 14977861238256290580, + 12538013124354067293, + 3408438202312564138 + ], + "y": [ + 14942105932194201701, + 12210090881357612547, + 14774705021036784261, + 2531694948512337448 + ], + "infinity": false + }, + "lookup_tables_commitments": [ + { + "x": [ + 10873859091125335643, + 3906092213625635374, + 17046157606087980048, + 3193402705223440293 + ], + "y": [ + 10158946293873382504, + 2171386304067884865, + 6918663094168980658, + 350601565475975409 + ], + "infinity": false + }, + { + "x": [ + 12822112641313049260, + 3646552465186399021, + 10324071010773924047, + 2209084192380614662 + ], + "y": [ + 11045141628975531869, + 12589678537679955590, + 3065046617868727674, + 2099447669854151830 + ], + "infinity": false + }, + { + "x": [ + 11395032673621937545, + 3000063650268118516, + 7857619430005721792, + 805706808484810738 + ], + "y": [ + 6817063666434679427, + 1646386051225388537, + 4677946977082722827, + 1369650305976868514 + ], + "infinity": false + }, + { + "x": [ + 2885179371868476351, + 159944842081142878, + 6092294387055034894, + 213843603626505240 + ], + "y": [ + 11868113133779277990, + 8509646480531194854, + 14088068011597639414, + 707070630614027545 + ], + "infinity": false + } + ], + "lookup_table_type_commitment": { + "x": [ + 1732877442096985191, + 7537030715658833452, + 14073502080301311448, + 2178792007727681099 + ], + "y": [ + 8513095304113652904, + 6581396660744182779, + 13939755637576387431, + 2477157044961106453 + ], + "infinity": false + }, + "non_residues": [ + [ + 5, + 0, + 0, + 0 + ], + [ + 7, + 0, + 0, + 0 + ], + [ + 10, + 0, + 0, + 0 + ] + ], + "g2_elements": [ + { + "x": { + "c0": [ + 5106727233969649389, + 7440829307424791261, + 4785637993704342649, + 1729627375292849782 + ], + "c1": [ + 10945020018377822914, + 17413811393473931026, + 8241798111626485029, + 1841571559660931130 + ] + }, + "y": { + "c0": [ + 5541340697920699818, + 16416156555105522555, + 5380518976772849807, + 1353435754470862315 + ], + "c1": [ + 6173549831154472795, + 13567992399387660019, + 17050234209342075797, + 650358724130500725 + ] + }, + "infinity": false + }, + { + "x": { + "c0": [ + 9089143573911733168, + 11482283522806384523, + 13585589533905622862, + 79029415676722370 + ], + "c1": [ + 5692040832573735873, + 16884514497384809355, + 16717166481813659368, + 2742131088506155463 + ] + }, + "y": { + "c0": [ + 9604638503594647125, + 1289961608472612514, + 6217038149984805214, + 2521661352385209130 + ], + "c1": [ + 17168069778630926308, + 11309277837895768996, + 15154989611154567813, + 359271377050603491 + ] + }, + "infinity": false + } + ] + } \ No newline at end of file diff --git a/src/keys/protocol_version/19/scheduler_key.json b/src/keys/protocol_version/19/scheduler_key.json new file mode 100644 index 0000000..01e0deb --- /dev/null +++ b/src/keys/protocol_version/19/scheduler_key.json @@ -0,0 +1,399 @@ +{ + "n": 16777215, + "num_inputs": 1, + "state_width": 4, + "num_witness_polys": 0, + "gate_setup_commitments": [ + { + "x": [ + 4563648229522529090, + 12621273650309129924, + 6198549568814142266, + 2552508369834820982 + ], + "y": [ + 6942986305285328922, + 13872369038900622115, + 16362071052994133467, + 804758705072609803 + ], + "infinity": false + }, + { + "x": [ + 11488992528554025682, + 12016824828223971094, + 11942004360057333370, + 316831626296641307 + ], + "y": [ + 304673622018339856, + 7139037552557818730, + 12475560967982555143, + 1055588351918295250 + ], + "infinity": false + }, + { + "x": [ + 2274984630539920017, + 5398167177582250136, + 16440396753384808945, + 1037682586893548769 + ], + "y": [ + 10168660308952593373, + 16526369642614237721, + 569062739734175056, + 155645558476901406 + ], + "infinity": false + }, + { + "x": [ + 14005362797509427677, + 2662603874351919260, + 14261489165672308143, + 1470528288349794782 + ], + "y": [ + 11144229651170108862, + 11439490264313454962, + 114993091474760680, + 1037267173208738614 + ], + "infinity": false + }, + { + "x": [ + 10726125240955612787, + 1916320162213728495, + 1058608086768277905, + 1651114031905829493 + ], + "y": [ + 13237242732587628574, + 4774776044666137690, + 14401013098807103799, + 2514139699916115771 + ], + "infinity": false + }, + { + "x": [ + 14434760601334248377, + 5316938318287831815, + 6221098547630910324, + 980422841280734466 + ], + "y": [ + 9201886393750447942, + 3840149540273146267, + 18179910191622136829, + 1563809864380914603 + ], + "infinity": false + }, + { + "x": [ + 17877966991893524598, + 5994085157062523222, + 1757001537059388699, + 624319710399696047 + ], + "y": [ + 4763287561833789230, + 2005258298669826833, + 16972767076042430273, + 143191352994868054 + ], + "infinity": false + }, + { + "x": [ + 11830690209042008764, + 11761396005838073769, + 18271188400274886574, + 2896734446482773484 + ], + "y": [ + 1890606551566554401, + 10220931290312275762, + 3256711195869515344, + 2466626485328709457 + ], + "infinity": false + } + ], + "gate_selectors_commitments": [ + { + "x": [ + 10865727529243127085, + 4083978853392244827, + 14303622309482785753, + 2263042021033673595 + ], + "y": [ + 3019601017411802529, + 880444282195426618, + 9998743525359587628, + 2891421025832200233 + ], + "infinity": false + }, + { + "x": [ + 5208608554346323426, + 8575970970223832576, + 2966209169082345602, + 239576408267301488 + ], + "y": [ + 17715084817752316452, + 2726293100894160682, + 17920596859559317135, + 3485576345363305439 + ], + "infinity": false + } + ], + "permutation_commitments": [ + { + "x": [ + 14761045450946573029, + 17157644513453531531, + 2555518804134782053, + 1415819224310783987 + ], + "y": [ + 17265629196749977462, + 4128711855633066822, + 8435602817910411328, + 1408116296902303196 + ], + "infinity": false + }, + { + "x": [ + 3307267823832528482, + 2406249680085831639, + 9091964031261402109, + 2846274000290842933 + ], + "y": [ + 17374905554931807856, + 6690578002079222163, + 11809376320193686210, + 2676076649992974574 + ], + "infinity": false + }, + { + "x": [ + 3159118708748226574, + 5508845413629697013, + 13350869305506486049, + 689297560178790472 + ], + "y": [ + 15696011303896469684, + 12551611148155235140, + 14438660833518031207, + 425021756161657108 + ], + "infinity": false + }, + { + "x": [ + 18349397811516917436, + 4473982696343317918, + 13070312540813307819, + 2109468484629113245 + ], + "y": [ + 13254534552549721008, + 17388411854346636521, + 17875890960520499518, + 1062184221180884481 + ], + "infinity": false + } + ], + "total_lookup_entries_length": 1787472, + "lookup_selector_commitment": { + "x": [ + 9324906502432882695, + 14977861238256290580, + 12538013124354067293, + 3408438202312564138 + ], + "y": [ + 14942105932194201701, + 12210090881357612547, + 14774705021036784261, + 2531694948512337448 + ], + "infinity": false + }, + "lookup_tables_commitments": [ + { + "x": [ + 10873859091125335643, + 3906092213625635374, + 17046157606087980048, + 3193402705223440293 + ], + "y": [ + 10158946293873382504, + 2171386304067884865, + 6918663094168980658, + 350601565475975409 + ], + "infinity": false + }, + { + "x": [ + 12822112641313049260, + 3646552465186399021, + 10324071010773924047, + 2209084192380614662 + ], + "y": [ + 11045141628975531869, + 12589678537679955590, + 3065046617868727674, + 2099447669854151830 + ], + "infinity": false + }, + { + "x": [ + 11395032673621937545, + 3000063650268118516, + 7857619430005721792, + 805706808484810738 + ], + "y": [ + 6817063666434679427, + 1646386051225388537, + 4677946977082722827, + 1369650305976868514 + ], + "infinity": false + }, + { + "x": [ + 2885179371868476351, + 159944842081142878, + 6092294387055034894, + 213843603626505240 + ], + "y": [ + 11868113133779277990, + 8509646480531194854, + 14088068011597639414, + 707070630614027545 + ], + "infinity": false + } + ], + "lookup_table_type_commitment": { + "x": [ + 1732877442096985191, + 7537030715658833452, + 14073502080301311448, + 2178792007727681099 + ], + "y": [ + 8513095304113652904, + 6581396660744182779, + 13939755637576387431, + 2477157044961106453 + ], + "infinity": false + }, + "non_residues": [ + [ + 5, + 0, + 0, + 0 + ], + [ + 7, + 0, + 0, + 0 + ], + [ + 10, + 0, + 0, + 0 + ] + ], + "g2_elements": [ + { + "x": { + "c0": [ + 5106727233969649389, + 7440829307424791261, + 4785637993704342649, + 1729627375292849782 + ], + "c1": [ + 10945020018377822914, + 17413811393473931026, + 8241798111626485029, + 1841571559660931130 + ] + }, + "y": { + "c0": [ + 5541340697920699818, + 16416156555105522555, + 5380518976772849807, + 1353435754470862315 + ], + "c1": [ + 6173549831154472795, + 13567992399387660019, + 17050234209342075797, + 650358724130500725 + ] + }, + "infinity": false + }, + { + "x": { + "c0": [ + 9089143573911733168, + 11482283522806384523, + 13585589533905622862, + 79029415676722370 + ], + "c1": [ + 5692040832573735873, + 16884514497384809355, + 16717166481813659368, + 2742131088506155463 + ] + }, + "y": { + "c0": [ + 9604638503594647125, + 1289961608472612514, + 6217038149984805214, + 2521661352385209130 + ], + "c1": [ + 17168069778630926308, + 11309277837895768996, + 15154989611154567813, + 359271377050603491 + ] + }, + "infinity": false + } + ] +} diff --git a/src/keys/scheduler_key.json b/src/keys/protocol_version/20/scheduler_key.json similarity index 100% rename from src/keys/scheduler_key.json rename to src/keys/protocol_version/20/scheduler_key.json diff --git a/src/main.rs b/src/main.rs index 5d0f2bb..8135715 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ use crate::requests::L1BatchAndProofData; use crate::snark_wrapper_verifier::{ generate_solidity_test, verify_snark, verify_snark_from_storage, L1BatchProofForL1, }; -use crate::utils::update_verification_key_if_needed; +use crate::utils::check_verification_key; pub mod block_header; use circuit_definitions::boojum::{ @@ -180,7 +180,7 @@ async fn main() { } } - update_verification_key_if_needed(opt.update_verification_key).await; + let protocol_version = requests::fetch_batch_protocol_version(batch_number, &network).await; println!("{}", "Fetching and validating the proof itself".on_blue()); if l1_rpc.is_none() { @@ -196,6 +196,9 @@ async fn main() { return; } } else { + + check_verification_key(protocol_version.clone().unwrap()).await; + let contract = ContractConfig::new(l1_rpc.clone().unwrap(), network.clone()); let resp = requests::fetch_l1_data(batch_number, &network, &l1_rpc.clone().unwrap()).await; @@ -210,8 +213,8 @@ async fn main() { { let vk_hash = contract.get_verification_key_hash(block_number).await; - let snark_vk_scheduler_key_file = "src/keys/scheduler_key.json"; - + let snark_vk_scheduler_key_file = format!("src/keys/protocol_version/{}/scheduler_key.json", protocol_version.unwrap()); + let mut batch_proof = L1BatchProofForL1 { aggregation_result_coords: aux_output.prepare_aggregation_result_coords(), scheduler_proof, diff --git a/src/requests.rs b/src/requests.rs index b33d497..4653fcc 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -317,6 +317,22 @@ pub struct L1BatchJson { proveTxHash: Option, } +#[derive(serde::Serialize, serde::Deserialize)] +pub struct L1BatchRangeJson { + result: Vec, +} + +#[derive(serde::Serialize, serde::Deserialize)] +pub struct JSONL2SyncRPCResponse { + result: L2SyncDetails, +} + +#[allow(non_snake_case)] +#[derive(serde::Serialize, serde::Deserialize)] +pub struct L2SyncDetails { + protocolVersion: String, +} + // Fetches given batch information from Era RPC pub async fn fetch_batch_commit_tx( batch_number: u64, @@ -374,6 +390,109 @@ pub async fn fetch_batch_commit_tx( } } +// Fetches given batch information from Era RPC +pub async fn fetch_batch_protocol_version( + batch_number: u64, + network: &str, +) -> Result { + println!( + "Fetching batch {} protocol version from zkSync Era on network {}", + batch_number, network + ); + + let domain; + if network == "sepolia" { + domain = "https://sepolia.era.zksync.dev" + } else if network == "mainnet" { + domain = "https://mainnet.era.zksync.io" + } else { + domain = "https://testnet.era.zksync.dev" + } + let client = reqwest::Client::new(); + + let response = client + .post(domain) + .header("Content-Type", "application/json") + .body(format!( + r#"{{ + "jsonrpc": "2.0", + "method": "zks_getL1BatchBlockRange", + "params": [{}], + "id": "1" + }}"#, + batch_number + )) + .send() + .await; + + if response.is_err() { + return Err(StatusCode::FailedToCallRPC); + } + + let response = response.unwrap(); + + if response.status().is_success() { + let json = response.json::() + .await; + + if json.is_err() { + return Err(StatusCode::FailedToCallRPC); + } + + let batch_range = json.unwrap(); + + let l2_block_hex = batch_range.result[0].clone(); + + let without_prefix = l2_block_hex.trim_start_matches("0x"); + let l2_block = i64::from_str_radix(without_prefix, 16); + + let response_2 = client + .post(domain) + .header("Content-Type", "application/json") + .body(format!( + r#"{{ + "jsonrpc": "2.0", + "method": "en_syncL2Block", + "params": [{}, false], + "id": "1" + }}"#, + l2_block.unwrap() + )) + .send() + .await; + + if response_2.is_err() { + return Err(StatusCode::FailedToCallRPC); + } + + let response_2 = response_2.unwrap(); + + if response_2.status().is_success() { + let json_2 = response_2.json::() + .await; + + if json_2.is_err() { + return Err(StatusCode::FailedToCallRPC); + } + + let sync_result = json_2.unwrap(); + + let version = sync_result.result.protocolVersion.strip_prefix("Version").unwrap(); + + println!( + "Batch {} has protocol version {}", + batch_number, version + ); + + return Ok(version.to_string()); + } else { + return Err(StatusCode::FailedToCallRPC); + } + } else { + return Err(StatusCode::FailedToCallRPC); + } +} + fn find_state_data_from_log( batch_number: u64, function: &Function, diff --git a/src/utils.rs b/src/utils.rs index e534a33..aad2e22 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,24 +1,13 @@ -use std::{env, fs::File, io}; +use std::env; -const VERIFICATION_KEY_FILE_GITHUB: &str = "https://raw.githubusercontent.com/matter-labs/era-contracts/main/tools/data/scheduler_key.json"; - -/// Checks to see if the verification key exists or an update has been requested and downloads it from github if needed. -pub async fn update_verification_key_if_needed(update_verification_key: Option) { - let file_path = "src/keys/scheduler_key.json"; +/// Checks to see if the verification key exists for a given protocol version or an update has been requested and downloads it from github if needed. +pub async fn check_verification_key(protocol_version: String) { + let file_path = format!("src/keys/protocol_version/{}/scheduler_key.json", protocol_version); let file = env::current_dir().unwrap().join(file_path); let file_exists = file.exists(); - let should_update = update_verification_key.unwrap_or_default(); - - if file_exists && !should_update { - println!("verification key exists") - } else { - println!("verification key does not exist or update requested, downloading..."); - let resp = reqwest::get(VERIFICATION_KEY_FILE_GITHUB) - .await - .expect(&format!("failed to download file from {VERIFICATION_KEY_FILE_GITHUB}")); - let body = resp.text().await.expect("body invalid"); - let mut out = File::create(file_path).expect(&format!("failed to create file {file_path}")); - io::copy(&mut body.as_bytes(), &mut out).expect(&format!("failed to write verification key to {file_path}")); + if !file_exists { + eprintln!("Verification key for protocol version {} is missing. Please add it to the keys folder.", protocol_version); + std::process::exit(1) } } From fe1c588c6e68983c6d518e51b693eb5aaaab5243 Mon Sep 17 00:00:00 2001 From: "Ramon \"9Tails\" Canales" Date: Fri, 9 Feb 2024 09:26:32 +0000 Subject: [PATCH 2/2] feat: removing unused arg and adding information about latest key --- README.md | 1 - src/main.rs | 3 --- src/utils.rs | 1 + 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 6f953b8..d744568 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,6 @@ cargo run -- --batch 109939 --network mainnet --l1-rpc https://rpc.ankr.com/eth --batch - The L1 batch number you want to verify the generated proof --network - Along with batch number, defines if you want to verify a proof for Era testnet or mainnet. It defaults to mainnet. Accepts "mainnet" or "testnet" --l1-rpc - The RPC url required to pull data from L1. ---update-verification-key - Specifes if the verification key should be updated from [github](https://github.com/matter-labs/era-contracts/blob/main/tools/data/scheduler_key.json). --json - Flag to specify if the output should be in json. Note that all the usual std out prints are silenced. ``` diff --git a/src/main.rs b/src/main.rs index 8135715..5cac6ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,9 +56,6 @@ struct Cli { #[arg(long)] /// RPC endpoint to use to fetch L1 information l1_rpc: Option, - /// Bool to request updating verification key - #[arg(long)] - update_verification_key: Option, /// Flag to print output as json #[arg(long)] json: bool, diff --git a/src/utils.rs b/src/utils.rs index aad2e22..fe9751a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,6 +7,7 @@ pub async fn check_verification_key(protocol_version: String) { let file_exists = file.exists(); if !file_exists { + // If the key for the latest protocol version is nout available in this repo yet, you can always find it at https://github.com/matter-labs/era-contracts/blob/main/tools/data/scheduler_key.json eprintln!("Verification key for protocol version {} is missing. Please add it to the keys folder.", protocol_version); std::process::exit(1) }