Skip to content

Commit

Permalink
tests: darkpool: testing multi-circuit verification
Browse files Browse the repository at this point in the history
  • Loading branch information
akirillo committed Aug 25, 2023
1 parent 9e98439 commit d1f76ee
Show file tree
Hide file tree
Showing 18 changed files with 1,221 additions and 875 deletions.
340 changes: 90 additions & 250 deletions Cargo.lock

Large diffs are not rendered by default.

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

[patch."https://github.com/starkware-libs/blockifier"]
blockifier = { git = "https://github.com/dojoengine/blockifier", rev = "c794d1b" }
blockifier = { git = "https://github.com/renegade-fi/blockifier.git", rev = "9c9bbe1" }

[patch.crates-io]
cairo-felt = { git = "https://github.com/dojoengine/cairo-rs.git", rev = "262b7eb4b11ab165a2a936a5f914e78aa732d4a2" }
Expand All @@ -34,3 +34,4 @@ cairo-lang-sierra-to-casm = { git = "https://github.com/starkware-libs/cairo.git
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", tag = "v2.1.1" }
cairo-lang-syntax = { git = "https://github.com/starkware-libs/cairo.git", tag = "v2.1.1" }
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo.git", tag = "v2.1.1" }
cairo-lang-sierra-type-size = { git = "https://github.com/starkware-libs/cairo.git", tag = "v2.1.1" }
409 changes: 210 additions & 199 deletions src/darkpool.cairo

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/verifier.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ mod Verifier {
// | CONSTANTS |
// -------------

const STEP_UNIT_GAS: u128 = 1000000;
/// Determines how many MSM points are processed in each invocation
/// of `step_verification`.
// TODO: The current value (50) was chosen arbitrarily, we should benchmark
// the optimal amount given Starknet parameters.
const MSM_CHUNK_SIZE: usize = 50;

// -----------
// | STORAGE |
Expand Down Expand Up @@ -302,11 +306,6 @@ mod Verifier {
// | HELPERS |
// -----------

fn step_cost_bound(self: @ContractState) -> u128 {
let multiplier = self.q.read().into() * self.n.read().into();
multiplier * STEP_UNIT_GAS
}

fn prep_rem_gens(n_plus: usize) -> (RemainingGenerators, RemainingGenerators) {
(
RemainingGeneratorsTrait::new(G_LABEL, n_plus),
Expand Down Expand Up @@ -499,9 +498,9 @@ mod Verifier {

fn step_verification_inner(ref self: ContractState, ref verification_job: VerificationJob) {
let mut verified = Option::None(());
let mut i = 0;
loop {
// Loop until we run out of gas or finish the job
if testing::get_available_gas() < step_cost_bound(@self) {
if i == MSM_CHUNK_SIZE {
break;
}

Expand All @@ -512,6 +511,8 @@ mod Verifier {
verified = Option::Some(verification_job.msm_result.unwrap() == ec_point_zero());
break;
};

i += 1;
};
verification_job.verified = verified;
}
Expand Down
10 changes: 7 additions & 3 deletions starknet_scripts/src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ pub async fn deploy_and_initialize(args: DeployArgs) -> Result<()> {
} else {
format!("{verifier_class_hash_felt:#64x}")
};
let (verifier_address, _, _) =
deploy_verifier(Some(verifier_class_hash_hex), &artifacts_path, &account)
.await?;
let (verifier_address, _, _) = deploy_verifier(
Some(verifier_class_hash_hex),
FieldElement::ZERO, /* salt */
&artifacts_path,
&account,
)
.await?;

// Initialize darkpool
debug!("Initializing darkpool contract...");
Expand Down
57 changes: 43 additions & 14 deletions starknet_scripts/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,11 @@ pub async fn deploy(
account: &ScriptAccount,
class_hash: FieldElement,
calldata: &[FieldElement],
salt: FieldElement,
) -> Result<InvokeTransactionResult> {
let contract_factory = ContractFactory::new(class_hash, account);
let deploy_result = contract_factory
.deploy(
calldata,
FieldElement::ZERO, /* salt */
false, /* unique */
)
.deploy(calldata, salt, false /* unique */)
.send()
.await?;

Expand Down Expand Up @@ -209,12 +206,13 @@ 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 */
FieldElement::ZERO, /* salt */
salt,
class_hash,
compute_hash_on_elements(constructor_calldata),
]) % ADDR_BOUND
Expand Down Expand Up @@ -280,9 +278,19 @@ pub async fn deploy_darkpool(
let calldata = vec![account.address()];
let InvokeTransactionResult {
transaction_hash, ..
} = deploy(account, darkpool_class_hash_felt, &calldata).await?;
} = deploy(
account,
darkpool_class_hash_felt,
&calldata,
FieldElement::ZERO, /* salt */
)
.await?;

let darkpool_address = calculate_contract_address(darkpool_class_hash_felt, &calldata);
let darkpool_address = calculate_contract_address(
darkpool_class_hash_felt,
FieldElement::ZERO, /* salt */
&calldata,
);

Ok((
darkpool_address,
Expand Down Expand Up @@ -313,9 +321,19 @@ pub async fn deploy_merkle(
debug!("Deploying merkle contract...");
let InvokeTransactionResult {
transaction_hash, ..
} = deploy(account, merkle_class_hash_felt, &[]).await?;
} = deploy(
account,
merkle_class_hash_felt,
&[],
FieldElement::ZERO, /* salt */
)
.await?;

let merkle_address = calculate_contract_address(merkle_class_hash_felt, &[]);
let merkle_address = calculate_contract_address(
merkle_class_hash_felt,
FieldElement::ZERO, /* salt */
&[],
);

Ok((merkle_address, merkle_class_hash_felt, transaction_hash))
}
Expand All @@ -339,9 +357,19 @@ pub async fn deploy_nullifier_set(
debug!("Deploying nullifier set contract...");
let InvokeTransactionResult {
transaction_hash, ..
} = deploy(account, nullifier_set_class_hash_felt, &[]).await?;
} = deploy(
account,
nullifier_set_class_hash_felt,
&[],
FieldElement::ZERO, /* salt */
)
.await?;

let nullifier_set_address = calculate_contract_address(nullifier_set_class_hash_felt, &[]);
let nullifier_set_address = calculate_contract_address(
nullifier_set_class_hash_felt,
FieldElement::ZERO, /* salt */
&[],
);

Ok((
nullifier_set_address,
Expand All @@ -352,6 +380,7 @@ pub async fn deploy_nullifier_set(

pub async fn deploy_verifier(
verifier_class_hash: Option<String>,
salt: FieldElement,
artifacts_path: &str,
account: &ScriptAccount,
) -> Result<(FieldElement, FieldElement, FieldElement)> {
Expand All @@ -369,9 +398,9 @@ pub async fn deploy_verifier(
debug!("Deploying verifier contract...");
let InvokeTransactionResult {
transaction_hash, ..
} = deploy(account, verifier_class_hash_felt, &[]).await?;
} = deploy(account, verifier_class_hash_felt, &[], salt).await?;

let verifier_address = calculate_contract_address(verifier_class_hash_felt, &[]);
let verifier_address = calculate_contract_address(verifier_class_hash_felt, salt, &[]);

Ok((verifier_address, verifier_class_hash_felt, transaction_hash))
}
Loading

0 comments on commit d1f76ee

Please sign in to comment.