Skip to content

Commit

Permalink
circuits: zk-circuits: valid-match-mpc: Refactor over mpc-plonk
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Nov 21, 2023
1 parent 82ec20a commit 9b13760
Show file tree
Hide file tree
Showing 20 changed files with 504 additions and 566 deletions.
74 changes: 37 additions & 37 deletions Cargo.lock

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

14 changes: 9 additions & 5 deletions circuit-types/src/fixed_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bigdecimal::{BigDecimal, ToPrimitive};
use circuit_macros::circuit_type;
use constants::{AuthenticatedScalar, Scalar, ScalarField};
use lazy_static::lazy_static;
use mpc_relation::{traits::Circuit, Variable};
use mpc_relation::{errors::CircuitError, traits::Circuit, Variable};
use num_bigint::BigUint;
use renegade_crypto::fields::{
bigint_to_scalar, biguint_to_scalar, scalar_to_bigdecimal, scalar_to_bigint, scalar_to_u64,
Expand Down Expand Up @@ -341,9 +341,13 @@ impl FixedPointVar {
/// converting the integer to a fixed-point representation. I.e. instead
/// of taking x * 2^M * y * 2^M * 2^-M, we can just directly multiply x
/// * 2^M * y
pub fn mul_integer<C: Circuit<ScalarField>>(&self, rhs: Variable, cs: &mut C) -> FixedPointVar {
let repr = cs.mul(self.repr, rhs).unwrap();
FixedPointVar { repr }
pub fn mul_integer<C: Circuit<ScalarField>>(
&self,
rhs: Variable,
cs: &mut C,
) -> Result<FixedPointVar, CircuitError> {
let repr = cs.mul(self.repr, rhs)?;
Ok(FixedPointVar { repr })
}
}

Expand Down Expand Up @@ -634,7 +638,7 @@ mod fixed_point_tests {
let fixed1 = FixedPoint::from_f32_round_down(fp1).create_witness(&mut cs);
let integer = Scalar::from(int).create_witness(&mut cs);

let res = fixed1.mul_integer(integer, &mut cs);
let res = fixed1.mul_integer(integer, &mut cs).unwrap();
let expected = fp1 * (int as f32);

check_within_tolerance(res.eval(&cs).to_f64(), expected as f64, INTEGER_TOLERANCE);
Expand Down
11 changes: 11 additions & 0 deletions circuit-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ pub type SizedMerkleOpening = MerkleOpening<MERKLE_HEIGHT>;
#[derive(Clone, Debug)]
pub struct AuthenticatedBool(AuthenticatedScalar);

/// This implementation does no validation of the underlying value, to do so
/// would require leaking privacy or otherwise complicated circuitry
///
/// The values here are eventually constrained in a collaborative proof, so
/// there is no need to validate them here
impl From<AuthenticatedScalar> for AuthenticatedBool {
fn from(value: AuthenticatedScalar) -> Self {
Self(value)
}
}

// -----------
// | Helpers |
// -----------
Expand Down
2 changes: 1 addition & 1 deletion circuit-types/src/macro_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod test {

// Allocate the dummy value in the constraint system
let dummy_allocated = value.allocate(PARTY0, &fabric);
let shared_var = dummy_allocated.create_shared_witness(&mut circuit).unwrap();
let shared_var = dummy_allocated.create_shared_witness(&mut circuit);

// Evaluate the first variable in the var type
let eval: AuthenticatedTestType = shared_var.eval_multiprover(&circuit);
Expand Down
13 changes: 8 additions & 5 deletions circuit-types/src/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ pub struct MatchResult {
pub quote_amount: u64,
/// The amount of the base token exchanged by this match
pub base_amount: u64,
/// The direction of the match, 0 implies that party 1 buys the base and
/// sells the quote; 1 implies that party 2 buys the base and sells the
/// quote
pub direction: u64, // Binary
/// The direction of the match, `true` implies that party 1 buys the quote
/// and sells the base, `false` implies that party 1 buys the base and
/// sells the quote
pub direction: bool,

/// The following are supporting variables, derivable from the above, but
/// useful for shrinking the size of the zero knowledge circuit. As
Expand All @@ -46,5 +46,8 @@ pub struct MatchResult {
pub max_minus_min_amount: u64,
/// The index of the order (0 or 1) that has the minimum amount, i.e. the
/// order that is completely filled by this match
pub min_amount_order_index: u64,
///
/// We serialize this as a `bool` to automatically constrain it to be 0 or 1
/// in a circuit. So `false` means 0 and `true` means 1
pub min_amount_order_index: bool,
}
Loading

0 comments on commit 9b13760

Please sign in to comment.