plonky2/src/fri/commitment.rs

352 lines
11 KiB
Rust
Raw Normal View History

use rayon::prelude::*;
2021-06-14 10:33:38 +02:00
use crate::field::extension_field::Extendable;
use crate::field::field_types::Field;
use crate::fri::proof::FriProof;
2021-07-14 21:43:55 -07:00
use crate::fri::{prover::fri_proof, verifier::verify_fri_proof};
use crate::hash::hash_types::HashOut;
use crate::hash::merkle_tree::MerkleTree;
use crate::iop::challenger::Challenger;
use crate::plonk::circuit_data::CommonCircuitData;
use crate::plonk::plonk_common::PlonkPolynomials;
use crate::plonk::proof::OpeningSet;
use crate::polynomial::polynomial::{PolynomialCoeffs, PolynomialValues};
use crate::timed;
2021-07-23 17:31:00 +02:00
use crate::util::reducing::ReducingFactor;
use crate::util::timing::TimingTree;
use crate::util::{log2_strict, reverse_bits, reverse_index_bits_in_place, transpose};
2021-04-30 15:07:54 +02:00
/// Two (~64 bit) field elements gives ~128 bit security.
2021-05-06 17:09:55 +02:00
pub const SALT_SIZE: usize = 2;
/// Represents a batch FRI based commitment to a list of polynomials.
pub struct PolynomialBatchCommitment<F: Field> {
2021-05-03 15:17:05 +02:00
pub polynomials: Vec<PolynomialCoeffs<F>>,
2021-04-30 15:07:54 +02:00
pub merkle_tree: MerkleTree<F>,
pub degree_log: usize,
pub rate_bits: usize,
pub blinding: bool,
2021-04-30 15:07:54 +02:00
}
impl<F: Field> PolynomialBatchCommitment<F> {
/// Creates a list polynomial commitment for the polynomials interpolating the values in `values`.
pub(crate) fn from_values(
values: Vec<PolynomialValues<F>>,
rate_bits: usize,
blinding: bool,
timing: &mut TimingTree,
) -> Self {
let coeffs = timed!(
timing,
"IFFT",
values.par_iter().map(|v| v.ifft()).collect::<Vec<_>>()
);
Self::from_coeffs(coeffs, rate_bits, blinding, timing)
}
/// Creates a list polynomial commitment for the polynomials `polynomials`.
pub(crate) fn from_coeffs(
polynomials: Vec<PolynomialCoeffs<F>>,
rate_bits: usize,
blinding: bool,
timing: &mut TimingTree,
) -> Self {
2021-05-03 15:17:05 +02:00
let degree = polynomials[0].len();
let lde_values = timed!(
timing,
"FFT + blinding",
Self::lde_values(&polynomials, rate_bits, blinding)
);
let mut leaves = timed!(timing, "transpose LDEs", transpose(&lde_values));
reverse_index_bits_in_place(&mut leaves);
let merkle_tree = timed!(timing, "build Merkle tree", MerkleTree::new(leaves, false));
Self {
polynomials,
merkle_tree,
degree_log: log2_strict(degree),
rate_bits,
blinding,
}
}
fn lde_values(
polynomials: &[PolynomialCoeffs<F>],
rate_bits: usize,
blinding: bool,
) -> Vec<Vec<F>> {
let degree = polynomials[0].len();
// If blinding, salt with two random elements to each leaf vector.
let salt_size = if blinding { SALT_SIZE } else { 0 };
polynomials
2021-05-07 16:49:27 +02:00
.par_iter()
2021-05-03 15:17:05 +02:00
.map(|p| {
assert_eq!(p.len(), degree, "Polynomial degrees inconsistent");
p.lde(rate_bits)
.coset_fft_with_options(F::coset_shift(), Some(rate_bits), None)
.values
2021-05-06 00:00:08 +02:00
})
.chain(
(0..salt_size)
.into_par_iter()
.map(|_| F::rand_vec(degree << rate_bits)),
)
.collect()
2021-04-30 15:07:54 +02:00
}
2021-05-03 15:17:05 +02:00
2021-06-24 14:11:47 +02:00
pub fn get_lde_values(&self, index: usize) -> &[F] {
let index = reverse_bits(index, self.degree_log + self.rate_bits);
2021-06-24 11:45:16 +02:00
let slice = &self.merkle_tree.leaves[index];
&slice[..slice.len() - if self.blinding { SALT_SIZE } else { 0 }]
}
2021-05-07 16:22:13 +02:00
2021-06-01 11:17:54 +02:00
/// Takes the commitments to the constants - sigmas - wires - zs - quotient — polynomials,
/// and an opening point `zeta` and produces a batched opening proof + opening set.
pub(crate) fn open_plonk<const D: usize>(
2021-06-25 11:24:26 +02:00
commitments: &[&Self; 4],
zeta: F::Extension,
2021-05-06 17:09:55 +02:00
challenger: &mut Challenger<F>,
2021-06-25 11:24:26 +02:00
common_data: &CommonCircuitData<F, D>,
timing: &mut TimingTree,
) -> (FriProof<F, D>, OpeningSet<F, D>)
2021-05-18 15:22:06 +02:00
where
2021-05-18 15:44:50 +02:00
F: Extendable<D>,
2021-05-18 15:22:06 +02:00
{
let config = &common_data.config;
2021-06-01 21:55:05 +02:00
assert!(D > 1, "Not implemented for D=1.");
let degree_log = commitments[0].degree_log;
let g = F::Extension::primitive_root_of_unity(degree_log);
for p in &[zeta, g * zeta] {
2021-05-06 17:09:55 +02:00
assert_ne!(
p.exp(1 << degree_log as u64),
2021-05-18 15:22:06 +02:00
F::Extension::ONE,
2021-05-06 17:09:55 +02:00
"Opening point is in the subgroup."
);
}
2021-08-02 15:49:06 -07:00
let os = timed!(
timing,
"construct the opening set",
OpeningSet::new(
zeta,
g,
commitments[0],
commitments[1],
commitments[2],
commitments[3],
common_data,
)
);
challenger.observe_opening_set(&os);
2021-05-06 17:09:55 +02:00
2021-05-18 15:22:06 +02:00
let alpha = challenger.get_extension_challenge();
2021-06-17 22:06:53 +02:00
let mut alpha = ReducingFactor::new(alpha);
2021-05-06 17:09:55 +02:00
// Final low-degree polynomial that goes into FRI.
let mut final_poly = PolynomialCoeffs::empty();
let mut zs_polys = commitments[PlonkPolynomials::ZS_PARTIAL_PRODUCTS.index]
.polynomials
.iter()
.collect::<Vec<_>>();
let partial_products_polys = zs_polys.split_off(common_data.zs_range().end);
2021-06-01 11:17:54 +02:00
// Polynomials opened at a single point.
2021-06-17 21:34:04 +02:00
let single_polys = [
2021-06-25 11:24:26 +02:00
PlonkPolynomials::CONSTANTS_SIGMAS,
PlonkPolynomials::WIRES,
2021-06-17 21:34:04 +02:00
PlonkPolynomials::QUOTIENT,
]
.iter()
.flat_map(|&p| &commitments[p.index].polynomials)
.chain(partial_products_polys);
2021-08-02 15:49:06 -07:00
let single_composition_poly = timed!(
timing,
"reduce single polys",
alpha.reduce_polys_base(single_polys)
2021-08-02 15:49:06 -07:00
);
2021-06-23 11:30:57 +02:00
let single_quotient = Self::compute_quotient([zeta], single_composition_poly);
final_poly += single_quotient;
2021-06-17 21:34:04 +02:00
alpha.reset();
2021-06-01 11:17:54 +02:00
// Zs polynomials are opened at `zeta` and `g*zeta`.
2021-08-02 15:49:06 -07:00
let zs_composition_poly = timed!(
timing,
"reduce Z polys",
alpha.reduce_polys_base(zs_polys.into_iter())
2021-08-02 15:49:06 -07:00
);
2021-06-23 11:30:57 +02:00
let zs_quotient = Self::compute_quotient([zeta, g * zeta], zs_composition_poly);
alpha.shift_poly(&mut final_poly);
2021-06-23 11:30:57 +02:00
final_poly += zs_quotient;
let lde_final_poly = final_poly.lde(config.rate_bits);
2021-08-02 15:49:06 -07:00
let lde_final_values = timed!(
timing,
&format!("perform final FFT {}", lde_final_poly.len()),
lde_final_poly.coset_fft(F::coset_shift().into())
);
2021-05-06 17:09:55 +02:00
let fri_proof = fri_proof(
&commitments
2021-05-07 16:49:27 +02:00
.par_iter()
2021-05-06 17:09:55 +02:00
.map(|c| &c.merkle_tree)
.collect::<Vec<_>>(),
2021-07-21 08:26:56 -07:00
lde_final_poly,
lde_final_values,
2021-05-06 17:09:55 +02:00
challenger,
&config.fri_config,
timing,
2021-05-06 17:09:55 +02:00
);
(fri_proof, os)
2021-05-06 17:09:55 +02:00
}
2021-05-05 18:32:24 +02:00
/// Given `points=(x_i)`, `evals=(y_i)` and `poly=P` with `P(x_i)=y_i`, computes the polynomial
/// `Q=(P-I)/Z` where `I` interpolates `(x_i, y_i)` and `Z` is the vanishing polynomial on `(x_i)`.
2021-06-23 11:30:57 +02:00
fn compute_quotient<const D: usize, const N: usize>(
points: [F::Extension; N],
poly: PolynomialCoeffs<F::Extension>,
2021-05-18 15:22:06 +02:00
) -> PolynomialCoeffs<F::Extension>
where
2021-05-18 15:44:50 +02:00
F: Extendable<D>,
2021-05-18 15:22:06 +02:00
{
2021-06-23 11:30:57 +02:00
let quotient = if N == 1 {
poly.divide_by_linear(points[0]).0
} else if N == 2 {
// The denominator is `(X - p0)(X - p1) = p0 p1 - (p0 + p1) X + X^2`.
let denominator = vec![
points[0] * points[1],
-points[0] - points[1],
F::Extension::ONE,
]
.into();
poly.div_rem_long_division(&denominator).0 // Could also use `divide_by_linear` twice.
} else {
unreachable!("This shouldn't happen. Plonk should open polynomials at 1 or 2 points.")
};
2021-05-05 18:32:24 +02:00
quotient.padded(quotient.degree_plus_one().next_power_of_two())
2021-05-05 18:32:24 +02:00
}
2021-05-03 15:17:05 +02:00
}
2021-05-04 17:48:26 +02:00
#[cfg(test)]
mod tests {
use anyhow::Result;
use super::*;
2021-07-15 09:11:54 +02:00
use crate::fri::FriConfig;
use crate::plonk::circuit_data::CircuitConfig;
2021-05-18 15:44:50 +02:00
fn gen_random_test_case<F: Field + Extendable<D>, const D: usize>(
2021-05-05 17:00:47 +02:00
k: usize,
degree_log: usize,
) -> Vec<PolynomialValues<F>> {
2021-05-05 17:00:47 +02:00
let degree = 1 << degree_log;
(0..k)
.map(|_| PolynomialValues::new(F::rand_vec(degree)))
.collect()
2021-05-05 17:00:47 +02:00
}
fn gen_random_point<F: Field + Extendable<D>, const D: usize>(
degree_log: usize,
) -> F::Extension {
let degree = 1 << degree_log;
2021-05-04 17:48:26 +02:00
let mut point = F::Extension::rand();
while point.exp(degree as u64).is_one() {
point = F::Extension::rand();
}
2021-05-04 17:48:26 +02:00
point
2021-05-06 17:09:55 +02:00
}
2021-05-18 16:06:47 +02:00
fn check_batch_polynomial_commitment<F: Field + Extendable<D>, const D: usize>() -> Result<()> {
let ks = [10, 2, 10, 8];
let degree_bits = 11;
2021-05-06 17:09:55 +02:00
let fri_config = FriConfig {
proof_of_work_bits: 2,
2021-05-31 18:00:53 +02:00
reduction_arity_bits: vec![2, 3, 1, 2],
2021-05-06 17:09:55 +02:00
num_query_rounds: 3,
};
2021-06-28 09:47:47 +02:00
// We only care about `fri_config, num_constants`, and `num_routed_wires` here.
2021-06-25 11:24:26 +02:00
let common_data = CommonCircuitData {
config: CircuitConfig {
fri_config,
2021-06-28 09:47:47 +02:00
num_routed_wires: 6,
2021-06-25 11:24:26 +02:00
..CircuitConfig::large_config()
},
degree_bits,
2021-06-25 11:24:26 +02:00
gates: vec![],
2021-07-08 15:13:29 +02:00
quotient_degree_factor: 0,
2021-06-25 11:24:26 +02:00
num_gate_constraints: 0,
num_constants: 4,
k_is: vec![F::ONE; 6],
2021-07-01 15:41:01 +02:00
num_partial_products: (0, 0),
circuit_digest: HashOut::from_partial(vec![]),
2021-06-25 11:24:26 +02:00
};
2021-05-06 17:09:55 +02:00
2021-06-25 11:24:26 +02:00
let lpcs = (0..4)
2021-05-31 18:19:44 +02:00
.map(|i| {
PolynomialBatchCommitment::<F>::from_values(
gen_random_test_case(ks[i], degree_bits),
common_data.config.rate_bits,
PlonkPolynomials::polynomials(i).blinding,
&mut TimingTree::default(),
)
})
.collect::<Vec<_>>();
2021-05-06 17:09:55 +02:00
let zeta = gen_random_point::<F, D>(degree_bits);
let (proof, os) = PolynomialBatchCommitment::open_plonk::<D>(
2021-06-25 11:24:26 +02:00
&[&lpcs[0], &lpcs[1], &lpcs[2], &lpcs[3]],
zeta,
2021-05-06 17:09:55 +02:00
&mut Challenger::new(),
2021-06-25 11:24:26 +02:00
&common_data,
&mut TimingTree::default(),
2021-05-06 17:09:55 +02:00
);
let merkle_roots = &[
lpcs[0].merkle_tree.root,
lpcs[1].merkle_tree.root,
lpcs[2].merkle_tree.root,
lpcs[3].merkle_tree.root,
];
verify_fri_proof(
&os,
zeta,
merkle_roots,
&proof,
2021-05-06 15:19:06 +02:00
&mut Challenger::new(),
&common_data,
2021-05-06 15:19:06 +02:00
)
2021-05-04 17:48:26 +02:00
}
2021-05-18 16:06:47 +02:00
2021-05-18 16:23:44 +02:00
mod quadratic {
2021-06-01 11:17:54 +02:00
use super::*;
2021-06-01 11:26:23 +02:00
use crate::field::crandall_field::CrandallField;
2021-06-01 11:17:54 +02:00
#[test]
fn test_batch_polynomial_commitment() -> Result<()> {
check_batch_polynomial_commitment::<CrandallField, 2>()
}
2021-05-18 16:06:47 +02:00
}
mod quartic {
use super::*;
2021-06-01 11:26:23 +02:00
use crate::field::crandall_field::CrandallField;
2021-06-01 11:17:54 +02:00
#[test]
fn test_batch_polynomial_commitment() -> Result<()> {
check_batch_polynomial_commitment::<CrandallField, 4>()
}
2021-05-18 16:06:47 +02:00
}
2021-04-30 15:07:54 +02:00
}