Skip to content

Commit

Permalink
pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-deng committed Nov 7, 2024
1 parent e685b9a commit 02548a0
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 120 deletions.
2 changes: 0 additions & 2 deletions plonky2/src/batch_fri/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ mod test {
hiding: false,
degree_bits: k0,
reduction_arity_bits,
final_poly_coeff_len: None,
min_degree_bits_to_support: None,
};

let n0 = 1 << k0;
Expand Down
4 changes: 0 additions & 4 deletions plonky2/src/batch_fri/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ mod tests {
hiding: false,
degree_bits: k,
reduction_arity_bits,
final_poly_coeff_len: None,
min_degree_bits_to_support: None,
};

let n = 1 << k;
Expand Down Expand Up @@ -357,8 +355,6 @@ mod tests {
hiding: false,
degree_bits: k0,
reduction_arity_bits,
final_poly_coeff_len: None,
min_degree_bits_to_support: None,
};

let n0 = 1 << k0;
Expand Down
19 changes: 1 addition & 18 deletions plonky2/src/fri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ impl FriConfig {
1.0 / ((1 << self.rate_bits) as f64)
}

pub fn fri_params(
&self,
degree_bits: usize,
final_poly_coeff_len: Option<usize>,
min_degree_bits_to_support: Option<usize>,
hiding: bool,
) -> FriParams {
pub fn fri_params(&self, degree_bits: usize, hiding: bool) -> FriParams {
let reduction_arity_bits = self.reduction_strategy.reduction_arity_bits(
degree_bits,
self.rate_bits,
Expand All @@ -63,8 +57,6 @@ impl FriConfig {
hiding,
degree_bits,
reduction_arity_bits,
final_poly_coeff_len,
min_degree_bits_to_support,
}
}

Expand All @@ -91,15 +83,6 @@ pub struct FriParams {
/// a 4-to-1 reduction, then a 2-to-1 reduction. After these reductions, the reduced polynomial
/// is sent directly.
pub reduction_arity_bits: Vec<usize>,

/// The length of the final polynomial's coefficients.
/// This is used only when the proof will be verified in a circuit generated with a
/// larger degree bit size.
pub final_poly_coeff_len: Option<usize>,

/// Specifies the minimum degree bit size to support verification of proofs with
/// varying degree bits.
pub min_degree_bits_to_support: Option<usize>,
}

impl FriParams {
Expand Down
2 changes: 2 additions & 0 deletions plonky2/src/fri/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
oracles: &[&Self],
challenger: &mut Challenger<F, C::Hasher>,
fri_params: &FriParams,
final_poly_coeff_len: Option<usize>,
timing: &mut TimingTree,
) -> FriProof<F, C::Hasher, D> {
assert!(D > 1, "Not implemented for D=1.");
Expand Down Expand Up @@ -226,6 +227,7 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
lde_final_values,
challenger,
fri_params,
final_poly_coeff_len,
timing,
);

Expand Down
10 changes: 5 additions & 5 deletions plonky2/src/fri/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn fri_proof<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const
lde_polynomial_values: PolynomialValues<F::Extension>,
challenger: &mut Challenger<F, C::Hasher>,
fri_params: &FriParams,
final_poly_coeff_len: Option<usize>,
timing: &mut TimingTree,
) -> FriProof<F, C::Hasher, D> {
let n = lde_polynomial_values.len();
Expand All @@ -41,6 +42,7 @@ pub fn fri_proof<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const
lde_polynomial_values,
challenger,
fri_params,
final_poly_coeff_len,
)
);

Expand Down Expand Up @@ -68,10 +70,7 @@ pub(crate) type FriCommitedTrees<F, C, const D: usize> = (
PolynomialCoeffs<<F as Extendable<D>>::Extension>,
);

fn final_poly_coeff_len(
mut degree_bits: usize,
reduction_arity_bits: &Vec<usize>,
) -> usize {
pub fn final_poly_coeff_len(mut degree_bits: usize, reduction_arity_bits: &Vec<usize>) -> usize {
for arity_bits in reduction_arity_bits {
degree_bits -= *arity_bits;
}
Expand All @@ -83,6 +82,7 @@ fn fri_committed_trees<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>,
mut values: PolynomialValues<F::Extension>,
challenger: &mut Challenger<F, C::Hasher>,
fri_params: &FriParams,
final_poly_coeff_len: Option<usize>,
) -> FriCommitedTrees<F, C, D> {
let mut trees = Vec::with_capacity(fri_params.reduction_arity_bits.len());

Expand Down Expand Up @@ -122,7 +122,7 @@ fn fri_committed_trees<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>,
challenger.observe_extension_elements(&coeffs.coeffs);
// When verifying this proof 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) = fri_params.final_poly_coeff_len {
if let Some(len) = final_poly_coeff_len {
let current_len = coeffs.coeffs.len();
for _ in current_len..len {
challenger.observe_extension_element(&F::Extension::ZERO);
Expand Down
7 changes: 4 additions & 3 deletions plonky2/src/fri/recursive_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
proof: &FriProofTarget<D>,
params: &FriParams,
current_degree_bits: Target,
min_degree_bits_to_support: usize,
) where
C::Hasher: AlgebraicHasher<F>,
{
Expand All @@ -206,8 +207,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
let log_n = params.config.rate_bits + params.degree_bits;
let mut current_log_n = self.constant(F::from_canonical_usize(params.config.rate_bits));
current_log_n = self.add(current_log_n, current_degree_bits);
let min_log_n_to_support =
log_n - (params.degree_bits - params.min_degree_bits_to_support.unwrap());
let min_log_n_to_support = log_n - (params.degree_bits - min_degree_bits_to_support);

with_context!(
self,
Expand Down Expand Up @@ -528,7 +528,8 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {

let g = self.constant(F::coset_shift());
// `subgroup_x` is `subgroup[x_index]`, i.e., the actual field element in the domain.
let subgroup_x_vec: Vec<_> = log_n_range.clone()
let subgroup_x_vec: Vec<_> = log_n_range
.clone()
.map(|n| {
with_context!(self, "compute x from its index", {
let phi = F::primitive_root_of_unity(n);
Expand Down
20 changes: 6 additions & 14 deletions plonky2/src/plonk/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,18 +827,10 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
(gate_idx, slot_idx)
}

fn fri_params(
&self,
degree_bits: usize,
final_poly_coeff_len: Option<usize>,
min_degree_bits_to_support: Option<usize>,
) -> FriParams {
self.config.fri_config.fri_params(
degree_bits,
final_poly_coeff_len,
min_degree_bits_to_support,
self.config.zero_knowledge,
)
fn fri_params(&self, degree_bits: usize) -> FriParams {
self.config
.fri_config
.fri_params(degree_bits, self.config.zero_knowledge)
}

/// The number of (base field) `arithmetic` operations that can be performed in a single gate.
Expand All @@ -863,7 +855,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
let degree_bits_estimate = log2_strict(degree_estimate);
let fri_queries = self.config.fri_config.num_query_rounds;
let arities: Vec<usize> = self
.fri_params(degree_bits_estimate, None, None)
.fri_params(degree_bits_estimate)
.reduction_arity_bits
.iter()
.map(|x| 1 << x)
Expand Down Expand Up @@ -1133,7 +1125,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
let degree = self.gate_instances.len();
debug!("Degree after blinding & padding: {}", degree);
let degree_bits = log2_strict(degree);
let fri_params = self.fri_params(degree_bits, None, None);
let fri_params = self.fri_params(degree_bits);
assert!(
fri_params.total_arities() <= degree_bits + rate_bits - cap_height,
"FRI total reduction arity is too large.",
Expand Down
1 change: 1 addition & 0 deletions plonky2/src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ where
],
&mut challenger,
&common_data.fri_params,
None,
timing,
)
);
Expand Down
2 changes: 0 additions & 2 deletions plonky2/src/recursion/dummy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ where
hiding: false,
degree_bits: 0,
reduction_arity_bits: vec![],
final_poly_coeff_len: None,
min_degree_bits_to_support: None,
},
gates: vec![],
selectors_info: SelectorsInfo {
Expand Down
29 changes: 0 additions & 29 deletions plonky2/src/util/serialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,26 +681,11 @@ pub trait Read {
let reduction_arity_bits = self.read_usize_vec()?;
let degree_bits = self.read_usize()?;
let hiding = self.read_bool()?;
let has_final_poly_coeff_len = self.read_bool()?;
let final_poly_coeff_len = if has_final_poly_coeff_len {
Some(self.read_usize()?)
} else {
None
};
let has_min_degree_bits_to_support = self.read_bool()?;
let min_degree_bits_to_support = if has_min_degree_bits_to_support {
Some(self.read_usize()?)
} else {
None
};

Ok(FriParams {
config,
reduction_arity_bits,
degree_bits,
hiding,
final_poly_coeff_len,
min_degree_bits_to_support,
})
}

Expand Down Expand Up @@ -1690,26 +1675,12 @@ pub trait Write {
reduction_arity_bits,
degree_bits,
hiding,
final_poly_coeff_len,
min_degree_bits_to_support,
} = fri_params;

self.write_fri_config(config)?;
self.write_usize_vec(reduction_arity_bits.as_slice())?;
self.write_usize(*degree_bits)?;
self.write_bool(*hiding)?;
if let Some(len) = final_poly_coeff_len {
self.write_bool(true)?;
self.write_usize(*len)?;
} else {
self.write_bool(false)?;
}
if let Some(db) = min_degree_bits_to_support {
self.write_bool(true)?;
self.write_usize(*db)?;
} else {
self.write_bool(false)?;
}

Ok(())
}
Expand Down
14 changes: 2 additions & 12 deletions starky/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,8 @@ impl StarkConfig {
}

/// Outputs the [`FriParams`] used during the FRI sub-protocol by this [`StarkConfig`].
pub fn fri_params(
&self,
degree_bits: usize,
final_poly_coeff_len: Option<usize>,
min_degree_bits_to_support: Option<usize>,
) -> FriParams {
self.fri_config.fri_params(
degree_bits,
final_poly_coeff_len,
min_degree_bits_to_support,
false,
)
pub fn fri_params(&self, degree_bits: usize) -> FriParams {
self.fri_config.fri_params(degree_bits, false)
}

/// Checks that this STARK configuration is consistent, i.e. that the different
Expand Down
25 changes: 18 additions & 7 deletions starky/src/fibonacci_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ mod tests {
&config,
trace,
&public_inputs,
None,
&mut TimingTree::default(),
)?;

Expand Down Expand Up @@ -215,12 +216,13 @@ mod tests {
&config,
trace,
&public_inputs,
None,
&mut TimingTree::default(),
)?;
verify_stark_proof(stark, proof.clone(), &config)?;
assert_eq!(degree_bits, proof.proof.recover_degree_bits(&config));

recursive_proof::<F, C, S, C, D>(stark, proof, &config, degree_bits, true)
recursive_proof::<F, C, S, C, D>(stark, proof, &config, degree_bits, None, true)
}

fn recursive_proof<
Expand All @@ -234,6 +236,7 @@ mod tests {
inner_proof: StarkProofWithPublicInputs<F, InnerC, D>,
inner_config: &StarkConfig,
degree_bits: usize,
min_degree_bits_to_support: Option<usize>,
print_gate_counts: bool,
) -> Result<()>
where
Expand All @@ -259,6 +262,7 @@ mod tests {
pt,
inner_config,
degree_bits,
min_degree_bits_to_support,
);

if print_gate_counts {
Expand All @@ -281,8 +285,10 @@ mod tests {
let mut config = StarkConfig::standard_fast_config();
config.fri_config.num_query_rounds = 8;

// Test first STARK
let degree_bits0 = 7;
let degree_bits1 = 8;

// Test first STARK
let num_rows = 1 << degree_bits0;
let public_inputs = [F::ZERO, F::ONE, fibonacci(num_rows - 1, F::ZERO, F::ONE)];
let stark0 = S::new(num_rows);
Expand All @@ -292,13 +298,11 @@ mod tests {
&config,
trace,
&public_inputs,
Some(degree_bits1),
&mut TimingTree::default(),
)?;
// verify_stark_proof(stark0, proof0.clone(), &config)?;
// recursive_proof::<F, C, S, C, D>(stark0, proof0.clone(), &config, degree_bits0, true)?;

// Test second STARK
let degree_bits1 = 8;
let num_rows = 1 << degree_bits1;
let public_inputs = [F::ZERO, F::ONE, fibonacci(num_rows - 1, F::ZERO, F::ONE)];
let stark1 = S::new(num_rows);
Expand All @@ -308,13 +312,20 @@ mod tests {
&config,
trace,
&public_inputs,
None,
&mut TimingTree::default(),
)?;
verify_stark_proof(stark1, proof1.clone(), &config)?;

// Verify proof0 with the recursion circuit at different degree.
// recursive_proof::<F, C, S, C, D>(stark1, proof1, &config, degree_bits1, true)?;
recursive_proof::<F, C, S, C, D>(stark1, proof0, &config, degree_bits1, true)?;
recursive_proof::<F, C, S, C, D>(
stark1,
proof0,
&config,
degree_bits1,
Some(degree_bits0),
true,
)?;
Ok(())
}
}
1 change: 1 addition & 0 deletions starky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
//! &CONFIG,
//! trace,
//! &public_inputs,
//! None,
//! &mut TimingTree::default(),
//! ).expect("We should have a valid proof!");
//!
Expand Down
Loading

0 comments on commit 02548a0

Please sign in to comment.