2022-06-27 07:18:21 -07:00
|
|
|
use plonky2::field::extension::Extendable;
|
2022-05-20 11:21:13 +02:00
|
|
|
use plonky2::fri::proof::{FriProof, FriProofTarget};
|
2022-05-18 09:22:58 +02:00
|
|
|
use plonky2::hash::hash_types::RichField;
|
2022-05-04 20:57:07 +02:00
|
|
|
use plonky2::iop::challenger::{Challenger, RecursiveChallenger};
|
|
|
|
|
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
|
|
|
|
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig};
|
|
|
|
|
|
2022-09-22 11:01:27 +02:00
|
|
|
use crate::all_stark::{AllStark, NUM_TABLES};
|
2022-05-04 20:57:07 +02:00
|
|
|
use crate::config::StarkConfig;
|
|
|
|
|
use crate::permutation::{
|
2022-09-23 16:28:20 +02:00
|
|
|
get_grand_product_challenge_set, get_n_grand_product_challenge_sets,
|
|
|
|
|
get_n_grand_product_challenge_sets_target,
|
2022-05-04 20:57:07 +02:00
|
|
|
};
|
|
|
|
|
use crate::proof::*;
|
|
|
|
|
|
2022-05-11 14:35:33 +02:00
|
|
|
impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize> AllProof<F, C, D> {
|
|
|
|
|
/// Computes all Fiat-Shamir challenges used in the STARK proof.
|
|
|
|
|
pub(crate) fn get_challenges(
|
|
|
|
|
&self,
|
|
|
|
|
all_stark: &AllStark<F, D>,
|
|
|
|
|
config: &StarkConfig,
|
|
|
|
|
) -> AllProofChallenges<F, D> {
|
|
|
|
|
let mut challenger = Challenger::<F, C::Hasher>::new();
|
|
|
|
|
|
2022-05-19 09:41:15 +02:00
|
|
|
for proof in &self.stark_proofs {
|
2022-08-25 12:24:22 -07:00
|
|
|
challenger.observe_cap(&proof.trace_cap);
|
2022-05-11 14:35:33 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-25 12:24:22 -07:00
|
|
|
// TODO: Observe public values.
|
|
|
|
|
|
2022-05-12 13:51:02 +02:00
|
|
|
let ctl_challenges =
|
|
|
|
|
get_grand_product_challenge_set(&mut challenger, config.num_challenges);
|
2022-05-11 14:35:33 +02:00
|
|
|
|
2022-08-26 10:12:45 +02:00
|
|
|
let num_permutation_zs = all_stark.nums_permutation_zs(config);
|
|
|
|
|
let num_permutation_batch_sizes = all_stark.permutation_batch_sizes();
|
|
|
|
|
|
2022-05-11 14:35:33 +02:00
|
|
|
AllProofChallenges {
|
2022-08-26 10:12:45 +02:00
|
|
|
stark_challenges: std::array::from_fn(|i| {
|
2022-09-22 11:17:02 +02:00
|
|
|
challenger.duplexing();
|
2022-08-26 10:12:45 +02:00
|
|
|
self.stark_proofs[i].get_challenges(
|
|
|
|
|
&mut challenger,
|
|
|
|
|
num_permutation_zs[i] > 0,
|
|
|
|
|
num_permutation_batch_sizes[i],
|
|
|
|
|
config,
|
|
|
|
|
)
|
|
|
|
|
}),
|
2022-05-11 14:35:33 +02:00
|
|
|
ctl_challenges,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-22 11:01:27 +02:00
|
|
|
pub(crate) fn get_challenger_states(
|
|
|
|
|
&self,
|
|
|
|
|
all_stark: &AllStark<F, D>,
|
|
|
|
|
config: &StarkConfig,
|
|
|
|
|
) -> AllChallengerState<F, D> {
|
|
|
|
|
let mut challenger = Challenger::<F, C::Hasher>::new();
|
|
|
|
|
|
|
|
|
|
for proof in &self.stark_proofs {
|
|
|
|
|
challenger.observe_cap(&proof.trace_cap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Observe public values.
|
|
|
|
|
|
|
|
|
|
let ctl_challenges =
|
|
|
|
|
get_grand_product_challenge_set(&mut challenger, config.num_challenges);
|
|
|
|
|
|
|
|
|
|
let num_permutation_zs = all_stark.nums_permutation_zs(config);
|
|
|
|
|
let num_permutation_batch_sizes = all_stark.permutation_batch_sizes();
|
|
|
|
|
|
|
|
|
|
challenger.duplexing();
|
|
|
|
|
let mut challenger_states = vec![challenger.state()];
|
|
|
|
|
for i in 0..NUM_TABLES {
|
|
|
|
|
self.stark_proofs[i].get_challenges(
|
|
|
|
|
&mut challenger,
|
|
|
|
|
num_permutation_zs[i] > 0,
|
|
|
|
|
num_permutation_batch_sizes[i],
|
|
|
|
|
config,
|
|
|
|
|
);
|
|
|
|
|
challenger.duplexing();
|
|
|
|
|
challenger_states.push(challenger.state());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AllChallengerState {
|
|
|
|
|
states: challenger_states.try_into().unwrap(),
|
|
|
|
|
ctl_challenges,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-11 14:35:33 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-25 12:24:22 -07:00
|
|
|
impl<F, C, const D: usize> StarkProof<F, C, D>
|
2022-05-04 20:57:07 +02:00
|
|
|
where
|
|
|
|
|
F: RichField + Extendable<D>,
|
|
|
|
|
C: GenericConfig<D, F = F>,
|
|
|
|
|
{
|
|
|
|
|
/// Computes all Fiat-Shamir challenges used in the STARK proof.
|
2022-05-19 09:41:15 +02:00
|
|
|
pub(crate) fn get_challenges(
|
2022-05-04 20:57:07 +02:00
|
|
|
&self,
|
2022-05-11 14:35:33 +02:00
|
|
|
challenger: &mut Challenger<F, C::Hasher>,
|
2022-05-20 08:37:18 +02:00
|
|
|
stark_use_permutation: bool,
|
|
|
|
|
stark_permutation_batch_size: usize,
|
2022-05-04 20:57:07 +02:00
|
|
|
config: &StarkConfig,
|
|
|
|
|
) -> StarkProofChallenges<F, D> {
|
2022-08-25 12:24:22 -07:00
|
|
|
let degree_bits = self.recover_degree_bits(config);
|
2022-05-11 14:35:33 +02:00
|
|
|
|
2022-05-04 20:57:07 +02:00
|
|
|
let StarkProof {
|
2022-05-12 22:29:10 +02:00
|
|
|
permutation_ctl_zs_cap,
|
2022-05-04 20:57:07 +02:00
|
|
|
quotient_polys_cap,
|
|
|
|
|
openings,
|
|
|
|
|
opening_proof:
|
|
|
|
|
FriProof {
|
|
|
|
|
commit_phase_merkle_caps,
|
|
|
|
|
final_poly,
|
|
|
|
|
pow_witness,
|
|
|
|
|
..
|
|
|
|
|
},
|
2022-05-12 22:35:13 +02:00
|
|
|
..
|
2022-08-25 12:24:22 -07:00
|
|
|
} = &self;
|
2022-05-04 20:57:07 +02:00
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
let num_challenges = config.num_challenges;
|
2022-05-04 20:57:07 +02:00
|
|
|
|
2022-05-20 08:37:18 +02:00
|
|
|
let permutation_challenge_sets = stark_use_permutation.then(|| {
|
|
|
|
|
get_n_grand_product_challenge_sets(
|
|
|
|
|
challenger,
|
|
|
|
|
num_challenges,
|
|
|
|
|
stark_permutation_batch_size,
|
|
|
|
|
)
|
2022-05-18 09:22:58 +02:00
|
|
|
});
|
2022-05-04 20:57:07 +02:00
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
challenger.observe_cap(permutation_ctl_zs_cap);
|
2022-05-04 20:57:07 +02:00
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
let stark_alphas = challenger.get_n_challenges(num_challenges);
|
2022-05-04 20:57:07 +02:00
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
challenger.observe_cap(quotient_polys_cap);
|
|
|
|
|
let stark_zeta = challenger.get_extension_challenge::<D>();
|
2022-05-04 20:57:07 +02:00
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
challenger.observe_openings(&openings.to_fri_openings());
|
2022-05-04 20:57:07 +02:00
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
StarkProofChallenges {
|
|
|
|
|
permutation_challenge_sets,
|
|
|
|
|
stark_alphas,
|
|
|
|
|
stark_zeta,
|
|
|
|
|
fri_challenges: challenger.fri_challenges::<C, D>(
|
|
|
|
|
commit_phase_merkle_caps,
|
|
|
|
|
final_poly,
|
|
|
|
|
*pow_witness,
|
|
|
|
|
degree_bits,
|
|
|
|
|
&config.fri_config,
|
|
|
|
|
),
|
|
|
|
|
}
|
2022-05-04 20:57:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 12:24:22 -07:00
|
|
|
impl<const D: usize> StarkProofTarget<D> {
|
2022-05-20 11:21:13 +02:00
|
|
|
pub(crate) fn get_challenges<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>>(
|
2022-05-04 20:57:07 +02:00
|
|
|
&self,
|
|
|
|
|
builder: &mut CircuitBuilder<F, D>,
|
2022-05-20 11:21:13 +02:00
|
|
|
challenger: &mut RecursiveChallenger<F, C::Hasher, D>,
|
|
|
|
|
stark_use_permutation: bool,
|
|
|
|
|
stark_permutation_batch_size: usize,
|
2022-05-04 20:57:07 +02:00
|
|
|
config: &StarkConfig,
|
|
|
|
|
) -> StarkProofChallengesTarget<D>
|
|
|
|
|
where
|
|
|
|
|
C::Hasher: AlgebraicHasher<F>,
|
|
|
|
|
{
|
2022-05-20 11:21:13 +02:00
|
|
|
let StarkProofTarget {
|
|
|
|
|
permutation_ctl_zs_cap,
|
|
|
|
|
quotient_polys_cap,
|
|
|
|
|
openings,
|
|
|
|
|
opening_proof:
|
|
|
|
|
FriProofTarget {
|
|
|
|
|
commit_phase_merkle_caps,
|
|
|
|
|
final_poly,
|
|
|
|
|
pow_witness,
|
|
|
|
|
..
|
|
|
|
|
},
|
|
|
|
|
..
|
2022-08-25 12:24:22 -07:00
|
|
|
} = &self;
|
2022-05-20 11:21:13 +02:00
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
let num_challenges = config.num_challenges;
|
2022-05-20 11:21:13 +02:00
|
|
|
|
|
|
|
|
let permutation_challenge_sets = stark_use_permutation.then(|| {
|
|
|
|
|
get_n_grand_product_challenge_sets_target(
|
|
|
|
|
builder,
|
|
|
|
|
challenger,
|
|
|
|
|
num_challenges,
|
|
|
|
|
stark_permutation_batch_size,
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
challenger.observe_cap(permutation_ctl_zs_cap);
|
|
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
let stark_alphas = challenger.get_n_challenges(builder, num_challenges);
|
2022-05-20 11:21:13 +02:00
|
|
|
|
|
|
|
|
challenger.observe_cap(quotient_polys_cap);
|
2022-05-18 09:22:58 +02:00
|
|
|
let stark_zeta = challenger.get_extension_challenge(builder);
|
2022-05-20 11:21:13 +02:00
|
|
|
|
|
|
|
|
challenger.observe_openings(&openings.to_fri_openings(builder.zero()));
|
|
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
StarkProofChallengesTarget {
|
|
|
|
|
permutation_challenge_sets,
|
|
|
|
|
stark_alphas,
|
|
|
|
|
stark_zeta,
|
|
|
|
|
fri_challenges: challenger.fri_challenges::<C>(
|
|
|
|
|
builder,
|
2022-05-20 11:21:13 +02:00
|
|
|
commit_phase_merkle_caps,
|
|
|
|
|
final_poly,
|
|
|
|
|
*pow_witness,
|
2022-05-18 09:22:58 +02:00
|
|
|
&config.fri_config,
|
|
|
|
|
),
|
|
|
|
|
}
|
2022-05-04 20:57:07 +02:00
|
|
|
}
|
|
|
|
|
}
|