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/r1cs for pedersen commitment #50

Merged
merged 44 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a27c4ab
✨ Implement abstract commitment scheme.
sheagrief Apr 10, 2024
2023905
🚧 WIP: Updating r1cs-std libraries for elliptic curve and its arounds
sheagrief Apr 18, 2024
e6d6a66
✨ Implement MpcUInt8 for mpc-algebra
sheagrief Apr 23, 2024
e18fcd2
🚧 WIP: Update curves and r1cs_helper
sheagrief Apr 23, 2024
41aa8fe
🚧 WIP: Implement CRH & Pedersen CRH
sheagrief Apr 23, 2024
3aebefa
✨ Implement Mpc ver for EdwardsCurve
sheagrief May 2, 2024
6e2e31d
✨ Implement MPC-PedersenCommitment (calc only)
sheagrief May 2, 2024
9e823fe
✅ Tests MPC-PedersenCommitment
sheagrief May 2, 2024
71d74ee
Merge branch 'main' into feat/r1cs_for_pedersen_commitment
sheagrief May 9, 2024
c11577e
🎨 Refactor r1cs_helper (remove needless FieldShare)
sheagrief May 9, 2024
33aad29
🚧 WIP, Implement constraints of Mpc Pederesen Commitment
sheagrief May 9, 2024
d624096
🚧 WIP, Update MpcUint8 & MpcEdwards
sheagrief May 28, 2024
6293ce9
Merge branch 'main' into feat/r1cs_for_pedersen_commitment
sheagrief May 28, 2024
903e170
Merge branch 'improve/mpc-boolean-field' into feat/r1cs_for_pedersen_…
sheagrief May 30, 2024
04d39a4
🚧 WIP, Revised pedersen commimtment implementation
sheagrief May 30, 2024
9e4e104
Merge branch 'main' into feat/r1cs_for_pedersen_commitment
sheagrief May 30, 2024
9b5dc58
✨ Implement MpcUint8
sheagrief May 30, 2024
20668ce
🚧 Implement Modulus-Conversion for MpcField
sheagrief Jun 1, 2024
382cd63
🚧 WIP, PedersenCom Circuit and modify around R1CS of MpcAffineVar
sheagrief Jun 6, 2024
74c0c8e
🚧 WIP, Support converting Curve shares and Add some comments.
sheagrief Jun 6, 2024
d679b11
🚧 Fix pederesn comm r1cs & add reveal
sheagrief Jun 13, 2024
9d26d79
🎨 Implement AllocVar for MpcUInt8
sheagrief Jun 13, 2024
40eff35
✨ Implement experimental modulus_conversion
sheagrief Jun 13, 2024
94e8a3c
✨ Implement & Fix Batch_normalization for mpc edwards curve
sheagrief Jun 20, 2024
a8a98c1
✨ Implement & Fix Mpc edwards curve conversion
sheagrief Jun 20, 2024
1733106
Merge branch 'main' into feat/r1cs_for_pedersen_commitment
sheagrief Jun 20, 2024
675e92b
🎨 Support Spdz Edwards Curve and improve structure.
sheagrief Jun 21, 2024
2cecf37
🐛 Modify R1CS according to the revised Edwards curve for MPC
sheagrief Jun 22, 2024
d5ba30d
🎨 Modify most circuits for new PedersenCom
sheagrief Jul 4, 2024
58e52a4
✨ Unified structure of Pedersen Com
sheagrief Jul 4, 2024
cfc2b79
🚧 Support spdz for PedersenCom Circuit and rearrange lib
sheagrief Jul 4, 2024
137a719
✅ Pass marlin PedCom test.
sheagrief Jul 4, 2024
6793046
Merge branch 'main' into feat/r1cs_for_pedersen_commitment
sheagrief Jul 4, 2024
75bfb60
✨ Implement MPC ElGamal Encryption
sheagrief Jul 11, 2024
61d0b91
🐛 Update werewolf env
sheagrief Jul 11, 2024
61a9bef
Merge branch 'main' into feat/r1cs_for_pedersen_commitment
sheagrief Jul 11, 2024
7725667
✨ Implement basic arithmetic for Mpcobject
sheagrief Aug 1, 2024
2af16c8
✨ Implement conversion between share of curve
sheagrief Aug 1, 2024
307ca3c
🐛 Fix marlin test
sheagrief Aug 1, 2024
fda1584
Merge branch 'main' into feat/r1cs_for_pedersen_commitment
sheagrief Aug 1, 2024
718d85b
🐛 Resolve compile error
sheagrief Aug 1, 2024
3ff8a74
🔧 Resolve feature error of configuration file.
sheagrief Aug 1, 2024
3500e62
🔧 Resolve feature error of configulation file
sheagrief Aug 1, 2024
86da687
📝 Replace todo into unimplemnted. and delete some comments.
sheagrief Aug 1, 2024
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
1 change: 1 addition & 0 deletions arkworks/curves/ed_on_bls12_377/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ ark-curve-constraint-tests = { path = "../curve-constraint-tests", default-featu
default = []
std = [ "ark-std/std", "ark-ff/std", "ark-ec/std", "ark-bls12-377/std" ]
r1cs = [ "ark-r1cs-std" ]
ed_on_bls12_377 = []
30 changes: 14 additions & 16 deletions examples/bin_werewolf.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use ark_bls12_377::{Fr, FrParameters};
use ark_crypto_primitives::encryption::AsymmetricEncryptionScheme;
use ark_ec::twisted_edwards_extended::GroupAffine;
use ark_ec::AffineCurve;
use ark_ff::FpParameters;
use ark_marlin::IndexProverKey;
use ark_mnt4_753::FqParameters;
use ark_serialize::{CanonicalDeserialize, Read};
use ark_std::test_rng;
use core::panic;
use mpc_algebra::encryption::elgamal::elgamal::Parameters;
use mpc_algebra::malicious_majority::*;
use mpc_algebra::Reveal;
use mpc_net::{MpcMultiNet as Net, MpcNet};
Expand Down Expand Up @@ -368,7 +368,7 @@ fn multi_divination(_opt: &Opt) -> Result<(), std::io::Error> {
mpc_input: mpc_input.clone(),
};

let _peculiar_is_werewolf_commitment: Vec<GroupAffine<MpcEdwardsParameters>> = mpc_input
let _peculiar_is_werewolf_commitment: Vec<MpcEdwardsAffine> = mpc_input
.peculiar
.clone()
.unwrap()
Expand All @@ -377,7 +377,7 @@ fn multi_divination(_opt: &Opt) -> Result<(), std::io::Error> {
.map(|x| x.commitment)
.collect::<Vec<_>>();

let _peculiar_is_target_commitment: Vec<GroupAffine<MpcEdwardsParameters>> = mpc_input
let _peculiar_is_target_commitment: Vec<MpcEdwardsAffine> = mpc_input
.peculiar
.clone()
.unwrap()
Expand All @@ -386,12 +386,10 @@ fn multi_divination(_opt: &Opt) -> Result<(), std::io::Error> {
.map(|x| x.commitment)
.collect::<Vec<_>>();

let elgamal_generator: ark_crypto_primitives::encryption::elgamal::Parameters<
MpcEdwardsProjective,
> = mpc_input.clone().common.unwrap().elgamal_param;
let elgamal_generator: Parameters<MpcEdwardsProjective> =
mpc_input.clone().common.unwrap().elgamal_param;

let elgamal_pubkey: GroupAffine<MpcEdwardsParameters> =
mpc_input.clone().common.unwrap().pub_key;
let elgamal_pubkey: MpcEdwardsAffine = mpc_input.clone().common.unwrap().pub_key;

let message = <MFr as ElGamalLocalOrMPC<MFr>>::ElGamalPlaintext::prime_subgroup_generator();

Expand All @@ -408,17 +406,17 @@ fn multi_divination(_opt: &Opt) -> Result<(), std::io::Error> {
let mut inputs = Vec::new();

// elgamal param
inputs.push(elgamal_generator.generator.x.reveal());
inputs.push(elgamal_generator.generator.y.reveal());
inputs.push(elgamal_generator.generator.reveal().x);
inputs.push(elgamal_generator.generator.reveal().y);
// elgamal pubkey
inputs.push(elgamal_pubkey.x.reveal());
inputs.push(elgamal_pubkey.y.reveal());
inputs.push(elgamal_pubkey.reveal().x);
inputs.push(elgamal_pubkey.reveal().y);

// elgamal ciphertext
inputs.push(enc_result.0.x.reveal());
inputs.push(enc_result.0.y.reveal());
inputs.push(enc_result.1.x.reveal());
inputs.push(enc_result.1.y.reveal());
inputs.push(enc_result.0.reveal().x);
inputs.push(enc_result.0.reveal().y);
inputs.push(enc_result.1.reveal().x);
inputs.push(enc_result.1.reveal().y);

// input commitment
// inputs.push(peculiar_is_werewolf_commitment[0].x.reveal());
Expand Down
8 changes: 4 additions & 4 deletions examples/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let peculiar_a_commitment = mpc_input.peculiar.clone().unwrap().a.commitment;
let peculiar_b_commitment = 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());
inputs.push(peculiar_a_commitment.reveal().x);
inputs.push(peculiar_a_commitment.reveal().y);
inputs.push(peculiar_b_commitment.reveal().x);
inputs.push(peculiar_b_commitment.reveal().y);

assert!(LocalMarlin::verify(&index_vk, &inputs, &proof, rng).unwrap());
}
Expand Down
2 changes: 1 addition & 1 deletion mpc-algebra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ark-poly = { path = "../arkworks/algebra/poly", version = "0.3.0" }
ark-serialize = { path = "../arkworks/algebra/serialize", version = "0.3.0" }
ark-std = { path = "../arkworks/std", version = "0.3.0", features = ["std", "print-trace"] }
ark-r1cs-std = { path = "../arkworks/r1cs-std", version = "0.3.0", default-features = false, optional = true }
ark-crypto-primitives = { path = "../arkworks/crypto-primitives", version = "0.3.0" }
ark-crypto-primitives = { path = "../arkworks/crypto-primitives", version = "0.3.0", features = ["r1cs"] }
ark-relations = { path = "../arkworks/snark/relations", version = "^0.3.0", default-features = false }

ark-bls12-377 = { path = "../arkworks/curves/bls12_377", version = "0.3.0" }
Expand Down
78 changes: 76 additions & 2 deletions mpc-algebra/examples/algebra.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use std::path::PathBuf;

use ark_crypto_primitives::{CommitmentScheme, CRH};
use ark_ff::PubUniformRand;
use ark_ff::{BigInteger, BigInteger256, FpParameters, PrimeField, UniformRand};
use ark_ff::{One, Zero};
use ark_poly::reveal;
use ark_std::PubUniformRand;
use ark_std::{end_timer, start_timer};
use log::debug;
use mpc_algebra::pedersen::Randomness;
use mpc_algebra::boolean_field::MpcBooleanField;
use mpc_algebra::{
AdditiveFieldShare, BitAdd, BitDecomposition, BitwiseLessThan, BooleanWire, EqualityZero,
LessThan, LogicalOperations, MpcField, Reveal, UniformBitRand,
edwards2, share, AdditiveFieldShare, BitAdd, BitDecomposition, BitwiseLessThan, BooleanWire,
CommitmentScheme as MpcCommitmentScheme, EqualityZero, LessThan,
LogicalOperations,
MpcEdwardsProjective, MpcField, Reveal, UniformBitRand,
};
use mpc_net::{MpcMultiNet as Net, MpcNet};

Expand Down Expand Up @@ -375,6 +381,71 @@ fn test_bit_decomposition() {
assert_eq!(res, random.reveal());
}

pub const PERDERSON_WINDOW_SIZE: usize = 256;
pub const PERDERSON_WINDOW_NUM: usize = 1;

#[derive(Clone)]
pub struct Window;
impl ark_crypto_primitives::crh::pedersen::Window for Window {
const WINDOW_SIZE: usize = PERDERSON_WINDOW_SIZE;
const NUM_WINDOWS: usize = PERDERSON_WINDOW_NUM;
}

impl mpc_algebra::crh::pedersen::Window for Window {
const WINDOW_SIZE: usize = PERDERSON_WINDOW_SIZE;
const NUM_WINDOWS: usize = PERDERSON_WINDOW_NUM;
}

type LocalPed = ark_crypto_primitives::commitment::pedersen::Commitment<
ark_ed_on_bls12_377::EdwardsProjective,
Window,
>;
type MpcPed = mpc_algebra::commitment::pedersen::Commitment<edwards2::MpcEdwardsProjective, Window>;

fn test_pedersen_commitment() {
let rng = &mut ark_std::test_rng();

let x = F::rand(rng);
let x_bytes = x.into_repr().to_bytes_le();
let x_bits = x.into_repr().to_bits_le();

// mpc calculation
let mpc_parameters = MpcPed::setup(rng).unwrap();

let scalar_x_bytes = if Net::am_king() {
x_bits
.iter()
.map(|b| {
MpcField::<ark_ed_on_bls12_377::Fr, AdditiveFieldShare<ark_ed_on_bls12_377::Fr>>::from_add_shared(ark_ed_on_bls12_377::Fr::from(*b))
})
.collect::<Vec<_>>()
} else {
x_bits
.iter()
.map(|b| {
MpcField::<ark_ed_on_bls12_377::Fr, AdditiveFieldShare<ark_ed_on_bls12_377::Fr>>::from_add_shared(ark_ed_on_bls12_377::Fr::zero())
})
.collect::<Vec<_>>()
};

let randomness = Randomness::<MpcEdwardsProjective>::rand(rng);

let result_mpc = MpcPed::commit(&mpc_parameters, &scalar_x_bytes, &randomness).unwrap();

// local calculation
let local_parameters = ark_crypto_primitives::commitment::pedersen::Parameters {
randomness_generator: mpc_parameters.randomness_generator.clone().reveal(),
generators: mpc_parameters.generators.reveal(),
};

let local_randomness =
ark_crypto_primitives::commitment::pedersen::Randomness(randomness.0.reveal());

let result_local = LocalPed::commit(&local_parameters, &x_bytes, &local_randomness).unwrap();

assert_eq!(result_local, result_mpc.reveal());
}

fn test_share() {
let rng = &mut ark_std::test_rng();

Expand Down Expand Up @@ -432,6 +503,9 @@ fn main() {
test_bit_decomposition();
println!("Test bit_decomposition passed");

test_pedersen_commitment();
println!("Test pedersen commitment passed");

test_share();
println!("Test share passed");
}
25 changes: 25 additions & 0 deletions mpc-algebra/src/commitment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use ark_ff::UniformRand;
use ark_std::rand::Rng;
use ark_std::{fmt::Debug, hash::Hash};

use ark_ff::bytes::ToBytes;

pub mod constraints;
pub mod pedersen;

use ark_crypto_primitives::Error;

pub trait CommitmentScheme {
type Input;
type Output: ToBytes + Clone + Default + Eq + Hash + Debug;
type Parameters: Clone;
type Randomness: Clone + ToBytes + Default + Eq + UniformRand + Debug;

fn setup<R: Rng>(r: &mut R) -> Result<Self::Parameters, Error>;

fn commit(
parameters: &Self::Parameters,
input: &Self::Input,
r: &Self::Randomness,
) -> Result<Self::Output, Error>;
}
23 changes: 23 additions & 0 deletions mpc-algebra/src/commitment/constraints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::commitment::CommitmentScheme;
use ark_ff::PrimeField;
use ark_r1cs_std::{alloc::AllocVar, R1CSVar, ToBytesGadget};
use ark_relations::r1cs::SynthesisError;
use core::fmt::Debug;

pub trait CommitmentGadget<C: CommitmentScheme, ConstraintF: PrimeField> {
type OutputVar: ToBytesGadget<ConstraintF>
+ AllocVar<C::Output, ConstraintF>
+ R1CSVar<ConstraintF>
+ Clone
+ Sized
+ Debug;
type ParametersVar: AllocVar<C::Parameters, ConstraintF> + Clone;
type RandomnessVar: AllocVar<C::Randomness, ConstraintF> + Clone;
type InputVar: AllocVar<C::Input, ConstraintF> + Clone;

fn commit(
parameters: &Self::ParametersVar,
input: &Self::InputVar,
r: &Self::RandomnessVar,
) -> Result<Self::OutputVar, SynthesisError>;
}
5 changes: 5 additions & 0 deletions mpc-algebra/src/commitment/pedersen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod pedersen;
pub use pedersen::*;
pub mod constraints;
pub use constraints::*;
pub mod local_pedersen;
Loading
Loading