Skip to content

Commit

Permalink
Merge pull request #39 from Yoii-Inc/feat/fix_constraints_equality_zero
Browse files Browse the repository at this point in the history
Feat/fix constraints equality zero
  • Loading branch information
sheagrief authored Apr 10, 2024
2 parents bc8d2f7 + 128776b commit a9f0350
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
18 changes: 7 additions & 11 deletions mpc-algebra/src/r1cs_helper/mpc_fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,18 +460,14 @@ impl<F: PrimeField, S: FieldShare<F>> MpcAllocatedFp<F, S> {

impl<F: PrimeField + SquareRootField, S: FieldShare<F>> MpcAllocatedFp<F, S> {
pub fn is_zero(&self) -> Result<MpcBoolean<F, S>, SynthesisError> {
let is_not_zero = MpcBoolean::new_witness(self.cs.clone(), || {
Ok(MpcField::one() - self.value.get()?.is_zero_shared())
})?;
let is_zero_value = self.value.get()?.is_zero_shared();

let multiplier = self.cs.new_witness_variable(|| {
// reveal is not recommended. It is better to avoid revealing.
if is_not_zero.value_field()?.reveal().is_one() {
(self.value.get()?).inverse().get()
} else {
Ok(MpcField::one())
}
})?;
let is_not_zero =
MpcBoolean::new_witness(self.cs.clone(), || Ok(MpcField::one() - is_zero_value))?;

let multiplier = self
.cs
.new_witness_variable(|| (self.value.get()? + is_zero_value).inverse().get())?;

self.cs
.enforce_constraint(lc!() + self.variable, lc!() + multiplier, is_not_zero.lc())?;
Expand Down
2 changes: 2 additions & 0 deletions mpc-algebra/src/wire/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ impl<F: PrimeField + SquareRootField, S: FieldShare<F>> BitDecomposition for Mpc
fn bit_decomposition(&self) -> Self::Output {
match self.is_shared() {
true => {
let timer = start_timer!(|| "Bit Decomposition");
let rng = &mut ark_std::test_rng();

let l = F::Params::MODULUS_BITS as usize;
Expand Down Expand Up @@ -714,6 +715,7 @@ impl<F: PrimeField + SquareRootField, S: FieldShare<F>> BitDecomposition for Mpc

// 6
assert!(h.len() == l + 1);
end_timer!(timer);
h[..l].to_vec() // remove the last element
}
false => {
Expand Down
4 changes: 3 additions & 1 deletion src/bin_test_marlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ fn main() {
let opt = Opt::from_args();
Net::init_from_file(opt.input.to_str().unwrap(), opt.id);
marlin::mpc_test_prove_and_verify(1);
// marlin::mpc_test_prove_and_verify_pedersen(1);
marlin::mpc_test_prove_and_verify_pedersen(1);
marlin::test_equality_zero(1);
marlin::test_bit_decomposition(1);
}
3 changes: 1 addition & 2 deletions src/circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ pub use pedersen::*;
pub mod werewolf;
pub use werewolf::*;
pub mod equality_zero;
pub use equality_zero::*;

pub mod bit_decomposition;
pub use bit_decomposition::*;
16 changes: 4 additions & 12 deletions src/circuits/bit_decomposition.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
use ark_ff::PrimeField;
use ark_r1cs_std::{
alloc::AllocVar,
boolean::Boolean,
eq::EqGadget,
fields::{fp::FpVar, FieldVar},
ToBitsGadget,
};
use ark_r1cs_std::{alloc::AllocVar, fields::fp::FpVar, ToBitsGadget};
use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystemRef, SynthesisError};
use mpc_algebra::{
malicious_majority::MpcField, MpcBoolean, MpcEqGadget, MpcFpVar, MpcToBitsGadget,
};
use mpc_algebra::{malicious_majority::MpcField, MpcFpVar, MpcToBitsGadget};

type Fr = ark_bls12_377::Fr;
type MFr = MpcField<Fr>;
Expand All @@ -22,7 +14,7 @@ impl ConstraintSynthesizer<MFr> for BitDecompositionCircuit<MFr> {
fn generate_constraints(self, cs: ConstraintSystemRef<MFr>) -> Result<(), SynthesisError> {
let a_var = MpcFpVar::new_witness(cs.clone(), || Ok(self.a))?;

let bits = a_var.to_bits_le()?;
let _bits = a_var.to_bits_le()?;

Ok(())
}
Expand All @@ -32,7 +24,7 @@ impl ConstraintSynthesizer<Fr> for BitDecompositionCircuit<Fr> {
fn generate_constraints(self, cs: ConstraintSystemRef<Fr>) -> Result<(), SynthesisError> {
let a_var = FpVar::new_witness(cs.clone(), || Ok(self.a))?;

let bits = a_var.to_bits_le()?;
let _bits = a_var.to_bits_le()?;

Ok(())
}
Expand Down

0 comments on commit a9f0350

Please sign in to comment.