Skip to content

Commit

Permalink
circuits: zk-circuits: valid-match-settle: Merge match and settle
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Nov 22, 2023
1 parent 9229aad commit d4ee06c
Show file tree
Hide file tree
Showing 7 changed files with 576 additions and 111 deletions.
3 changes: 2 additions & 1 deletion circuit-macros/src/circuit_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ pub(crate) fn circuit_type_impl(target_struct: ItemStruct, macro_args: MacroArgs

// Build secret share types
if macro_args.build_secret_share_types {
let secret_share_type_tokens = build_secret_share_types(&target_struct, macro_args.serde);
let secret_share_type_tokens =
build_secret_share_types(&target_struct, macro_args.build_mpc_types, macro_args.serde);
out_tokens.extend(secret_share_type_tokens);
}

Expand Down
16 changes: 12 additions & 4 deletions circuit-macros/src/circuit_type/secret_share_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::circuit_type::{
};

use super::{
build_base_type_impl, build_serde_methods, ident_with_generics,
build_base_type_impl, build_serde_methods, ident_with_generics, mpc_types::build_mpc_types,
multiprover_circuit_types::VAR_SUFFIX, singleprover_circuit_types::build_circuit_types,
str_to_path, FROM_SCALARS_METHOD_NAME, SCALAR_TYPE_IDENT, TO_SCALARS_METHOD_NAME,
};
Expand All @@ -30,10 +30,10 @@ const SECRET_SHARE_VAR_TRAIT_NAME: &str = "SecretShareVarType";
const SHARE_SUFFIX: &str = "Share";

/// Build the secret share types for the base type
pub fn build_secret_share_types(base_type: &ItemStruct, serde: bool) -> TokenStream2 {
pub fn build_secret_share_types(base_type: &ItemStruct, mpc: bool, serde: bool) -> TokenStream2 {
// Implement `SecretShareBaseType`
let mut res = build_secret_share_base_type_impl(base_type);
res.extend(build_secret_share_type(base_type, serde));
res.extend(build_secret_share_type(base_type, mpc, serde));

res
}
Expand Down Expand Up @@ -63,7 +63,7 @@ fn build_secret_share_base_type_impl(base_type: &ItemStruct) -> TokenStream2 {
}

/// Build the secret share type
fn build_secret_share_type(base_type: &ItemStruct, serde: bool) -> TokenStream2 {
fn build_secret_share_type(base_type: &ItemStruct, mpc: bool, serde: bool) -> TokenStream2 {
// Build the derived struct
let new_name = ident_with_suffix(&base_type.ident.to_string(), SHARE_SUFFIX);
let derive: Attribute = parse_quote!(#[derive(Clone, Debug, Eq, PartialEq)]);
Expand Down Expand Up @@ -93,6 +93,14 @@ fn build_secret_share_type(base_type: &ItemStruct, serde: bool) -> TokenStream2
res.extend(build_circuit_types(&secret_share_type));
res.extend(build_share_var_impl(&secret_share_type));

// Build MPC and multiprover base types
if mpc {
res.extend(build_mpc_types(
&secret_share_type,
true, // multiprover
));
}

res.extend(secret_share_type.to_token_stream());

// Implement serialization
Expand Down
2 changes: 1 addition & 1 deletion circuit-types/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ pub trait MultiProverCircuit {
// Allocate the witness and statement in the constraint system
let mut circuit = MpcPlonkCircuit::new(fabric.clone());
let witness_var = witness.create_shared_witness(&mut circuit);
let statement_var = statement.create_shared_witness(&mut circuit);
let statement_var = statement.create_shared_public_var(&mut circuit);

// Apply the constraints
Self::apply_constraints_multiprover(witness_var, statement_var, &fabric, &mut circuit)?;
Expand Down
60 changes: 37 additions & 23 deletions circuits/src/zk_circuits/valid_commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ use circuit_types::{
balance::{Balance, BalanceVar},
fee::{Fee, FeeVar},
order::{Order, OrderVar},
traits::{BaseType, CircuitBaseType, CircuitVarType, SecretShareVarType},
traits::{
BaseType, CircuitBaseType, CircuitVarType, MpcBaseType, MpcType,
MultiproverCircuitBaseType, SecretShareVarType,
},
wallet::{WalletShare, WalletVar},
PlonkCircuit,
Fabric, PlonkCircuit,
};
use constants::{Scalar, ScalarField, MAX_BALANCES, MAX_FEES, MAX_ORDERS};
use constants::{AuthenticatedScalar, Scalar, ScalarField, MAX_BALANCES, MAX_FEES, MAX_ORDERS};
use mpc_plonk::errors::PlonkError;
use mpc_relation::{errors::CircuitError, traits::Circuit, Variable};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -85,7 +88,7 @@ where
// of zero for the received mint of the order. This augmented balance
// must come in place of a previous balance that was zero.
Self::verify_wallets_equal_with_augmentation(
statement.balance_receive_index,
statement.indices.balance_receive,
receive_mint,
&base_wallet,
&augmented_wallet,
Expand All @@ -94,7 +97,7 @@ where

// Verify that the send balance is at the correct index
Self::contains_balance_at_index(
statement.balance_send_index,
statement.indices.balance_send,
&witness.balance_send,
&augmented_wallet,
cs,
Expand All @@ -103,7 +106,7 @@ where

// Verify that the receive balance is at the correct index
Self::contains_balance_at_index(
statement.balance_receive_index,
statement.indices.balance_receive,
&witness.balance_receive,
&augmented_wallet,
cs,
Expand All @@ -117,7 +120,7 @@ where

// Verify that the order is at the correct index
Self::contains_order_at_index(
statement.order_index,
statement.indices.order,
&witness.order,
&augmented_wallet,
cs,
Expand Down Expand Up @@ -328,16 +331,25 @@ pub type SizedValidCommitmentsWitness = ValidCommitmentsWitness<MAX_BALANCES, MA

/// The statement type for `VALID COMMITMENTS`
#[circuit_type(singleprover_circuit)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct ValidCommitmentsStatement {
/// The indices used in settling this order once matched
pub indices: OrderSettlementIndices,
}

/// The indices that specify where settlement logic should modify the wallet
/// shares
#[circuit_type(serde, singleprover_circuit, mpc, multiprover_circuit)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct OrderSettlementIndices {
/// The index of the balance that holds the mint that the wallet will
/// send if a successful match occurs
pub balance_send_index: u64,
pub balance_send: u64,
/// The index of the balance that holds the mint that the wallet will
/// receive if a successful match occurs
pub balance_receive_index: u64,
pub balance_receive: u64,
/// The index of the order that is to be matched
pub order_index: u64,
pub order: u64,
}

// ---------------------
Expand Down Expand Up @@ -378,7 +390,7 @@ pub mod test_helpers {
create_wallet_shares, MAX_BALANCES, MAX_FEES, MAX_ORDERS,
};

use super::{ValidCommitmentsStatement, ValidCommitmentsWitness};
use super::{OrderSettlementIndices, ValidCommitmentsStatement, ValidCommitmentsWitness};

/// A type alias for the VALID COMMITMENTS witness with size parameters
/// attached
Expand Down Expand Up @@ -457,9 +469,11 @@ pub mod test_helpers {
};

let statement = ValidCommitmentsStatement {
balance_send_index: ind_send as u64,
balance_receive_index: ind_receive as u64,
order_index: ind_order as u64,
indices: OrderSettlementIndices {
balance_send: ind_send as u64,
balance_receive: ind_receive as u64,
order: ind_order as u64,
},
};

(witness, statement)
Expand Down Expand Up @@ -583,7 +597,7 @@ mod test {
let (mut witness, statement) = create_witness_and_statement(&wallet);

// Prover attempt to augment the wallet with a non-zero balance
let augmented_balance_index = statement.balance_receive_index;
let augmented_balance_index = statement.indices.balance_receive;
witness.augmented_public_shares.balances[augmented_balance_index as usize].amount +=
Scalar::one();
witness.balance_receive.amount += 1u64;
Expand All @@ -601,7 +615,7 @@ mod test {
let (mut witness, statement) = create_witness_and_statement(&wallet);

// Reset the original wallet such that the augmented balance was non-zero
let augmentation_index = statement.balance_receive_index;
let augmentation_index = statement.indices.balance_receive;
witness.public_secret_shares.balances[augmentation_index as usize] = BalanceShare {
amount: Scalar::one(),
mint: Scalar::one(),
Expand Down Expand Up @@ -676,7 +690,7 @@ mod test {
let (witness, mut statement) = create_witness_and_statement(&wallet);

// Modify the index of the send balance
statement.balance_send_index += 1;
statement.indices.balance_send += 1;

assert!(!check_constraint_satisfaction::<SizedCommitments>(
&witness, &statement
Expand All @@ -690,7 +704,7 @@ mod test {
let (witness, mut statement) = create_witness_and_statement(&wallet);

// Modify the index of the send balance
statement.balance_receive_index += 1;
statement.indices.balance_receive += 1;

assert!(!check_constraint_satisfaction::<SizedCommitments>(
&witness, &statement
Expand All @@ -704,7 +718,7 @@ mod test {
let (witness, mut statement) = create_witness_and_statement(&wallet);

// Modify the index of the order
statement.order_index += 1;
statement.indices.order += 1;

assert!(!check_constraint_satisfaction::<SizedCommitments>(
&witness, &statement
Expand All @@ -718,7 +732,7 @@ mod test {
let (mut witness, statement) = create_witness_and_statement(&wallet);

// Modify the send balance from the order
witness.augmented_public_shares.balances[statement.balance_send_index as usize] =
witness.augmented_public_shares.balances[statement.indices.balance_send as usize] =
BalanceShare {
mint: Scalar::zero(),
amount: Scalar::zero(),
Expand All @@ -736,7 +750,7 @@ mod test {
let (mut witness, statement) = create_witness_and_statement(&wallet);

// Modify the receive balance from the order
witness.augmented_public_shares.balances[statement.balance_receive_index as usize] =
witness.augmented_public_shares.balances[statement.indices.balance_receive as usize] =
BalanceShare {
mint: Scalar::zero(),
amount: Scalar::zero(),
Expand Down Expand Up @@ -778,7 +792,7 @@ mod test {
let (mut witness, statement) = create_witness_and_statement(&wallet);

// Modify the order being proved on
witness.augmented_public_shares.orders[statement.order_index as usize] = OrderShare {
witness.augmented_public_shares.orders[statement.indices.order as usize] = OrderShare {
quote_mint: Scalar::zero(),
base_mint: Scalar::zero(),
side: Scalar::zero(),
Expand Down
Loading

0 comments on commit d4ee06c

Please sign in to comment.