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

[Design Discussion] Zk Accel API via extra function parameter [Don't merge] #14

Draft
wants to merge 3 commits into
base: taiko/unstable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ backtrace = { version = "0.3", optional = true }
rayon = "1.5.1"
ff = "0.13"
group = "0.13"
halo2curves = { git = 'https://github.com/privacy-scaling-explorations/halo2curves', tag = "0.3.2" }
halo2curves = { git = 'https://github.com/taikoxyz/halo2curves', branch = "zal-on-0.3.2" }
rand_core = { version = "0.6", default-features = false }
tracing = "0.1"
blake2b_simd = "1"
Expand Down
7 changes: 4 additions & 3 deletions halo2_proofs/benches/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[macro_use]
extern crate criterion;

use crate::arithmetic::small_multiexp;
use crate::halo2curves::pasta::{EqAffine, Fp};
use group::ff::Field;
use halo2_proofs::*;
use halo2curves::pasta::{EqAffine, Fp};
use halo2curves::zal::{H2cEngine, MsmAccel};

use halo2_proofs::poly::{commitment::ParamsProver, ipa::commitment::ParamsIPA};

Expand All @@ -16,6 +16,7 @@ fn criterion_benchmark(c: &mut Criterion) {

// small multiexp
{
let engine = H2cEngine::new();
let params: ParamsIPA<EqAffine> = ParamsIPA::new(5);
let g = &mut params.get_g().to_vec();
let len = g.len() / 2;
Expand All @@ -27,7 +28,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]);
engine.msm(&[black_box(coeff_1), black_box(coeff_2)], &[*g_lo, *g_hi]);
}
})
});
Expand Down
11 changes: 8 additions & 3 deletions halo2_proofs/examples/cost-model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use std::{
use ff::Field;
use group::{Curve, Group};
use gumdrop::Options;
use halo2_proofs::arithmetic::best_multiexp;
use halo2curves::pasta::pallas;
use halo2curves::{
pasta::pallas,
zal::{H2cEngine, MsmAccel},
};

struct Estimator {
/// Scalars for estimating multiexp performance.
Expand Down Expand Up @@ -41,7 +43,10 @@ impl Estimator {

fn multiexp(&self, size: usize) -> Duration {
let start = Instant::now();
best_multiexp(&self.multiexp_scalars[..size], &self.multiexp_bases[..size]);
// TODO: at the moment we use the default MSM for estimating cost.
// is it beneficial to use the real engine?
let engine = H2cEngine::new();
engine.msm(&self.multiexp_scalars[..size], &self.multiexp_bases[..size]);
Instant::now().duration_since(start)
}
}
Expand Down
3 changes: 3 additions & 0 deletions halo2_proofs/examples/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use halo2_proofs::{
SerdeFormat,
};
use halo2curves::bn256::{Bn256, Fr, G1Affine};
use halo2curves::zal::H2cEngine;
use rand_core::OsRng;

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -129,6 +130,7 @@ impl Circuit<Fr> for StandardPlonk {
}

fn main() {
let engine = H2cEngine::new();
let k = 4;
let circuit = StandardPlonk(Fr::random(OsRng));
let params = ParamsKZG::<Bn256>::setup(k, OsRng);
Expand Down Expand Up @@ -163,6 +165,7 @@ fn main() {
Blake2bWrite<Vec<u8>, G1Affine, Challenge255<_>>,
_,
>(
&engine,
&params,
&pk,
&[circuit],
Expand Down
8 changes: 6 additions & 2 deletions halo2_proofs/examples/shuffle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use halo2_proofs::{
Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer,
},
};
use halo2curves::zal::{H2cEngine, MsmAccel};
use rand_core::{OsRng, RngCore};
use std::iter;

Expand Down Expand Up @@ -275,6 +276,7 @@ fn test_mock_prover<F: Ord + FromUniformBytes<64>, const W: usize, const H: usiz
}

fn test_prover<C: CurveAffine, const W: usize, const H: usize>(
engine: &dyn MsmAccel<C>,
k: u32,
circuit: MyCircuit<C::Scalar, W, H>,
expected: bool,
Expand All @@ -289,6 +291,7 @@ fn test_prover<C: CurveAffine, const W: usize, const H: usize>(
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);

create_proof::<IPACommitmentScheme<C>, ProverIPA<C>, _, _, _, _>(
engine,
&params,
&pk,
&[circuit],
Expand Down Expand Up @@ -324,11 +327,12 @@ fn main() {
const H: usize = 32;
const K: u32 = 8;

let engine = H2cEngine::new();
let circuit = &MyCircuit::<_, W, H>::rand(&mut OsRng);

{
test_mock_prover(K, circuit.clone(), Ok(()));
test_prover::<EqAffine, W, H>(K, circuit.clone(), true);
test_prover::<EqAffine, W, H>(&engine, K, circuit.clone(), true);
}

#[cfg(not(feature = "sanity-checks"))]
Expand All @@ -352,6 +356,6 @@ fn main() {
},
)]),
);
test_prover::<EqAffine, W, H>(K, circuit, false);
test_prover::<EqAffine, W, H>(&engine, K, circuit, false);
}
}
14 changes: 14 additions & 0 deletions halo2_proofs/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ where
/// TEMP
pub static mut MULTIEXP_TOTAL_TIME: usize = 0;

#[deprecated(
since = "0.3.2",
note = "please use ZAL api engine instead,\nsee: https://github.com/privacy-scaling-explorations/halo2/issues/216"
)]
fn multiexp_serial<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C], acc: &mut C::Curve) {
let coeffs: Vec<_> = coeffs.iter().map(|a| a.to_repr()).collect();

Expand Down Expand Up @@ -130,6 +134,10 @@ fn multiexp_serial<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C], acc: &mut

/// Performs a small multi-exponentiation operation.
/// Uses the double-and-add algorithm with doublings shared across points.
#[deprecated(
since = "0.3.2",
note = "please use ZAL api engine instead,\nsee: https://github.com/privacy-scaling-explorations/halo2/issues/216"
)]
pub fn small_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C::Curve {
let coeffs: Vec<_> = coeffs.iter().map(|a| a.to_repr()).collect();
let mut acc = C::Curve::identity();
Expand Down Expand Up @@ -157,6 +165,10 @@ pub fn small_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C::C
/// This function will panic if coeffs and bases have a different length.
///
/// This will use multithreading if beneficial.
#[deprecated(
since = "0.3.2",
note = "please use ZAL api engine instead,\nsee: https://github.com/privacy-scaling-explorations/halo2/issues/216"
)]
pub fn best_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C::Curve {
assert_eq!(coeffs.len(), bases.len());

Expand All @@ -177,13 +189,15 @@ pub fn best_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C::Cu
.zip(results.iter_mut())
{
scope.spawn(move |_| {
#[allow(deprecated)]
multiexp_serial(coeffs, bases, acc);
});
}
});
results.iter().fold(C::Curve::identity(), |a, b| a + b)
} else {
let mut acc = C::Curve::identity();
#[allow(deprecated)]
multiexp_serial(coeffs, bases, &mut acc);
acc
};
Expand Down
16 changes: 12 additions & 4 deletions halo2_proofs/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::ops::Range;

use ff::{Field, FromUniformBytes};
use group::Curve;
use halo2curves::zal::H2cEngine;

use super::{
circuit::{
Expand Down Expand Up @@ -214,6 +215,8 @@ where
ConcreteCircuit: Circuit<C::Scalar>,
C::Scalar: FromUniformBytes<64>,
{
// ZAL: Verification is (supposedly) cheap, hence we don't use an accelerator engine
let default_engine = H2cEngine::new();
let (domain, cs, config) = create_domain::<C, ConcreteCircuit>(
params.k(),
#[cfg(feature = "circuit-params")]
Expand Down Expand Up @@ -249,13 +252,18 @@ where
.map(|poly| domain.lagrange_from_vec(poly)),
);

let permutation_vk = assembly
.permutation
.build_vk(params, &domain, &cs.permutation);
let permutation_vk =
assembly
.permutation
.build_vk(&default_engine, params, &domain, &cs.permutation);

let fixed_commitments = fixed
.iter()
.map(|poly| params.commit_lagrange(poly, Blind::default()).to_affine())
.map(|poly| {
params
.commit_lagrange(&default_engine, poly, Blind::default())
.to_affine()
})
.collect();

Ok(VerifyingKey::from_parts(
Expand Down
9 changes: 7 additions & 2 deletions halo2_proofs/src/plonk/lookup/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use group::{
ff::{BatchInvert, Field},
Curve,
};
use halo2curves::zal::MsmAccel;
use rand_core::RngCore;
use std::{any::TypeId, convert::TryInto, num::ParseIntError, ops::Index};
use std::{
Expand Down Expand Up @@ -72,6 +73,7 @@ impl<F: WithSmallOrderMulGroup<3>> Argument<F> {
T: TranscriptWrite<C, E>,
>(
&self,
engine: &dyn MsmAccel<C>,
pk: &ProvingKey<C>,
params: &P,
domain: &EvaluationDomain<C::Scalar>,
Expand Down Expand Up @@ -128,7 +130,7 @@ impl<F: WithSmallOrderMulGroup<3>> Argument<F> {
let mut commit_values = |values: &Polynomial<C::Scalar, LagrangeCoeff>| {
let poly = pk.vk.domain.lagrange_to_coeff(values.clone());
let blind = Blind(C::Scalar::random(&mut rng));
let commitment = params.commit_lagrange(values, blind).to_affine();
let commitment = params.commit_lagrange(engine, values, blind).to_affine();
(poly, blind, commitment)
};

Expand Down Expand Up @@ -173,6 +175,7 @@ impl<C: CurveAffine> Permuted<C> {
T: TranscriptWrite<C, E>,
>(
self,
engine: &dyn MsmAccel<C>,
pk: &ProvingKey<C>,
params: &P,
beta: ChallengeBeta<C>,
Expand Down Expand Up @@ -289,7 +292,9 @@ impl<C: CurveAffine> Permuted<C> {
}

let product_blind = Blind(C::Scalar::random(rng));
let product_commitment = params.commit_lagrange(&z, product_blind).to_affine();
let product_commitment = params
.commit_lagrange(engine, &z, product_blind)
.to_affine();
let z = pk.vk.domain.lagrange_to_coeff(z);

// Hash product commitment
Expand Down
4 changes: 3 additions & 1 deletion halo2_proofs/src/plonk/permutation/keygen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ff::{Field, PrimeField};
use group::Curve;
use halo2curves::zal::MsmAccel;

use super::{Argument, ProvingKey, VerifyingKey};
use crate::{
Expand Down Expand Up @@ -104,6 +105,7 @@ impl Assembly {

pub(crate) fn build_vk<'params, C: CurveAffine, P: Params<'params, C>>(
self,
engine: &dyn MsmAccel<C>,
params: &P,
domain: &EvaluationDomain<C::Scalar>,
p: &Argument,
Expand Down Expand Up @@ -156,7 +158,7 @@ impl Assembly {
// Compute commitment to permutation polynomial
commitments.push(
params
.commit_lagrange(permutation, Blind::default())
.commit_lagrange(engine, permutation, Blind::default())
.to_affine(),
);
}
Expand Down
5 changes: 4 additions & 1 deletion halo2_proofs/src/plonk/permutation/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use group::{
ff::{BatchInvert, Field},
Curve,
};
use halo2curves::zal::MsmAccel;
use rand_core::RngCore;
use std::iter::{self, ExactSizeIterator};

Expand Down Expand Up @@ -51,6 +52,7 @@ impl Argument {
T: TranscriptWrite<C, E>,
>(
&self,
engine: &dyn MsmAccel<C>,
params: &P,
pk: &plonk::ProvingKey<C>,
pkey: &ProvingKey<C>,
Expand Down Expand Up @@ -167,7 +169,8 @@ impl Argument {

let blind = Blind(C::Scalar::random(&mut rng));

let permutation_product_commitment_projective = params.commit_lagrange(&z, blind);
let permutation_product_commitment_projective =
params.commit_lagrange(engine, &z, blind);
let permutation_product_blind = blind;
let z = domain.lagrange_to_coeff(z);
let permutation_product_poly = z.clone();
Expand Down
Loading
Loading