Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deterministic tests #345

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"halo2_frontend",
"halo2_middleware",
"halo2_backend",
"p3_frontend",
"halo2_debug",
"p3_frontend"
]
resolver = "2"
1 change: 1 addition & 0 deletions halo2_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ gumdrop = "0.8"
proptest = "1"
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }
serde_json = "1"
halo2_debug = { path = "../halo2_debug" }

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand Down
6 changes: 2 additions & 4 deletions halo2_backend/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,13 @@ pub(crate) fn powers<F: Field>(base: F) -> impl Iterator<Item = F> {
std::iter::successors(Some(F::ONE), move |power| Some(base * power))
}

#[cfg(test)]
use rand_core::OsRng;

#[cfg(test)]
use halo2curves::pasta::Fp;

#[test]
fn test_lagrange_interpolate() {
let rng = OsRng;
use halo2_debug::test_rng;
let rng = test_rng();

let points = (0..5).map(|_| Fp::random(rng)).collect::<Vec<_>>();
let evals = (0..5).map(|_| Fp::random(rng)).collect::<Vec<_>>();
Expand Down
4 changes: 2 additions & 2 deletions halo2_backend/src/poly/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait CommitmentScheme {
type ParamsVerifier: for<'params> ParamsVerifier<'params, Self::Curve>;

/// Wrapper for parameter generator
fn new_params(k: u32) -> Self::ParamsProver;
fn new_params(k: u32, rng: impl RngCore) -> Self::ParamsProver;

/// Wrapper for parameter reader
fn read_params<R: io::Read>(reader: &mut R) -> io::Result<Self::ParamsProver>;
Expand Down Expand Up @@ -69,7 +69,7 @@ pub trait Params<C: CurveAffine>: Sized + Clone + Debug {
/// Parameters for circuit synthesis and prover parameters.
pub trait ParamsProver<C: CurveAffine>: Params<C> {
/// Returns new instance of parameters
fn new(k: u32) -> Self;
fn new(k: u32, rng: impl RngCore) -> Self;

/// This computes a commitment to a polynomial described by the provided
/// slice of coefficients. The commitment may be blinded by the blinding
Expand Down
10 changes: 4 additions & 6 deletions halo2_backend/src/poly/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,12 @@ pub struct PinnedEvaluationDomain<'a, F: Field> {

#[test]
fn test_rotate() {
use rand_core::OsRng;

use crate::arithmetic::eval_polynomial;
use halo2_debug::test_rng;
use halo2curves::pasta::pallas::Scalar;

let domain = EvaluationDomain::<Scalar>::new(1, 3);
let rng = OsRng;
let rng = test_rng();

let mut poly = domain.empty_lagrange();
assert_eq!(poly.len(), 8);
Expand Down Expand Up @@ -518,9 +517,8 @@ fn test_rotate() {

#[test]
fn test_l_i() {
use rand_core::OsRng;

use crate::arithmetic::{eval_polynomial, lagrange_interpolate};
use halo2_debug::test_rng;
use halo2curves::pasta::pallas::Scalar;
let domain = EvaluationDomain::<Scalar>::new(1, 3);

Expand All @@ -536,7 +534,7 @@ fn test_l_i() {
l.push(l_i);
}

let x = Scalar::random(OsRng);
let x = Scalar::random(test_rng());
let xn = x.pow([8]);

let evaluations = domain.l_i_range(x, xn, -7..=7);
Expand Down
25 changes: 11 additions & 14 deletions halo2_backend/src/poly/ipa/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::poly::{Coeff, LagrangeCoeff, Polynomial};

use group::{Curve, Group};
use halo2_middleware::zal::traits::MsmAccel;
use rand_core::RngCore;
use std::marker::PhantomData;

mod prover;
Expand Down Expand Up @@ -45,8 +46,8 @@ impl<C: CurveAffine> CommitmentScheme for IPACommitmentScheme<C> {
type ParamsProver = ParamsIPA<C>;
type ParamsVerifier = ParamsVerifierIPA<C>;

fn new_params(k: u32) -> Self::ParamsProver {
ParamsIPA::new(k)
fn new_params(k: u32, rng: impl RngCore) -> Self::ParamsProver {
ParamsIPA::new(k, rng)
}

fn read_params<R: io::Read>(reader: &mut R) -> io::Result<Self::ParamsProver> {
Expand Down Expand Up @@ -150,7 +151,7 @@ impl<C: CurveAffine> Params<C> for ParamsIPA<C> {
impl<C: CurveAffine> ParamsProver<C> for ParamsIPA<C> {
/// Initializes parameters for the curve, given a random oracle to draw
/// points from.
fn new(k: u32) -> Self {
fn new(k: u32, _: impl RngCore) -> Self {
// This is usually a limitation on the curve, but we also want 32-bit
// architectures to be supported.
assert!(k < 32);
Expand Down Expand Up @@ -240,20 +241,19 @@ mod test {
use crate::poly::ipa::msm::MSMIPA;

use group::Curve;
use halo2_debug::test_rng;
use halo2_middleware::ff::Field;
use halo2_middleware::zal::impls::H2cEngine;

#[test]
fn test_commit_lagrange_epaffine() {
const K: u32 = 6;

use rand_core::OsRng;

use crate::poly::EvaluationDomain;
use halo2curves::pasta::{EpAffine, Fq};

let engine = H2cEngine::new();
let params = ParamsIPA::<EpAffine>::new(K);
let params = ParamsIPA::<EpAffine>::new(K, test_rng());
let domain = EvaluationDomain::new(1, K);

let mut a = domain.empty_lagrange();
Expand All @@ -264,7 +264,7 @@ mod test {

let b = domain.lagrange_to_coeff(a.clone());

let alpha = Blind(Fq::random(OsRng));
let alpha = Blind(Fq::random(test_rng()));

assert_eq!(
params.commit(&engine, &b, alpha),
Expand All @@ -276,13 +276,11 @@ mod test {
fn test_commit_lagrange_eqaffine() {
const K: u32 = 6;

use rand_core::OsRng;

use crate::poly::EvaluationDomain;
use halo2curves::pasta::{EqAffine, Fp};

let engine = H2cEngine::new();
let params: ParamsIPA<EqAffine> = ParamsIPA::<EqAffine>::new(K);
let params: ParamsIPA<EqAffine> = ParamsIPA::<EqAffine>::new(K, test_rng());
let domain = EvaluationDomain::new(1, K);

let mut a = domain.empty_lagrange();
Expand All @@ -293,7 +291,7 @@ mod test {

let b = domain.lagrange_to_coeff(a.clone());

let alpha = Blind(Fp::random(OsRng));
let alpha = Blind(Fp::random(test_rng()));

assert_eq!(
params.commit(&engine, &b, alpha),
Expand All @@ -306,7 +304,6 @@ mod test {
const K: u32 = 6;

use halo2_middleware::ff::Field;
use rand_core::OsRng;

use super::super::commitment::{Blind, Params};
use crate::arithmetic::eval_polynomial;
Expand All @@ -319,10 +316,10 @@ mod test {
use crate::transcript::TranscriptReadBuffer;
use crate::transcript::TranscriptWriterBuffer;

let rng = OsRng;
let rng = test_rng();

let engine = H2cEngine::new();
let params = ParamsIPA::<EpAffine>::new(K);
let params = ParamsIPA::<EpAffine>::new(K, test_rng());
let mut params_buffer = vec![];
<ParamsIPA<_> as Params<_>>::write(&params, &mut params_buffer).unwrap();
let params: ParamsIPA<EpAffine> = Params::read::<_>(&mut &params_buffer[..]).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion halo2_backend/src/poly/ipa/msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ mod tests {
commitment::{ParamsProver, MSM},
ipa::{commitment::ParamsIPA, msm::MSMIPA},
};
use halo2_debug::test_rng;
use halo2_middleware::zal::impls::H2cEngine;
use halo2curves::{
pasta::{Ep, EpAffine, Fp, Fq},
Expand All @@ -234,7 +235,7 @@ mod tests {
let base_viol = base + base;

let engine = H2cEngine::new();
let params = ParamsIPA::new(4);
let params = ParamsIPA::new(4, test_rng());
let mut a: MSMIPA<EpAffine> = MSMIPA::new(&params);
a.append_term(Fq::one(), base);
// a = [1] P
Expand Down
19 changes: 9 additions & 10 deletions halo2_backend/src/poly/kzg/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use halo2_middleware::ff::{Field, PrimeField};
use halo2_middleware::zal::traits::MsmAccel;
use halo2curves::pairing::Engine;
use halo2curves::{CurveAffine, CurveExt};
use rand_core::{OsRng, RngCore};
use rand_core::RngCore;
use std::fmt::Debug;
use std::marker::PhantomData;

Expand Down Expand Up @@ -139,8 +139,8 @@ where
type ParamsProver = ParamsKZG<E>;
type ParamsVerifier = ParamsVerifierKZG<E>;

fn new_params(k: u32) -> Self::ParamsProver {
ParamsKZG::new(k)
fn new_params(k: u32, rng: impl RngCore) -> Self::ParamsProver {
ParamsKZG::new(k, rng)
}

fn read_params<R: io::Read>(reader: &mut R) -> io::Result<Self::ParamsProver> {
Expand Down Expand Up @@ -429,8 +429,8 @@ where
E::G1: CurveExt<AffineExt = E::G1Affine>,
E::G2Affine: SerdeCurveAffine,
{
fn new(k: u32) -> Self {
Self::setup(k, OsRng)
fn new(k: u32, rng: impl RngCore) -> Self {
Self::setup(k, rng)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put the new rng here directly (in setup()) instead of having it in new()?.
We just want randomness in the setup of mock KZG parameters, but we don't need it in IPA so IMO it's better not to modify the common interface of ParamsProver.

}

fn commit(
Expand All @@ -453,20 +453,19 @@ mod test {
use crate::poly::commitment::ParamsProver;
use crate::poly::commitment::{Blind, Params};
use crate::poly::kzg::commitment::ParamsKZG;
use halo2_debug::test_rng;
use halo2_middleware::ff::Field;
use halo2_middleware::zal::impls::H2cEngine;

#[test]
fn test_commit_lagrange() {
const K: u32 = 6;

use rand_core::OsRng;

use crate::poly::EvaluationDomain;
use halo2curves::bn256::{Bn256, Fr};

let engine = H2cEngine::new();
let params = ParamsKZG::<Bn256>::new(K);
let params = ParamsKZG::<Bn256>::new(K, test_rng());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This randomness is used to generate mock params for testing, so test_rng() is fine here.

let domain = EvaluationDomain::new(1, K);

let mut a = domain.empty_lagrange();
Expand All @@ -477,7 +476,7 @@ mod test {

let b = domain.lagrange_to_coeff(a.clone());

let alpha = Blind(Fr::random(OsRng));
let alpha = Blind(Fr::random(test_rng()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this randomness is part of the real protocol, here we need some security guarantees so we probably want to use a different randomness source rather than test_rng(). I think keeping OsRng here is fine.


assert_eq!(
params.commit(&engine, &b, alpha),
Expand All @@ -492,7 +491,7 @@ mod test {
use super::super::commitment::Params;
use halo2curves::bn256::Bn256;

let params0 = ParamsKZG::<Bn256>::new(K);
let params0 = ParamsKZG::<Bn256>::new(K, test_rng());
let mut data = vec![];
<ParamsKZG<_> as Params<_>>::write(&params0, &mut data).unwrap();
let params1: ParamsKZG<Bn256> = Params::read::<_>(&mut &data[..]).unwrap();
Expand Down
14 changes: 7 additions & 7 deletions halo2_backend/src/poly/multiopen_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ mod test {
TranscriptReadBuffer, TranscriptWriterBuffer,
};
use group::Curve;
use halo2_debug::test_rng;
use halo2_middleware::ff::WithSmallOrderMulGroup;
use halo2_middleware::zal::{impls::H2cEngine, traits::MsmAccel};
use rand_core::OsRng;

#[test]
fn test_roundtrip_ipa() {
Expand All @@ -29,7 +29,7 @@ mod test {
const K: u32 = 4;

let engine = H2cEngine::new();
let params = ParamsIPA::<EqAffine>::new(K);
let params = ParamsIPA::<EqAffine>::new(K, test_rng());

let proof = create_proof::<
IPACommitmentScheme<EqAffine>,
Expand Down Expand Up @@ -67,7 +67,7 @@ mod test {
const K: u32 = 4;

let engine = H2cEngine::new();
let params = ParamsIPA::<EqAffine>::new(K);
let params = ParamsIPA::<EqAffine>::new(K, test_rng());

let proof = create_proof::<
IPACommitmentScheme<EqAffine>,
Expand Down Expand Up @@ -105,7 +105,7 @@ mod test {
const K: u32 = 4;

let engine = H2cEngine::new();
let params = ParamsKZG::<Bn256>::new(K);
let params = ParamsKZG::<Bn256>::new(K, test_rng());

let proof = create_proof::<_, ProverGWC<_>, _, Blake2bWrite<_, _, Challenge255<_>>>(
&engine, &params,
Expand Down Expand Up @@ -138,7 +138,7 @@ mod test {
const K: u32 = 4;

let engine = H2cEngine::new();
let params = ParamsKZG::<Bn256>::new(K);
let params = ParamsKZG::<Bn256>::new(K, test_rng());

let proof = create_proof::<
KZGCommitmentScheme<Bn256>,
Expand Down Expand Up @@ -256,7 +256,7 @@ mod test {

let mut transcript = T::init(vec![]);

let blind = Blind::new(&mut OsRng);
let blind = Blind::new(&mut test_rng());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as previous comment, this randomness is not part of the testing.

let a = params.commit(engine, &ax, blind).to_affine();
let b = params.commit(engine, &bx, blind).to_affine();
let c = params.commit(engine, &cx, blind).to_affine();
Expand Down Expand Up @@ -297,7 +297,7 @@ mod test {

let prover = P::new(params);
prover
.create_proof(&mut OsRng, &mut transcript, queries)
.create_proof(&mut test_rng(), &mut transcript, queries)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% on this one, but I'd say its not part of testing either.

.unwrap();

transcript.finalize()
Expand Down
26 changes: 26 additions & 0 deletions halo2_debug/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "halo2_debug"
version = "0.3.0"
authors = [
"Privacy Scaling Explorations team",
]
edition = "2021"
rust-version = "1.66.0"
description = """
Halo2 Debug. This package contains utilities for debugging and testing within
the halo2 ecosystem.
"""
license = "MIT OR Apache-2.0"
repository = "https://github.com/privacy-scaling-explorations/halo2"
documentation = "https://privacy-scaling-explorations.github.io/halo2/"
categories = ["cryptography"]
keywords = ["halo", "proofs", "zkp", "zkSNARKs"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "katex-header.html"]

[dependencies]
tiny-keccak = { version = "2.0.2", features=["keccak"] }
hex = "0.4.3"
rand_core = "0.6.4"
Loading
Loading