Skip to content

Commit

Permalink
renegade_contracts: darkpool: align parameterize_circuit & add_circui…
Browse files Browse the repository at this point in the history
…t functions
  • Loading branch information
akirillo committed Sep 1, 2023
1 parent fdd75be commit 207498b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 87 deletions.
19 changes: 14 additions & 5 deletions src/darkpool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ trait IDarkpool<TContractState> {
verifier_class_hash: ClassHash,
height: u8,
);
fn parameterize_verifier(
fn add_circuit(ref self: TContractState, circuit: Circuit);
fn parameterize_circuit(
ref self: TContractState, circuit: Circuit, circuit_params: CircuitParams,
);
// OZ
Expand Down Expand Up @@ -321,16 +322,24 @@ mod Darkpool {
_get_merkle_tree(@self).initialize(height);
}

/// Parameterizes the verifier for a given circuit
/// Adds a circuit to the verifier
/// Parameters:
/// - `circuit`: The circuit for which to initialize the verifier contract
/// - `circuit`: The circuit to add
fn add_circuit(ref self: ContractState, circuit: Circuit) {
ownable__assert_only_owner(@self);
_get_verifier(@self).add_circuit(circuit.into());
}

/// Parameterizes the given circuit within the verifier
/// Parameters:
/// - `circuit`: The circuit to parameterize
/// - `circuit_params`: The parameters of the circuit
fn parameterize_verifier(
fn parameterize_circuit(
ref self: ContractState, circuit: Circuit, circuit_params: CircuitParams
) {
ownable__assert_only_owner(@self);

// Initialize the verifier
// Parameterize the circuit
_get_verifier(@self).parameterize_circuit(circuit.into(), circuit_params);
}

Expand Down
12 changes: 6 additions & 6 deletions src/testing/tests/darkpool_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ fn initialize_darkpool(ref darkpool: IDarkpoolDispatcher, ) {
TEST_MERKLE_HEIGHT,
);

darkpool.parameterize_verifier(Circuit::ValidWalletCreate(()), get_dummy_circuit_params());
darkpool.parameterize_verifier(Circuit::ValidWalletUpdate(()), get_dummy_circuit_params());
darkpool.parameterize_verifier(Circuit::ValidCommitments(()), get_dummy_circuit_params());
darkpool.parameterize_verifier(Circuit::ValidReblind(()), get_dummy_circuit_params());
darkpool.parameterize_verifier(Circuit::ValidMatchMpc(()), get_dummy_circuit_params());
darkpool.parameterize_verifier(Circuit::ValidSettle(()), get_dummy_circuit_params());
darkpool.parameterize_circuit(Circuit::ValidWalletCreate(()), get_dummy_circuit_params());
darkpool.parameterize_circuit(Circuit::ValidWalletUpdate(()), get_dummy_circuit_params());
darkpool.parameterize_circuit(Circuit::ValidCommitments(()), get_dummy_circuit_params());
darkpool.parameterize_circuit(Circuit::ValidReblind(()), get_dummy_circuit_params());
darkpool.parameterize_circuit(Circuit::ValidMatchMpc(()), get_dummy_circuit_params());
darkpool.parameterize_circuit(Circuit::ValidSettle(()), get_dummy_circuit_params());
}

fn assert_not_verified(ref darkpool: IDarkpoolDispatcher, verification_job_id: felt252) {
Expand Down
92 changes: 38 additions & 54 deletions tests/src/darkpool/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ use crate::{
utils::{
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,
get_sierra_class_hash_from_artifact, global_setup, invoke_contract, parameterize_circuit,
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,
Expand All @@ -58,7 +58,6 @@ const DUMMY_UPGRADE_TARGET_CONTRACT_NAME: &str = "renegade_contracts_DummyUpgrad
pub const INIT_BALANCE: u64 = 1000;
pub const TRANSFER_AMOUNT: u64 = 100;

const PARAMETERIZE_VERIFIER_FN_NAME: &str = "parameterize_verifier";
const GET_WALLET_BLINDER_TRANSACTION_FN_NAME: &str = "get_wallet_blinder_transaction";
const IS_NULLIFIER_AVAILABLE_FN_NAME: &str = "is_nullifier_available";
const NEW_WALLET_FN_NAME: &str = "new_wallet";
Expand Down Expand Up @@ -130,7 +129,41 @@ pub async fn init_darkpool_test_state() -> Result<TestSequencer> {
]
.into_iter()
{
parameterize_verifier(&account, circuit, darkpool_address).await?;
let mut statement_scalars = get_dummy_statement_scalars(circuit).into_iter();

let circuit_params = match circuit {
Circuit::ValidWalletCreate(_) => get_circuit_params::<DummyValidWalletCreate>(
(),
SizedValidWalletCreateStatement::from_scalars(&mut statement_scalars),
),
Circuit::ValidWalletUpdate(_) => get_circuit_params::<DummyValidWalletUpdate>(
(),
SizedValidWalletUpdateStatement::from_scalars(&mut statement_scalars),
),
Circuit::ValidCommitments(_) => get_circuit_params::<DummyValidCommitments>(
(),
ValidCommitmentsStatement::from_scalars(&mut statement_scalars),
),
Circuit::ValidReblind(_) => get_circuit_params::<DummyValidReblind>(
(),
ValidReblindStatement::from_scalars(&mut statement_scalars),
),
Circuit::ValidMatchMpc(_) => {
get_circuit_params::<DummyValidMatchMpc>(Scalar::from(DUMMY_VALUE), ())
}
Circuit::ValidSettle(_) => get_circuit_params::<DummyValidSettle>(
(),
SizedValidSettleStatement::from_scalars(&mut statement_scalars),
),
};

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

debug!("Declaring & deploying dummy ERC20 contract...");
Expand Down Expand Up @@ -267,55 +300,6 @@ pub async fn initialize_darkpool(
.map(|_| ())
}

pub async fn parameterize_verifier(
account: &ScriptAccount,
circuit: Circuit,
darkpool_address: FieldElement,
) -> Result<()> {
let mut statement_scalars = get_dummy_statement_scalars(circuit).into_iter();

let circuit_params = match circuit {
Circuit::ValidWalletCreate(_) => get_circuit_params::<DummyValidWalletCreate>(
(),
SizedValidWalletCreateStatement::from_scalars(&mut statement_scalars),
),
Circuit::ValidWalletUpdate(_) => get_circuit_params::<DummyValidWalletUpdate>(
(),
SizedValidWalletUpdateStatement::from_scalars(&mut statement_scalars),
),
Circuit::ValidCommitments(_) => get_circuit_params::<DummyValidCommitments>(
(),
ValidCommitmentsStatement::from_scalars(&mut statement_scalars),
),
Circuit::ValidReblind(_) => get_circuit_params::<DummyValidReblind>(
(),
ValidReblindStatement::from_scalars(&mut statement_scalars),
),
Circuit::ValidMatchMpc(_) => {
get_circuit_params::<DummyValidMatchMpc>(Scalar::from(DUMMY_VALUE), ())
}
Circuit::ValidSettle(_) => get_circuit_params::<DummyValidSettle>(
(),
SizedValidSettleStatement::from_scalars(&mut statement_scalars),
),
};

let calldata = circuit
.to_calldata()
.into_iter()
.chain(circuit_params.to_calldata())
.collect();

invoke_contract(
account,
darkpool_address,
PARAMETERIZE_VERIFIER_FN_NAME,
calldata,
)
.await
.map(|_| ())
}

pub async fn get_wallet_blinder_transaction(
account: &ScriptAccount,
wallet_blinder_share: Scalar,
Expand Down
21 changes: 21 additions & 0 deletions tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ fn init_test_statics(test_config: &TestConfig, sequencer: &TestSequencer) -> Res
// | CONTRACT INTERACTION HELPERS |
// --------------------------------

pub const PARAMETERIZE_CIRCUIT_FN_NAME: &str = "parameterize_circuit";
pub const GET_ROOT_FN_NAME: &str = "get_root";
pub const CHECK_VERIFICATION_JOB_STATUS_FN_NAME: &str = "check_verification_job_status";

Expand Down Expand Up @@ -384,6 +385,26 @@ pub async fn check_verification_job_status(
})
}

pub async fn parameterize_circuit(
account: &ScriptAccount,
contract_address: FieldElement,
circuit_id: FieldElement,
circuit_params: CircuitParams,
) -> Result<()> {
let calldata = iter::once(circuit_id)
.chain(circuit_params.to_calldata())
.collect();

invoke_contract(
account,
contract_address,
PARAMETERIZE_CIRCUIT_FN_NAME,
calldata,
)
.await
.map(|_| ())
}

// ----------------
// | MISC HELPERS |
// ----------------
Expand Down
31 changes: 9 additions & 22 deletions tests/src/verifier/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ use std::{env, iter};
use tracing::debug;

use crate::utils::{
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, parameterize_circuit,
CalldataSerializable, CircuitParams, ARTIFACTS_PATH_ENV_VAR, TRANSCRIPT_SEED,
};

pub const FUZZ_ROUNDS: usize = 1;

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";

Expand All @@ -46,7 +45,13 @@ pub async fn init_verifier_test_state() -> Result<TestSequencer> {

debug!("Initializing verifier contract...");
add_circuit(&account, verifier_address).await?;
parameterize_circuit(&account, verifier_address).await?;
parameterize_circuit(
&account,
verifier_address,
DUMMY_CIRCUIT_ID,
get_dummy_circuit_params(),
)
.await?;

Ok(sequencer)
}
Expand Down Expand Up @@ -99,24 +104,6 @@ pub async fn add_circuit(account: &ScriptAccount, verifier_address: FieldElement
.map(|_| ())
}

pub async fn parameterize_circuit(
account: &ScriptAccount,
verifier_address: FieldElement,
) -> Result<()> {
let calldata = iter::once(DUMMY_CIRCUIT_ID)
.chain(get_dummy_circuit_params().to_calldata())
.collect();

invoke_contract(
account,
verifier_address,
PARAMETERIZE_CIRCUIT_FN_NAME,
calldata,
)
.await
.map(|_| ())
}

pub async fn queue_verification_job(
account: &ScriptAccount,
proof: &R1CSProof,
Expand Down

0 comments on commit 207498b

Please sign in to comment.