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-04-21 22:31:45 +02:00
|
|
|
use crate::merkle_proofs::{MerkleProof, MerkleProofTarget};
|
2021-05-18 15:22:06 +02:00
|
|
|
use crate::polynomial::commitment::{ListPolynomialCommitment, OpeningProof, EXTENSION_DEGREE};
|
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-04-09 12:40:43 -07:00
|
|
|
use std::convert::TryInto;
|
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-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:22:06 +02:00
|
|
|
pub struct Proof<F: Field + Extendable<EXTENSION_DEGREE>> {
|
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-02-09 21:25:21 -08:00
|
|
|
|
|
|
|
|
/// Purported values of each polynomial at each challenge point.
|
2021-05-18 15:22:06 +02:00
|
|
|
pub openings: Vec<OpeningSet<F::Extension>>,
|
2021-02-09 21:25:21 -08:00
|
|
|
|
2021-04-06 19:11:21 -07:00
|
|
|
/// A FRI argument for each FRI query.
|
2021-05-07 16:22:13 +02:00
|
|
|
pub opening_proof: OpeningProof<F>,
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-30 10:02:00 -07:00
|
|
|
pub struct ProofTarget {
|
2021-02-09 21:25:21 -08:00
|
|
|
/// Merkle root of LDEs of wire values.
|
|
|
|
|
pub wires_root: HashTarget,
|
|
|
|
|
/// 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: HashTarget,
|
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: HashTarget,
|
2021-02-09 21:25:21 -08:00
|
|
|
|
|
|
|
|
/// Purported values of each polynomial at each challenge point.
|
|
|
|
|
pub openings: Vec<OpeningSetTarget>,
|
|
|
|
|
|
2021-02-26 13:18:41 -08:00
|
|
|
/// A FRI argument for each FRI query.
|
|
|
|
|
pub fri_proofs: Vec<FriProofTarget>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 08:44:34 +02:00
|
|
|
/// Evaluations and Merkle proof produced by the prover in a FRI query step.
|
|
|
|
|
// TODO: Implement FriQueryStepTarget
|
2021-05-18 15:22:06 +02:00
|
|
|
pub struct FriQueryStep<F: Field + Extendable<EXTENSION_DEGREE>> {
|
|
|
|
|
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-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
|
|
|
// TODO: Implement FriInitialTreeProofTarget
|
|
|
|
|
pub struct FriInitialTreeProof<F: Field> {
|
|
|
|
|
pub evals_proofs: Vec<(Vec<F>, MerkleProof<F>)>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 08:44:34 +02:00
|
|
|
/// Proof for a FRI query round.
|
2021-04-21 22:31:45 +02:00
|
|
|
// TODO: Implement FriQueryRoundTarget
|
2021-05-18 15:22:06 +02:00
|
|
|
pub struct FriQueryRound<F: Field + Extendable<EXTENSION_DEGREE>> {
|
2021-05-04 17:48:26 +02:00
|
|
|
pub initial_trees_proof: FriInitialTreeProof<F>,
|
2021-04-27 08:44:34 +02:00
|
|
|
pub steps: Vec<FriQueryStep<F>>,
|
2021-04-21 22:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-18 15:22:06 +02:00
|
|
|
pub struct FriProof<F: Field + Extendable<EXTENSION_DEGREE>> {
|
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
|
|
|
|
|
pub query_round_proofs: Vec<FriQueryRound<F>>,
|
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-02-26 13:18:41 -08:00
|
|
|
/// Represents a single FRI query, i.e. a path through the reduction tree.
|
|
|
|
|
pub struct FriProofTarget {
|
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<HashTarget>,
|
2021-02-26 13:18:41 -08:00
|
|
|
/// Merkle proofs for the original purported codewords, i.e. the subject of the LDT.
|
|
|
|
|
pub initial_merkle_proofs: Vec<MerkleProofTarget>,
|
|
|
|
|
/// Merkle proofs for the reduced polynomials that were sent in the commit phase.
|
|
|
|
|
pub intermediate_merkle_proofs: Vec<MerkleProofTarget>,
|
2021-04-06 19:11:21 -07:00
|
|
|
/// The final polynomial in coefficient form.
|
2021-02-26 13:18:41 -08:00
|
|
|
pub final_poly: Vec<Target>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-09 21:25:21 -08:00
|
|
|
/// The purported values of each polynomial at a single point.
|
|
|
|
|
pub struct OpeningSet<F: Field> {
|
|
|
|
|
pub constants: Vec<F>,
|
|
|
|
|
pub plonk_sigmas: Vec<F>,
|
|
|
|
|
pub wires: Vec<F>,
|
2021-04-01 12:49:31 -07:00
|
|
|
pub plonk_zs: Vec<F>,
|
|
|
|
|
pub quotient_polys: Vec<F>,
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|
|
|
|
|
|
2021-05-06 23:14:37 +02:00
|
|
|
impl<F: Field> OpeningSet<F> {
|
|
|
|
|
pub fn new(
|
|
|
|
|
z: F,
|
|
|
|
|
constant_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
plonk_sigmas_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
wires_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
plonk_zs_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
quotient_polys_commitment: &ListPolynomialCommitment<F>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
let eval_commitment = |z: F, c: &ListPolynomialCommitment<F>| {
|
|
|
|
|
c.polynomials.iter().map(|p| p.eval(z)).collect::<Vec<_>>()
|
|
|
|
|
};
|
|
|
|
|
Self {
|
|
|
|
|
constants: eval_commitment(z, constant_commitment),
|
|
|
|
|
plonk_sigmas: eval_commitment(z, plonk_sigmas_commitment),
|
|
|
|
|
wires: eval_commitment(z, wires_commitment),
|
|
|
|
|
plonk_zs: eval_commitment(z, plonk_zs_commitment),
|
|
|
|
|
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.
|
|
|
|
|
pub struct OpeningSetTarget {
|
2021-02-26 13:18:41 -08:00
|
|
|
pub constants: Vec<Target>,
|
|
|
|
|
pub plonk_sigmas: Vec<Target>,
|
|
|
|
|
pub wires: Vec<Target>,
|
2021-04-01 12:49:31 -07:00
|
|
|
pub plonk_zs: Vec<Target>,
|
|
|
|
|
pub quotient_polys: Vec<Target>,
|
2021-02-09 21:25:21 -08:00
|
|
|
}
|