This commit is contained in:
wborgeaud 2022-02-07 11:47:58 +01:00
parent afe89a61f4
commit 61fcc9048e
3 changed files with 87 additions and 89 deletions

View File

@ -3,9 +3,12 @@ use plonky2_field::extension_field::Extendable;
use plonky2_util::{log2_strict, reverse_index_bits_in_place}; use plonky2_util::{log2_strict, reverse_index_bits_in_place};
use crate::fri::proof::{ use crate::fri::proof::{
FriInitialTreeProofTarget, FriProofTarget, FriQueryRoundTarget, FriQueryStepTarget, FriChallengesTarget, FriInitialTreeProofTarget, FriProofTarget, FriQueryRoundTarget,
FriQueryStepTarget,
};
use crate::fri::structure::{
FriBatchInfoTarget, FriInstanceInfoTarget, FriOpenings, FriOpeningsTarget,
}; };
use crate::fri::structure::{FriBatchInfoTarget, FriInstanceInfoTarget, FriOpeningsTarget};
use crate::fri::{FriConfig, FriParams}; use crate::fri::{FriConfig, FriParams};
use crate::gadgets::interpolation::InterpolationGate; use crate::gadgets::interpolation::InterpolationGate;
use crate::gates::gate::Gate; use crate::gates::gate::Gate;
@ -107,16 +110,11 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
fn fri_verify_proof_of_work<H: AlgebraicHasher<F>>( fn fri_verify_proof_of_work<H: AlgebraicHasher<F>>(
&mut self, &mut self,
proof: &FriProofTarget<D>, fri_pow_response: Target,
challenger: &mut RecursiveChallenger<F, H, D>,
config: &FriConfig, config: &FriConfig,
) { ) {
let mut inputs = challenger.get_hash(self).elements.to_vec();
inputs.push(proof.pow_witness);
let hash = self.hash_n_to_m_no_pad::<H>(inputs, 1)[0];
self.assert_leading_zeros( self.assert_leading_zeros(
hash, fri_pow_response,
config.proof_of_work_bits + (64 - F::order().bits()) as u32, config.proof_of_work_bits + (64 - F::order().bits()) as u32,
); );
} }
@ -124,11 +122,10 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
pub fn verify_fri_proof<C: GenericConfig<D, F = F>>( pub fn verify_fri_proof<C: GenericConfig<D, F = F>>(
&mut self, &mut self,
instance: &FriInstanceInfoTarget<D>, instance: &FriInstanceInfoTarget<D>,
// Openings of the PLONK polynomials. os: &FriOpeningsTarget<D>,
os: &OpeningSetTarget<D>, challenges: &FriChallengesTarget<D>,
initial_merkle_caps: &[MerkleCapTarget], initial_merkle_caps: &[MerkleCapTarget],
proof: &FriProofTarget<D>, proof: &FriProofTarget<D>,
challenger: &mut RecursiveChallenger<F, C::Hasher, D>,
params: &FriParams, params: &FriParams,
) where ) where
C::Hasher: AlgebraicHasher<F>, C::Hasher: AlgebraicHasher<F>,
@ -146,29 +143,10 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
// Size of the LDE domain. // Size of the LDE domain.
let n = params.lde_size(); let n = params.lde_size();
challenger.observe_openings(&os.to_fri_openings());
// Scaling factor to combine polynomials.
let alpha = challenger.get_extension_challenge(self);
let betas = with_context!(
self,
"recover the random betas used in the FRI reductions.",
proof
.commit_phase_merkle_caps
.iter()
.map(|cap| {
challenger.observe_cap(cap);
challenger.get_extension_challenge(self)
})
.collect::<Vec<_>>()
);
challenger.observe_extension_elements(&proof.final_poly.0);
with_context!( with_context!(
self, self,
"check PoW", "check PoW",
self.fri_verify_proof_of_work::<C::Hasher>(proof, challenger, &params.config) self.fri_verify_proof_of_work::<C::Hasher>(challenges.fri_pow_response, &params.config)
); );
// Check that parameters are coherent. // Check that parameters are coherent.
@ -181,7 +159,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
let precomputed_reduced_evals = with_context!( let precomputed_reduced_evals = with_context!(
self, self,
"precompute reduced evaluations", "precompute reduced evaluations",
PrecomputedReducedOpeningsTarget::from_os_and_alpha(&os.to_fri_openings(), alpha, self) PrecomputedReducedOpeningsTarget::from_os_and_alpha(os, challenges.fri_alpha, self)
); );
for (i, round_proof) in proof.query_round_proofs.iter().enumerate() { for (i, round_proof) in proof.query_round_proofs.iter().enumerate() {
@ -201,13 +179,12 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
&format!("verify one (of {}) query rounds", num_queries), &format!("verify one (of {}) query rounds", num_queries),
self.fri_verifier_query_round::<C>( self.fri_verifier_query_round::<C>(
instance, instance,
alpha, challenges,
&precomputed_reduced_evals, &precomputed_reduced_evals,
initial_merkle_caps, initial_merkle_caps,
proof, proof,
challenger, challenges.fri_query_indices[i],
n, n,
&betas,
round_proof, round_proof,
params, params,
) )
@ -291,13 +268,12 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
fn fri_verifier_query_round<C: GenericConfig<D, F = F>>( fn fri_verifier_query_round<C: GenericConfig<D, F = F>>(
&mut self, &mut self,
instance: &FriInstanceInfoTarget<D>, instance: &FriInstanceInfoTarget<D>,
alpha: ExtensionTarget<D>, challenges: &FriChallengesTarget<D>,
precomputed_reduced_evals: &PrecomputedReducedOpeningsTarget<D>, precomputed_reduced_evals: &PrecomputedReducedOpeningsTarget<D>,
initial_merkle_caps: &[MerkleCapTarget], initial_merkle_caps: &[MerkleCapTarget],
proof: &FriProofTarget<D>, proof: &FriProofTarget<D>,
challenger: &mut RecursiveChallenger<F, C::Hasher, D>, x_index: Target,
n: usize, n: usize,
betas: &[ExtensionTarget<D>],
round_proof: &FriQueryRoundTarget<D>, round_proof: &FriQueryRoundTarget<D>,
params: &FriParams, params: &FriParams,
) where ) where
@ -308,7 +284,6 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
// Note that this `low_bits` decomposition permits non-canonical binary encodings. Here we // Note that this `low_bits` decomposition permits non-canonical binary encodings. Here we
// verify that this has a negligible impact on soundness error. // verify that this has a negligible impact on soundness error.
Self::assert_noncanonical_indices_ok(&params.config); Self::assert_noncanonical_indices_ok(&params.config);
let x_index = challenger.get_challenge(self);
let mut x_index_bits = self.low_bits(x_index, n_log, F::BITS); let mut x_index_bits = self.low_bits(x_index, n_log, F::BITS);
let cap_index = let cap_index =
@ -341,7 +316,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
self.fri_combine_initial::<C>( self.fri_combine_initial::<C>(
instance, instance,
&round_proof.initial_trees_proof, &round_proof.initial_trees_proof,
alpha, challenges.fri_alpha,
subgroup_x, subgroup_x,
precomputed_reduced_evals, precomputed_reduced_evals,
params, params,
@ -368,7 +343,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
x_index_within_coset_bits, x_index_within_coset_bits,
arity_bits, arity_bits,
evals, evals,
betas[i], challenges.fri_betas[i],
) )
); );

View File

@ -3,7 +3,7 @@ use std::collections::HashSet;
use plonky2_field::extension_field::Extendable; use plonky2_field::extension_field::Extendable;
use plonky2_field::polynomial::PolynomialCoeffs; use plonky2_field::polynomial::PolynomialCoeffs;
use crate::fri::proof::{CompressedFriProof, FriChallenges, FriProof}; use crate::fri::proof::{CompressedFriProof, FriChallenges, FriProof, FriProofTarget};
use crate::fri::verifier::{compute_evaluation, fri_combine_initial, PrecomputedReducedOpenings}; use crate::fri::verifier::{compute_evaluation, fri_combine_initial, PrecomputedReducedOpenings};
use crate::gadgets::polynomial::PolynomialCoeffsExtTarget; use crate::gadgets::polynomial::PolynomialCoeffsExtTarget;
use crate::hash::hash_types::{HashOutTarget, MerkleCapTarget, RichField}; use crate::hash::hash_types::{HashOutTarget, MerkleCapTarget, RichField};
@ -15,7 +15,8 @@ use crate::plonk::circuit_data::CommonCircuitData;
use crate::plonk::config::{AlgebraicHasher, GenericConfig, Hasher}; use crate::plonk::config::{AlgebraicHasher, GenericConfig, Hasher};
use crate::plonk::proof::{ use crate::plonk::proof::{
CompressedProof, CompressedProofWithPublicInputs, FriInferredElements, OpeningSet, CompressedProof, CompressedProofWithPublicInputs, FriInferredElements, OpeningSet,
OpeningSetTarget, Proof, ProofChallenges, ProofChallengesTarget, ProofWithPublicInputs, OpeningSetTarget, Proof, ProofChallenges, ProofChallengesTarget, ProofTarget,
ProofWithPublicInputs, ProofWithPublicInputsTarget,
}; };
use crate::util::reverse_bits; use crate::util::reverse_bits;
@ -277,3 +278,44 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
} }
} }
} }
impl<const D: usize> ProofWithPublicInputsTarget<D> {
pub(crate) fn get_challenges<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>>(
&self,
builder: &mut CircuitBuilder<F, D>,
public_inputs_hash: HashOutTarget,
inner_common_data: &CommonCircuitData<F, C, D>,
) -> ProofChallengesTarget<D>
where
C::Hasher: AlgebraicHasher<F>,
{
let ProofTarget {
wires_cap,
plonk_zs_partial_products_cap,
quotient_polys_cap,
openings,
opening_proof:
FriProofTarget {
commit_phase_merkle_caps,
final_poly,
pow_witness,
..
},
} = &self.proof;
let public_inputs_hash =
builder.hash_n_to_hash_no_pad::<C::InnerHasher>(self.public_inputs.clone());
builder.get_challenges(
public_inputs_hash,
wires_cap,
plonk_zs_partial_products_cap,
quotient_polys_cap,
openings,
commit_phase_merkle_caps,
final_poly,
*pow_witness,
inner_common_data,
)
}
}

View File

@ -5,7 +5,9 @@ use crate::iop::challenger::RecursiveChallenger;
use crate::plonk::circuit_builder::CircuitBuilder; use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::{CommonCircuitData, VerifierCircuitTarget}; use crate::plonk::circuit_data::{CommonCircuitData, VerifierCircuitTarget};
use crate::plonk::config::{AlgebraicHasher, GenericConfig}; use crate::plonk::config::{AlgebraicHasher, GenericConfig};
use crate::plonk::proof::{OpeningSetTarget, ProofTarget, ProofWithPublicInputsTarget}; use crate::plonk::proof::{
OpeningSetTarget, ProofChallengesTarget, ProofTarget, ProofWithPublicInputsTarget,
};
use crate::plonk::vanishing_poly::eval_vanishing_poly_recursively; use crate::plonk::vanishing_poly::eval_vanishing_poly_recursively;
use crate::plonk::vars::EvaluationTargets; use crate::plonk::vars::EvaluationTargets;
use crate::util::reducing::ReducingFactorTarget; use crate::util::reducing::ReducingFactorTarget;
@ -13,7 +15,7 @@ use crate::with_context;
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> { impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Recursively verifies an inner proof. /// Recursively verifies an inner proof.
pub fn verify_proof_with_pis<C: GenericConfig<D, F = F>>( pub fn verify_proof<C: GenericConfig<D, F = F>>(
&mut self, &mut self,
proof_with_pis: ProofWithPublicInputsTarget<D>, proof_with_pis: ProofWithPublicInputsTarget<D>,
inner_verifier_data: &VerifierCircuitTarget, inner_verifier_data: &VerifierCircuitTarget,
@ -21,27 +23,29 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
) where ) where
C::Hasher: AlgebraicHasher<F>, C::Hasher: AlgebraicHasher<F>,
{ {
let ProofWithPublicInputsTarget { assert_eq!(
proof, proof_with_pis.public_inputs.len(),
public_inputs, inner_common_data.num_public_inputs
} = proof_with_pis; );
let public_inputs_hash =
self.hash_n_to_hash_no_pad::<C::InnerHasher>(proof_with_pis.public_inputs.clone());
let challenges = proof_with_pis.get_challenges(self, public_inputs_hash, inner_common_data);
assert_eq!(public_inputs.len(), inner_common_data.num_public_inputs); self.verify_proof_with_challenges(
let public_inputs_hash = self.hash_n_to_hash_no_pad::<C::InnerHasher>(public_inputs); proof_with_pis.proof,
self.verify_proof(
proof,
public_inputs_hash, public_inputs_hash,
challenges,
inner_verifier_data, inner_verifier_data,
inner_common_data, inner_common_data,
); );
} }
/// Recursively verifies an inner proof. /// Recursively verifies an inner proof.
pub fn verify_proof<C: GenericConfig<D, F = F>>( fn verify_proof_with_challenges<C: GenericConfig<D, F = F>>(
&mut self, &mut self,
proof: ProofTarget<D>, proof: ProofTarget<D>,
public_inputs_hash: HashOutTarget, public_inputs_hash: HashOutTarget,
challenges: ProofChallengesTarget<D>,
inner_verifier_data: &VerifierCircuitTarget, inner_verifier_data: &VerifierCircuitTarget,
inner_common_data: &CommonCircuitData<F, C, D>, inner_common_data: &CommonCircuitData<F, C, D>,
) where ) where
@ -51,30 +55,6 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
let num_challenges = inner_common_data.config.num_challenges; let num_challenges = inner_common_data.config.num_challenges;
let mut challenger = RecursiveChallenger::<F, C::Hasher, D>::new(self);
let (betas, gammas, alphas, zeta) =
with_context!(self, "observe proof and generates challenges", {
// Observe the instance.
let digest = HashOutTarget::from_vec(
self.constants(&inner_common_data.circuit_digest.elements),
);
challenger.observe_hash(&digest);
challenger.observe_hash(&public_inputs_hash);
challenger.observe_cap(&proof.wires_cap);
let betas = challenger.get_n_challenges(self, num_challenges);
let gammas = challenger.get_n_challenges(self, num_challenges);
challenger.observe_cap(&proof.plonk_zs_partial_products_cap);
let alphas = challenger.get_n_challenges(self, num_challenges);
challenger.observe_cap(&proof.quotient_polys_cap);
let zeta = challenger.get_extension_challenge(self);
(betas, gammas, alphas, zeta)
});
let local_constants = &proof.openings.constants; let local_constants = &proof.openings.constants;
let local_wires = &proof.openings.wires; let local_wires = &proof.openings.wires;
let vars = EvaluationTargets { let vars = EvaluationTargets {
@ -87,23 +67,24 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
let s_sigmas = &proof.openings.plonk_sigmas; let s_sigmas = &proof.openings.plonk_sigmas;
let partial_products = &proof.openings.partial_products; let partial_products = &proof.openings.partial_products;
let zeta_pow_deg = self.exp_power_of_2_extension(zeta, inner_common_data.degree_bits); let zeta_pow_deg =
self.exp_power_of_2_extension(challenges.plonk_zeta, inner_common_data.degree_bits);
let vanishing_polys_zeta = with_context!( let vanishing_polys_zeta = with_context!(
self, self,
"evaluate the vanishing polynomial at our challenge point, zeta.", "evaluate the vanishing polynomial at our challenge point, zeta.",
eval_vanishing_poly_recursively( eval_vanishing_poly_recursively(
self, self,
inner_common_data, inner_common_data,
zeta, challenges.plonk_zeta,
zeta_pow_deg, zeta_pow_deg,
vars, vars,
local_zs, local_zs,
next_zs, next_zs,
partial_products, partial_products,
s_sigmas, s_sigmas,
&betas, &challenges.plonk_betas,
&gammas, &challenges.plonk_gammas,
&alphas, &challenges.plonk_alphas,
) )
); );
@ -128,16 +109,16 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
proof.quotient_polys_cap, proof.quotient_polys_cap,
]; ];
let fri_instance = inner_common_data.get_fri_instance_target(self, zeta); let fri_instance = inner_common_data.get_fri_instance_target(self, challenges.plonk_zeta);
with_context!( with_context!(
self, self,
"verify FRI proof", "verify FRI proof",
self.verify_fri_proof::<C>( self.verify_fri_proof::<C>(
&fri_instance, &fri_instance,
&proof.openings, &proof.openings.to_fri_openings(),
&challenges.fri_challenges,
merkle_caps, merkle_caps,
&proof.opening_proof, &proof.opening_proof,
&mut challenger,
&inner_common_data.fri_params, &inner_common_data.fri_params,
) )
); );
@ -392,7 +373,7 @@ mod tests {
&inner_vd.constants_sigmas_cap, &inner_vd.constants_sigmas_cap,
); );
builder.verify_proof_with_pis(pt, &inner_data, &inner_cd); builder.verify_proof(pt, &inner_data, &inner_cd);
if print_gate_counts { if print_gate_counts {
builder.print_gate_counts(0); builder.print_gate_counts(0);