From 126d761834b70e0c79a8f7cd72e7596528639857 Mon Sep 17 00:00:00 2001 From: Andrew Kirillov <20803092+akirillo@users.noreply.github.com> Date: Fri, 1 Sep 2023 12:03:54 -0700 Subject: [PATCH] tests: verifier: adopt multiverifier interface --- starknet_scripts/src/commands/deploy.rs | 23 +--- starknet_scripts/src/commands/utils.rs | 81 ++---------- tests/src/darkpool/utils.rs | 156 ++++++------------------ tests/src/merkle/utils.rs | 8 +- tests/src/nullifier_set/utils.rs | 8 +- tests/src/poseidon/utils.rs | 16 +-- tests/src/statement_serde/utils.rs | 9 +- tests/src/transcript/utils.rs | 15 +-- tests/src/utils.rs | 32 ++++- tests/src/verifier/utils.rs | 102 +++++++++------- tests/src/verifier_utils/utils.rs | 9 +- tests/tests/verifier.rs | 50 +++++--- 12 files changed, 183 insertions(+), 326 deletions(-) diff --git a/starknet_scripts/src/commands/deploy.rs b/starknet_scripts/src/commands/deploy.rs index b15461dc..f3d71349 100644 --- a/starknet_scripts/src/commands/deploy.rs +++ b/starknet_scripts/src/commands/deploy.rs @@ -7,8 +7,8 @@ use tracing::{debug, info}; use crate::{ cli::{Contract, DeployArgs}, commands::utils::{ - deploy_darkpool, deploy_merkle, deploy_nullifier_set, deploy_verifier, dump_deployment, - initialize, setup_account, MERKLE_HEIGHT, + deploy_darkpool, deploy_merkle, deploy_nullifier_set, dump_deployment, initialize, + setup_account, MERKLE_HEIGHT, }, }; @@ -66,33 +66,20 @@ pub async fn deploy_and_initialize(args: DeployArgs) -> Result<()> { ); if should_initialize { - // Deploy verifier - let verifier_class_hash_hex = if let Some(verifier_class_hash) = verifier_class_hash - { - verifier_class_hash - } else { - format!("{verifier_class_hash_felt:#64x}") - }; - let (verifier_address, _, _) = deploy_verifier( - Some(verifier_class_hash_hex), - FieldElement::ZERO, /* salt */ - &artifacts_path, - &account, - ) - .await?; - // Initialize darkpool debug!("Initializing darkpool contract..."); let calldata = vec![ merkle_class_hash_felt, nullifier_set_class_hash_felt, - verifier_address, + verifier_class_hash_felt, FieldElement::from(MERKLE_HEIGHT), // TODO: Need to get circuit params! Prob best to read them in from file. ]; let initialization_result = initialize(&account, darkpool_address, calldata).await?; + // TODO: Parameterize verifiers + info!( "Darkpool contract initialized!\n\ Transaction hash: {:#64x}\n", diff --git a/starknet_scripts/src/commands/utils.rs b/starknet_scripts/src/commands/utils.rs index e4234940..96813e05 100644 --- a/starknet_scripts/src/commands/utils.rs +++ b/starknet_scripts/src/commands/utils.rs @@ -48,7 +48,7 @@ const ADDR_BOUND: FieldElement = FieldElement::from_mont([ pub const DARKPOOL_CONTRACT_NAME: &str = "renegade_contracts_Darkpool"; pub const MERKLE_CONTRACT_NAME: &str = "renegade_contracts_Merkle"; pub const NULLIFIER_SET_CONTRACT_NAME: &str = "renegade_contracts_NullifierSet"; -pub const VERIFIER_CONTRACT_NAME: &str = "renegade_contracts_Verifier"; +pub const VERIFIER_CONTRACT_NAME: &str = "renegade_contracts_MultiVerifier"; pub const SIERRA_FILE_EXTENSION: &str = "sierra.json"; pub const CASM_FILE_EXTENSION: &str = "casm.json"; @@ -171,11 +171,14 @@ pub async fn deploy( account: &ScriptAccount, class_hash: FieldElement, calldata: &[FieldElement], - salt: FieldElement, ) -> Result { let contract_factory = ContractFactory::new(class_hash, account); let deploy_result = contract_factory - .deploy(calldata, salt, false /* unique */) + .deploy( + calldata, + FieldElement::ZERO, /* salt */ + false, /* unique */ + ) .send() .await?; @@ -206,13 +209,12 @@ pub async fn initialize( // Taken from https://github.com/xJonathanLEI/starknet-rs/blob/master/starknet-accounts/src/factory/mod.rs pub fn calculate_contract_address( class_hash: FieldElement, - salt: FieldElement, constructor_calldata: &[FieldElement], ) -> FieldElement { compute_hash_on_elements(&[ PREFIX_CONTRACT_ADDRESS, FieldElement::ZERO, /* deployer address */ - salt, + FieldElement::ZERO, /* salt */ class_hash, compute_hash_on_elements(constructor_calldata), ]) % ADDR_BOUND @@ -278,19 +280,9 @@ pub async fn deploy_darkpool( let calldata = vec![account.address()]; let InvokeTransactionResult { transaction_hash, .. - } = deploy( - account, - darkpool_class_hash_felt, - &calldata, - FieldElement::ZERO, /* salt */ - ) - .await?; + } = deploy(account, darkpool_class_hash_felt, &calldata).await?; - let darkpool_address = calculate_contract_address( - darkpool_class_hash_felt, - FieldElement::ZERO, /* salt */ - &calldata, - ); + let darkpool_address = calculate_contract_address(darkpool_class_hash_felt, &calldata); Ok(( darkpool_address, @@ -321,19 +313,9 @@ pub async fn deploy_merkle( debug!("Deploying merkle contract..."); let InvokeTransactionResult { transaction_hash, .. - } = deploy( - account, - merkle_class_hash_felt, - &[], - FieldElement::ZERO, /* salt */ - ) - .await?; + } = deploy(account, merkle_class_hash_felt, &[]).await?; - let merkle_address = calculate_contract_address( - merkle_class_hash_felt, - FieldElement::ZERO, /* salt */ - &[], - ); + let merkle_address = calculate_contract_address(merkle_class_hash_felt, &[]); Ok((merkle_address, merkle_class_hash_felt, transaction_hash)) } @@ -357,19 +339,9 @@ pub async fn deploy_nullifier_set( debug!("Deploying nullifier set contract..."); let InvokeTransactionResult { transaction_hash, .. - } = deploy( - account, - nullifier_set_class_hash_felt, - &[], - FieldElement::ZERO, /* salt */ - ) - .await?; + } = deploy(account, nullifier_set_class_hash_felt, &[]).await?; - let nullifier_set_address = calculate_contract_address( - nullifier_set_class_hash_felt, - FieldElement::ZERO, /* salt */ - &[], - ); + let nullifier_set_address = calculate_contract_address(nullifier_set_class_hash_felt, &[]); Ok(( nullifier_set_address, @@ -377,30 +349,3 @@ pub async fn deploy_nullifier_set( transaction_hash, )) } - -pub async fn deploy_verifier( - verifier_class_hash: Option, - salt: FieldElement, - artifacts_path: &str, - account: &ScriptAccount, -) -> Result<(FieldElement, FieldElement, FieldElement)> { - let (verifier_sierra_path, verifier_casm_path) = - get_artifacts(artifacts_path, VERIFIER_CONTRACT_NAME); - let verifier_class_hash_felt = get_or_declare( - verifier_class_hash, - verifier_sierra_path, - verifier_casm_path, - account, - ) - .await?; - - // Deploy verifier - debug!("Deploying verifier contract..."); - let InvokeTransactionResult { - transaction_hash, .. - } = deploy(account, verifier_class_hash_felt, &[], salt).await?; - - let verifier_address = calculate_contract_address(verifier_class_hash_felt, salt, &[]); - - Ok((verifier_address, verifier_class_hash_felt, transaction_hash)) -} diff --git a/tests/src/darkpool/utils.rs b/tests/src/darkpool/utils.rs index 568a39d9..20163205 100644 --- a/tests/src/darkpool/utils.rs +++ b/tests/src/darkpool/utils.rs @@ -28,8 +28,8 @@ use starknet::{ }; use starknet_client::types::StarknetU256; use starknet_scripts::commands::utils::{ - calculate_contract_address, declare, deploy, deploy_darkpool, deploy_verifier, get_artifacts, - initialize, ScriptAccount, DARKPOOL_CONTRACT_NAME, + calculate_contract_address, declare, deploy, deploy_darkpool, get_artifacts, initialize, + ScriptAccount, DARKPOOL_CONTRACT_NAME, }; use std::{env, iter}; @@ -41,13 +41,14 @@ use crate::{ utils::TEST_MERKLE_HEIGHT, }, utils::{ - call_contract, felt_to_u128, get_circuit_params, get_contract_address_from_artifact, - get_dummy_statement_scalars, get_sierra_class_hash_from_artifact, global_setup, - invoke_contract, random_felt, scalar_to_felt, setup_sequencer, singleprover_prove, - CalldataSerializable, Circuit, DummyValidCommitments, DummyValidMatchMpc, - DummyValidReblind, DummyValidSettle, DummyValidWalletCreate, DummyValidWalletUpdate, - MatchPayload, NewWalletArgs, ProcessMatchArgs, TestConfig, UpdateWalletArgs, - ARTIFACTS_PATH_ENV_VAR, DUMMY_VALUE, SK_ROOT, + call_contract, check_verification_job_status, felt_to_u128, get_circuit_params, + get_contract_address_from_artifact, get_dummy_statement_scalars, + get_sierra_class_hash_from_artifact, global_setup, invoke_contract, random_felt, + scalar_to_felt, setup_sequencer, singleprover_prove, CalldataSerializable, Circuit, + DummyValidCommitments, DummyValidMatchMpc, DummyValidReblind, DummyValidSettle, + DummyValidWalletCreate, DummyValidWalletUpdate, MatchPayload, NewWalletArgs, + ProcessMatchArgs, TestConfig, UpdateWalletArgs, ARTIFACTS_PATH_ENV_VAR, DUMMY_VALUE, + SK_ROOT, }, }; @@ -60,7 +61,6 @@ pub const TRANSFER_AMOUNT: u64 = 100; const INITIALIZE_VERIFIER_FN_NAME: &str = "initialize_verifier"; const GET_WALLET_BLINDER_TRANSACTION_FN_NAME: &str = "get_wallet_blinder_transaction"; const IS_NULLIFIER_AVAILABLE_FN_NAME: &str = "is_nullifier_available"; -const CHECK_VERIFICATION_JOB_STATUS_FN_NAME: &str = "check_verification_job_status"; const NEW_WALLET_FN_NAME: &str = "new_wallet"; const POLL_NEW_WALLET_FN_NAME: &str = "poll_new_wallet"; const UPDATE_WALLET_FN_NAME: &str = "update_wallet"; @@ -105,7 +105,7 @@ pub async fn init_darkpool_test_state() -> Result { let sequencer = global_setup(None).await; let account = sequencer.account(); debug!("Declaring & deploying darkpool contract..."); - let (darkpool_address, _, merkle_class_hash, nullifier_set_class_hash, verifier_class_hash, _) = + let (darkpool_address, _, merkle_class_hash, nullifier_set_class_hash, _verifier_class_hash, _) = deploy_darkpool(None, None, None, None, &artifacts_path, &account).await?; debug!("Initializing darkpool contract..."); @@ -118,29 +118,19 @@ pub async fn init_darkpool_test_state() -> Result { ) .await?; - let verifier_class_hash_hex = Some(format!("{verifier_class_hash:#64x}")); - - for (i, circuit) in [ - Circuit::ValidWalletCreate(DummyValidWalletCreate {}), - Circuit::ValidWalletUpdate(DummyValidWalletUpdate {}), - Circuit::ValidCommitments(DummyValidCommitments {}), - Circuit::ValidReblind(DummyValidReblind {}), - Circuit::ValidMatchMpc(DummyValidMatchMpc {}), - Circuit::ValidSettle(DummyValidSettle {}), - ] - .into_iter() - .enumerate() - { - deploy_and_initialize_verifier( - &account, - &artifacts_path, - circuit, - darkpool_address, - verifier_class_hash_hex.clone(), - FieldElement::from(i), /* salt */ - ) - .await?; - } + // for (i, circuit) in [ + // Circuit::ValidWalletCreate(DummyValidWalletCreate {}), + // Circuit::ValidWalletUpdate(DummyValidWalletUpdate {}), + // Circuit::ValidCommitments(DummyValidCommitments {}), + // Circuit::ValidReblind(DummyValidReblind {}), + // Circuit::ValidMatchMpc(DummyValidMatchMpc {}), + // Circuit::ValidSettle(DummyValidSettle {}), + // ] + // .into_iter() + // .enumerate() + // { + // todo!() + // } debug!("Declaring & deploying dummy ERC20 contract..."); let erc20_address = deploy_dummy_erc20(&artifacts_path, &account, darkpool_address).await?; @@ -167,7 +157,6 @@ pub fn init_darkpool_test_statics(account: &ScriptAccount) -> Result<()> { let darkpool_address = get_contract_address_from_artifact( &artifacts_path, DARKPOOL_CONTRACT_NAME, - FieldElement::ZERO, /* salt */ &[account.address()], )?; if DARKPOOL_ADDRESS.get().is_none() { @@ -177,7 +166,6 @@ pub fn init_darkpool_test_statics(account: &ScriptAccount) -> Result<()> { let erc20_address = get_contract_address_from_artifact( &artifacts_path, DUMMY_ERC20_CONTRACT_NAME, - FieldElement::ZERO, /* salt */ &get_dummy_erc20_calldata(account.address(), darkpool_address)?, )?; if ERC20_ADDRESS.get().is_none() { @@ -234,18 +222,8 @@ async fn deploy_dummy_erc20( let calldata = get_dummy_erc20_calldata(account.address(), darkpool_address)?; - deploy( - account, - class_hash, - &calldata, - FieldElement::ZERO, /* salt */ - ) - .await?; - Ok(calculate_contract_address( - class_hash, - FieldElement::ZERO, /* salt */ - &calldata, - )) + deploy(account, class_hash, &calldata).await?; + Ok(calculate_contract_address(class_hash, &calldata)) } async fn declare_dummy_upgrade_target( @@ -333,29 +311,6 @@ pub async fn initialize_verifier( .map(|_| ()) } -pub async fn deploy_and_initialize_verifier( - account: &ScriptAccount, - artifacts_path: &str, - circuit: Circuit, - darkpool_address: FieldElement, - verifier_class_hash_hex: Option, - salt: FieldElement, -) -> Result<()> { - debug!("Declaring & deploying {circuit:?} verifier contract..."); - let (verifier_address, _, _) = deploy_verifier( - verifier_class_hash_hex.clone(), - salt, - artifacts_path, - account, - ) - .await?; - - debug!("Initializing {circuit:?} verifier contract..."); - initialize_verifier(account, circuit, darkpool_address, verifier_address) - .await - .map(|_| ()) -} - pub async fn get_wallet_blinder_transaction( account: &ScriptAccount, wallet_blinder_share: Scalar, @@ -381,36 +336,6 @@ pub async fn is_nullifier_available(account: &ScriptAccount, nullifier: Scalar) .map(|r| r[0] == FieldElement::ONE) } -pub async fn check_verification_job_status( - account: &ScriptAccount, - circuit: Circuit, - verification_job_id: FieldElement, -) -> Result> { - let calldata = circuit - .to_calldata() - .into_iter() - .chain(iter::once(verification_job_id)) - .collect(); - - call_contract( - account, - *DARKPOOL_ADDRESS.get().unwrap(), - CHECK_VERIFICATION_JOB_STATUS_FN_NAME, - calldata, - ) - .await - .map(|r| { - // The Cairo corelib serializes an Option::None(()) as 1, - // and an Option::Some(x) as [0, ..serialize(x)]. - // In our case, x is a bool => serializes as a true = 1, false = 0. - if r[0] == FieldElement::ONE { - None - } else { - Some(r[1] == FieldElement::ONE) - } - }) -} - pub async fn new_wallet(account: &ScriptAccount, args: &NewWalletArgs) -> Result { let calldata = args.to_calldata(); @@ -445,7 +370,7 @@ pub async fn poll_new_wallet_to_completion( let tx_hash = new_wallet(account, args).await?; let mut verification_job_status = check_verification_job_status( account, - Circuit::ValidWalletCreate(DummyValidWalletCreate {}), + *DARKPOOL_ADDRESS.get().unwrap(), args.verification_job_id, ) .await?; @@ -455,7 +380,7 @@ pub async fn poll_new_wallet_to_completion( verification_job_status = check_verification_job_status( account, - Circuit::ValidWalletCreate(DummyValidWalletCreate {}), + *DARKPOOL_ADDRESS.get().unwrap(), args.verification_job_id, ) .await?; @@ -505,7 +430,7 @@ pub async fn poll_update_wallet_to_completion( let tx_hash = update_wallet(account, args).await?; let mut verification_job_status = check_verification_job_status( account, - Circuit::ValidWalletUpdate(DummyValidWalletUpdate {}), + *DARKPOOL_ADDRESS.get().unwrap(), args.verification_job_id, ) .await?; @@ -514,7 +439,7 @@ pub async fn poll_update_wallet_to_completion( poll_update_wallet(account, args.verification_job_id).await?; verification_job_status = check_verification_job_status( account, - Circuit::ValidWalletUpdate(DummyValidWalletUpdate {}), + *DARKPOOL_ADDRESS.get().unwrap(), args.verification_job_id, ) .await?; @@ -561,20 +486,13 @@ pub async fn process_match_verification_jobs_are_done( account: &ScriptAccount, verification_job_ids: &[FieldElement], ) -> Result { - for (verification_job_id, circuit) in verification_job_ids.iter().zip( - // Matches expected order of verification job IDs - [ - Circuit::ValidCommitments(DummyValidCommitments {}), - Circuit::ValidReblind(DummyValidReblind {}), - Circuit::ValidCommitments(DummyValidCommitments {}), - Circuit::ValidReblind(DummyValidReblind {}), - Circuit::ValidMatchMpc(DummyValidMatchMpc {}), - Circuit::ValidSettle(DummyValidSettle {}), - ] - .into_iter(), - ) { - let verification_job_status = - check_verification_job_status(account, circuit, *verification_job_id).await?; + for verification_job_id in verification_job_ids { + let verification_job_status = check_verification_job_status( + account, + *DARKPOOL_ADDRESS.get().unwrap(), + *verification_job_id, + ) + .await?; if verification_job_status.is_none() { return Ok(false); } else if let Some(false) = verification_job_status { diff --git a/tests/src/merkle/utils.rs b/tests/src/merkle/utils.rs index 996c9abd..cfc41be3 100644 --- a/tests/src/merkle/utils.rs +++ b/tests/src/merkle/utils.rs @@ -54,12 +54,8 @@ pub async fn init_merkle_test_state() -> Result { pub fn init_merkle_test_statics() -> Result<()> { let artifacts_path = env::var(ARTIFACTS_PATH_ENV_VAR).unwrap(); - let merkle_address = get_contract_address_from_artifact( - &artifacts_path, - MERKLE_CONTRACT_NAME, - FieldElement::ZERO, /* salt */ - &[], - )?; + let merkle_address = + get_contract_address_from_artifact(&artifacts_path, MERKLE_CONTRACT_NAME, &[])?; if MERKLE_ADDRESS.get().is_none() { MERKLE_ADDRESS.set(merkle_address).unwrap(); } diff --git a/tests/src/nullifier_set/utils.rs b/tests/src/nullifier_set/utils.rs index bd968892..71269738 100644 --- a/tests/src/nullifier_set/utils.rs +++ b/tests/src/nullifier_set/utils.rs @@ -41,12 +41,8 @@ pub async fn init_nullifier_set_test_state() -> Result { pub fn init_nullifier_set_test_statics() -> Result<()> { let artifacts_path = env::var(ARTIFACTS_PATH_ENV_VAR).unwrap(); - let nullifier_set_address = get_contract_address_from_artifact( - &artifacts_path, - NULLIFIER_SET_CONTRACT_NAME, - FieldElement::ZERO, - &[], - )?; + let nullifier_set_address = + get_contract_address_from_artifact(&artifacts_path, NULLIFIER_SET_CONTRACT_NAME, &[])?; if NULLIFIER_SET_ADDRESS.get().is_none() { NULLIFIER_SET_ADDRESS.set(nullifier_set_address).unwrap(); } diff --git a/tests/src/poseidon/utils.rs b/tests/src/poseidon/utils.rs index c63febdd..82563022 100644 --- a/tests/src/poseidon/utils.rs +++ b/tests/src/poseidon/utils.rs @@ -47,12 +47,8 @@ pub async fn init_poseidon_test_state() -> Result { pub fn init_poseidon_test_statics() -> Result<()> { let artifacts_path = env::var(ARTIFACTS_PATH_ENV_VAR).unwrap(); - let poseidon_wrapper_address = get_contract_address_from_artifact( - &artifacts_path, - POSEIDON_WRAPPER_CONTRACT_NAME, - FieldElement::ZERO, /* salt */ - &[], - )?; + let poseidon_wrapper_address = + get_contract_address_from_artifact(&artifacts_path, POSEIDON_WRAPPER_CONTRACT_NAME, &[])?; if POSEIDON_WRAPPER_ADDRESS.get().is_none() { POSEIDON_WRAPPER_ADDRESS .set(poseidon_wrapper_address) @@ -70,12 +66,8 @@ pub async fn deploy_poseidon_wrapper( get_artifacts(artifacts_path, POSEIDON_WRAPPER_CONTRACT_NAME); let DeclareTransactionResult { class_hash, .. } = declare(poseidon_sierra_path, poseidon_casm_path, account).await?; - deploy(account, class_hash, &[], FieldElement::ZERO /* salt */).await?; - Ok(calculate_contract_address( - class_hash, - FieldElement::ZERO, /* salt */ - &[], - )) + deploy(account, class_hash, &[]).await?; + Ok(calculate_contract_address(class_hash, &[])) } // -------------------------------- diff --git a/tests/src/statement_serde/utils.rs b/tests/src/statement_serde/utils.rs index 4708ed9a..8515de53 100644 --- a/tests/src/statement_serde/utils.rs +++ b/tests/src/statement_serde/utils.rs @@ -78,7 +78,6 @@ pub fn init_statement_serde_test_statics() -> Result<()> { let statement_serde_wrapper_address = get_contract_address_from_artifact( &artifacts_path, STATEMENT_SERDE_WRAPPER_CONTRACT_NAME, - FieldElement::ZERO, /* salt */ &[], )?; if STATEMENT_SERDE_WRAPPER_ADDRESS.get().is_none() { @@ -129,12 +128,8 @@ async fn deploy_statement_serde_wrapper( ) .await?; - deploy(account, class_hash, &[], FieldElement::ZERO /* salt */).await?; - Ok(calculate_contract_address( - class_hash, - FieldElement::ZERO, /* salt */ - &[], - )) + deploy(account, class_hash, &[]).await?; + Ok(calculate_contract_address(class_hash, &[])) } // -------------------------------- diff --git a/tests/src/transcript/utils.rs b/tests/src/transcript/utils.rs index 79855e26..478b6d4a 100644 --- a/tests/src/transcript/utils.rs +++ b/tests/src/transcript/utils.rs @@ -70,7 +70,6 @@ pub fn init_transcript_test_statics() -> Result<()> { let transcript_wrapper_address = get_contract_address_from_artifact( &artifacts_path, TRANSCRIPT_WRAPPER_CONTRACT_NAME, - FieldElement::ZERO, /* salt */ &calldata, )?; @@ -100,18 +99,8 @@ pub async fn deploy_transcript_wrapper( declare(transcript_sierra_path, transcript_casm_path, account).await?; let calldata = get_transcript_wrapper_constructor_calldata()?; - deploy( - account, - class_hash, - &calldata, - FieldElement::ZERO, /* salt */ - ) - .await?; - Ok(calculate_contract_address( - class_hash, - FieldElement::ZERO, /* salt */ - &calldata, - )) + deploy(account, class_hash, &calldata).await?; + Ok(calculate_contract_address(class_hash, &calldata)) } // -------------------------------- diff --git a/tests/src/utils.rs b/tests/src/utils.rs index 6e8eb992..56a7904a 100644 --- a/tests/src/utils.rs +++ b/tests/src/utils.rs @@ -230,15 +230,10 @@ pub fn get_sierra_class_hash_from_artifact( pub fn get_contract_address_from_artifact( artifacts_path: &str, contract_name: &str, - salt: FieldElement, constructor_calldata: &[FieldElement], ) -> Result { let class_hash = get_sierra_class_hash_from_artifact(artifacts_path, contract_name)?; - Ok(calculate_contract_address( - class_hash, - salt, - constructor_calldata, - )) + Ok(calculate_contract_address(class_hash, constructor_calldata)) } pub async fn setup_sequencer(test_config: TestConfig) -> Result { @@ -318,6 +313,7 @@ fn init_test_statics(test_config: &TestConfig, sequencer: &TestSequencer) -> Res // -------------------------------- pub const GET_ROOT_FN_NAME: &str = "get_root"; +pub const CHECK_VERIFICATION_JOB_STATUS_FN_NAME: &str = "check_verification_job_status"; pub async fn call_contract( account: &ScriptAccount, @@ -364,6 +360,30 @@ pub async fn get_root(account: &ScriptAccount, contract_address: FieldElement) - .map(|r| felt_to_scalar(&r[0])) } +pub async fn check_verification_job_status( + account: &ScriptAccount, + contract_address: FieldElement, + verification_job_id: FieldElement, +) -> Result> { + call_contract( + account, + contract_address, + CHECK_VERIFICATION_JOB_STATUS_FN_NAME, + vec![verification_job_id], + ) + .await + .map(|r| { + // The Cairo corelib serializes an Option::None(()) as 1, + // and an Option::Some(x) as [0, ..serialize(x)]. + // In our case, x is a bool => serializes as a true = 1, false = 0. + if r[0] == FieldElement::ONE { + None + } else { + Some(r[1] == FieldElement::ONE) + } + }) +} + // ---------------- // | MISC HELPERS | // ---------------- diff --git a/tests/src/verifier/utils.rs b/tests/src/verifier/utils.rs index 687279c8..347825cc 100644 --- a/tests/src/verifier/utils.rs +++ b/tests/src/verifier/utils.rs @@ -9,21 +9,23 @@ use mpc_bulletproof::{ use mpc_stark::algebra::{scalar::Scalar, stark_curve::StarkPoint}; use once_cell::sync::OnceCell; use rand::thread_rng; -use starknet::core::types::FieldElement; +use starknet::core::types::{DeclareTransactionResult, FieldElement, InvokeTransactionResult}; use starknet_scripts::commands::utils::{ - deploy_verifier, initialize, ScriptAccount, VERIFIER_CONTRACT_NAME, + calculate_contract_address, declare, deploy, get_artifacts, ScriptAccount, + VERIFIER_CONTRACT_NAME, }; -use std::env; +use std::{env, iter}; use tracing::debug; use crate::utils::{ - call_contract, get_contract_address_from_artifact, global_setup, invoke_contract, - CalldataSerializable, CircuitParams, ARTIFACTS_PATH_ENV_VAR, TRANSCRIPT_SEED, + get_contract_address_from_artifact, global_setup, invoke_contract, CalldataSerializable, + CircuitParams, ARTIFACTS_PATH_ENV_VAR, TRANSCRIPT_SEED, }; pub const FUZZ_ROUNDS: usize = 1; -const CHECK_VERIFICATION_JOB_STATUS_FN_NAME: &str = "check_verification_job_status"; +const ADD_CIRCUIT_FN_NAME: &str = "add_circuit"; +const PARAMETERIZE_CIRCUIT_FN_NAME: &str = "parameterize_circuit"; const QUEUE_VERIFICATION_JOB_FN_NAME: &str = "queue_verification_job"; const STEP_VERIFICATION_FN_NAME: &str = "step_verification"; @@ -40,16 +42,11 @@ pub async fn init_verifier_test_state() -> Result { let account = sequencer.account(); debug!("Declaring & deploying verifier contract..."); - let (verifier_address, _, _) = deploy_verifier( - None, - FieldElement::ZERO, /* salt */ - &artifacts_path, - &account, - ) - .await?; + let (verifier_address, _, _) = declare_and_deploy_verifier(&artifacts_path, &account).await?; debug!("Initializing verifier contract..."); - initialize_verifier(&account, verifier_address).await?; + add_circuit(&account, verifier_address).await?; + parameterize_circuit(&account, verifier_address).await?; Ok(sequencer) } @@ -57,12 +54,8 @@ pub async fn init_verifier_test_state() -> Result { pub fn init_verifier_test_statics() -> Result<()> { let artifacts_path = env::var(ARTIFACTS_PATH_ENV_VAR).unwrap(); - let verifier_address = get_contract_address_from_artifact( - &artifacts_path, - VERIFIER_CONTRACT_NAME, - FieldElement::ZERO, /* salt */ - &[], - )?; + let verifier_address = + get_contract_address_from_artifact(&artifacts_path, VERIFIER_CONTRACT_NAME, &[])?; if VERIFIER_ADDRESS.get().is_none() { VERIFIER_ADDRESS.set(verifier_address).unwrap(); } @@ -70,44 +63,58 @@ pub fn init_verifier_test_statics() -> Result<()> { Ok(()) } +pub async fn declare_and_deploy_verifier( + artifacts_path: &str, + account: &ScriptAccount, +) -> Result<(FieldElement, FieldElement, FieldElement)> { + let (verifier_sierra_path, verifier_casm_path) = + get_artifacts(artifacts_path, VERIFIER_CONTRACT_NAME); + + let DeclareTransactionResult { + class_hash: verifier_class_hash, + .. + } = declare(verifier_sierra_path, verifier_casm_path, account).await?; + + let InvokeTransactionResult { + transaction_hash, .. + } = deploy(account, verifier_class_hash, &[]).await?; + + let verifier_address = calculate_contract_address(verifier_class_hash, &[]); + + Ok((verifier_address, verifier_class_hash, transaction_hash)) +} + // -------------------------------- // | CONTRACT INTERACTION HELPERS | // -------------------------------- -pub async fn initialize_verifier<'t, 'g>( - account: &ScriptAccount, - verifier_address: FieldElement, -) -> Result<()> { - initialize( +pub async fn add_circuit(account: &ScriptAccount, verifier_address: FieldElement) -> Result<()> { + invoke_contract( account, verifier_address, - get_dummy_circuit_params().to_calldata(), + ADD_CIRCUIT_FN_NAME, + vec![DUMMY_CIRCUIT_ID], ) .await .map(|_| ()) } -pub async fn check_verification_job_status( +pub async fn parameterize_circuit( account: &ScriptAccount, - verification_job_id: FieldElement, -) -> Result> { - call_contract( + verifier_address: FieldElement, +) -> Result<()> { + let calldata = iter::once(DUMMY_CIRCUIT_ID) + .chain(get_dummy_circuit_params().to_calldata()) + .collect(); + + invoke_contract( account, - *VERIFIER_ADDRESS.get().unwrap(), - CHECK_VERIFICATION_JOB_STATUS_FN_NAME, - vec![verification_job_id], + verifier_address, + PARAMETERIZE_CIRCUIT_FN_NAME, + calldata, ) .await - .map(|r| { - // The Cairo corelib serializes an Option::None(()) as 1, - // and an Option::Some(x) as [0, ..serialize(x)]. - // In our case, x is a bool => serializes as a true = 1, false = 0. - if r[0] == FieldElement::ONE { - None - } else { - Some(r[1] == FieldElement::ONE) - } - }) + .map(|_| ()) } pub async fn queue_verification_job( @@ -116,9 +123,8 @@ pub async fn queue_verification_job( witness_commitments: &Vec, verification_job_id: FieldElement, ) -> Result<()> { - let calldata = proof - .to_calldata() - .into_iter() + let calldata = iter::once(DUMMY_CIRCUIT_ID) + .chain(proof.to_calldata()) .chain(witness_commitments.to_calldata()) .chain(verification_job_id.to_calldata()) .collect(); @@ -141,7 +147,7 @@ pub async fn step_verification( account, *VERIFIER_ADDRESS.get().unwrap(), STEP_VERIFICATION_FN_NAME, - vec![verification_job_id], + vec![DUMMY_CIRCUIT_ID, verification_job_id], ) .await .map(|_| ()) @@ -184,6 +190,8 @@ pub const DUMMY_CIRCUIT_K: usize = 2; pub const DUMMY_CIRCUIT_M: usize = 4; pub const DUMMY_CIRCUIT_Q: usize = 8; +pub const DUMMY_CIRCUIT_ID: FieldElement = FieldElement::ONE; + pub fn singleprover_prove_dummy_circuit() -> Result<(R1CSProof, Vec)> { debug!("Generating proof for dummy circuit..."); let mut transcript = HashChainTranscript::new(TRANSCRIPT_SEED.as_bytes()); diff --git a/tests/src/verifier_utils/utils.rs b/tests/src/verifier_utils/utils.rs index 34afd15b..2c876852 100644 --- a/tests/src/verifier_utils/utils.rs +++ b/tests/src/verifier_utils/utils.rs @@ -70,7 +70,6 @@ pub fn init_verifier_utils_test_statics() -> Result<()> { let verifier_utils_wrapper_address = get_contract_address_from_artifact( &artifacts_path, VERIFIER_UTILS_WRAPPER_CONTRACT_NAME, - FieldElement::ZERO, /* salt */ &[], )?; if VERIFIER_UTILS_WRAPPER_ADDRESS.get().is_none() { @@ -95,12 +94,8 @@ pub async fn deploy_verifier_utils_wrapper( ) .await?; - deploy(account, class_hash, &[], FieldElement::ZERO /* salt */).await?; - Ok(calculate_contract_address( - class_hash, - FieldElement::ZERO, /* salt */ - &[], - )) + deploy(account, class_hash, &[]).await?; + Ok(calculate_contract_address(class_hash, &[])) } // -------------------------------- diff --git a/tests/tests/verifier.rs b/tests/tests/verifier.rs index 72d29c7d..b0b2cfbf 100644 --- a/tests/tests/verifier.rs +++ b/tests/tests/verifier.rs @@ -2,10 +2,12 @@ use eyre::Result; use mpc_stark::algebra::scalar::Scalar; use rand::thread_rng; use tests::{ - utils::{global_teardown, random_felt, setup_sequencer, TestConfig}, + utils::{ + check_verification_job_status, global_teardown, random_felt, setup_sequencer, TestConfig, + }, verifier::utils::{ - check_verification_job_status, queue_verification_job, singleprover_prove_dummy_circuit, - step_verification, FUZZ_ROUNDS, + queue_verification_job, singleprover_prove_dummy_circuit, step_verification, FUZZ_ROUNDS, + VERIFIER_ADDRESS, }, }; @@ -19,16 +21,24 @@ async fn test_full_verification_fuzz() -> Result<()> { let verification_job_id = random_felt(); queue_verification_job(&account, &proof, &witness_commitments, verification_job_id).await?; - while check_verification_job_status(&account, verification_job_id) - .await? - .is_none() + while check_verification_job_status( + &account, + *VERIFIER_ADDRESS.get().unwrap(), + verification_job_id, + ) + .await? + .is_none() { step_verification(&account, verification_job_id).await?; } - assert!(check_verification_job_status(&account, verification_job_id) - .await? - .unwrap()); + assert!(check_verification_job_status( + &account, + *VERIFIER_ADDRESS.get().unwrap(), + verification_job_id + ) + .await? + .unwrap()); } global_teardown(sequencer); @@ -46,18 +56,24 @@ async fn test_full_verification_invalid_proof() -> Result<()> { let verification_job_id = random_felt(); queue_verification_job(&account, &proof, &witness_commitments, verification_job_id).await?; - while check_verification_job_status(&account, verification_job_id) - .await? - .is_none() + while check_verification_job_status( + &account, + *VERIFIER_ADDRESS.get().unwrap(), + verification_job_id, + ) + .await? + .is_none() { step_verification(&account, verification_job_id).await?; } - assert!( - !check_verification_job_status(&account, verification_job_id) - .await? - .unwrap() - ); + assert!(!check_verification_job_status( + &account, + *VERIFIER_ADDRESS.get().unwrap(), + verification_job_id + ) + .await? + .unwrap()); global_teardown(sequencer); Ok(())