2021-06-10 14:10:35 -07:00
|
|
|
use std::convert::TryInto;
|
|
|
|
|
|
2021-06-04 10:47:46 +02:00
|
|
|
use crate::field::extension_field::target::ExtensionTarget;
|
2021-05-18 15:22:06 +02:00
|
|
|
use crate::field::extension_field::Extendable;
|
2021-02-09 21:25:21 -08:00
|
|
|
use crate::field::field::Field;
|
2021-06-01 19:13:22 -07:00
|
|
|
use crate::fri::FriConfig;
|
2021-06-04 10:47:46 +02:00
|
|
|
use crate::gadgets::polynomial::PolynomialCoeffsExtTarget;
|
2021-04-21 22:31:45 +02:00
|
|
|
use crate::merkle_proofs::{MerkleProof, MerkleProofTarget};
|
2021-06-04 10:47:46 +02:00
|
|
|
use crate::polynomial::commitment::{ListPolynomialCommitment, OpeningProof, OpeningProofTarget};
|
2021-04-21 22:31:45 +02:00
|
|
|
use crate::polynomial::polynomial::PolynomialCoeffs;
|
2021-02-26 13:18:41 -08:00
|
|
|
use crate::target::Target;
|
2021-02-09 21:25:21 -08:00
|
|
|
|
2021-03-25 15:20:14 -07:00
|
|
|
/// Represents a ~256 bit hash output.
|
2021-04-09 18:24:19 +02:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
2021-02-09 21:25:21 -08:00
|
|
|
pub struct Hash<F: Field> {
|
2021-03-25 15:20:14 -07:00
|
|
|
pub(crate) elements: [F; 4],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<F: Field> Hash<F> {
|
2021-04-09 12:53:33 -07:00
|
|
|
pub(crate) fn from_vec(elements: Vec<F>) -> Self {
|
|
|
|
|
debug_assert!(elements.len() == 4);
|
2021-04-21 22:31:45 +02:00
|
|
|
Self {
|
|
|
|
|
elements: elements.try_into().unwrap(),
|
|
|
|
|
}
|
2021-04-09 12:53:33 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-25 15:20:14 -07:00
|
|
|
pub(crate) fn from_partial(mut elements: Vec<F>) -> Self {
|
|
|
|
|
debug_assert!(elements.len() <= 4);
|
|
|
|
|
while elements.len() < 4 {
|
|
|
|
|
elements.push(F::ZERO);
|
|
|
|
|
}
|
2021-04-21 22:31:45 +02:00
|
|
|
Self {
|
|
|
|
|
elements: [elements[0], elements[1], elements[2], elements[3]],
|
|
|
|
|
}
|
2021-03-25 15:20:14 -07:00
|
|
|
}
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|
|
|
|
|
|
2021-04-07 22:21:45 -07:00
|
|
|
/// Represents a ~256 bit hash output.
|
2021-06-04 17:36:48 +02:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2021-02-09 21:25:21 -08:00
|
|
|
pub struct HashTarget {
|
2021-04-09 12:40:43 -07:00
|
|
|
pub(crate) elements: [Target; 4],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl HashTarget {
|
|
|
|
|
pub(crate) fn from_vec(elements: Vec<Target>) -> Self {
|
|
|
|
|
debug_assert!(elements.len() == 4);
|
2021-04-21 22:31:45 +02:00
|
|
|
Self {
|
|
|
|
|
elements: elements.try_into().unwrap(),
|
|
|
|
|
}
|
2021-04-09 12:40:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn from_partial(mut elements: Vec<Target>, zero: Target) -> Self {
|
|
|
|
|
debug_assert!(elements.len() <= 4);
|
|
|
|
|
while elements.len() < 4 {
|
|
|
|
|
elements.push(zero);
|
|
|
|
|
}
|
2021-04-21 22:31:45 +02:00
|
|
|
Self {
|
|
|
|
|
elements: [elements[0], elements[1], elements[2], elements[3]],
|
|
|
|
|
}
|
2021-04-09 12:40:43 -07:00
|
|
|
}
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|
|
|
|
|
|
2021-05-18 15:44:50 +02:00
|
|
|
pub struct Proof<F: Field + Extendable<D>, const D: usize> {
|
2021-02-09 21:25:21 -08:00
|
|
|
/// Merkle root of LDEs of wire values.
|
|
|
|
|
pub wires_root: Hash<F>,
|
|
|
|
|
/// Merkle root of LDEs of Z, in the context of Plonk's permutation argument.
|
2021-04-01 12:49:31 -07:00
|
|
|
pub plonk_zs_root: Hash<F>,
|
2021-02-09 21:25:21 -08:00
|
|
|
/// Merkle root of LDEs of the quotient polynomial components.
|
2021-04-01 12:49:31 -07:00
|
|
|
pub quotient_polys_root: Hash<F>,
|
2021-05-31 17:49:04 +02:00
|
|
|
/// Purported values of each polynomial at the challenge point.
|
|
|
|
|
pub openings: OpeningSet<F, D>,
|
2021-04-06 19:11:21 -07:00
|
|
|
/// A FRI argument for each FRI query.
|
2021-05-18 15:44:50 +02:00
|
|
|
pub opening_proof: OpeningProof<F, D>,
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 10:47:46 +02:00
|
|
|
pub struct ProofTarget<const D: usize> {
|
2021-02-09 21:25:21 -08:00
|
|
|
pub wires_root: HashTarget,
|
2021-04-01 12:49:31 -07:00
|
|
|
pub plonk_zs_root: HashTarget,
|
|
|
|
|
pub quotient_polys_root: HashTarget,
|
2021-06-04 10:47:46 +02:00
|
|
|
pub openings: Vec<OpeningSetTarget<D>>,
|
|
|
|
|
pub opening_proof: Vec<OpeningProofTarget<D>>,
|
2021-02-26 13:18:41 -08:00
|
|
|
}
|
|
|
|
|
|
2021-04-27 08:44:34 +02:00
|
|
|
/// Evaluations and Merkle proof produced by the prover in a FRI query step.
|
2021-05-18 15:44:50 +02:00
|
|
|
pub struct FriQueryStep<F: Field + Extendable<D>, const D: usize> {
|
2021-05-18 15:22:06 +02:00
|
|
|
pub evals: Vec<F::Extension>,
|
2021-04-27 08:44:34 +02:00
|
|
|
pub merkle_proof: MerkleProof<F>,
|
2021-04-21 22:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 10:47:46 +02:00
|
|
|
pub struct FriQueryStepTarget<const D: usize> {
|
|
|
|
|
pub evals: Vec<ExtensionTarget<D>>,
|
|
|
|
|
pub merkle_proof: MerkleProofTarget,
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 15:14:43 +02:00
|
|
|
/// Evaluations and Merkle proofs of the original set of polynomials,
|
|
|
|
|
/// before they are combined into a composition polynomial.
|
2021-05-04 17:48:26 +02:00
|
|
|
pub struct FriInitialTreeProof<F: Field> {
|
|
|
|
|
pub evals_proofs: Vec<(Vec<F>, MerkleProof<F>)>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 19:13:22 -07:00
|
|
|
impl<F: Field> FriInitialTreeProof<F> {
|
|
|
|
|
pub(crate) fn unsalted_evals(&self, i: usize, config: &FriConfig) -> &[F] {
|
|
|
|
|
let evals = &self.evals_proofs[i].0;
|
|
|
|
|
&evals[..evals.len() - config.salt_size(i)]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 10:47:46 +02:00
|
|
|
pub struct FriInitialTreeProofTarget {
|
|
|
|
|
pub evals_proofs: Vec<(Vec<Target>, MerkleProofTarget)>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 17:36:48 +02:00
|
|
|
impl FriInitialTreeProofTarget {
|
|
|
|
|
pub(crate) fn unsalted_evals(&self, i: usize, config: &FriConfig) -> &[Target] {
|
|
|
|
|
let evals = &self.evals_proofs[i].0;
|
|
|
|
|
&evals[..evals.len() - config.salt_size(i)]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 08:44:34 +02:00
|
|
|
/// Proof for a FRI query round.
|
2021-05-18 15:44:50 +02:00
|
|
|
pub struct FriQueryRound<F: Field + Extendable<D>, const D: usize> {
|
2021-05-04 17:48:26 +02:00
|
|
|
pub initial_trees_proof: FriInitialTreeProof<F>,
|
2021-05-18 15:44:50 +02:00
|
|
|
pub steps: Vec<FriQueryStep<F, D>>,
|
2021-04-21 22:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 10:47:46 +02:00
|
|
|
pub struct FriQueryRoundTarget<const D: usize> {
|
|
|
|
|
pub initial_trees_proof: FriInitialTreeProofTarget,
|
|
|
|
|
pub steps: Vec<FriQueryStepTarget<D>>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 15:44:50 +02:00
|
|
|
pub struct FriProof<F: Field + Extendable<D>, const D: usize> {
|
2021-04-07 09:10:06 -07:00
|
|
|
/// A Merkle root for each reduced polynomial in the commit phase.
|
|
|
|
|
pub commit_phase_merkle_roots: Vec<Hash<F>>,
|
2021-04-21 22:31:45 +02:00
|
|
|
/// Query rounds proofs
|
2021-05-18 15:44:50 +02:00
|
|
|
pub query_round_proofs: Vec<FriQueryRound<F, D>>,
|
2021-04-06 19:11:21 -07:00
|
|
|
/// The final polynomial in coefficient form.
|
2021-05-18 15:22:06 +02:00
|
|
|
pub final_poly: PolynomialCoeffs<F::Extension>,
|
2021-04-22 15:50:08 +02:00
|
|
|
/// Witness showing that the prover did PoW.
|
|
|
|
|
pub pow_witness: F,
|
2021-04-06 19:11:21 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 10:47:46 +02:00
|
|
|
pub struct FriProofTarget<const D: usize> {
|
2021-04-07 09:10:06 -07:00
|
|
|
pub commit_phase_merkle_roots: Vec<HashTarget>,
|
2021-06-04 10:47:46 +02:00
|
|
|
pub query_round_proofs: Vec<FriQueryRoundTarget<D>>,
|
|
|
|
|
pub final_poly: PolynomialCoeffsExtTarget<D>,
|
|
|
|
|
pub pow_witness: Target,
|
2021-02-26 13:18:41 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 17:55:49 +02:00
|
|
|
#[derive(Clone, Debug)]
|
2021-02-09 21:25:21 -08:00
|
|
|
/// The purported values of each polynomial at a single point.
|
2021-05-31 17:49:04 +02:00
|
|
|
pub struct OpeningSet<F: Field + Extendable<D>, const D: usize> {
|
|
|
|
|
pub constants: Vec<F::Extension>,
|
2021-06-08 21:23:52 -07:00
|
|
|
pub plonk_s_sigmas: Vec<F::Extension>,
|
2021-05-31 17:49:04 +02:00
|
|
|
pub wires: Vec<F::Extension>,
|
|
|
|
|
pub plonk_zs: Vec<F::Extension>,
|
|
|
|
|
pub plonk_zs_right: Vec<F::Extension>,
|
|
|
|
|
pub quotient_polys: Vec<F::Extension>,
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|
|
|
|
|
|
2021-05-31 17:49:04 +02:00
|
|
|
impl<F: Field + Extendable<D>, const D: usize> OpeningSet<F, D> {
|
2021-05-06 23:14:37 +02:00
|
|
|
pub fn new(
|
2021-05-31 17:49:04 +02:00
|
|
|
z: F::Extension,
|
|
|
|
|
g: F::Extension,
|
2021-05-06 23:14:37 +02:00
|
|
|
constant_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
plonk_sigmas_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
wires_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
plonk_zs_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
quotient_polys_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
) -> Self {
|
2021-05-31 17:49:04 +02:00
|
|
|
let eval_commitment = |z: F::Extension, c: &ListPolynomialCommitment<F>| {
|
|
|
|
|
c.polynomials
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|p| p.to_extension().eval(z))
|
|
|
|
|
.collect::<Vec<_>>()
|
2021-05-06 23:14:37 +02:00
|
|
|
};
|
|
|
|
|
Self {
|
|
|
|
|
constants: eval_commitment(z, constant_commitment),
|
2021-06-08 21:23:52 -07:00
|
|
|
plonk_s_sigmas: eval_commitment(z, plonk_sigmas_commitment),
|
2021-05-06 23:14:37 +02:00
|
|
|
wires: eval_commitment(z, wires_commitment),
|
|
|
|
|
plonk_zs: eval_commitment(z, plonk_zs_commitment),
|
2021-05-31 17:49:04 +02:00
|
|
|
plonk_zs_right: eval_commitment(g * z, plonk_zs_commitment),
|
2021-05-06 23:14:37 +02:00
|
|
|
quotient_polys: eval_commitment(z, quotient_polys_commitment),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-09 21:25:21 -08:00
|
|
|
/// The purported values of each polynomial at a single point.
|
2021-06-04 10:47:46 +02:00
|
|
|
pub struct OpeningSetTarget<const D: usize> {
|
|
|
|
|
pub constants: Vec<ExtensionTarget<D>>,
|
|
|
|
|
pub plonk_sigmas: Vec<ExtensionTarget<D>>,
|
|
|
|
|
pub wires: Vec<ExtensionTarget<D>>,
|
|
|
|
|
pub plonk_zs: Vec<ExtensionTarget<D>>,
|
2021-06-08 14:56:49 +02:00
|
|
|
pub plonk_zs_right: Vec<ExtensionTarget<D>>,
|
2021-06-04 10:47:46 +02:00
|
|
|
pub quotient_polys: Vec<ExtensionTarget<D>>,
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|