Skip to content

Commit

Permalink
wip: profiling circuit parameterization & new_wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
akirillo committed Sep 8, 2023
1 parent 50751f2 commit 9048403
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 164 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/cairo_project.toml
/.vscode
/.gitattributes
/*.log
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ starknet = "0.5.0"
mpc-stark = "0.2"

[patch."https://github.com/starkware-libs/blockifier"]
# blockifier = { git = "https://github.com/renegade-fi/blockifier.git", rev = "9c9bbe1" }
blockifier = { path = "../blockifier/crates/blockifier" }
blockifier = { git = "https://github.com/renegade-fi/blockifier.git", branch = "renegade-testing" }

[patch.crates-io]
cairo-felt = { git = "https://github.com/dojoengine/cairo-rs.git", rev = "262b7eb4b11ab165a2a936a5f914e78aa732d4a2" }
Expand Down
2 changes: 1 addition & 1 deletion src/verifier.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ mod MultiVerifier {
// Assert weight matrix has `q` rows
assert(w_v.len() == self.q.read(circuit_id), 'W_V has wrong number of rows');
// Assert weight matrix has correct max number of columns
w_v.assert_width(self.n.read(circuit_id));
w_v.assert_width(self.m.read(circuit_id));

self.W_V.write(circuit_id, StoreSerdeWrapper { inner: w_v });

Expand Down
4 changes: 2 additions & 2 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ num-bigint = { version = "0.4.3", features = ["rand"] }
byteorder = "1.4.3"

starknet = { workspace = true }
katana-core = { git = "https://github.com/dojoengine/dojo.git", rev = "954dbb3" }
dojo-test-utils = { git = "https://github.com/dojoengine/dojo.git", rev = "954dbb3" }
katana-core = { git = "https://github.com/renegade-fi/dojo.git", branch = "renegade-testing" }
dojo-test-utils = { git = "https://github.com/renegade-fi/dojo.git", branch = "renegade-testing" }
mpc-stark = { workspace = true }
mpc-bulletproof = { git = "https://github.com/renegade-fi/mpc-bulletproof.git", features = ["integration_test"] }
merlin = { git = "https://github.com/renegade-fi/merlin.git" }
Expand Down
1 change: 1 addition & 0 deletions tests/src/darkpool/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ pub async fn is_nullifier_available(account: &ScriptAccount, nullifier: Scalar)
}

pub async fn new_wallet(account: &ScriptAccount, args: &NewWalletArgs) -> Result<FieldElement> {
debug!("Running new_wallet until {:?}", args.breakpoint);
let calldata = args.to_calldata();

invoke_contract(
Expand Down
110 changes: 25 additions & 85 deletions tests/src/profiling/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ use circuits::zk_circuits::{
};
use dojo_test_utils::sequencer::TestSequencer;
use eyre::{eyre, Result};
use lazy_static::lazy_static;
use merlin::HashChainTranscript;
use mpc_bulletproof::{
r1cs::{Prover, R1CSProof, Verifier},
r1cs::{R1CSProof, Verifier},
BulletproofGens, PedersenGens,
};
use mpc_stark::algebra::scalar::Scalar;
Expand All @@ -44,10 +43,9 @@ use crate::{
darkpool::utils::{initialize_darkpool, DARKPOOL_ADDRESS},
merkle::utils::TEST_MERKLE_HEIGHT,
utils::{
fully_parameterize_circuit, get_circuit_params, get_contract_address_from_artifact,
global_setup, random_felt, Breakpoint, CalldataSerializable, Circuit, MatchPayload,
NewWalletArgs, ProcessMatchArgs, UpdateWalletArgs, ARTIFACTS_PATH_ENV_VAR, SK_ROOT,
TRANSCRIPT_SEED,
get_contract_address_from_artifact, global_setup, random_felt, singleprover_prove,
Breakpoint, CalldataSerializable, Circuit, MatchPayload, NewWalletArgs, ProcessMatchArgs,
UpdateWalletArgs, ARTIFACTS_PATH_ENV_VAR, SK_ROOT, TRANSCRIPT_SEED,
},
};

Expand All @@ -58,25 +56,14 @@ pub type SizedValidCommitments = ValidCommitments<MAX_BALANCES, MAX_ORDERS, MAX_
pub type SizedValidReblind = ValidReblind<MAX_BALANCES, MAX_ORDERS, MAX_FEES, TEST_MERKLE_HEIGHT>;
pub type SizedValidSettle = ValidSettle<MAX_BALANCES, MAX_ORDERS, MAX_FEES>;

// Mirrors pre-allocated BP generators from relayer repo
lazy_static! {
/// The maximum number of generators that may be needed for any of the circuits
/// so that the generators may be pre-allocated and re-used between proofs
static ref MAX_GENERATORS: usize = vec![
SizedValidWalletCreate::BP_GENS_CAPACITY,
SizedValidWalletUpdate::BP_GENS_CAPACITY,
SizedValidReblind::BP_GENS_CAPACITY,
SizedValidCommitments::BP_GENS_CAPACITY,
ValidMatchMpcSingleProver::BP_GENS_CAPACITY,
SizedValidSettle::BP_GENS_CAPACITY,
]
.into_iter()
.max()
.unwrap();

/// The pre-allocated bulletproof generators for the circuits
static ref PRE_ALLOCATED_GENS: BulletproofGens = BulletproofGens::new(*MAX_GENERATORS, 1 /* party_capacity */);
}
pub type TestParamsCircuit = Circuit<
SizedValidWalletCreate,
SizedValidWalletUpdate,
SizedValidCommitments,
SizedValidReblind,
ValidMatchMpcSingleProver,
SizedValidSettle,
>;

// ---------------------
// | META TEST HELPERS |
Expand Down Expand Up @@ -116,36 +103,6 @@ pub async fn init_profiling_test_state() -> Result<TestSequencer> {
)
.await?;

for circuit in [
Circuit::ValidWalletCreate(SizedValidWalletCreate {}),
Circuit::ValidWalletUpdate(SizedValidWalletUpdate {}),
Circuit::ValidCommitments(SizedValidCommitments {}),
Circuit::ValidReblind(SizedValidReblind {}),
Circuit::ValidMatchMpc(ValidMatchMpcSingleProver {}),
Circuit::ValidSettle(SizedValidSettle {}),
]
.into_iter()
{
debug!("Parameterizing circuit {circuit}");

let circuit_params = match circuit {
Circuit::ValidWalletCreate(_) => get_circuit_params::<SizedValidWalletCreate>(),
Circuit::ValidWalletUpdate(_) => get_circuit_params::<SizedValidWalletUpdate>(),
Circuit::ValidCommitments(_) => get_circuit_params::<SizedValidCommitments>(),
Circuit::ValidReblind(_) => get_circuit_params::<SizedValidReblind>(),
Circuit::ValidMatchMpc(_) => get_circuit_params::<ValidMatchMpcSingleProver>(),
Circuit::ValidSettle(_) => get_circuit_params::<SizedValidSettle>(),
};

fully_parameterize_circuit(
&account,
darkpool_address,
circuit.to_calldata()[0],
circuit_params,
)
.await?;
}

Ok(sequencer)
}

Expand Down Expand Up @@ -179,21 +136,8 @@ pub fn init_profiling_test_statics(account: &ScriptAccount) -> Result<()> {
// | MISC HELPERS |
// ----------------

/// Mirrors `singleprover_prove` from the relayer repo, but uses our pre-allocated BP gens
pub fn singleprover_prove_prealloc<C: SingleProverCircuit>(
witness: C::Witness,
statement: C::Statement,
) -> Result<(<C::Witness as CircuitBaseType>::CommitmentType, R1CSProof)> {
let mut transcript = HashChainTranscript::new(TRANSCRIPT_SEED.as_bytes());
let pc_gens = PedersenGens::default();
let prover = Prover::new(&pc_gens, &mut transcript);

C::prove(witness, statement, &PRE_ALLOCATED_GENS, prover)
.map_err(|e| eyre!("Error proving circuit: {}", e))
}

/// Mirrors `verify_singleprover_proof` from the relayer repo, but uses our pre-allocated BP gens
pub fn verify_singleprover_proof_prealloc<C: SingleProverCircuit>(
pub fn verify_singleprover_proof<C: SingleProverCircuit>(
statement: C::Statement,
witness_commitment: <C::Witness as CircuitBaseType>::CommitmentType,
proof: R1CSProof,
Expand All @@ -203,24 +147,20 @@ pub fn verify_singleprover_proof_prealloc<C: SingleProverCircuit>(
let pc_gens = PedersenGens::default();
let verifier = Verifier::new(&pc_gens, &mut verifier_transcript);

C::verify(
witness_commitment,
statement,
proof,
&PRE_ALLOCATED_GENS,
verifier,
)
.map_err(|e| eyre!("Error verifying proof: {}", e))
let bp_gens = BulletproofGens::new(C::BP_GENS_CAPACITY, 1);

C::verify(witness_commitment, statement, proof, &bp_gens, verifier)
.map_err(|e| eyre!("Error verifying proof: {}", e))
}

pub fn get_new_wallet_args(breakpoint: Breakpoint) -> Result<NewWalletArgs> {
debug!("Generating new_wallet args...");
let (witness, statement) = valid_wallet_create_witness_statement();
let wallet_blinder_share = statement.public_wallet_shares.blinder;
let (witness_commitment, proof) =
singleprover_prove_prealloc::<SizedValidWalletCreate>(witness, statement.clone())?;
singleprover_prove::<SizedValidWalletCreate>(witness, statement.clone())?;

verify_singleprover_proof_prealloc::<SizedValidWalletCreate>(
verify_singleprover_proof::<SizedValidWalletCreate>(
statement.clone(),
witness_commitment.clone(),
proof.clone(),
Expand Down Expand Up @@ -258,9 +198,9 @@ pub fn get_update_wallet_args(
let statement_signature = sign_scalar_message(&statement.to_scalars(), &SK_ROOT);

let (witness_commitment, proof) =
singleprover_prove_prealloc::<SizedValidWalletUpdate>(witness, statement.clone())?;
singleprover_prove::<SizedValidWalletUpdate>(witness, statement.clone())?;

verify_singleprover_proof_prealloc::<SizedValidWalletUpdate>(
verify_singleprover_proof::<SizedValidWalletUpdate>(
statement.clone(),
witness_commitment.clone(),
proof.clone(),
Expand Down Expand Up @@ -296,9 +236,9 @@ pub async fn get_process_match_args(
.await
.0?;
let (valid_match_mpc_witness_commitment, valid_match_mpc_proof) =
singleprover_prove_prealloc::<ValidMatchMpcSingleProver>(valid_match_mpc_witness, ())?;
singleprover_prove::<ValidMatchMpcSingleProver>(valid_match_mpc_witness, ())?;

verify_singleprover_proof_prealloc::<ValidMatchMpcSingleProver>(
verify_singleprover_proof::<ValidMatchMpcSingleProver>(
(),
valid_match_mpc_witness_commitment.clone(),
valid_match_mpc_proof.clone(),
Expand All @@ -307,12 +247,12 @@ pub async fn get_process_match_args(
let (valid_settle_witness, valid_settle_statement) =
valid_settle_witness_statement(party0_wallet.clone(), party1_wallet.clone(), match_res);
let (valid_settle_witness_commitment, valid_settle_proof) =
singleprover_prove_prealloc::<SizedValidSettle>(
singleprover_prove::<SizedValidSettle>(
valid_settle_witness,
valid_settle_statement.clone(),
)?;

verify_singleprover_proof_prealloc::<SizedValidSettle>(
verify_singleprover_proof::<SizedValidSettle>(
valid_settle_statement.clone(),
valid_settle_witness_commitment.clone(),
valid_settle_proof.clone(),
Expand Down
27 changes: 19 additions & 8 deletions tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ use crate::{
nullifier_set::utils::{init_nullifier_set_test_state, init_nullifier_set_test_statics},
poseidon::utils::{init_poseidon_test_state, init_poseidon_test_statics},
profiling::utils::{
init_profiling_test_state, init_profiling_test_statics, singleprover_prove_prealloc,
verify_singleprover_proof_prealloc, SizedValidCommitments, SizedValidReblind,
init_profiling_test_state, init_profiling_test_statics, verify_singleprover_proof,
SizedValidCommitments, SizedValidReblind,
},
statement_serde::utils::{init_statement_serde_test_state, init_statement_serde_test_statics},
transcript::utils::{init_transcript_test_state, init_transcript_test_statics},
Expand Down Expand Up @@ -188,7 +188,7 @@ pub async fn global_setup(init_state: Option<SerializableState>) -> TestSequence
TRACING_INIT.call_once(|| {
fmt()
.with_env_filter(EnvFilter::from_default_env())
// .with_ansi(false)
.with_ansi(false)
.init();
});

Expand All @@ -201,7 +201,16 @@ pub async fn global_setup(init_state: Option<SerializableState>) -> TestSequence
.await
}

pub fn global_teardown(sequencer: TestSequencer) {
pub async fn global_teardown(test_config: TestConfig, sequencer: TestSequencer, force_dump: bool) {
if force_dump {
let (state_dumped_lock, state_separator) = get_state_lock_and_separator(&test_config);
let mut state_dumped = state_dumped_lock.lock().await;
// Dump the state
debug!("Dumping state...");
dump_state(&sequencer, state_separator).await.unwrap();
// Mark the state as dumped
*state_dumped = true;
}
debug!("Stopping test sequencer...");
sequencer.stop().unwrap();
}
Expand Down Expand Up @@ -359,6 +368,7 @@ pub async fn invoke_contract(
selector: get_selector_from_name(entry_point)?,
calldata,
}])
.max_fee(FieldElement::ZERO)
.send()
.await
.map_err(|e| eyre!("Error invoking {}: {}", entry_point, e))
Expand Down Expand Up @@ -556,24 +566,24 @@ impl MatchPayload {
valid_reblind_statement.merkle_root = merkle_root;

let (valid_commitments_witness_commitment, valid_commitments_proof) =
singleprover_prove_prealloc::<SizedValidCommitments>(
singleprover_prove::<SizedValidCommitments>(
valid_commitments_witness,
valid_commitments_statement.clone(),
)?;

verify_singleprover_proof_prealloc::<SizedValidCommitments>(
verify_singleprover_proof::<SizedValidCommitments>(
valid_commitments_statement.clone(),
valid_commitments_witness_commitment.clone(),
valid_commitments_proof.clone(),
)?;

let (valid_reblind_witness_commitment, valid_reblind_proof) =
singleprover_prove_prealloc::<SizedValidReblind>(
singleprover_prove::<SizedValidReblind>(
valid_reblind_witness,
valid_reblind_statement.clone(),
)?;

verify_singleprover_proof_prealloc::<SizedValidReblind>(
verify_singleprover_proof::<SizedValidReblind>(
valid_reblind_statement.clone(),
valid_reblind_witness_commitment.clone(),
valid_reblind_proof.clone(),
Expand Down Expand Up @@ -689,6 +699,7 @@ pub struct ProcessMatchArgs {
pub breakpoint: Breakpoint,
}

#[derive(Debug)]
pub enum Breakpoint {
None,
ReadCircuitParams,
Expand Down
Loading

0 comments on commit 9048403

Please sign in to comment.