From adf5c2d4ecc8e0afa7812120f631ef667d65a892 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Tue, 18 May 2021 15:44:50 +0200 Subject: [PATCH] Const generics everywhere --- src/bin/bench_recursion.rs | 4 +-- src/circuit_data.rs | 14 ++++++--- src/fri/mod.rs | 4 +-- src/fri/prover.rs | 22 ++++++--------- src/fri/verifier.rs | 43 +++++++++------------------- src/polynomial/commitment.rs | 55 ++++++++++++++++++------------------ src/proof.rs | 14 ++++----- src/prover.rs | 4 +-- 8 files changed, 72 insertions(+), 88 deletions(-) diff --git a/src/bin/bench_recursion.rs b/src/bin/bench_recursion.rs index a9b0aa0c..12af72df 100644 --- a/src/bin/bench_recursion.rs +++ b/src/bin/bench_recursion.rs @@ -20,7 +20,7 @@ fn main() { // change this to info or warn later. env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init(); - bench_prove::(); + bench_prove::(); // bench_field_mul::(); @@ -29,7 +29,7 @@ fn main() { // bench_gmimc::(); } -fn bench_prove>() { +fn bench_prove, const D: usize>() { let gmimc_gate = GMiMCGate::::with_automatic_constants(); let config = CircuitConfig { diff --git a/src/circuit_data.rs b/src/circuit_data.rs index f9bdf420..cae1501f 100644 --- a/src/circuit_data.rs +++ b/src/circuit_data.rs @@ -56,8 +56,11 @@ pub struct CircuitData { pub(crate) common: CommonCircuitData, } -impl> CircuitData { - pub fn prove(&self, inputs: PartialWitness) -> Proof { +impl CircuitData { + pub fn prove(&self, inputs: PartialWitness) -> Proof + where + F: Extendable, + { prove(&self.prover_only, &self.common, inputs) } @@ -78,8 +81,11 @@ pub struct ProverCircuitData { pub(crate) common: CommonCircuitData, } -impl> ProverCircuitData { - pub fn prove(&self, inputs: PartialWitness) -> Proof { +impl ProverCircuitData { + pub fn prove(&self, inputs: PartialWitness) -> Proof + where + F: Extendable, + { prove(&self.prover_only, &self.common, inputs) } } diff --git a/src/fri/mod.rs b/src/fri/mod.rs index f00fe8f8..a81fda1e 100644 --- a/src/fri/mod.rs +++ b/src/fri/mod.rs @@ -100,9 +100,9 @@ mod tests { PolynomialValues::new(coset_lde.values.into_iter().map(|x| F2::from(x)).collect()); let root = tree.root; let mut challenger = Challenger::new(); - let proof = fri_proof( + let proof = fri_proof::( &[&tree], - &coeffs.to_extension::(), + &coeffs.to_extension::<2>(), &coset_lde, &mut challenger, &config, diff --git a/src/fri/prover.rs b/src/fri/prover.rs index cfbf40e1..21e24296 100644 --- a/src/fri/prover.rs +++ b/src/fri/prover.rs @@ -11,7 +11,7 @@ use crate::proof::{FriInitialTreeProof, FriProof, FriQueryRound, FriQueryStep, H use crate::util::reverse_index_bits_in_place; /// Builds a FRI proof. -pub fn fri_proof( +pub fn fri_proof, const D: usize>( initial_merkle_trees: &[&MerkleTree], // Coefficients of the polynomial on which the LDT is performed. Only the first `1/rate` coefficients are non-zero. lde_polynomial_coeffs: &PolynomialCoeffs, @@ -19,10 +19,7 @@ pub fn fri_proof( lde_polynomial_values: &PolynomialValues, challenger: &mut Challenger, config: &FriConfig, -) -> FriProof -where - F: Extendable, -{ +) -> FriProof { let n = lde_polynomial_values.values.len(); assert_eq!(lde_polynomial_coeffs.coeffs.len(), n); @@ -50,15 +47,12 @@ where } } -fn fri_committed_trees( +fn fri_committed_trees, const D: usize>( polynomial_coeffs: &PolynomialCoeffs, polynomial_values: &PolynomialValues, challenger: &mut Challenger, config: &FriConfig, -) -> (Vec>, PolynomialCoeffs) -where - F: Extendable, -{ +) -> (Vec>, PolynomialCoeffs) { let mut values = polynomial_values.clone(); let mut coeffs = polynomial_coeffs.clone(); @@ -120,25 +114,25 @@ fn fri_proof_of_work(current_hash: Hash, config: &FriConfig) -> F { .expect("Proof of work failed.") } -fn fri_prover_query_rounds>( +fn fri_prover_query_rounds, const D: usize>( initial_merkle_trees: &[&MerkleTree], trees: &[MerkleTree], challenger: &mut Challenger, n: usize, config: &FriConfig, -) -> Vec> { +) -> Vec> { (0..config.num_query_rounds) .map(|_| fri_prover_query_round(initial_merkle_trees, trees, challenger, n, config)) .collect() } -fn fri_prover_query_round>( +fn fri_prover_query_round, const D: usize>( initial_merkle_trees: &[&MerkleTree], trees: &[MerkleTree], challenger: &mut Challenger, n: usize, config: &FriConfig, -) -> FriQueryRound { +) -> FriQueryRound { let mut query_steps = Vec::new(); let x = challenger.get_challenge(); let mut x_index = x.to_canonical_u64() as usize % n; diff --git a/src/fri/verifier.rs b/src/fri/verifier.rs index 09819743..ed090ed3 100644 --- a/src/fri/verifier.rs +++ b/src/fri/verifier.rs @@ -13,16 +13,13 @@ use anyhow::{ensure, Result}; /// Computes P'(x^arity) from {P(x*g^i)}_(i=0..arity), where g is a `arity`-th root of unity /// and P' is the FRI reduced polynomial. -fn compute_evaluation( +fn compute_evaluation, const D: usize>( x: F, old_x_index: usize, arity_bits: usize, last_evals: &[F::Extension], beta: F::Extension, -) -> F::Extension -where - F: Extendable, -{ +) -> F::Extension { debug_assert_eq!(last_evals.len(), 1 << arity_bits); let g = F::primitive_root_of_unity(arity_bits); @@ -43,14 +40,11 @@ where interpolate(&points, beta, &barycentric_weights) } -fn fri_verify_proof_of_work( - proof: &FriProof, +fn fri_verify_proof_of_work, const D: usize>( + proof: &FriProof, challenger: &mut Challenger, config: &FriConfig, -) -> Result<()> -where - F: Extendable, -{ +) -> Result<()> { let hash = hash_n_to_1( challenger .get_hash() @@ -70,20 +64,17 @@ where Ok(()) } -pub fn verify_fri_proof( +pub fn verify_fri_proof, const D: usize>( purported_degree_log: usize, // Point-evaluation pairs for polynomial commitments. points: &[(F::Extension, F::Extension)], // Scaling factor to combine polynomials. alpha: F::Extension, initial_merkle_roots: &[Hash], - proof: &FriProof, + proof: &FriProof, challenger: &mut Challenger, config: &FriConfig, -) -> Result<()> -where - F: Extendable, -{ +) -> Result<()> { let total_arities = config.reduction_arity_bits.iter().sum::(); ensure!( purported_degree_log @@ -149,17 +140,14 @@ fn fri_verify_initial_proof( Ok(()) } -fn fri_combine_initial( +fn fri_combine_initial, const D: usize>( proof: &FriInitialTreeProof, alpha: F::Extension, interpolant: &PolynomialCoeffs, points: &[(F::Extension, F::Extension)], subgroup_x: F, config: &FriConfig, -) -> F::Extension -where - F: Extendable, -{ +) -> F::Extension { let e = proof .evals_proofs .iter() @@ -175,21 +163,18 @@ where numerator / denominator } -fn fri_verifier_query_round( +fn fri_verifier_query_round, const D: usize>( interpolant: &PolynomialCoeffs, points: &[(F::Extension, F::Extension)], alpha: F::Extension, initial_merkle_roots: &[Hash], - proof: &FriProof, + proof: &FriProof, challenger: &mut Challenger, n: usize, betas: &[F::Extension], - round_proof: &FriQueryRound, + round_proof: &FriQueryRound, config: &FriConfig, -) -> Result<()> -where - F: Extendable, -{ +) -> Result<()> { let mut evaluations: Vec> = Vec::new(); let x = challenger.get_challenge(); let mut domain_size = n; diff --git a/src/polynomial/commitment.rs b/src/polynomial/commitment.rs index 7b7df924..8a386d6a 100644 --- a/src/polynomial/commitment.rs +++ b/src/polynomial/commitment.rs @@ -15,7 +15,6 @@ use crate::timed; use crate::util::{log2_strict, reverse_index_bits_in_place, transpose}; pub const SALT_SIZE: usize = 2; -pub const EXTENSION_DEGREE: usize = 2; pub struct ListPolynomialCommitment { pub polynomials: Vec>, @@ -77,14 +76,14 @@ impl ListPolynomialCommitment { &leaf[0..leaf.len() - if self.blinding { SALT_SIZE } else { 0 }] } - pub fn open( + pub fn open( &self, points: &[F::Extension], challenger: &mut Challenger, config: &FriConfig, - ) -> (OpeningProof, Vec>) + ) -> (OpeningProof, Vec>) where - F: Extendable, + F: Extendable, { assert_eq!(self.rate_bits, config.rate_bits); assert_eq!(config.blinding.len(), 1); @@ -152,14 +151,14 @@ impl ListPolynomialCommitment { ) } - pub fn batch_open( + pub fn batch_open( commitments: &[&Self], points: &[F::Extension], challenger: &mut Challenger, config: &FriConfig, - ) -> (OpeningProof, Vec>>) + ) -> (OpeningProof, Vec>>) where - F: Extendable, + F: Extendable, { let degree = commitments[0].degree; assert_eq!(config.blinding.len(), commitments.len()); @@ -250,14 +249,14 @@ impl ListPolynomialCommitment { ) } - pub fn batch_open_plonk( + pub fn batch_open_plonk( commitments: &[&Self; 5], points: &[F::Extension], challenger: &mut Challenger, config: &FriConfig, - ) -> (OpeningProof, Vec>) + ) -> (OpeningProof, Vec>) where - F: Extendable, + F: Extendable, { let (op, mut evaluations) = Self::batch_open(commitments, points, challenger, config); let opening_sets = evaluations @@ -278,13 +277,13 @@ impl ListPolynomialCommitment { /// 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)`. - fn compute_quotient( + fn compute_quotient( points: &[F::Extension], evals: &[F::Extension], poly: &PolynomialCoeffs, ) -> PolynomialCoeffs where - F: Extendable, + F: Extendable, { let pairs = points .iter() @@ -305,13 +304,13 @@ impl ListPolynomialCommitment { } } -pub struct OpeningProof> { - fri_proof: FriProof, +pub struct OpeningProof, const D: usize> { + fri_proof: FriProof, // TODO: Get the degree from `CommonCircuitData` instead. quotient_degree: usize, } -impl> OpeningProof { +impl, const D: usize> OpeningProof { pub fn verify( &self, points: &[F::Extension], @@ -364,7 +363,7 @@ mod tests { use super::*; - fn gen_random_test_case>( + fn gen_random_test_case, const D: usize>( k: usize, degree_log: usize, num_points: usize, @@ -396,10 +395,10 @@ mod tests { num_query_rounds: 3, blinding: vec![false], }; - let (polys, points) = gen_random_test_case::(k, degree_log, num_points); + let (polys, points) = gen_random_test_case::(k, degree_log, num_points); let lpc = ListPolynomialCommitment::new(polys, fri_config.rate_bits, false); - let (proof, evaluations) = lpc.open(&points, &mut Challenger::new(), &fri_config); + let (proof, evaluations) = lpc.open::<2>(&points, &mut Challenger::new(), &fri_config); proof.verify( &points, &evaluations.into_iter().map(|e| vec![e]).collect::>(), @@ -423,10 +422,10 @@ mod tests { num_query_rounds: 3, blinding: vec![true], }; - let (polys, points) = gen_random_test_case::(k, degree_log, num_points); + let (polys, points) = gen_random_test_case::(k, degree_log, num_points); let lpc = ListPolynomialCommitment::new(polys, fri_config.rate_bits, true); - let (proof, evaluations) = lpc.open(&points, &mut Challenger::new(), &fri_config); + let (proof, evaluations) = lpc.open::<2>(&points, &mut Challenger::new(), &fri_config); proof.verify( &points, &evaluations.into_iter().map(|e| vec![e]).collect::>(), @@ -452,15 +451,15 @@ mod tests { num_query_rounds: 3, blinding: vec![false, false, false], }; - let (polys0, _) = gen_random_test_case::(k0, degree_log, num_points); - let (polys1, _) = gen_random_test_case::(k0, degree_log, num_points); - let (polys2, points) = gen_random_test_case::(k0, degree_log, num_points); + let (polys0, _) = gen_random_test_case::(k0, degree_log, num_points); + let (polys1, _) = gen_random_test_case::(k0, degree_log, num_points); + let (polys2, points) = gen_random_test_case::(k0, degree_log, num_points); let lpc0 = ListPolynomialCommitment::new(polys0, fri_config.rate_bits, false); let lpc1 = ListPolynomialCommitment::new(polys1, fri_config.rate_bits, false); let lpc2 = ListPolynomialCommitment::new(polys2, fri_config.rate_bits, false); - let (proof, evaluations) = ListPolynomialCommitment::batch_open( + let (proof, evaluations) = ListPolynomialCommitment::batch_open::<2>( &[&lpc0, &lpc1, &lpc2], &points, &mut Challenger::new(), @@ -495,15 +494,15 @@ mod tests { num_query_rounds: 3, blinding: vec![true, false, true], }; - let (polys0, _) = gen_random_test_case::(k0, degree_log, num_points); - let (polys1, _) = gen_random_test_case::(k0, degree_log, num_points); - let (polys2, points) = gen_random_test_case::(k0, degree_log, num_points); + let (polys0, _) = gen_random_test_case::(k0, degree_log, num_points); + let (polys1, _) = gen_random_test_case::(k0, degree_log, num_points); + let (polys2, points) = gen_random_test_case::(k0, degree_log, num_points); let lpc0 = ListPolynomialCommitment::new(polys0, fri_config.rate_bits, true); let lpc1 = ListPolynomialCommitment::new(polys1, fri_config.rate_bits, false); let lpc2 = ListPolynomialCommitment::new(polys2, fri_config.rate_bits, true); - let (proof, evaluations) = ListPolynomialCommitment::batch_open( + let (proof, evaluations) = ListPolynomialCommitment::batch_open::<2>( &[&lpc0, &lpc1, &lpc2], &points, &mut Challenger::new(), diff --git a/src/proof.rs b/src/proof.rs index ef3b9705..03e78f16 100644 --- a/src/proof.rs +++ b/src/proof.rs @@ -55,7 +55,7 @@ impl HashTarget { } } -pub struct Proof> { +pub struct Proof, const D: usize> { /// Merkle root of LDEs of wire values. pub wires_root: Hash, /// Merkle root of LDEs of Z, in the context of Plonk's permutation argument. @@ -67,7 +67,7 @@ pub struct Proof> { pub openings: Vec>, /// A FRI argument for each FRI query. - pub opening_proof: OpeningProof, + pub opening_proof: OpeningProof, } pub struct ProofTarget { @@ -87,7 +87,7 @@ pub struct ProofTarget { /// Evaluations and Merkle proof produced by the prover in a FRI query step. // TODO: Implement FriQueryStepTarget -pub struct FriQueryStep> { +pub struct FriQueryStep, const D: usize> { pub evals: Vec, pub merkle_proof: MerkleProof, } @@ -101,16 +101,16 @@ pub struct FriInitialTreeProof { /// Proof for a FRI query round. // TODO: Implement FriQueryRoundTarget -pub struct FriQueryRound> { +pub struct FriQueryRound, const D: usize> { pub initial_trees_proof: FriInitialTreeProof, - pub steps: Vec>, + pub steps: Vec>, } -pub struct FriProof> { +pub struct FriProof, const D: usize> { /// A Merkle root for each reduced polynomial in the commit phase. pub commit_phase_merkle_roots: Vec>, /// Query rounds proofs - pub query_round_proofs: Vec>, + pub query_round_proofs: Vec>, /// The final polynomial in coefficient form. pub final_poly: PolynomialCoeffs, /// Witness showing that the prover did PoW. diff --git a/src/prover.rs b/src/prover.rs index b93031ac..f9847af5 100644 --- a/src/prover.rs +++ b/src/prover.rs @@ -22,11 +22,11 @@ use crate::witness::PartialWitness; /// Corresponds to constants - sigmas - wires - zs - quotient — polynomial commitments. pub const PLONK_BLINDING: [bool; 5] = [false, false, true, true, true]; -pub(crate) fn prove>( +pub(crate) fn prove, const D: usize>( prover_data: &ProverOnlyCircuitData, common_data: &CommonCircuitData, inputs: PartialWitness, -) -> Proof { +) -> Proof { let fri_config = &common_data.config.fri_config; let start_proof_gen = Instant::now();