plonky2/evm/src/verifier.rs

303 lines
10 KiB
Rust
Raw Normal View History

2022-12-02 14:31:18 -08:00
use std::any::type_name;
2022-05-04 20:57:07 +02:00
use anyhow::{ensure, Result};
use plonky2::field::extension::{Extendable, FieldExtension};
use plonky2::field::types::Field;
2022-05-04 20:57:07 +02:00
use plonky2::fri::verifier::verify_fri_proof;
use plonky2::hash::hash_types::RichField;
2022-11-22 08:48:48 -05:00
use plonky2::plonk::config::GenericConfig;
2022-05-04 20:57:07 +02:00
use plonky2::plonk::plonk_common::reduce_with_powers;
2022-05-18 09:22:58 +02:00
use crate::all_stark::{AllStark, Table};
use crate::arithmetic::arithmetic_stark::ArithmeticStark;
2022-05-04 20:57:07 +02:00
use crate::config::StarkConfig;
use crate::constraint_consumer::ConstraintConsumer;
2022-05-18 09:22:58 +02:00
use crate::cpu::cpu_stark::CpuStark;
2022-05-16 20:45:30 +02:00
use crate::cross_table_lookup::{verify_cross_table_lookups, CtlCheckVars};
2022-05-18 09:22:58 +02:00
use crate::keccak::keccak_stark::KeccakStark;
use crate::keccak_sponge::keccak_sponge_stark::KeccakSpongeStark;
use crate::logic::LogicStark;
use crate::memory::memory_stark::MemoryStark;
2022-05-04 20:57:07 +02:00
use crate::permutation::PermutationCheckVars;
2022-05-11 14:35:33 +02:00
use crate::proof::{
2022-08-25 12:24:22 -07:00
AllProof, AllProofChallenges, StarkOpeningSet, StarkProof, StarkProofChallenges,
2022-05-11 14:35:33 +02:00
};
2022-05-04 20:57:07 +02:00
use crate::stark::Stark;
use crate::vanishing_poly::eval_vanishing_poly;
use crate::vars::StarkEvaluationVars;
pub fn verify_proof<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>(
all_stark: &AllStark<F, D>,
all_proof: AllProof<F, C, D>,
2022-05-04 20:57:07 +02:00
config: &StarkConfig,
) -> Result<()>
where
[(); ArithmeticStark::<F, D>::COLUMNS]:,
2022-05-12 20:38:11 +02:00
[(); CpuStark::<F, D>::COLUMNS]:,
2022-05-11 14:35:33 +02:00
[(); KeccakStark::<F, D>::COLUMNS]:,
[(); KeccakSpongeStark::<F, D>::COLUMNS]:,
[(); LogicStark::<F, D>::COLUMNS]:,
[(); MemoryStark::<F, D>::COLUMNS]:,
2022-05-04 20:57:07 +02:00
{
2022-05-11 14:35:33 +02:00
let AllProofChallenges {
2022-05-19 09:41:15 +02:00
stark_challenges,
2022-05-11 14:35:33 +02:00
ctl_challenges,
} = all_proof.get_challenges(all_stark, config);
2022-05-11 14:35:33 +02:00
2022-05-13 10:48:56 +02:00
let nums_permutation_zs = all_stark.nums_permutation_zs(config);
2022-05-11 16:09:12 +02:00
let AllStark {
arithmetic_stark,
2022-05-11 16:09:12 +02:00
cpu_stark,
keccak_stark,
keccak_sponge_stark,
logic_stark,
memory_stark,
2022-05-11 16:09:12 +02:00
cross_table_lookups,
} = all_stark;
2022-05-11 14:35:33 +02:00
2022-05-16 20:45:30 +02:00
let ctl_vars_per_table = CtlCheckVars::from_proofs(
2022-05-19 09:41:15 +02:00
&all_proof.stark_proofs,
cross_table_lookups,
2022-05-12 22:29:10 +02:00
&ctl_challenges,
2022-05-13 10:48:56 +02:00
&nums_permutation_zs,
2022-05-12 22:29:10 +02:00
);
2022-05-12 13:47:55 +02:00
verify_stark_proof_with_challenges(
arithmetic_stark,
&all_proof.stark_proofs[Table::Arithmetic as usize].proof,
&stark_challenges[Table::Arithmetic as usize],
&ctl_vars_per_table[Table::Arithmetic as usize],
config,
)?;
2022-05-12 13:47:55 +02:00
verify_stark_proof_with_challenges(
cpu_stark,
&all_proof.stark_proofs[Table::Cpu as usize].proof,
2022-05-19 09:41:15 +02:00
&stark_challenges[Table::Cpu as usize],
2022-05-12 13:47:55 +02:00
&ctl_vars_per_table[Table::Cpu as usize],
2022-05-11 14:35:33 +02:00
config,
)?;
verify_stark_proof_with_challenges(
2022-05-11 16:09:12 +02:00
keccak_stark,
&all_proof.stark_proofs[Table::Keccak as usize].proof,
2022-05-19 09:41:15 +02:00
&stark_challenges[Table::Keccak as usize],
2022-05-12 13:47:55 +02:00
&ctl_vars_per_table[Table::Keccak as usize],
config,
)?;
2022-08-14 16:36:07 -07:00
verify_stark_proof_with_challenges(
keccak_sponge_stark,
&all_proof.stark_proofs[Table::KeccakSponge as usize].proof,
&stark_challenges[Table::KeccakSponge as usize],
&ctl_vars_per_table[Table::KeccakSponge as usize],
2022-08-14 16:36:07 -07:00
config,
)?;
verify_stark_proof_with_challenges(
memory_stark,
&all_proof.stark_proofs[Table::Memory as usize].proof,
&stark_challenges[Table::Memory as usize],
&ctl_vars_per_table[Table::Memory as usize],
config,
)?;
verify_stark_proof_with_challenges(
logic_stark,
&all_proof.stark_proofs[Table::Logic as usize].proof,
&stark_challenges[Table::Logic as usize],
&ctl_vars_per_table[Table::Logic as usize],
config,
)?;
2022-05-12 13:47:55 +02:00
verify_cross_table_lookups::<F, D>(
2022-05-12 13:47:55 +02:00
cross_table_lookups,
all_proof.stark_proofs.map(|p| p.proof.openings.ctl_zs_last),
2022-05-11 14:35:33 +02:00
config,
)
2022-05-04 20:57:07 +02:00
}
pub(crate) fn verify_stark_proof_with_challenges<
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
2022-05-04 20:57:07 +02:00
S: Stark<F, D>,
const D: usize,
>(
stark: &S,
proof: &StarkProof<F, C, D>,
2022-05-19 09:41:15 +02:00
challenges: &StarkProofChallenges<F, D>,
2022-05-16 20:45:30 +02:00
ctl_vars: &[CtlCheckVars<F, F::Extension, F::Extension, D>],
2022-05-04 20:57:07 +02:00
config: &StarkConfig,
) -> Result<()>
where
[(); S::COLUMNS]:,
{
2022-12-02 14:31:18 -08:00
log::debug!("Checking proof: {}", type_name::<S>());
validate_proof_shape(stark, proof, config, ctl_vars.len())?;
2022-05-04 20:57:07 +02:00
let StarkOpeningSet {
local_values,
next_values,
2022-05-13 11:20:29 +02:00
permutation_ctl_zs,
2022-06-02 23:55:56 +02:00
permutation_ctl_zs_next,
2022-05-13 11:20:29 +02:00
ctl_zs_last,
2022-05-04 20:57:07 +02:00
quotient_polys,
} = &proof.openings;
let vars = StarkEvaluationVars {
2022-05-13 14:16:28 +02:00
local_values: &local_values.to_vec().try_into().unwrap(),
next_values: &next_values.to_vec().try_into().unwrap(),
2022-05-04 20:57:07 +02:00
};
2022-05-11 14:35:33 +02:00
let degree_bits = proof.recover_degree_bits(config);
2022-09-12 08:09:17 +02:00
let (l_0, l_last) = eval_l_0_and_l_last(degree_bits, challenges.stark_zeta);
2022-05-04 20:57:07 +02:00
let last = F::primitive_root_of_unity(degree_bits).inverse();
let z_last = challenges.stark_zeta - last.into();
let mut consumer = ConstraintConsumer::<F::Extension>::new(
challenges
.stark_alphas
.iter()
.map(|&alpha| F::Extension::from_basefield(alpha))
.collect::<Vec<_>>(),
z_last,
2022-09-12 08:09:17 +02:00
l_0,
2022-05-04 20:57:07 +02:00
l_last,
);
2022-05-13 10:48:56 +02:00
let num_permutation_zs = stark.num_permutation_batches(config);
2022-05-04 20:57:07 +02:00
let permutation_data = stark.uses_permutation_args().then(|| PermutationCheckVars {
local_zs: permutation_ctl_zs[..num_permutation_zs].to_vec(),
2022-06-02 23:55:56 +02:00
next_zs: permutation_ctl_zs_next[..num_permutation_zs].to_vec(),
2022-05-19 09:41:15 +02:00
permutation_challenge_sets: challenges.permutation_challenge_sets.clone().unwrap(),
2022-05-04 20:57:07 +02:00
});
eval_vanishing_poly::<F, F::Extension, F::Extension, S, D, D>(
stark,
2022-05-04 20:57:07 +02:00
config,
vars,
permutation_data,
2022-05-13 11:20:29 +02:00
ctl_vars,
2022-05-04 20:57:07 +02:00
&mut consumer,
);
let vanishing_polys_zeta = consumer.accumulators();
// Check each polynomial identity, of the form `vanishing(x) = Z_H(x) quotient(x)`, at zeta.
let zeta_pow_deg = challenges.stark_zeta.exp_power_of_2(degree_bits);
let z_h_zeta = zeta_pow_deg - F::Extension::ONE;
// `quotient_polys_zeta` holds `num_challenges * quotient_degree_factor` evaluations.
// Each chunk of `quotient_degree_factor` holds the evaluations of `t_0(zeta),...,t_{quotient_degree_factor-1}(zeta)`
// where the "real" quotient polynomial is `t(X) = t_0(X) + t_1(X)*X^n + t_2(X)*X^{2n} + ...`.
// So to reconstruct `t(zeta)` we can compute `reduce_with_powers(chunk, zeta^n)` for each
// `quotient_degree_factor`-sized chunk of the original evaluations.
for (i, chunk) in quotient_polys
.chunks(stark.quotient_degree_factor())
.enumerate()
{
ensure!(
vanishing_polys_zeta[i] == z_h_zeta * reduce_with_powers(chunk, zeta_pow_deg),
"Mismatch between evaluation and opening of quotient polynomial"
);
}
let merkle_caps = vec![
proof.trace_cap.clone(),
proof.permutation_ctl_zs_cap.clone(),
proof.quotient_polys_cap.clone(),
];
2022-05-04 20:57:07 +02:00
verify_fri_proof::<F, C, D>(
2022-05-04 20:57:07 +02:00
&stark.fri_instance(
challenges.stark_zeta,
F::primitive_root_of_unity(degree_bits),
2022-05-10 15:08:08 +02:00
degree_bits,
2022-05-13 11:20:29 +02:00
ctl_zs_last.len(),
2022-05-04 20:57:07 +02:00
config,
),
&proof.openings.to_fri_openings(),
&challenges.fri_challenges,
&merkle_caps,
&proof.opening_proof,
&config.fri_params(degree_bits),
)?;
Ok(())
}
fn validate_proof_shape<F, C, S, const D: usize>(
2022-09-19 20:54:45 -07:00
stark: &S,
proof: &StarkProof<F, C, D>,
2022-09-19 20:54:45 -07:00
config: &StarkConfig,
2022-09-19 21:30:14 -07:00
num_ctl_zs: usize,
2022-09-19 20:54:45 -07:00
) -> anyhow::Result<()>
where
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
2022-09-19 20:54:45 -07:00
S: Stark<F, D>,
[(); S::COLUMNS]:,
{
let StarkProof {
trace_cap,
permutation_ctl_zs_cap,
quotient_polys_cap,
openings,
// The shape of the opening proof will be checked in the FRI verifier (see
// validate_fri_proof_shape), so we ignore it here.
opening_proof: _,
} = proof;
2022-09-19 23:04:53 -07:00
let StarkOpeningSet {
local_values,
next_values,
permutation_ctl_zs,
permutation_ctl_zs_next,
ctl_zs_last,
quotient_polys,
} = openings;
2022-09-19 20:54:45 -07:00
let degree_bits = proof.recover_degree_bits(config);
let fri_params = config.fri_params(degree_bits);
let cap_height = fri_params.config.cap_height;
2022-09-19 23:04:53 -07:00
let num_zs = num_ctl_zs + stark.num_permutation_batches(config);
2022-09-19 20:54:45 -07:00
ensure!(trace_cap.height() == cap_height);
ensure!(permutation_ctl_zs_cap.height() == cap_height);
ensure!(quotient_polys_cap.height() == cap_height);
2022-09-19 23:04:53 -07:00
ensure!(local_values.len() == S::COLUMNS);
ensure!(next_values.len() == S::COLUMNS);
ensure!(permutation_ctl_zs.len() == num_zs);
ensure!(permutation_ctl_zs_next.len() == num_zs);
ensure!(ctl_zs_last.len() == num_ctl_zs);
ensure!(quotient_polys.len() == stark.num_quotient_polys(config));
2022-09-19 20:54:45 -07:00
Ok(())
}
2022-09-12 08:09:17 +02:00
/// Evaluate the Lagrange polynomials `L_0` and `L_(n-1)` at a point `x`.
/// `L_0(x) = (x^n - 1)/(n * (x - 1))`
/// `L_(n-1)(x) = (x^n - 1)/(n * (g * x - 1))`, with `g` the first element of the subgroup.
fn eval_l_0_and_l_last<F: Field>(log_n: usize, x: F) -> (F, F) {
2022-05-04 20:57:07 +02:00
let n = F::from_canonical_usize(1 << log_n);
let g = F::primitive_root_of_unity(log_n);
let z_x = x.exp_power_of_2(log_n) - F::ONE;
let invs = F::batch_multiplicative_inverse(&[n * (x - F::ONE), n * (g * x - F::ONE)]);
(z_x * invs[0], z_x * invs[1])
}
#[cfg(test)]
mod tests {
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::field::polynomial::PolynomialValues;
use plonky2::field::types::Sample;
2022-05-04 20:57:07 +02:00
2022-09-12 08:09:17 +02:00
use crate::verifier::eval_l_0_and_l_last;
2022-05-04 20:57:07 +02:00
#[test]
2022-09-12 08:09:17 +02:00
fn test_eval_l_0_and_l_last() {
2022-05-04 20:57:07 +02:00
type F = GoldilocksField;
let log_n = 5;
let n = 1 << log_n;
let x = F::rand(); // challenge point
let expected_l_first_x = PolynomialValues::selector(n, 0).ifft().eval(x);
let expected_l_last_x = PolynomialValues::selector(n, n - 1).ifft().eval(x);
2022-09-12 08:09:17 +02:00
let (l_first_x, l_last_x) = eval_l_0_and_l_last(log_n, x);
2022-05-04 20:57:07 +02:00
assert_eq!(l_first_x, expected_l_first_x);
assert_eq!(l_last_x, expected_l_last_x);
}
}