From 0cc345f08034ab38af5336b52cf8405d68402726 Mon Sep 17 00:00:00 2001 From: kunxian xia Date: Tue, 14 Nov 2023 16:52:25 +0800 Subject: [PATCH] add todo in mv_lookup's prover --- halo2_proofs/src/arithmetic.rs | 13 ++++- halo2_proofs/src/plonk/mv_lookup.rs | 5 +- halo2_proofs/src/plonk/mv_lookup/prover.rs | 62 +++++++++++++++++++--- halo2_proofs/tests/plonk_api.rs | 4 +- 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/halo2_proofs/src/arithmetic.rs b/halo2_proofs/src/arithmetic.rs index e89bc5c697..39eacc2991 100644 --- a/halo2_proofs/src/arithmetic.rs +++ b/halo2_proofs/src/arithmetic.rs @@ -534,7 +534,7 @@ where /// This simple utility function will parallelize an operation that is to be /// performed over a mutable slice. -pub fn parallelize(v: &mut [T], f: F) { +pub(crate) fn parallelize_internal(v: &mut [T], f: F) -> Vec { let n = v.len(); let num_threads = multicore::current_num_threads(); let mut chunk = (n as usize) / num_threads; @@ -543,14 +543,23 @@ pub fn parallelize(v: &mu } multicore::scope(|scope| { + let mut chunk_starts = vec![]; for (chunk_num, v) in v.chunks_mut(chunk).enumerate() { let f = f.clone(); scope.spawn(move |_| { let start = chunk_num * chunk; f(v, start); }); + let start = chunk_num * chunk; + chunk_starts.push(start); } - }); + + chunk_starts + }) +} + +pub fn parallelize(v: &mut [T], f: F) { + parallelize_internal(v, f); } fn log2_floor(num: usize) -> u32 { diff --git a/halo2_proofs/src/plonk/mv_lookup.rs b/halo2_proofs/src/plonk/mv_lookup.rs index 86022912ad..b4fd2d825f 100644 --- a/halo2_proofs/src/plonk/mv_lookup.rs +++ b/halo2_proofs/src/plonk/mv_lookup.rs @@ -63,10 +63,7 @@ impl Argument { .map(|input_expressions| expr_degree(input_expressions)) .sum(); - let mut table_degree = 0; - for expr in self.table_expressions.iter() { - table_degree = std::cmp::max(table_degree, expr.degree()); - } + let table_degree = expr_degree(&self.table_expressions); /* φ_i(X) = f_i(X) + α diff --git a/halo2_proofs/src/plonk/mv_lookup/prover.rs b/halo2_proofs/src/plonk/mv_lookup/prover.rs index 18a57c3058..864eba4d54 100644 --- a/halo2_proofs/src/plonk/mv_lookup/prover.rs +++ b/halo2_proofs/src/plonk/mv_lookup/prover.rs @@ -30,6 +30,7 @@ use std::{ }; use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; +use crate::arithmetic::parallelize_internal; #[derive(Debug)] pub(in crate::plonk) struct Prepared { @@ -67,7 +68,7 @@ impl Argument { fixed_values: &'a [Polynomial], instance_values: &'a [Polynomial], challenges: &'a [C::Scalar], - mut _rng: R, // in case we want to blind (do we actually need zk?) + mut rng: R, // in case we want to blind (do we actually need zk?) transcript: &mut T, ) -> Result, Error> where @@ -107,6 +108,7 @@ impl Argument { let blinding_factors = pk.vk.cs.blinding_factors(); + // TODO: construction of BTreeMap for a large vector // compute m(X) let table_index_value_mapping: BTreeMap = compressed_table_expression .iter() @@ -153,7 +155,7 @@ impl Argument { } // check sums - let alpha = C::Scalar::random(&mut _rng); + let alpha = C::Scalar::random(&mut rng); let cs_input_sum = |compressed_input_expression: &Polynomial| { let mut lhs_sum = C::Scalar::zero(); @@ -182,7 +184,8 @@ impl Argument { } // commit to m(X) - let blind = Blind(C::Scalar::zero()); + // TODO: should we use zero instead? + let blind = Blind(C::Scalar::random(rng)); let m_commitment = params.commit_lagrange(&m_values, blind).to_affine(); // write commitment of m(X) to transcript @@ -212,8 +215,8 @@ impl Prepared { transcript: &mut T, ) -> Result, Error> { /* - φ_i(X) = f_i(X) + α - τ(X) = t(X) + α + φ_i(X) = f_i(X) + beta + τ(X) = t(X) + beta LHS = τ(X) * Π(φ_i(X)) * (ϕ(gX) - ϕ(X)) RHS = τ(X) * Π(φ_i(X)) * (∑ 1/(φ_i(X)) - m(X) / τ(X)))) */ @@ -234,6 +237,7 @@ impl Prepared { } }, ); + // TODO: use parallelized batch invert input_log_derivatives.iter_mut().batch_invert(); // TODO: remove last blinders from this @@ -256,6 +260,7 @@ impl Prepared { }, ); + // TODO: use parallelized batch invert table_log_derivatives.iter_mut().batch_invert(); // (Σ 1/(φ_i(X)) - m(X) / τ(X)) @@ -276,6 +281,7 @@ impl Prepared { // over our domain, starting with phi[0] = 0 let blinding_factors = pk.vk.cs.blinding_factors(); let phi = { + // parallelized version of log_derivatives_diff.scan() let active_size = params.n() as usize - blinding_factors; let chunk = { let num_threads = crate::multicore::current_num_threads(); @@ -291,6 +297,8 @@ impl Prepared { .chain(log_derivatives_diff) .take(active_size) .collect::>(); + // TODO: remove the implicit assumption that parallelize() split the grand_sum + // into segments that each has `chunk` elements except the last. parallelize(&mut grand_sum, |segment_grand_sum, _| { for i in 1..segment_grand_sum.len() { segment_grand_sum[i] += segment_grand_sum[i - 1]; @@ -372,7 +380,7 @@ impl Prepared { assert_eq!(phi[u], C::Scalar::zero()); } - let grand_sum_blind = Blind(C::Scalar::zero()); + let grand_sum_blind = Blind(C::Scalar::random(rng)); let phi_commitment = params.commit_lagrange(&phi, grand_sum_blind).to_affine(); // Hash grand sum commitment @@ -438,3 +446,45 @@ impl Evaluated { })) } } + +mod benches { + use std::collections::BTreeMap; + use std::time::Instant; + use ark_std::rand::thread_rng; + use env_logger::init; + use ff::Field; + use halo2curves::bn256::Fr; + + // bench the time to construct a BTreeMap out of a large table + // tivm is short for table_index_value_mapping + #[ignore] + #[test] + fn bench_tivm_btree_map() { + init(); + let mut rng = thread_rng(); + + for log_n in 20..26 { + let n = 1 << log_n; + let dur = Instant::now(); + let table: BTreeMap = (0..n) + .into_iter() + .map(|_| Fr::random(&mut rng)) + .enumerate() + .map(|(i, x)| (x, i)) + .collect(); + log::info!("construct btreemap from random vec (len = {}) took {:?}", n, dur.elapsed()); + } + + for log_n in 20..26 { + let n = 1 << log_n; + let dur = Instant::now(); + let table: BTreeMap = (0..n) + .into_iter() + .map(|i| Fr::from(i)) + .enumerate() + .map(|(i, x)| (x, i)) + .collect(); + log::info!("construct btreemap from increasing vec (len = {}) took {:?}", n, dur.elapsed()); + } + } +} \ No newline at end of file diff --git a/halo2_proofs/tests/plonk_api.rs b/halo2_proofs/tests/plonk_api.rs index ceb196d46b..dbe7fcf8e2 100644 --- a/halo2_proofs/tests/plonk_api.rs +++ b/halo2_proofs/tests/plonk_api.rs @@ -672,7 +672,7 @@ fn plonk_api() { >(verifier_params, pk.get_vk(), &proof[..]); } - env_logger::init(); + let _logger_err = env_logger::try_init(); test_plonk_api_ipa(); test_plonk_api_gwc(); test_plonk_api_shplonk(); @@ -854,7 +854,7 @@ fn plonk_api_with_many_subregions() { type Scheme = KZGCommitmentScheme; // bad_keys!(Scheme); - env_logger::try_init().unwrap(); + let _logger_err = env_logger::try_init(); let (a, instance, lookup_table) = common!(Scheme); let circuit: MyCircuit<::Scalar> = MyCircuit {