Skip to content

Commit

Permalink
merge changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-deng committed Nov 16, 2024
2 parents 1ca436f + 1e32adf commit edea57b
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 26 deletions.
2 changes: 2 additions & 0 deletions plonky2/src/batch_fri/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ mod test {
proof.pow_witness,
k0,
&fri_params.config,
None,
None,
);
let degree_bits = [k0, k1, k2];
let merkle_cap = trace_oracle.batch_merkle_tree.cap;
Expand Down
4 changes: 4 additions & 0 deletions plonky2/src/batch_fri/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ mod tests {
proof.pow_witness,
k,
&fri_params.config,
None,
None,
);

let fri_opening_batch = FriOpeningBatch {
Expand Down Expand Up @@ -440,6 +442,8 @@ mod tests {
proof.pow_witness,
k0,
&fri_params.config,
None,
None,
);
let fri_opening_batch_0 = FriOpenings {
batches: vec![FriOpeningBatch {
Expand Down
27 changes: 26 additions & 1 deletion plonky2/src/fri/challenges.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#[cfg(not(feature = "std"))]
use alloc::vec;

use crate::field::extension::Extendable;
use crate::field::polynomial::PolynomialCoeffs;
use crate::field::types::Field;
use crate::fri::proof::{FriChallenges, FriChallengesTarget};
use crate::fri::structure::{FriOpenings, FriOpeningsTarget};
use crate::fri::FriConfig;
use crate::gadgets::polynomial::PolynomialCoeffsExtTarget;
use crate::hash::hash_types::{MerkleCapTarget, RichField};
use crate::hash::hash_types::{MerkleCapTarget, RichField, NUM_HASH_OUT_ELTS};
use crate::hash::merkle_tree::MerkleCap;
use crate::iop::challenger::{Challenger, RecursiveChallenger};
use crate::iop::target::Target;
Expand All @@ -28,6 +32,8 @@ impl<F: RichField, H: Hasher<F>> Challenger<F, H> {
pow_witness: F,
degree_bits: usize,
config: &FriConfig,
final_poly_coeff_len: Option<usize>,
query_round_step_count: Option<usize>,
) -> FriChallenges<F, D>
where
F: RichField + Extendable<D>,
Expand All @@ -46,7 +52,26 @@ impl<F: RichField, H: Hasher<F>> Challenger<F, H> {
})
.collect();

// When this proof was generated in a circuit with a different number of query steps,
// the challenger needs to observe the additional hash caps.
if let Some(step_count) = query_round_step_count {
let cap_len = (1 << config.cap_height) * NUM_HASH_OUT_ELTS;
let zero_cap = vec![F::ZERO; cap_len];
for _ in commit_phase_merkle_caps.len()..step_count {
self.observe_elements(&zero_cap);
self.get_extension_challenge::<D>();
}
}

self.observe_extension_elements(&final_poly.coeffs);
// When this proof was generated in a circuit with a different final polynomial length,
// the challenger needs to observe the full length of the final polynomial.
if let Some(len) = final_poly_coeff_len {
let current_len = final_poly.coeffs.len();
for _ in current_len..len {
self.observe_extension_element(&F::Extension::ZERO);
}
}

self.observe_element(pow_witness);
let fri_pow_response = self.get_challenge();
Expand Down
2 changes: 2 additions & 0 deletions plonky2/src/plonk/get_challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ fn get_challenges<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, cons
pow_witness,
common_data.degree_bits(),
&config.fri_config,
None,
None,
),
})
}
Expand Down
16 changes: 12 additions & 4 deletions starky/src/fibonacci_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod tests {
&mut TimingTree::default(),
)?;

verify_stark_proof(stark, proof, &config)
verify_stark_proof(stark, proof, &config, None)
}

#[test]
Expand Down Expand Up @@ -223,7 +223,7 @@ mod tests {
None,
&mut TimingTree::default(),
)?;
verify_stark_proof(stark, proof.clone(), &config)?;
verify_stark_proof(stark, proof.clone(), &config, None)?;
assert_eq!(degree_bits, proof.proof.degree_bits);

recursive_proof::<F, C, S, C, D>(stark, proof, &config, true)
Expand Down Expand Up @@ -275,6 +275,10 @@ mod tests {
stark_config.fri_config.num_query_rounds = 1;

let min_degree_bits_to_support = 4;
// Currently, we only support verifier_degree_bits to be {30, 26, 22, 18, …}, as they
// generate the max final polynomial length when using the default configuration
// ConstantArityBits(4, 5). This ensures that for other degrees, the final proof polynomial
// will not be longer than the circuit’s final polynomial length.
let verifier_degree_bits = 30;
let degree_bits = 4..=15;
let verifier_fri_params = stark_config.fri_params(verifier_degree_bits);
Expand Down Expand Up @@ -304,8 +308,12 @@ mod tests {
// Configure the circuit for recursive verification
let num_rows = 1 << verifier_degree_bits;
let stark = S::new(num_rows);
let circuit_config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(circuit_config);
for p in proofs.clone() {
verify_stark_proof(stark, p, &stark_config, Some(verifier_fri_params.clone()))?;
}

let recursive_verification_circuit_config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(recursive_verification_circuit_config);
let zero = builder.zero();

// Set up proof verification within the circuit
Expand Down
30 changes: 28 additions & 2 deletions starky/src/get_challenges.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use plonky2::field::extension::Extendable;
use plonky2::field::polynomial::PolynomialCoeffs;
use plonky2::fri::proof::{FriProof, FriProofTarget};
use plonky2::fri::prover::final_poly_coeff_len;
use plonky2::fri::FriParams;
use plonky2::gadgets::polynomial::PolynomialCoeffsExtTarget;
use plonky2::hash::hash_types::{MerkleCapTarget, RichField};
use plonky2::hash::merkle_tree::MerkleCap;
Expand Down Expand Up @@ -35,6 +37,7 @@ fn get_challenges<F, C, const D: usize>(
pow_witness: F,
config: &StarkConfig,
degree_bits: usize,
verifier_circuit_fri_params: Option<FriParams>,
) -> StarkProofChallenges<F, D>
where
F: RichField + Extendable<D>,
Expand Down Expand Up @@ -67,6 +70,19 @@ where

challenger.observe_openings(&openings.to_fri_openings());

let (final_poly_coeff_len, query_round_step_count) =
if let Some(verifier_circuit_fri_params) = verifier_circuit_fri_params {
(
Some(final_poly_coeff_len(
verifier_circuit_fri_params.degree_bits,
&verifier_circuit_fri_params.reduction_arity_bits,
)),
Some(verifier_circuit_fri_params.reduction_arity_bits.len()),
)
} else {
(None, None)
};

StarkProofChallenges {
lookup_challenge_set,
stark_alphas,
Expand All @@ -77,6 +93,8 @@ where
pow_witness,
degree_bits,
&config.fri_config,
final_poly_coeff_len,
query_round_step_count,
),
}
}
Expand All @@ -99,6 +117,7 @@ where
challenges: Option<&GrandProductChallengeSet<F>>,
ignore_trace_cap: bool,
config: &StarkConfig,
verifier_circuit_fri_params: Option<FriParams>,
) -> StarkProofChallenges<F, D> {
let StarkProof {
trace_cap,
Expand Down Expand Up @@ -133,6 +152,7 @@ where
*pow_witness,
config,
*degree_bits,
verifier_circuit_fri_params,
)
}
}
Expand All @@ -155,10 +175,16 @@ where
challenges: Option<&GrandProductChallengeSet<F>>,
ignore_trace_cap: bool,
config: &StarkConfig,
verifier_circuit_fri_params: Option<FriParams>,
) -> StarkProofChallenges<F, D> {
challenger.observe_elements(&self.public_inputs);
self.proof
.get_challenges(challenger, challenges, ignore_trace_cap, config)
self.proof.get_challenges(
challenger,
challenges,
ignore_trace_cap,
config,
verifier_circuit_fri_params,
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion starky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
//! &mut TimingTree::default(),
//! ).expect("We should have a valid proof!");
//!
//! verify_stark_proof(stark, proof, &CONFIG)
//! verify_stark_proof(stark, proof, &CONFIG, None)
//! .expect("We should be able to verify this proof!")
//! }
//! ```
Expand Down
4 changes: 2 additions & 2 deletions starky/src/permutation_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ mod tests {
&mut TimingTree::default(),
)?;

verify_stark_proof(stark, proof, &config)
verify_stark_proof(stark, proof, &config, None)
}

#[test]
Expand Down Expand Up @@ -194,7 +194,7 @@ mod tests {
None,
&mut TimingTree::default(),
)?;
verify_stark_proof(stark, proof.clone(), &config)?;
verify_stark_proof(stark, proof.clone(), &config, None)?;

recursive_proof::<F, C, S, C, D>(stark, proof, &config, true)
}
Expand Down
34 changes: 21 additions & 13 deletions starky/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use plonky2::field::types::Field;
use plonky2::field::zero_poly_coset::ZeroPolyOnCoset;
use plonky2::fri::oracle::PolynomialBatch;
use plonky2::fri::prover::final_poly_coeff_len;
use plonky2::fri::reduction_strategies::FriReductionStrategy;
use plonky2::fri::FriParams;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::challenger::Challenger;
Expand Down Expand Up @@ -58,6 +59,26 @@ where
fri_params.total_arities() <= degree_bits + rate_bits - cap_height,
"FRI total reduction arity is too large.",
);
let (final_poly_coeff_len, query_round_step_count) =
if let Some(verifier_circuit_fri_params) = verifier_circuit_fri_params {
assert_eq!(verifier_circuit_fri_params.config, fri_params.config);
match &config.fri_config.reduction_strategy {
FriReductionStrategy::ConstantArityBits(_, final_poly_bits) => {
let len = final_poly_coeff_len(
verifier_circuit_fri_params.degree_bits,
&verifier_circuit_fri_params.reduction_arity_bits,
);
assert_eq!(len, 1 << (1 + *final_poly_bits));
(
Some(len),
Some(verifier_circuit_fri_params.reduction_arity_bits.len()),
)
}
_ => panic!("Fri Reduction Strategy is not ConstantArityBits"),
}
} else {
(None, None)
};

let trace_commitment = timed!(
timing,
Expand All @@ -76,19 +97,6 @@ where
let mut challenger = Challenger::new();
challenger.observe_elements(public_inputs);
challenger.observe_cap(&trace_cap);

let (final_poly_coeff_len, query_round_step_count) =
if let Some(verifier_circuit_fri_params) = verifier_circuit_fri_params {
(
Some(final_poly_coeff_len(
verifier_circuit_fri_params.degree_bits,
&verifier_circuit_fri_params.reduction_arity_bits,
)),
Some(verifier_circuit_fri_params.reduction_arity_bits.len()),
)
} else {
(None, None)
};
prove_with_commitment(
&stark,
config,
Expand Down
4 changes: 2 additions & 2 deletions starky/src/unconstrained_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod tests {
let proof =
prove::<F, C, S, D>(stark, &config, trace, &[], None, &mut TimingTree::default())?;

verify_stark_proof(stark, proof, &config)
verify_stark_proof(stark, proof, &config, None)
}

#[test]
Expand Down Expand Up @@ -159,7 +159,7 @@ mod tests {
let trace = stark.generate_trace();
let proof =
prove::<F, C, S, D>(stark, &config, trace, &[], None, &mut TimingTree::default())?;
verify_stark_proof(stark, proof.clone(), &config)?;
verify_stark_proof(stark, proof.clone(), &config, None)?;

recursive_proof::<F, C, S, C, D>(stark, proof, &config, true)
}
Expand Down
10 changes: 9 additions & 1 deletion starky/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use itertools::Itertools;
use plonky2::field::extension::{Extendable, FieldExtension};
use plonky2::field::types::Field;
use plonky2::fri::verifier::verify_fri_proof;
use plonky2::fri::FriParams;
use plonky2::hash::hash_types::RichField;
use plonky2::hash::merkle_tree::MerkleCap;
use plonky2::iop::challenger::Challenger;
Expand All @@ -35,11 +36,18 @@ pub fn verify_stark_proof<
stark: S,
proof_with_pis: StarkProofWithPublicInputs<F, C, D>,
config: &StarkConfig,
verifier_circuit_fri_params: Option<FriParams>,
) -> Result<()> {
ensure!(proof_with_pis.public_inputs.len() == S::PUBLIC_INPUTS);
let mut challenger = Challenger::<F, C::Hasher>::new();

let challenges = proof_with_pis.get_challenges(&mut challenger, None, false, config);
let challenges = proof_with_pis.get_challenges(
&mut challenger,
None,
false,
config,
verifier_circuit_fri_params,
);

verify_stark_proof_with_challenges(
&stark,
Expand Down

0 comments on commit edea57b

Please sign in to comment.