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

Trusted Setup Phase 2 #172

Merged
merged 61 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
df1efb6
wip: dummy commit
Jul 15, 2022
ecb3b40
feat: add phase 2 initialization
tsunrise Jul 15, 2022
75519b0
chore: update accumulator visibility
tsunrise Jul 15, 2022
520409c
feat: add contribution generation logic
tsunrise Jul 15, 2022
74e7fc1
feat: add more phase2 functions
Jul 15, 2022
0c6f5cd
feat: add `verify_transform` for phase 2
tsunrise Jul 18, 2022
51aae16
wip: add e2e test for phase2
Jul 18, 2022
b3ae19e
chore: add `random_linear_combination` and some name changes
tsunrise Jul 19, 2022
53a0a65
fix: run `cargo hakari generate`
tsunrise Jul 19, 2022
cdcbd46
wip: add comments
Jul 19, 2022
9fd820f
wip: add e2e test for phase2
Jul 19, 2022
113372a
wip: implement traits for phase2
Jul 20, 2022
1384104
wip: add e2e test for phase2
Jul 20, 2022
75365b5
fix: parallelization for random_linear_combinations
GhostOfGauss Jul 20, 2022
7fc26aa
fix: parallelization for random_linear_combinations missed changes
GhostOfGauss Jul 20, 2022
8fbf4a9
wip: e2e test for phase2
Jul 20, 2022
5ea6f07
fix: e2e test compile issues
tsunrise Jul 20, 2022
9528894
wip: e2e test
tsunrise Jul 20, 2022
5f2a75e
wip: e2e test
Jul 20, 2022
affd0c4
feat: finish e2e
Jul 21, 2022
ddee79a
fix phase2 hash_to_g2
GhostOfGauss Jul 21, 2022
ffe09e0
feat: refactor sample & distribution traits; fix a hasher function issue
Jul 21, 2022
86c66c9
wip: sanity check
Jul 22, 2022
701ab51
feat: clean code
Jul 22, 2022
f135e77
Merge branch 'main' into feat/phase2
bhgomes Jul 22, 2022
daf0955
wip: refactor
Jul 22, 2022
7c9d6b3
wip: refactor
Jul 25, 2022
200d938
feat: refactor serialize & clean cargo.toml
Jul 25, 2022
839378f
wip: refactor
Jul 25, 2022
f7dcbba
wip: refactor
Jul 26, 2022
c13284a
feat: update CHANGELOG
Jul 26, 2022
d576efa
feat: refactor phase2 trusted setup
Jul 26, 2022
5efd085
feat: Clean up lint & comments
Jul 26, 2022
3342fe2
feat: Add HashToGroup trait
Jul 26, 2022
5a7b486
feat: fix lint issues
Jul 26, 2022
36055b0
feat: update hakari
Jul 26, 2022
c681471
chore: sort dependency order
Jul 27, 2022
4d77529
chore: update dependency
Jul 27, 2022
9d50059
chore: fix a lint issue
Jul 27, 2022
1646ed8
chore: fix a ci issue
Jul 27, 2022
6ce8543
feat: check non-zero ratio proof
Jul 27, 2022
ff106dc
feat: use factored out ratio-proof/pairing
bhgomes Jul 27, 2022
79d4353
chore: add TODO
Jul 27, 2022
ed8d790
feat: add tests
Jul 27, 2022
3f4fb3a
feat: refactor & documentation
Jul 27, 2022
633ceba
feat: add verify_transform_all
Jul 27, 2022
385ebe7
fix: optimize dependencies and update on nightly compiler
bhgomes Jul 28, 2022
bc13335
chore: update hakari dependency list
bhgomes Jul 28, 2022
cd9770b
chore: update dependencies and hakari
bhgomes Jul 28, 2022
9850743
Merge branch 'main' into feat/phase2
bhgomes Jul 28, 2022
561b5d8
wip: fixing comments
Jul 28, 2022
04f324c
feat: update serde, type alias, & error type
Jul 28, 2022
ed99b64
feat: reduce latency for verify_transform_all
Jul 28, 2022
2718c05
feat: clean traits
Jul 28, 2022
1bb8228
feat: remove new() for BlakeHasher & update docs
Jul 28, 2022
eaffe6e
chore: use default for hasher
Jul 28, 2022
bd50aa1
Merge branch 'main' into feat/phase2
bhgomes Jul 28, 2022
e8875bf
fix: refactor tests and dependencies
bhgomes Jul 29, 2022
dc58951
fix: don't depend on `workspace-hack-check` for other CI
bhgomes Jul 29, 2022
4e85bab
feat: add `ratio_check_is_correct` test & more documents
Jul 29, 2022
1442283
feat: update tests & docs
Jul 29, 2022
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added
- [\#131](https://github.com/Manta-Network/manta-rs/pull/131) Add abstract Phase 1 for Groth16 trusted setup
- [\#175](https://github.com/Manta-Network/manta-rs/pull/175) Add more documentation around `cargo-hakari`
- [\#172](https://github.com/Manta-Network/manta-rs/pull/172) Add abstract Phase 2 for Groth16 trusted setup

### Changed

Expand Down
20 changes: 10 additions & 10 deletions manta-pay/src/crypto/constraint/arkworks/groth16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ where
} = &self.0;
vk.serialize(&mut writer)?;
alpha_g1_beta_g2.serialize(&mut writer)?;
<E::G2Prepared as HasSerialization>::Serialize::from(gamma_g2_neg_pc)
<E::G2Prepared as HasSerialization<'_>>::Serialize::from(gamma_g2_neg_pc)
.serialize(&mut writer)?;
<E::G2Prepared as HasSerialization>::Serialize::from(delta_g2_neg_pc)
<E::G2Prepared as HasSerialization<'_>>::Serialize::from(delta_g2_neg_pc)
.serialize(&mut writer)?;
Ok(())
}
Expand All @@ -269,9 +269,9 @@ where
} = &self.0;
vk.serialized_size()
+ alpha_g1_beta_g2.serialized_size()
+ <E::G2Prepared as HasSerialization>::Serialize::from(gamma_g2_neg_pc)
+ <E::G2Prepared as HasSerialization<'_>>::Serialize::from(gamma_g2_neg_pc)
.serialized_size()
+ <E::G2Prepared as HasSerialization>::Serialize::from(delta_g2_neg_pc)
+ <E::G2Prepared as HasSerialization<'_>>::Serialize::from(delta_g2_neg_pc)
.serialized_size()
}

Expand All @@ -288,9 +288,9 @@ where
} = &self.0;
vk.serialize_uncompressed(&mut writer)?;
alpha_g1_beta_g2.serialize_uncompressed(&mut writer)?;
<E::G2Prepared as HasSerialization>::Serialize::from(gamma_g2_neg_pc)
<E::G2Prepared as HasSerialization<'_>>::Serialize::from(gamma_g2_neg_pc)
.serialize_uncompressed(&mut writer)?;
<E::G2Prepared as HasSerialization>::Serialize::from(delta_g2_neg_pc)
<E::G2Prepared as HasSerialization<'_>>::Serialize::from(delta_g2_neg_pc)
.serialize_uncompressed(&mut writer)?;
Ok(())
}
Expand All @@ -308,9 +308,9 @@ where
} = &self.0;
vk.serialize_unchecked(&mut writer)?;
alpha_g1_beta_g2.serialize_unchecked(&mut writer)?;
<E::G2Prepared as HasSerialization>::Serialize::from(gamma_g2_neg_pc)
<E::G2Prepared as HasSerialization<'_>>::Serialize::from(gamma_g2_neg_pc)
.serialize_unchecked(&mut writer)?;
<E::G2Prepared as HasSerialization>::Serialize::from(delta_g2_neg_pc)
<E::G2Prepared as HasSerialization<'_>>::Serialize::from(delta_g2_neg_pc)
.serialize_unchecked(&mut writer)?;
Ok(())
}
Expand All @@ -325,9 +325,9 @@ where
} = &self.0;
vk.uncompressed_size()
+ alpha_g1_beta_g2.uncompressed_size()
+ <E::G2Prepared as HasSerialization>::Serialize::from(gamma_g2_neg_pc)
+ <E::G2Prepared as HasSerialization<'_>>::Serialize::from(gamma_g2_neg_pc)
.uncompressed_size()
+ <E::G2Prepared as HasSerialization>::Serialize::from(delta_g2_neg_pc)
+ <E::G2Prepared as HasSerialization<'_>>::Serialize::from(delta_g2_neg_pc)
.uncompressed_size()
}
}
Expand Down
17 changes: 12 additions & 5 deletions manta-trusted-setup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ maintenance = { status = "actively-developed" }

[features]
# Rayon Parallelization
rayon = ["dep:rayon", "manta-util/rayon"]
rayon = ["manta-util/rayon"]

# Standard Library
std = []
Expand All @@ -35,13 +35,20 @@ std = []
ark-ec = { version = "0.3.0", default-features = false }
ark-ff = { version = "0.3.0", default-features = false }
ark-groth16 = { version = "0.3.0", default-features = false }
ark-r1cs-std = { version = "0.3.1", default-features = false }
ark-poly = { version = "0.3.0", default-features = false }
ark-relations = { version = "0.3.0", default-features = false }
ark-serialize = { version = "0.3.0", default-features = false, features = ["derive"] }
ark-snark = { version = "0.3.0", default-features = false }
ark-std = { version = "0.3.0", default-features = false }
blake2 = { version = "0.10.4", default-features = false }
byteorder = { version = "1.4.3", default-features = false }
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
manta-crypto = { path = "../manta-crypto", default-features = false, features = ["getrandom"] }
manta-util = { path = "../manta-util", default-features = false, features = ["alloc"] }
rayon = { version = "1.5.3", optional = true, default-features = false }
manta-util = { path = "../manta-util", default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }
workspace-hack = { version = "0.1.0", path = "../workspace-hack" }

[dev-dependencies]
ark-bls12-381 = { version = "0.3.0", default-features = false, features = ["curve", "scalar_field"] }
ark-r1cs-std = { version = "0.3.1", default-features = false }
ark-snark = { version = "0.3.0", default-features = false }
manta-pay = { path = "../manta-pay", default-features = false, features = ["groth16"] } # TODO: To be removed
183 changes: 34 additions & 149 deletions manta-trusted-setup/src/groth16/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@

//! KZG Trusted Setup for Groth16

use crate::util::{
power_pairs, scalar_mul, CanonicalDeserialize, CanonicalSerialize, Deserializer,
HasDistribution, NonZero, One, PairingEngineExt, Read, Sample, SerializationError, Serializer,
Write, Zero,
use crate::{
pairing::{Pairing, PairingEngineExt},
ratio::{HashToGroup, RatioProof},
util::{
power_pairs, scalar_mul, CanonicalDeserialize, CanonicalSerialize, Deserializer, NonZero,
One, Read, Sample, SerializationError, Serializer, UniformRand, Write,
},
};
use alloc::{vec, vec::Vec};
use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ff::{PrimeField, UniformRand};
use core::{iter, ops::Mul};
use manta_crypto::rand::{CryptoRng, Rand, RngCore};
use manta_crypto::rand::{CryptoRng, RngCore};
use manta_util::{cfg_iter, cfg_iter_mut, from_variant, vec::VecExt};

#[cfg(feature = "rayon")]
use rayon::iter::{IndexedParallelIterator, ParallelIterator};
use manta_util::rayon::iter::{IndexedParallelIterator, ParallelIterator};

/// KZG Trusted Setup Size
pub trait Size {
Expand All @@ -44,42 +45,6 @@ pub trait Size {
const G2_POWERS: usize;
}

/// Pairing Configuration
pub trait Pairing: HasDistribution {
/// Underlying Scalar Field
type Scalar: PrimeField;

/// First Group of the Pairing
type G1: AffineCurve<ScalarField = Self::Scalar>
+ Into<Self::G1Prepared>
+ Sample<Self::Distribution>;

/// First Group Pairing-Prepared Point
type G1Prepared;

/// Second Group of the Pairing
type G2: AffineCurve<ScalarField = Self::Scalar>
+ Into<Self::G2Prepared>
+ Sample<Self::Distribution>;

/// Second Group Pairing-Prepared Point
type G2Prepared;

/// Pairing Engine Type
type Pairing: PairingEngine<
G1Affine = Self::G1,
G2Affine = Self::G2,
G1Prepared = Self::G1Prepared,
G2Prepared = Self::G2Prepared,
>;

/// Returns the base G1 generator for this configuration.
fn g1_prime_subgroup_generator() -> Self::G1;

/// Returns the base G2 generator for this configuration.
fn g2_prime_subgroup_generator() -> Self::G2;
}

/// Trusted Setup Configuration
pub trait Configuration: Pairing + Size {
/// Domain Tag
Expand All @@ -91,21 +56,20 @@ pub trait Configuration: Pairing + Size {
/// Response Type
type Response;

/// Tau Domain Tag for [`hash_to_g2`](Self::hash_to_g2)
/// Hash To Group Type
type HashToGroup: HashToGroup<Self, Self::Challenge>;

/// Tau Domain Tag Type
const TAU_DOMAIN_TAG: Self::DomainTag;

/// Alpha Domain Tag for [`hash_to_g2`](Self::hash_to_g2)
/// Alpha Domain Tag Type
const ALPHA_DOMAIN_TAG: Self::DomainTag;

/// Beta Domain Tag for [`hash_to_g2`](Self::hash_to_g2)
/// Beta Domain Tag Type
const BETA_DOMAIN_TAG: Self::DomainTag;

/// Computes the challenge G2 point from `domain_tag`, `challenge`, and `ratio`.
fn hash_to_g2(
domain_tag: Self::DomainTag,
challenge: &Self::Challenge,
ratio: (&Self::G1, &Self::G1),
) -> Self::G2;
/// Generates a [`HashToGroup`](Self::HashToGroup) instance paramterized by `domain_tag`.
fn hasher(domain_tag: Self::DomainTag) -> Self::HashToGroup;

/// Computes the challenge response from `state`, `challenge`, and `proof`.
fn response(
Expand Down Expand Up @@ -190,9 +154,9 @@ where
R: CryptoRng + RngCore + ?Sized,
{
Some(Proof {
tau: RatioProof::build(C::TAU_DOMAIN_TAG, challenge, &self.tau, rng)?,
alpha: RatioProof::build(C::ALPHA_DOMAIN_TAG, challenge, &self.alpha, rng)?,
beta: RatioProof::build(C::BETA_DOMAIN_TAG, challenge, &self.beta, rng)?,
tau: RatioProof::prove(&C::hasher(C::TAU_DOMAIN_TAG), challenge, &self.tau, rng)?,
alpha: RatioProof::prove(&C::hasher(C::ALPHA_DOMAIN_TAG), challenge, &self.alpha, rng)?,
beta: RatioProof::prove(&C::hasher(C::BETA_DOMAIN_TAG), challenge, &self.beta, rng)?,
})
}
}
Expand All @@ -214,88 +178,6 @@ where
}
}

/// Pairing Ratio Proof of Knowledge
#[derive(derivative::Derivative)]
#[derivative(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct RatioProof<C>
where
C: Pairing + ?Sized,
{
/// Ratio in G1
pub ratio: (C::G1, C::G1),

/// Matching Point in G2
pub matching_point: C::G2,
}

impl<C> RatioProof<C>
where
C: Pairing,
{
/// Builds a [`RatioProof`] for `scalar` against `challenge`.
#[inline]
pub fn build<R>(
domain_tag: C::DomainTag,
challenge: &C::Challenge,
scalar: &C::Scalar,
rng: &mut R,
) -> Option<Self>
where
C: Configuration,
R: CryptoRng + RngCore + ?Sized,
{
let g1_point = rng.gen::<_, C::G1>();
if g1_point.is_zero() {
return None;
}
let scaled_g1_point = g1_point.mul(*scalar).into_affine();
if scaled_g1_point.is_zero() {
return None;
}
let ratio = (g1_point, scaled_g1_point);
let g2_point = C::hash_to_g2(domain_tag, challenge, (&ratio.0, &ratio.1));
if g2_point.is_zero() {
return None;
}
let scaled_g2_point = g2_point.mul(*scalar).into_affine();
if scaled_g2_point.is_zero() {
return None;
}
Some(Self {
ratio,
matching_point: scaled_g2_point,
})
}

/// Computes the challenge point that corresponds with the given `challenge`.
#[inline]
pub fn challenge_point(&self, domain_tag: C::DomainTag, challenge: &C::Challenge) -> C::G2
where
C: Configuration,
{
C::hash_to_g2(domain_tag, challenge, (&self.ratio.0, &self.ratio.1))
}

/// Verifies that `self` is a valid ratio proof-of-knowledge, returning the G2 ratio of the
/// underlying scalar.
#[inline]
pub fn verify(
self,
domain_tag: C::DomainTag,
challenge: &C::Challenge,
) -> Option<(C::G2Prepared, C::G2Prepared)>
where
C: Configuration,
{
let challenge_point = self.challenge_point(domain_tag, challenge);
let ((_, matching_point), (_, challenge_point)) = C::Pairing::same(
(self.ratio.0, self.matching_point),
(self.ratio.1, challenge_point),
)?;
Some((matching_point, challenge_point))
}
}

/// Knowledge Proof Certificate
pub struct KnowledgeProofCertificate<C>
where
Expand Down Expand Up @@ -345,16 +227,19 @@ where
Ok(KnowledgeProofCertificate {
tau: self
.tau
.verify(C::TAU_DOMAIN_TAG, challenge)
.ok_or(KnowledgeError::TauKnowledgeProof)?,
.verify(&C::hasher(C::TAU_DOMAIN_TAG), challenge)
.ok_or(KnowledgeError::TauKnowledgeProof)?
.1,
alpha: self
.alpha
.verify(C::ALPHA_DOMAIN_TAG, challenge)
.ok_or(KnowledgeError::AlphaKnowledgeProof)?,
.verify(&C::hasher(C::ALPHA_DOMAIN_TAG), challenge)
.ok_or(KnowledgeError::AlphaKnowledgeProof)?
.1,
beta: self
.beta
.verify(C::BETA_DOMAIN_TAG, challenge)
.ok_or(KnowledgeError::BetaKnowledgeProof)?,
.verify(&C::hasher(C::BETA_DOMAIN_TAG), challenge)
.ok_or(KnowledgeError::BetaKnowledgeProof)?
.1,
})
}
}
Expand Down Expand Up @@ -502,19 +387,19 @@ where
C: Pairing + Size + ?Sized,
{
/// Vector of Tau Powers in G1 of size [`G1_POWERS`](Size::G1_POWERS)
tau_powers_g1: Vec<C::G1>,
pub tau_powers_g1: Vec<C::G1>,

/// Vector of Tau Powers in G2 of size [`G2_POWERS`](Size::G2_POWERS)
tau_powers_g2: Vec<C::G2>,
pub tau_powers_g2: Vec<C::G2>,

/// Vector of Alpha Multiplied by Tau Powers in G1 of size [`G2_POWERS`](Size::G2_POWERS)
alpha_tau_powers_g1: Vec<C::G1>,
pub alpha_tau_powers_g1: Vec<C::G1>,

/// Vector of Beta Multiplied by Tau Powers in G1 of size [`G2_POWERS`](Size::G2_POWERS)
beta_tau_powers_g1: Vec<C::G1>,
pub beta_tau_powers_g1: Vec<C::G1>,

/// Beta in G2
beta_g2: C::G2,
pub beta_g2: C::G2,
}

impl<C> Accumulator<C>
Expand Down
4 changes: 4 additions & 0 deletions manta-trusted-setup/src/groth16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
//! Groth16 Trusted Setup

pub mod kzg;
pub mod mpc;

#[cfg(test)]
pub mod test;
Loading