From 5f92611df1a6085b05419d168280651f72f8554e Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Sun, 21 Mar 2021 11:57:33 -0700 Subject: [PATCH] Bit of prover work --- src/circuit_builder.rs | 4 ++++ src/circuit_data.rs | 15 +++++++++++- src/field/fft.rs | 8 +++++-- src/prover.rs | 54 ++++++++++++++++++++++++++++++++++++++---- src/util.rs | 2 +- 5 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/circuit_builder.rs b/src/circuit_builder.rs index c76abcd4..5f24a54b 100644 --- a/src/circuit_builder.rs +++ b/src/circuit_builder.rs @@ -10,8 +10,12 @@ use crate::wire::Wire; pub struct CircuitBuilder2 { config: CircuitConfig, + + /// The types of gates used in this circuit. gates: HashSet>, + gate_instances: Vec>, + generators: Vec>>, } diff --git a/src/circuit_data.rs b/src/circuit_data.rs index 54036e7f..434a685f 100644 --- a/src/circuit_data.rs +++ b/src/circuit_data.rs @@ -5,6 +5,7 @@ use crate::proof::{Hash, Proof2}; use crate::prover::prove; use crate::verifier::verify; use crate::witness::PartialWitness; +use crate::gates::gate::{GateRef, Gate}; #[derive(Copy, Clone)] pub struct CircuitConfig { @@ -14,7 +15,7 @@ pub struct CircuitConfig { } impl CircuitConfig { - pub fn advice_wires(&self) -> usize { + pub fn num_advice_wires(&self) -> usize { self.num_wires - self.num_routed_wires } } @@ -74,9 +75,21 @@ pub(crate) struct CommonCircuitData { pub degree: usize, + /// The types of gates used in this circuit. + pub gates: Vec>, + /// A commitment to each constant polynomial. pub constants_root: Hash, /// A commitment to each permutation polynomial. pub sigmas_root: Hash, } + +impl CommonCircuitData { + pub fn constraint_degree(&self, config: CircuitConfig) -> usize { + self.gates.iter() + .map(|g| g.0.degree(config)) + .max() + .expect("No gates?") + } +} diff --git a/src/field/fft.rs b/src/field/fft.rs index bc0353b0..a91df5b2 100644 --- a/src/field/fft.rs +++ b/src/field/fft.rs @@ -128,10 +128,14 @@ pub fn coset_fft(coefficients: Vec, shift: F) -> Vec { .collect() } -pub fn coset_ifft(points: Vec, shift: F) -> Vec { - let shift_inv = shift.inverse(); +pub fn ifft(points: Vec) -> Vec { let precomputation = fft_precompute(points.len()); ifft_with_precomputation_power_of_2(points, &precomputation) +} + +pub fn coset_ifft(points: Vec, shift: F) -> Vec { + let shift_inv = shift.inverse(); + ifft(points) .into_iter() .map(|x| x * shift_inv) .collect() diff --git a/src/prover.rs b/src/prover.rs index 4550e7a3..fdad16f3 100644 --- a/src/prover.rs +++ b/src/prover.rs @@ -1,7 +1,10 @@ use crate::circuit_data::{CommonCircuitData, ProverOnlyCircuitData}; +use crate::field::fft::{fft, ifft}; use crate::field::field::Field; use crate::generator::generate_partial_witness; -use crate::proof::Proof2; +use crate::proof::{Proof2, Hash}; +use crate::util::log2_ceil; +use crate::wire::Wire; use crate::witness::PartialWitness; pub(crate) fn prove( @@ -12,10 +15,51 @@ pub(crate) fn prove( let mut witness = inputs; generate_partial_witness(&mut witness, &prover_data.generators); + let config = common_data.config; + let constraint_degree = 1 << log2_ceil(common_data.constraint_degree(config)); + let lde_size = constraint_degree * common_data.degree; + + let num_wires = config.num_wires; + let wire_ldes = (0..num_wires) + .map(|i| compute_wire_lde(i, &witness, common_data.degree, lde_size)) + .collect::>(); + let wires_root = merkle_root_batch(wire_ldes); + + let z_ldes = todo!(); + let plonk_z_root = merkle_root_batch(z_ldes); + + let plonk_t_root = todo!(); + + let openings = todo!(); + Proof2 { - wires_root: todo!(), - plonk_z_root: todo!(), - plonk_t_root: todo!(), - openings: todo!(), + wires_root, + plonk_z_root, + plonk_t_root, + openings, } } + +fn merkle_root(vec: Vec) -> Hash { + todo!() +} + +fn merkle_root_batch(vecs: Vec>) -> Hash { + todo!() +} + +fn compute_wire_lde( + input: usize, + witness: &PartialWitness, + degree: usize, + lde_size: usize, +) -> Vec { + let wire = (0..degree) + .map(|gate| witness.get_wire(Wire { gate, input })) + .collect(); + let mut coeffs = ifft(wire); + for _ in 0..(lde_size - degree) { + coeffs.push(F::ZERO); + } + fft(coeffs) +} diff --git a/src/util.rs b/src/util.rs index 967add8a..fa4f39ec 100644 --- a/src/util.rs +++ b/src/util.rs @@ -13,7 +13,7 @@ pub(crate) fn log2_ceil(n: usize) -> usize { } /// Computes `log_2(n)`, panicking if `n` is not a power of two. -pub fn log2_strict(n: usize) -> usize { +pub(crate) fn log2_strict(n: usize) -> usize { assert!(n.is_power_of_two(), "Not a power of two"); log2_ceil(n) }