Skip to content

Commit

Permalink
fix: tests and benches
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-camuto committed Sep 2, 2024
1 parent d9d470c commit 70939f0
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 41 deletions.
1 change: 1 addition & 0 deletions halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ gumdrop = "0.8"
proptest = "1"
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }
serde_json = "1"
ark-std = "0.4.0"

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/benches/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate criterion;

use crate::arithmetic::small_multiexp;
use crate::arithmetic::best_multiexp_cpu;
use crate::halo2curves::pasta::{EqAffine, Fp};
use group::ff::Field;
use halo2_proofs::*;
Expand All @@ -27,7 +27,7 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("double-and-add", |b| {
b.iter(|| {
for (g_lo, g_hi) in g_lo.iter().zip(g_hi.iter()) {
small_multiexp(&[black_box(coeff_1), black_box(coeff_2)], &[*g_lo, *g_hi]);
best_multiexp_cpu(&[black_box(coeff_1), black_box(coeff_2)], &[*g_lo, *g_hi]);
}
})
});
Expand Down
12 changes: 10 additions & 2 deletions halo2_proofs/benches/fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ use halo2_proofs::*;
use halo2curves::pasta::Fp;

use criterion::{BenchmarkId, Criterion};
use poly::EvaluationDomain;
use rand_core::OsRng;

fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("fft");
for k in 3..19 {
group.bench_function(BenchmarkId::new("k", k), |b| {
let mut a = (0..(1 << k)).map(|_| Fp::random(OsRng)).collect::<Vec<_>>();
let domain = EvaluationDomain::<Fp>::new(1, k);
let n = domain.get_n() as usize;

let input = vec![Fp::random(OsRng); n];

let mut a = input.clone();
let l_a = a.len();

let omega = Fp::random(OsRng); // would be weird if this mattered
b.iter(|| {
best_fft(&mut a, omega, k as u32);
best_fft(&mut a, omega, k as u32, domain.get_fft_data(l_a), false);
});
});
}
Expand Down
8 changes: 0 additions & 8 deletions halo2_proofs/src/circuit/floor_planner/v1/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,8 @@ pub fn slot_in_biggest_advice_first(
// We now use `sort_by_cached_key` with non-unique keys, and rely on `region_shapes`
// being sorted by region index (which we also rely on below to return `RegionStart`s
// in the correct order).
#[cfg(not(feature = "floor-planner-v1-legacy-pdqsort"))]
sorted_regions.sort_by_cached_key(sort_key);

// To preserve compatibility, when the "floor-planner-v1-legacy-pdqsort" feature is enabled,
// we use a copy of the pdqsort implementation from the Rust 1.56.1 standard library, fixed
// to its behaviour on 64-bit platforms.
// https://github.com/rust-lang/rust/blob/1.56.1/library/core/src/slice/mod.rs#L2365-L2402
#[cfg(feature = "floor-planner-v1-legacy-pdqsort")]
halo2_legacy_pdqsort::sort::quicksort(&mut sorted_regions, |a, b| sort_key(a).lt(&sort_key(b)));

sorted_regions.reverse();

// Lay out the sorted regions.
Expand Down
4 changes: 3 additions & 1 deletion halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ use crate::{

use maybe_rayon::prelude::{
IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator, ParallelIterator,
ParallelSliceMut,
};

#[cfg(not(feature = "mv-lookup"))]
use maybe_rayon::prelude::ParallelSliceMut;

pub mod metadata;
use metadata::Column as ColumnMetadata;
mod util;
Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ mod circuit;
mod error;
mod evaluation;
mod keygen;
#[cfg(not(feature = "mv-lookup"))]
mod lookup;
#[cfg(feature = "mv-lookup")]
mod mv_lookup;
pub mod permutation;
mod shuffle;
Expand Down
6 changes: 1 addition & 5 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,11 +1374,6 @@ impl<F: Field> Product<Self> for Expression<F> {
}
}

/// Represents an index into a vector where each entry corresponds to a distinct
/// point that polynomials are queried at.
#[derive(Copy, Clone, Debug)]
pub(crate) struct PointIndex(pub usize);

/// A "virtual cell" is a PLONK cell that has been queried at a particular relative offset
/// within a custom gate.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -1554,6 +1549,7 @@ impl<F: Field> Gate<F> {

/// TODO doc
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct LookupTracker<F: Field> {
pub(crate) table: Vec<Expression<F>>,
pub(crate) inputs: Vec<Vec<Expression<F>>>,
Expand Down
1 change: 1 addition & 0 deletions halo2_proofs/src/plonk/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
};

use group::ff::{Field, PrimeField, WithSmallOrderMulGroup};
#[cfg(feature = "mv-lookup")]
use maybe_rayon::iter::IndexedParallelIterator;
use maybe_rayon::iter::IntoParallelRefIterator;
use maybe_rayon::iter::ParallelIterator;
Expand Down
3 changes: 3 additions & 0 deletions halo2_proofs/src/plonk/lookup/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub(in crate::plonk) struct Permuted<C: CurveAffine> {
}

#[derive(Debug)]
#[allow(dead_code)]
pub(in crate::plonk) struct Committed<C: CurveAffine> {
pub(in crate::plonk) permuted_input_poly: Polynomial<C::Scalar, Coeff>,
permuted_input_blind: Blind<C::Scalar>,
Expand All @@ -56,6 +57,7 @@ pub(in crate::plonk) struct Committed<C: CurveAffine> {
}

impl<C: SerdeCurveAffine> Committed<C> {
#[allow(dead_code)]
pub fn write<W: std::io::Write>(
&self,
writer: &mut W,
Expand All @@ -74,6 +76,7 @@ impl<C: SerdeCurveAffine> Committed<C> {
self.commitment.write(writer, format)
}

#[allow(dead_code)]
pub fn read<R: std::io::Read>(reader: &mut R, format: SerdeFormat) -> std::io::Result<Self>
where
<C as CurveAffine>::ScalarExt: crate::helpers::SerdePrimeField,
Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/src/plonk/mv_lookup/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub(in crate::plonk) struct Committed<C: CurveAffine> {
}

impl<C: SerdeCurveAffine> Committed<C> {
#[allow(dead_code)]
pub fn write<W: std::io::Write>(
&self,
writer: &mut W,
Expand All @@ -56,6 +57,7 @@ impl<C: SerdeCurveAffine> Committed<C> {
self.commitment.write(writer, format)
}

#[allow(dead_code)]
pub fn read<R: std::io::Read>(reader: &mut R, format: SerdeFormat) -> std::io::Result<Self>
where
<C as CurveAffine>::ScalarExt: crate::helpers::SerdePrimeField,
Expand Down
37 changes: 18 additions & 19 deletions halo2_proofs/src/plonk/prover.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use ff::{Field, FromUniformBytes, WithSmallOrderMulGroup};
use group::Curve;
use halo2curves::serde::SerdeObject;

use instant::Instant;
use rand_core::RngCore;
use rustc_hash::FxBuildHasher;
Expand All @@ -20,18 +18,16 @@ use super::{
permutation, shuffle, vanishing, ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX,
ChallengeY, Error, ProvingKey,
};
use maybe_rayon::iter::IndexedParallelIterator;
use maybe_rayon::iter::ParallelIterator;
#[cfg(feature = "mv-lookup")]
use maybe_rayon::iter::{IndexedParallelIterator, ParallelIterator};

#[cfg(not(feature = "mv-lookup"))]
use super::lookup;
#[cfg(feature = "mv-lookup")]
use super::mv_lookup as lookup;

#[cfg(feature = "mv-lookup")]
use maybe_rayon::iter::{
IntoParallelIterator, IntoParallelRefIterator, IntoParallelRefMutIterator,
};
use maybe_rayon::iter::{IntoParallelIterator, IntoParallelRefIterator};

use crate::{
arithmetic::{eval_polynomial, CurveAffine},
Expand Down Expand Up @@ -69,8 +65,7 @@ pub fn create_proof<
transcript: &mut T,
) -> Result<(), Error>
where
Scheme::Scalar: WithSmallOrderMulGroup<3> + FromUniformBytes<64> + SerdeObject,
Scheme::Curve: SerdeObject,
Scheme::Scalar: WithSmallOrderMulGroup<3> + FromUniformBytes<64>,
Scheme::ParamsProver: Send + Sync,
{
#[cfg(feature = "counter")]
Expand Down Expand Up @@ -498,11 +493,13 @@ where
.collect::<Result<Vec<_>, _>>()?;

#[cfg(feature = "mv-lookup")]
lookups.iter().for_each(|lookups| {
lookups.iter().for_each(|lookup| {
transcript.write_point(lookup.commitment);
});
});
{
for lookups_ in &lookups {
for lookup in lookups_.iter() {
transcript.write_point(lookup.commitment)?;
}
}
}

#[cfg(not(feature = "mv-lookup"))]
let lookups: Vec<Vec<lookup::prover::Permuted<Scheme::Curve>>> = instance
Expand Down Expand Up @@ -603,11 +600,13 @@ where
let lookups = commit_lookups()?;

#[cfg(feature = "mv-lookup")]
lookups.iter().for_each(|lookups| {
lookups.iter().for_each(|lookup| {
transcript.write_point(lookup.commitment);
});
});
{
for lookups_ in &lookups {
for lookup in lookups_.iter() {
transcript.write_point(lookup.commitment)?;
}
}
}

log::trace!("Lookup commitment: {:?}", start.elapsed());

Expand Down
6 changes: 4 additions & 2 deletions halo2_proofs/src/poly/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ impl<F: Field> Default for Blind<F> {
}

impl<F: Field + SerdePrimeField> Blind<F> {
/// Writes polynomial to buffer using `SerdePrimeField::write`.
/// Writes blind to buffer using `SerdePrimeField::write`.
#[allow(dead_code)]
pub(crate) fn write<W: io::Write>(
&self,
writer: &mut W,
Expand All @@ -209,7 +210,8 @@ impl<F: Field + SerdePrimeField> Blind<F> {
Ok(())
}

/// Reads polynomial from buffer using `SerdePrimeField::read`.
/// Reads blind from buffer using `SerdePrimeField::read`.
#[allow(dead_code)]
pub(crate) fn read<R: io::Read>(
reader: &mut R,
format: crate::SerdeFormat,
Expand Down
5 changes: 4 additions & 1 deletion halo2_proofs/tests/plonk_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn plonk_api() {

/// This represents an advice column at a certain row in the ConstraintSystem
#[derive(Copy, Clone, Debug)]
#[allow(dead_code)]
pub struct Variable(Column<Advice>, usize);

#[derive(Clone)]
Expand Down Expand Up @@ -459,7 +460,7 @@ fn plonk_api() {
Scheme: CommitmentScheme,
P: Prover<'params, Scheme>,
E: EncodedChallenge<Scheme::Curve>,
R: RngCore,
R: RngCore + Send + Sync,
T: TranscriptWriterBuffer<Vec<u8>, Scheme::Curve, E>,
>(
rng: R,
Expand All @@ -468,6 +469,8 @@ fn plonk_api() {
) -> Vec<u8>
where
Scheme::Scalar: Ord + WithSmallOrderMulGroup<3> + FromUniformBytes<64>,
Scheme::Curve: Send + Sync,
Scheme::ParamsProver: Send + Sync,
{
let (a, instance, lookup_table) = common!(Scheme);

Expand Down
4 changes: 3 additions & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
nightly-2024-07-18
[toolchain]
channel = "nightly-2024-07-18"
components = ["rustfmt", "clippy"]

0 comments on commit 70939f0

Please sign in to comment.