Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/less than constraints #45

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions mpc-algebra/examples/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ fn test_bitwise_lt() {

fn test_interval_test_half_modulus() {
let rng = &mut thread_rng();
let mut half_modulus =
<<ark_ff::Fp256<ark_bls12_377::FrParameters> as ark_ff::PrimeField>::Params>::MODULUS;
half_modulus.div2();
let half_modulus =
<<ark_ff::Fp256<ark_bls12_377::FrParameters> as ark_ff::PrimeField>::Params>::MODULUS_MINUS_ONE_DIV_TWO;

let n = 10;
let timer = start_timer!(|| format!("interval_test_half_modulus test x {}", n));
Expand Down
13 changes: 10 additions & 3 deletions mpc-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
sync::Mutex,
};

use ark_std::{end_timer, start_timer};
use ark_std::{end_timer, perf_trace::TimerInfo, start_timer};
use lazy_static::lazy_static;
use log::debug;
use rayon::prelude::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator};
Expand Down Expand Up @@ -151,7 +151,11 @@ impl Connections {
self.id == 0
}
fn broadcast(&mut self, bytes_out: &[u8]) -> Vec<Vec<u8>> {
let timer = start_timer!(|| format!("Broadcast {}", bytes_out.len()));
let timer: TimerInfo;
#[cfg(feature = "log_broadcast")]
{
timer = start_timer!(|| format!("Broadcast {}", bytes_out.len()));
}
let m = bytes_out.len();
let own_id = self.id;
self.stats.bytes_sent += (self.peers.len() - 1) * m;
Expand Down Expand Up @@ -181,7 +185,10 @@ impl Connections {
bytes_in
})
.collect();
end_timer!(timer);
#[cfg(feature = "log_broadcast")]
{
end_timer!(timer);
}
r
}
fn send_to_king(&mut self, bytes_out: &[u8]) -> Option<Vec<Vec<u8>>> {
Expand Down
191 changes: 87 additions & 104 deletions src/marlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ark_ff::{BigInteger, PrimeField};
use ark_marlin::{ahp::prover::*, *};
use ark_poly::univariate::DensePolynomial;
use ark_poly_commit::marlin_pc::MarlinKZG10;
use ark_relations::r1cs::ConstraintSynthesizer;
use ark_std::{end_timer, start_timer, test_rng, PubUniformRand, UniformRand};

use blake2::Blake2s;
Expand All @@ -22,6 +23,32 @@ use crate::{
input::{MpcInputTrait, SampleMpcInput},
};

fn setup_and_index<C: ConstraintSynthesizer<Fr>>(
circuit: C,
) -> (
IndexProverKey<MFr, MpcMarlinKZG10>,
IndexVerifierKey<Fr, LocalMarlinKZG10>,
) {
let rng = &mut test_rng();
let srs = LocalMarlin::universal_setup(10000, 50, 100, rng).unwrap();
let (index_pk, index_vk) = LocalMarlin::index(&srs, circuit).unwrap();
let mpc_index_pk = IndexProverKey::from_public(index_pk);
(mpc_index_pk, index_vk)
}

fn prove_and_verify<C: ConstraintSynthesizer<MFr>>(
mpc_index_pk: &IndexProverKey<MFr, MpcMarlinKZG10>,
index_vk: &IndexVerifierKey<Fr, LocalMarlinKZG10>,
circuit: C,
inputs: Vec<Fr>,
) -> bool {
let rng = &mut test_rng();
let mpc_proof = MpcMarlin::prove(mpc_index_pk, circuit, rng).unwrap();
let proof = pf_publicize(mpc_proof);

LocalMarlin::verify(index_vk, &inputs, &proof, rng).unwrap()
}

fn prover_message_publicize(
p: ProverMsg<MpcField<ark_bls12_377::Fr>>,
) -> ProverMsg<ark_bls12_377::Fr> {
Expand Down Expand Up @@ -103,67 +130,50 @@ pub type MpcMarlin = Marlin<MFr, MpcMarlinKZG10, Blake2s>;

pub fn mpc_test_prove_and_verify(n_iters: usize) {
let rng = &mut test_rng();

let srs = LocalMarlin::universal_setup(10000, 50, 100, rng).unwrap();

let local_input = SampleMpcInput::<Fr>::rand(rng);

let local_circuit = MyCircuit {
mpc_input: local_input,
};

let (index_pk, index_vk) = LocalMarlin::index(&srs, local_circuit).unwrap();
let mpc_index_pk = IndexProverKey::from_public(index_pk);
let (mpc_index_pk, index_vk) = setup_and_index(local_circuit);

for _ in 0..n_iters {
// Pedersen commitment
//// commom parameter

let mut mpc_input: SampleMpcInput<MFr> = SampleMpcInput::init();

mpc_input.set_public_input(rng, None);
mpc_input.set_private_input(Some((Fr::rand(rng), Fr::rand(rng))));
mpc_input.generate_input(rng);

let mpc_circuit = MyCircuit {
mpc_input: mpc_input.clone(),
};
let c = mpc_input.clone().peculiar.unwrap().a.input
* mpc_input.clone().peculiar.unwrap().b.input;
let mut inputs = vec![c.reveal()];

let peculiar_a_commitment: MpcEdwardsAffine =
mpc_input.peculiar.clone().unwrap().a.commitment;
let peculiar_b_commitment: MpcEdwardsAffine = mpc_input.peculiar.unwrap().b.commitment;

inputs.push(peculiar_a_commitment.x.reveal());
inputs.push(peculiar_a_commitment.y.reveal());
inputs.push(peculiar_b_commitment.x.reveal());
inputs.push(peculiar_b_commitment.y.reveal());

// then, inputs is like [c, h_x_1.x, h_x_1.y, h_x_2.x, h_x_2.y, ...]

let mpc_proof = MpcMarlin::prove(&mpc_index_pk, mpc_circuit, rng).unwrap();
let proof = pf_publicize(mpc_proof);
// let public_a = a.reveal();
let is_valid = LocalMarlin::verify(&index_vk, &inputs, &proof, rng).unwrap();
assert!(is_valid);
let is_valid = LocalMarlin::verify(&index_vk, &[c.reveal()], &proof, rng).unwrap();
assert!(!is_valid);
let peculiar = mpc_input.peculiar.unwrap();
let c = peculiar.a.input * peculiar.b.input;
let inputs = vec![
c.reveal(),
peculiar.a.commitment.x.reveal(),
peculiar.a.commitment.y.reveal(),
peculiar.b.commitment.x.reveal(),
peculiar.b.commitment.y.reveal(),
];
let invalid_inputs = vec![c.reveal()];

assert!(prove_and_verify(
&mpc_index_pk,
&index_vk,
mpc_circuit.clone(),
inputs,
));
assert!(!prove_and_verify(
&mpc_index_pk,
&index_vk,
mpc_circuit,
invalid_inputs,
));
}
}

pub fn mpc_test_prove_and_verify_pedersen(n_iters: usize) {
// setup
let rng = &mut test_rng();

let srs = LocalMarlin::universal_setup(5000, 50, 100, rng).unwrap();

// Pedersen commitment
//// commom parameter
let params = <Fr as LocalOrMPC<Fr>>::PedersenComScheme::setup(rng).unwrap();

//// input
let x = Fr::from(4);
let input_bit = x
.into_repr()
Expand All @@ -172,39 +182,27 @@ pub fn mpc_test_prove_and_verify_pedersen(n_iters: usize) {
.map(|b| Fr::from(*b))
.collect::<Vec<_>>();
let x_bytes = x.into_repr().to_bytes_le();

//// randomness
let randomness = <Fr as LocalOrMPC<Fr>>::PedersenRandomness::pub_rand(rng);

let open_bit = randomness
.0
.into_repr()
.to_bits_le()
.iter()
.map(|b| Fr::from(*b))
.collect::<Vec<_>>();

//// commitment
let h_x_local =
<Fr as LocalOrMPC<Fr>>::PedersenComScheme::commit(&params, &x_bytes, &randomness).unwrap();

let empty_circuit = PedersenComCircuit {
param: Some(params.clone()),
input: x,
input_bit,
open_bit,
commit: Some(h_x_local),
};

let (index_pk, index_vk) = LocalMarlin::index(&srs, empty_circuit).unwrap();
let mpc_index_pk = IndexProverKey::from_public(index_pk);
let (mpc_index_pk, index_vk) = setup_and_index(empty_circuit);

for _ in 0..n_iters {
// Pedersen commitment
//// commom parameter
let mpc_params = <MFr as LocalOrMPC<MFr>>::PedersenParam::from_local(&params);

//// input
let x = MFr::rand(rng);
let input_bit = match MpcMultiNet::party_id() {
0 => x
Expand All @@ -224,105 +222,90 @@ pub fn mpc_test_prove_and_verify_pedersen(n_iters: usize) {
.map(|_b| MFr::from_add_shared(Fr::from(false)))
.collect::<Vec<_>>(),
};

//// randomness
let randomness = <MFr as LocalOrMPC<MFr>>::PedersenRandomness::pub_rand(rng);

let open_bit = randomness
.0
.into_repr()
.to_bits_le()
.iter()
.map(|b| MFr::from(*b))
.collect::<Vec<_>>();

//// commitment
let h_x = <Fr as LocalOrMPC<Fr>>::PedersenComScheme::commit(
&params,
&x.clone().reveal().into_repr().to_bytes_le(),
&x.reveal().into_repr().to_bytes_le(),
&randomness.clone().reveal(),
)
.unwrap();

let h_x_mpc = GroupAffine::<MpcEdwardsParameters>::new(
MFr::from_public(h_x.x),
MFr::from_public(h_x.y),
);

let circuit = PedersenComCircuit {
param: Some(mpc_params.clone()),
input: x,
input_bit,
open_bit,
commit: Some(h_x_mpc),
};

// inputs
let inputs = vec![h_x_mpc.x.reveal(), h_x_mpc.y.reveal()];
let invalid_inputs = vec![h_x_mpc.y.reveal(), h_x_mpc.x.reveal()];

// prove
let mpc_proof = MpcMarlin::prove(&mpc_index_pk, circuit, rng).unwrap();
let proof = pf_publicize(mpc_proof);

// verify
let is_valid = LocalMarlin::verify(&index_vk, &inputs, &proof, rng).unwrap();
assert!(is_valid);
let is_valid = LocalMarlin::verify(&index_vk, &invalid_inputs, &proof, rng).unwrap();
assert!(!is_valid);
assert!(prove_and_verify(
&mpc_index_pk,
&index_vk,
circuit.clone(),
inputs
));
assert!(!prove_and_verify(
&mpc_index_pk,
&index_vk,
circuit,
invalid_inputs
));
}
}

pub fn test_equality_zero(n_iters: usize) {
let rng = &mut test_rng();

let srs = LocalMarlin::universal_setup(10000, 50, 100, rng).unwrap();

let local_circuit = EqualityZeroCircuit { a: Fr::zero() };

let (index_pk, index_vk) = LocalMarlin::index(&srs, local_circuit).unwrap();
let mpc_index_pk = IndexProverKey::from_public(index_pk);
let (mpc_index_pk, index_vk) = setup_and_index(local_circuit);

for _ in 0..n_iters {
let mpc_circuit = EqualityZeroCircuit {
let valid_mpc_circuit = EqualityZeroCircuit {
a: MFr::from_add_shared(Fr::zero()),
};

let mpc_proof = MpcMarlin::prove(&mpc_index_pk, mpc_circuit, rng).unwrap();
let proof = pf_publicize(mpc_proof);

let is_valid = LocalMarlin::verify(&index_vk, &[], &proof, rng).unwrap();
assert!(is_valid);

let invalid_mpc_circuit = EqualityZeroCircuit {
a: MFr::from_add_shared(Fr::one()),
};

let invalid_mpc_proof = MpcMarlin::prove(&mpc_index_pk, invalid_mpc_circuit, rng).unwrap();
let invalid_proof = pf_publicize(invalid_mpc_proof);

let is_not_valid = LocalMarlin::verify(&index_vk, &[], &invalid_proof, rng).unwrap();
assert!(!is_not_valid);
assert!(prove_and_verify(
&mpc_index_pk,
&index_vk,
valid_mpc_circuit,
vec![]
));
assert!(!prove_and_verify(
&mpc_index_pk,
&index_vk,
invalid_mpc_circuit,
vec![]
));
}
}

pub fn test_bit_decomposition(n_iters: usize) {
let rng = &mut test_rng();

let srs = LocalMarlin::universal_setup(10000, 50, 100, rng).unwrap();

let local_circuit = BitDecompositionCircuit { a: Fr::zero() };

let (index_pk, index_vk) = LocalMarlin::index(&srs, local_circuit).unwrap();
let mpc_index_pk = IndexProverKey::from_public(index_pk);
let (mpc_index_pk, index_vk) = setup_and_index(local_circuit);

for _ in 0..n_iters {
let mpc_circuit = BitDecompositionCircuit { a: MFr::rand(rng) };

let mpc_proof = MpcMarlin::prove(&mpc_index_pk, mpc_circuit, rng).unwrap();
let proof = pf_publicize(mpc_proof);

let is_valid = LocalMarlin::verify(&index_vk, &[], &proof, rng).unwrap();
assert!(is_valid);
// TODO
assert!(prove_and_verify(
&mpc_index_pk,
&index_vk,
mpc_circuit,
vec![]
));
}
}
Loading