plonky2/starky/src/prover.rs

313 lines
11 KiB
Rust
Raw Normal View History

use std::iter::once;
2022-01-27 12:58:56 +01:00
use anyhow::{ensure, Result};
use itertools::Itertools;
use plonky2::field::extension_field::Extendable;
2022-01-27 12:58:56 +01:00
use plonky2::field::field_types::Field;
use plonky2::field::packable::Packable;
use plonky2::field::packed_field::PackedField;
2022-01-26 16:08:04 +01:00
use plonky2::field::polynomial::{PolynomialCoeffs, PolynomialValues};
use plonky2::field::zero_poly_coset::ZeroPolyOnCoset;
use plonky2::fri::oracle::PolynomialBatch;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::challenger::Challenger;
use plonky2::plonk::config::{GenericConfig, Hasher};
use plonky2::timed;
use plonky2::util::timing::TimingTree;
use plonky2::util::transpose;
2022-02-04 16:36:22 +01:00
use plonky2_util::{log2_ceil, log2_strict};
use rayon::prelude::*;
use crate::config::StarkConfig;
2022-01-26 16:08:04 +01:00
use crate::constraint_consumer::ConstraintConsumer;
2022-02-23 09:36:28 +01:00
use crate::permutation::PermutationCheckVars;
2022-02-21 16:05:24 +01:00
use crate::permutation::{
compute_permutation_z_polys, get_n_permutation_challenge_sets, PermutationChallengeSet,
};
2022-01-29 12:49:00 +01:00
use crate::proof::{StarkOpeningSet, StarkProof, StarkProofWithPublicInputs};
use crate::stark::Stark;
2022-02-21 16:05:24 +01:00
use crate::vanishing_poly::eval_vanishing_poly;
2022-01-26 16:08:04 +01:00
use crate::vars::StarkEvaluationVars;
pub fn prove<F, C, S, const D: usize>(
stark: S,
2022-01-31 18:00:07 +01:00
config: &StarkConfig,
trace_poly_values: Vec<PolynomialValues<F>>,
2022-01-28 05:02:31 +01:00
public_inputs: [F; S::PUBLIC_INPUTS],
timing: &mut TimingTree,
2022-01-29 12:49:00 +01:00
) -> Result<StarkProofWithPublicInputs<F, C, D>>
where
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
S: Stark<F, D>,
[(); S::COLUMNS]:,
2022-01-26 16:08:04 +01:00
[(); S::PUBLIC_INPUTS]:,
[(); <<F as Packable>::Packing>::WIDTH]:,
[(); C::Hasher::HASH_SIZE]:,
{
let degree = trace_poly_values[0].len();
2022-01-27 12:58:56 +01:00
let degree_bits = log2_strict(degree);
2022-02-16 13:37:01 +01:00
let fri_params = config.fri_params(degree_bits);
2022-02-16 13:51:10 +01:00
let rate_bits = config.fri_config.rate_bits;
let cap_height = config.fri_config.cap_height;
2022-02-16 13:37:01 +01:00
assert!(
2022-02-16 13:51:10 +01:00
fri_params.total_arities() <= degree_bits + rate_bits - cap_height,
2022-02-16 13:37:01 +01:00
"FRI total reduction arity is too large.",
);
let trace_commitment = timed!(
timing,
"compute trace commitment",
PolynomialBatch::<F, C, D>::from_values(
// TODO: Cloning this isn't great; consider having `from_values` accept a reference,
// or having `compute_permutation_z_polys` read trace values from the `PolynomialBatch`.
trace_poly_values.clone(),
rate_bits,
false,
cap_height,
timing,
None,
)
);
2022-01-26 16:08:04 +01:00
let trace_cap = trace_commitment.merkle_tree.cap.clone();
let mut challenger = Challenger::new();
challenger.observe_cap(&trace_cap);
// Permutation arguments.
2022-02-22 17:30:08 +01:00
let permutation_zs_commitment_challenges = stark.uses_permutation_args().then(|| {
2022-02-21 16:05:24 +01:00
let permutation_challenge_sets = get_n_permutation_challenge_sets(
&mut challenger,
config.num_challenges,
stark.permutation_batch_size(),
);
let permutation_z_polys = compute_permutation_z_polys::<F, C, S, D>(
&stark,
config,
&trace_poly_values,
2022-02-21 16:05:24 +01:00
&permutation_challenge_sets,
);
2022-02-21 16:05:24 +01:00
2022-02-23 09:36:28 +01:00
let permutation_zs_commitment = timed!(
timing,
"compute permutation Z commitments",
2022-02-23 09:36:28 +01:00
PolynomialBatch::from_values(
permutation_z_polys,
rate_bits,
false,
config.fri_config.cap_height,
timing,
None,
2022-02-22 17:30:08 +01:00
)
2022-02-23 09:36:28 +01:00
);
(permutation_zs_commitment, permutation_challenge_sets)
2022-02-22 17:30:08 +01:00
});
2022-02-21 16:05:24 +01:00
let permutation_zs_commitment = permutation_zs_commitment_challenges
.as_ref()
.map(|(comm, _)| comm);
let permutation_zs_cap = permutation_zs_commitment
.as_ref()
.map(|commit| commit.merkle_tree.cap.clone());
for cap in &permutation_zs_cap {
challenger.observe_cap(cap);
}
2022-01-26 16:08:04 +01:00
let alphas = challenger.get_n_challenges(config.num_challenges);
let quotient_polys = compute_quotient_polys::<F, <F as Packable>::Packing, C, S, D>(
2022-01-26 16:08:04 +01:00
&stark,
&trace_commitment,
2022-02-21 16:05:24 +01:00
&permutation_zs_commitment_challenges,
2022-01-28 05:02:31 +01:00
public_inputs,
2022-01-28 17:06:40 +01:00
alphas,
2022-01-26 16:08:04 +01:00
degree_bits,
2022-02-21 16:05:24 +01:00
config,
2022-01-26 16:08:04 +01:00
);
2022-01-27 12:58:56 +01:00
let all_quotient_chunks = quotient_polys
.into_par_iter()
.flat_map(|mut quotient_poly| {
quotient_poly
.trim_to_len(degree * stark.quotient_degree_factor())
.expect("Quotient has failed, the vanishing polynomial is not divisible by Z_H");
2022-01-27 13:27:06 +01:00
// Split quotient into degree-n chunks.
2022-01-27 12:58:56 +01:00
quotient_poly.chunks(degree)
})
.collect();
let quotient_commitment = timed!(
timing,
"compute quotient commitment",
PolynomialBatch::from_coeffs(
all_quotient_chunks,
rate_bits,
false,
config.fri_config.cap_height,
timing,
None,
)
);
2022-01-31 18:00:07 +01:00
let quotient_polys_cap = quotient_commitment.merkle_tree.cap.clone();
2022-01-31 16:19:30 +01:00
challenger.observe_cap(&quotient_polys_cap);
2022-01-27 12:58:56 +01:00
let zeta = challenger.get_extension_challenge::<D>();
// To avoid leaking witness data, we want to ensure that our opening locations, `zeta` and
// `g * zeta`, are not in our subgroup `H`. It suffices to check `zeta` only, since
// `(g * zeta)^n = zeta^n`, where `n` is the order of `g`.
2022-02-08 18:16:33 +01:00
let g = F::primitive_root_of_unity(degree_bits);
2022-01-27 12:58:56 +01:00
ensure!(
zeta.exp_power_of_2(degree_bits) != F::Extension::ONE,
"Opening point is in the subgroup."
);
let openings = StarkOpeningSet::new(
zeta,
g,
&trace_commitment,
2022-02-21 16:05:24 +01:00
permutation_zs_commitment,
&quotient_commitment,
);
challenger.observe_openings(&openings.to_fri_openings());
let initial_merkle_trees = once(&trace_commitment)
2022-02-21 16:05:24 +01:00
.chain(permutation_zs_commitment)
.chain(once(&quotient_commitment))
.collect_vec();
2022-01-27 12:58:56 +01:00
let opening_proof = timed!(
timing,
2022-01-27 12:58:56 +01:00
"compute openings proof",
PolynomialBatch::prove_openings(
&stark.fri_instance(zeta, g, config),
&initial_merkle_trees,
2022-01-27 12:58:56 +01:00
&mut challenger,
&fri_params,
timing,
)
);
2022-01-29 12:49:00 +01:00
let proof = StarkProof {
trace_cap,
permutation_zs_cap,
2022-01-29 12:49:00 +01:00
quotient_polys_cap,
openings,
opening_proof,
2022-01-29 12:49:00 +01:00
};
Ok(StarkProofWithPublicInputs {
proof,
public_inputs: public_inputs.to_vec(),
2022-01-27 12:58:56 +01:00
})
}
2022-01-26 16:08:04 +01:00
2022-01-27 13:27:06 +01:00
/// Computes the quotient polynomials `(sum alpha^i C_i(x)) / Z_H(x)` for `alpha` in `alphas`,
/// where the `C_i`s are the Stark constraints.
fn compute_quotient_polys<'a, F, P, C, S, const D: usize>(
2022-01-26 16:08:04 +01:00
stark: &S,
2022-02-21 18:00:03 +01:00
trace_commitment: &'a PolynomialBatch<F, C, D>,
permutation_zs_commitment_challenges: &'a Option<(
2022-02-21 16:05:24 +01:00
PolynomialBatch<F, C, D>,
Vec<PermutationChallengeSet<F>>,
)>,
2022-01-28 05:02:31 +01:00
public_inputs: [F; S::PUBLIC_INPUTS],
2022-01-28 17:06:40 +01:00
alphas: Vec<F>,
2022-01-26 16:08:04 +01:00
degree_bits: usize,
2022-02-21 16:05:24 +01:00
config: &StarkConfig,
2022-01-26 16:08:04 +01:00
) -> Vec<PolynomialCoeffs<F>>
where
F: RichField + Extendable<D>,
P: PackedField<Scalar = F>,
2022-01-26 16:08:04 +01:00
C: GenericConfig<D, F = F>,
S: Stark<F, D>,
[(); S::COLUMNS]:,
[(); S::PUBLIC_INPUTS]:,
[(); P::WIDTH]:,
2022-01-26 16:08:04 +01:00
{
let degree = 1 << degree_bits;
2022-02-21 16:05:24 +01:00
let rate_bits = config.fri_config.rate_bits;
2022-01-26 16:08:04 +01:00
2022-02-04 20:42:49 +01:00
let quotient_degree_bits = log2_ceil(stark.quotient_degree_factor());
2022-02-04 16:36:22 +01:00
assert!(
2022-02-04 20:42:49 +01:00
quotient_degree_bits <= rate_bits,
2022-02-04 16:36:22 +01:00
"Having constraints of degree higher than the rate is not supported yet."
);
2022-02-04 20:42:49 +01:00
let step = 1 << (rate_bits - quotient_degree_bits);
2022-02-04 16:36:22 +01:00
// When opening the `Z`s polys at the "next" point, need to look at the point `next_step` steps away.
2022-02-04 20:42:49 +01:00
let next_step = 1 << quotient_degree_bits;
2022-02-04 16:36:22 +01:00
2022-01-27 13:27:06 +01:00
// Evaluation of the first Lagrange polynomial on the LDE domain.
let lagrange_first = PolynomialValues::selector(degree, 0).lde_onto_coset(quotient_degree_bits);
2022-01-27 13:27:06 +01:00
// Evaluation of the last Lagrange polynomial on the LDE domain.
let lagrange_last =
PolynomialValues::selector(degree, degree - 1).lde_onto_coset(quotient_degree_bits);
2022-01-26 16:08:04 +01:00
2022-02-04 20:42:49 +01:00
let z_h_on_coset = ZeroPolyOnCoset::<F>::new(degree_bits, quotient_degree_bits);
2022-01-26 16:08:04 +01:00
2022-01-27 13:27:06 +01:00
// Retrieve the LDE values at index `i`.
let get_trace_values_packed = |i_start| -> [P; S::COLUMNS] {
trace_commitment
.get_lde_values_packed(i_start, step)
.try_into()
.unwrap()
};
2022-02-23 09:36:28 +01:00
2022-02-01 17:34:03 +01:00
// Last element of the subgroup.
2022-01-31 18:00:07 +01:00
let last = F::primitive_root_of_unity(degree_bits).inverse();
2022-02-04 20:42:49 +01:00
let size = degree << quotient_degree_bits;
2022-01-31 18:00:07 +01:00
let coset = F::cyclic_subgroup_coset_known_order(
2022-02-04 20:42:49 +01:00
F::primitive_root_of_unity(degree_bits + quotient_degree_bits),
2022-01-31 18:00:07 +01:00
F::coset_shift(),
2022-02-04 16:36:22 +01:00
size,
2022-01-31 18:00:07 +01:00
);
2022-01-27 13:27:06 +01:00
// We will step by `P::WIDTH`, and in each iteration, evaluate the quotient polynomial at
// a batch of `P::WIDTH` points.
2022-02-04 16:36:22 +01:00
let quotient_values = (0..size)
2022-01-28 17:06:40 +01:00
.into_par_iter()
.step_by(P::WIDTH)
.map(|i_start| {
let i_next_start = (i_start + next_step) % size;
let i_range = i_start..i_start + P::WIDTH;
let x = *P::from_slice(&coset[i_range.clone()]);
let z_last = x - last;
let lagrange_basis_first = *P::from_slice(&lagrange_first.values[i_range.clone()]);
let lagrange_basis_last = *P::from_slice(&lagrange_last.values[i_range]);
let mut consumer = ConstraintConsumer::new(
2022-01-28 17:06:40 +01:00
alphas.clone(),
z_last,
lagrange_basis_first,
lagrange_basis_last,
2022-01-26 16:08:04 +01:00
);
let vars = StarkEvaluationVars {
local_values: &get_trace_values_packed(i_start),
next_values: &get_trace_values_packed(i_next_start),
2022-01-28 17:06:40 +01:00
public_inputs: &public_inputs,
};
2022-02-21 16:05:24 +01:00
let permutation_check_data = permutation_zs_commitment_challenges.as_ref().map(
2022-02-23 09:36:28 +01:00
|(permutation_zs_commitment, permutation_challenge_sets)| PermutationCheckVars {
local_zs: permutation_zs_commitment.get_lde_values_packed(i_start, step),
next_zs: permutation_zs_commitment.get_lde_values_packed(i_next_start, step),
2022-02-21 16:05:24 +01:00
permutation_challenge_sets: permutation_challenge_sets.to_vec(),
},
);
eval_vanishing_poly::<F, F, P, C, S, D, 1>(
2022-02-21 16:05:24 +01:00
stark,
config,
vars,
permutation_check_data,
&mut consumer,
);
2022-01-28 17:06:40 +01:00
let mut constraints_evals = consumer.accumulators();
2022-02-03 11:49:44 +01:00
// We divide the constraints evaluations by `Z_H(x)`.
let denominator_inv = z_h_on_coset.eval_inverse_packed(i_start);
2022-01-28 17:06:40 +01:00
for eval in &mut constraints_evals {
*eval *= denominator_inv;
2022-01-28 17:06:40 +01:00
}
constraints_evals
2022-01-26 16:08:04 +01:00
})
2022-01-28 17:06:40 +01:00
.collect::<Vec<_>>();
transpose(&quotient_values)
.into_par_iter()
.map(PolynomialValues::new)
.map(|values| values.coset_ifft(F::coset_shift()))
2022-01-26 16:08:04 +01:00
.collect()
}