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

Rename enum items in ChallengeGenerator #138

Closed
wants to merge 4 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ debug = true
ark-ff = { git = "https://github.com/arkworks-rs/algebra/" }
ark-ec = { git = "https://github.com/arkworks-rs/algebra/" }
ark-serialize = { git = "https://github.com/arkworks-rs/algebra/" }
ark-crypto-primitives = { git = "https://github.com/arkworks-rs/crypto-primitives" }
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std/" }
ark-crypto-primitives = { git = "https://github.com/autquis/crypto-primitives" }
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std/", branch = "add-convert-traits-to-prelude" }

ark-bls12-377 = { git = "https://github.com/arkworks-rs/curves/" }
ark-bls12-381 = { git = "https://github.com/arkworks-rs/curves/" }
8 changes: 4 additions & 4 deletions bench-templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ where
[&labeled_poly],
&coms,
&point,
&mut ChallengeGenerator::new_univariate(&mut test_sponge()),
&mut ChallengeGenerator::new_correlated(&mut test_sponge()),
&randomness,
Some(rng),
)
Expand Down Expand Up @@ -156,7 +156,7 @@ where
[&labeled_poly],
&coms,
&point,
&mut ChallengeGenerator::new_univariate(&mut test_sponge()),
&mut ChallengeGenerator::new_correlated(&mut test_sponge()),
&randomness,
Some(rng),
)
Expand Down Expand Up @@ -193,7 +193,7 @@ where
[&labeled_poly],
&coms,
&point,
&mut ChallengeGenerator::new_univariate(&mut test_sponge()),
&mut ChallengeGenerator::new_correlated(&mut test_sponge()),
&randomness,
Some(rng),
)
Expand All @@ -206,7 +206,7 @@ where
&point,
[claimed_eval],
&proof,
&mut ChallengeGenerator::new_univariate(&mut test_sponge()),
&mut ChallengeGenerator::new_correlated(&mut test_sponge()),
None,
)
.unwrap();
Expand Down
40 changes: 20 additions & 20 deletions poly-commit/src/challenge.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
use ark_crypto_primitives::sponge::{CryptographicSponge, FieldElementSize};
use ark_ff::PrimeField;

/// `ChallengeGenerator` generates opening challenges using multivariate or univariate strategy.
/// For multivariate strategy, each challenge is freshly squeezed from a sponge.
/// For univariate strategy, each challenge is a power of one squeezed element from sponge.
/// `ChallengeGenerator` generates opening challenges using independent or correlated strategy.
/// For independent strategy, each challenge is freshly squeezed from a sponge.
/// For correlated strategy, each challenge is a power of one squeezed element from sponge.
///
/// Note that mutable reference cannot be cloned.
#[derive(Clone)]
pub enum ChallengeGenerator<F: PrimeField, S: CryptographicSponge> {
/// Each challenge is freshly squeezed from a sponge.
Multivariate(S),
Independent(S),
/// Each challenge is a power of one squeezed element from sponge.
///
/// `Univariate(generator, next_element)`
Univariate(F, F),
/// `Correlated(generator, next_element)`
Correlated(F, F),
}

impl<F: PrimeField, S: CryptographicSponge> ChallengeGenerator<F, S> {
/// Returns a challenge generator with multivariate strategy. Each challenge is freshly squeezed
/// Returns a challenge generator with independent strategy. Each challenge is freshly squeezed
/// from a sponge.
pub fn new_multivariate(sponge: S) -> Self {
Self::Multivariate(sponge)
pub fn new_independent(sponge: S) -> Self {
Self::Independent(sponge)
}

/// Returns a challenge generator with univariate strategy. Each challenge is a power of one
/// Returns a challenge generator with correlated strategy. Each challenge is a power of one
/// squeezed element from sponge.
pub fn new_univariate(sponge: &mut S) -> Self {
pub fn new_correlated(sponge: &mut S) -> Self {
let gen = sponge.squeeze_field_elements(1)[0];
Self::Univariate(gen, gen)
Self::Correlated(gen, gen)
}

/// Returns a challenge of size `size`.
/// * If `self == Self::Multivariate(...)`, then this squeezes out a challenge of size `size`.
/// * If `self == Self::Univariate(...)`, then this ignores the `size` argument and simply squeezes out
/// * If `self == Self::Independent(...)`, then this squeezes out a challenge of size `size`.
/// * If `self == Self::Correlated(...)`, then this ignores the `size` argument and simply squeezes out
/// the next field element.
pub fn try_next_challenge_of_size(&mut self, size: FieldElementSize) -> F {
match self {
// multivariate (full)
Self::Multivariate(sponge) => sponge.squeeze_field_elements_with_sizes(&[size])[0],
// univariate
Self::Univariate(gen, next) => {
// independent (full)
Self::Independent(sponge) => sponge.squeeze_field_elements_with_sizes(&[size])[0],
// correlated
Self::Correlated(gen, next) => {
let result = next.clone();
*next *= *gen;
result
Expand All @@ -51,10 +51,10 @@ impl<F: PrimeField, S: CryptographicSponge> ChallengeGenerator<F, S> {
self.try_next_challenge_of_size(FieldElementSize::Full)
}

/// Returns the sponge state if `self` is multivariate. Returns `None` otherwise.
/// Returns the sponge state if `self` is independent. Returns `None` otherwise.
pub fn into_sponge(self) -> Option<S> {
match self {
Self::Multivariate(s) => Some(s),
Self::Independent(s) => Some(s),
_ => None,
}
}
Expand Down
20 changes: 10 additions & 10 deletions poly-commit/src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
use ark_crypto_primitives::sponge::CryptographicSponge;
use ark_ff::PrimeField;
use ark_poly::Polynomial;
use ark_r1cs_std::fields::nonnative::NonNativeFieldVar;
use ark_r1cs_std::fields::emulated_fp::EmulatedFpVar;
use ark_r1cs_std::{fields::fp::FpVar, prelude::*};
use ark_relations::r1cs::{ConstraintSystemRef, Namespace, Result as R1CSResult, SynthesisError};
use ark_std::{borrow::Borrow, cmp::Eq, cmp::PartialEq, hash::Hash, marker::Sized};
Expand All @@ -24,8 +24,8 @@ pub enum LinearCombinationCoeffVar<TargetField: PrimeField, BaseField: PrimeFiel
One,
/// Coefficient -1.
MinusOne,
/// Other coefficient, represented as a nonnative field element.
Var(NonNativeFieldVar<TargetField, BaseField>),
/// Other coefficient, represented as a "emulated" field element.
Var(EmulatedFpVar<TargetField, BaseField>),
}

/// An allocated version of `LinearCombination`.
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<TargetField: PrimeField, BaseField: PrimeField>
let (f, lc_term) = term;

let fg =
NonNativeFieldVar::new_variable(ark_relations::ns!(cs, "term"), || Ok(f), mode)
EmulatedFpVar::new_variable(ark_relations::ns!(cs, "term"), || Ok(f), mode)
.unwrap();

(LinearCombinationCoeffVar::Var(fg), lc_term.clone())
Expand All @@ -79,12 +79,12 @@ impl<TargetField: PrimeField, BaseField: PrimeField>
pub struct PCCheckRandomDataVar<TargetField: PrimeField, BaseField: PrimeField> {
/// Opening challenges.
/// The prover and the verifier MUST use the same opening challenges.
pub opening_challenges: Vec<NonNativeFieldVar<TargetField, BaseField>>,
pub opening_challenges: Vec<EmulatedFpVar<TargetField, BaseField>>,
/// Bit representations of the opening challenges.
pub opening_challenges_bits: Vec<Vec<Boolean<BaseField>>>,
/// Batching random numbers.
/// The verifier can choose these numbers freely, as long as they are random.
pub batching_rands: Vec<NonNativeFieldVar<TargetField, BaseField>>,
pub batching_rands: Vec<EmulatedFpVar<TargetField, BaseField>>,
/// Bit representations of the batching random numbers.
pub batching_rands_bits: Vec<Vec<Boolean<BaseField>>>,
}
Expand Down Expand Up @@ -172,7 +172,7 @@ pub struct LabeledPointVar<TargetField: PrimeField, BaseField: PrimeField> {
/// MUST be a unique identifier in a query set.
pub name: String,
/// The point value.
pub value: NonNativeFieldVar<TargetField, BaseField>,
pub value: EmulatedFpVar<TargetField, BaseField>,
}

/// An allocated version of `QuerySet`.
Expand All @@ -184,16 +184,16 @@ pub struct QuerySetVar<TargetField: PrimeField, BaseField: PrimeField>(
/// An allocated version of `Evaluations`.
#[derive(Clone)]
pub struct EvaluationsVar<TargetField: PrimeField, BaseField: PrimeField>(
pub HashMap<LabeledPointVar<TargetField, BaseField>, NonNativeFieldVar<TargetField, BaseField>>,
pub HashMap<LabeledPointVar<TargetField, BaseField>, EmulatedFpVar<TargetField, BaseField>>,
);

impl<TargetField: PrimeField, BaseField: PrimeField> EvaluationsVar<TargetField, BaseField> {
/// find the evaluation result
pub fn get_lc_eval(
&self,
lc_string: &str,
point: &NonNativeFieldVar<TargetField, BaseField>,
) -> Result<NonNativeFieldVar<TargetField, BaseField>, SynthesisError> {
point: &EmulatedFpVar<TargetField, BaseField>,
) -> Result<EmulatedFpVar<TargetField, BaseField>, SynthesisError> {
let key = LabeledPointVar::<TargetField, BaseField> {
name: String::from(lc_string),
value: point.clone(),
Expand Down
12 changes: 6 additions & 6 deletions poly-commit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,8 @@ pub mod tests {
S: CryptographicSponge,
{
let challenge_generators = vec![
ChallengeGenerator::new_multivariate(sponge()),
ChallengeGenerator::new_univariate(&mut sponge()),
ChallengeGenerator::new_independent(sponge()),
ChallengeGenerator::new_correlated(&mut sponge()),
];

for challenge_gen in challenge_generators {
Expand Down Expand Up @@ -774,8 +774,8 @@ pub mod tests {
} = info;

let challenge_gens = vec![
ChallengeGenerator::new_multivariate(sponge()),
ChallengeGenerator::new_univariate(&mut sponge()),
ChallengeGenerator::new_independent(sponge()),
ChallengeGenerator::new_correlated(&mut sponge()),
];

for challenge_gen in challenge_gens {
Expand Down Expand Up @@ -919,8 +919,8 @@ pub mod tests {
} = info;

let challenge_gens = vec![
ChallengeGenerator::new_multivariate(sponge()),
ChallengeGenerator::new_univariate(&mut sponge()),
ChallengeGenerator::new_independent(sponge()),
ChallengeGenerator::new_correlated(&mut sponge()),
];

for challenge_gen in challenge_gens {
Expand Down
Loading