From f0626be1dda2aa4b4604aea539a3898018f4f2a3 Mon Sep 17 00:00:00 2001 From: Han Date: Thu, 8 Feb 2024 15:04:57 +0800 Subject: [PATCH] feat: upgrade `halo2_proofs` version (#14) --- Cargo.toml | 7 ++---- src/codegen.rs | 18 +------------ src/test.rs | 68 ++++++++++++++++++++++---------------------------- 3 files changed, 33 insertions(+), 60 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index affc3f5..e8d05c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,23 +4,20 @@ version = "0.1.0" edition = "2021" [dependencies] -halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2", tag = "v2023_04_20" } +halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2", tag = "v0.3.0" } askama = { version = "0.12.0", features = ["config"], default-features = false } hex = "0.4.3" ruint = "1" sha3 = "0.10" itertools = "0.11.0" -# Remove when `vk.transcript_repr()` is ready for usage. -blake2b_simd = "1" - # For feature = "evm" revm = { version = "3.3.0", default-features = false, optional = true } [dev-dependencies] rand = "0.8.5" revm = { version = "3.3.0", default-features = false } -halo2_maingate = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2023_04_20", package = "maingate" } +halo2_maingate = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2024_01_31", package = "maingate" } [features] default = [] diff --git a/src/codegen.rs b/src/codegen.rs index 2554c4f..7c09ace 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -157,7 +157,7 @@ impl<'a> SolidityGenerator<'a> { fn generate_vk(&self) -> Halo2VerifyingKey { let constants = { let domain = self.vk.get_domain(); - let vk_digest = fr_to_u256(vk_transcript_repr(self.vk)); + let vk_digest = fr_to_u256(self.vk.transcript_repr()); let num_instances = U256::from(self.num_instances); let k = U256::from(domain.k()); let n_inv = fr_to_u256(bn256::Fr::from(1 << domain.k()).invert().unwrap()); @@ -298,19 +298,3 @@ impl<'a> SolidityGenerator<'a> { * 0x20 } } - -// Remove when `vk.transcript_repr()` is ready for usage. -fn vk_transcript_repr(vk: &VerifyingKey) -> bn256::Fr { - use blake2b_simd::Params; - use halo2_proofs::halo2curves::ff::FromUniformBytes; - - let fmtted_pinned_vk = format!("{:?}", vk.pinned()); - let mut hasher = Params::new() - .hash_length(64) - .personal(b"Halo2-Verify-Key") - .to_state(); - hasher - .update(&(fmtted_pinned_vk.len() as u64).to_le_bytes()) - .update(fmtted_pinned_vk.as_bytes()); - FromUniformBytes::from_uniform_bytes(hasher.finalize().as_array()) -} diff --git a/src/test.rs b/src/test.rs index 7e54ab0..c5c5da2 100644 --- a/src/test.rs +++ b/src/test.rs @@ -253,17 +253,18 @@ mod halo2 { fn random_accumulator_limbs( acc_encoding: AccumulatorEncoding, mut rng: impl RngCore, - ) -> Vec + ) -> Vec where M: MultiMillerLoop, + M::G1Affine: CurveAffine, ::Base: PrimeField, - M::Scalar: PrimeField, + ::ScalarExt: PrimeField, { - let s = M::Scalar::random(&mut rng); + let s = M::Fr::random(&mut rng); let g1 = M::G1Affine::generator(); let g2 = M::G2Affine::generator(); let neg_s_g2 = (g2 * -s).to_affine(); - let lhs_scalar = M::Scalar::random(&mut rng); + let lhs_scalar = M::Fr::random(&mut rng); let rhs_scalar = lhs_scalar * s.invert().unwrap(); let [lhs, rhs] = [lhs_scalar, rhs_scalar].map(|scalar| (g1 * scalar).to_affine()); @@ -337,13 +338,14 @@ mod halo2 { use std::{array, fmt::Debug, iter, mem}; #[derive(Clone, Debug, Default)] - pub struct HugeCircuit(Vec); + pub struct HugeCircuit(Vec); - impl TestCircuit for HugeCircuit + impl TestCircuit for HugeCircuit where M: MultiMillerLoop, + M::G1Affine: CurveAffine, ::Base: PrimeField, - M::Scalar: PrimeField, + ::ScalarExt: PrimeField, { fn min_k() -> u32 { 6 @@ -353,22 +355,19 @@ mod halo2 { let instances = if let Some(acc_encoding) = acc_encoding { random_accumulator_limbs::(acc_encoding, rng) } else { - iter::repeat_with(|| M::Scalar::random(&mut rng)) + iter::repeat_with(|| M::Fr::random(&mut rng)) .take(10) .collect() }; Self(instances) } - fn instances(&self) -> Vec { + fn instances(&self) -> Vec { self.0.clone() } } - impl Circuit for HugeCircuit - where - M::Scalar: PrimeField, - { + impl Circuit for HugeCircuit { type Config = ( [Selector; 10], [Selector; 10], @@ -384,7 +383,7 @@ mod halo2 { unimplemented!() } - fn configure(meta: &mut ConstraintSystem) -> Self::Config { + fn configure(meta: &mut ConstraintSystem) -> Self::Config { let selectors = [(); 10].map(|_| meta.selector()); let complex_selectors = [(); 10].map(|_| meta.complex_selector()); let fixeds = [(); 10].map(|_| meta.fixed_column()); @@ -411,7 +410,7 @@ mod halo2 { meta.create_gate("", |meta| { let selectors = selectors.map(|selector| meta.query_selector(selector)); - let advices: [Expression; 10] = array::from_fn(|idx| { + let advices: [Expression; 10] = array::from_fn(|idx| { let rotation = Rotation((idx as i32 - advices.len() as i32) / 2); meta.query_advice(advices[idx], rotation) }); @@ -457,7 +456,7 @@ mod halo2 { fn synthesize( &self, (selectors, complex_selectors, fixeds, advices, instance): Self::Config, - mut layouter: impl Layouter, + mut layouter: impl Layouter, ) -> Result<(), plonk::Error> { let assigneds = layouter.assign_region( || "", @@ -472,7 +471,7 @@ mod halo2 { q.enable(&mut region, next_offset())?; } for (idx, column) in izip!(1.., fixeds) { - let value = Value::known(M::Scalar::from(idx)); + let value = Value::known(M::Fr::from(idx)); region.assign_fixed(|| "", column, next_offset(), || value)?; } izip!(advices, &self.0) @@ -549,14 +548,15 @@ mod halo2 { #[derive(Clone, Default)] pub struct MainGateWithRange { - instances: Vec, + instances: Vec, } - impl TestCircuit for MainGateWithRange + impl TestCircuit for MainGateWithRange where M: MultiMillerLoop, + M::G1Affine: CurveAffine, ::Base: PrimeField, - M::Scalar: PrimeField, + ::ScalarExt: PrimeField, { fn min_k() -> u32 { 9 @@ -566,22 +566,19 @@ mod halo2 { let instances = if let Some(acc_encoding) = acc_encoding { random_accumulator_limbs::(acc_encoding, rng) } else { - iter::repeat_with(|| M::Scalar::random(&mut rng)) + iter::repeat_with(|| M::Fr::random(&mut rng)) .take(10) .collect() }; Self { instances } } - fn instances(&self) -> Vec { + fn instances(&self) -> Vec { self.instances.clone() } } - impl Circuit for MainGateWithRange - where - M::Scalar: PrimeField, - { + impl Circuit for MainGateWithRange { type Config = MainGateWithRangeConfig; type FloorPlanner = SimpleFloorPlanner; #[cfg(feature = "halo2_circuit_params")] @@ -591,14 +588,14 @@ mod halo2 { unimplemented!() } - fn configure(meta: &mut ConstraintSystem) -> Self::Config { + fn configure(meta: &mut ConstraintSystem) -> Self::Config { MainGateWithRangeConfig::configure(meta, vec![8], vec![4, 7]) } fn synthesize( &self, config: Self::Config, - mut layouter: impl Layouter, + mut layouter: impl Layouter, ) -> Result<(), Error> { let main_gate = config.main_gate(); let range_chip = config.range_chip(); @@ -618,25 +615,20 @@ mod halo2 { // Dummy gates to make all fixed column with values range_chip.decompose( &mut ctx, - Value::known(M::Scalar::from(u64::MAX)), + Value::known(M::Fr::from(u64::MAX)), 8, 64, )?; range_chip.decompose( &mut ctx, - Value::known(M::Scalar::from(u32::MAX as u64)), + Value::known(M::Fr::from(u32::MAX as u64)), 8, 39, )?; let a = &advices[0]; - let b = main_gate.sub_sub_with_constant( - &mut ctx, - a, - a, - a, - M::Scalar::from(2), - )?; - let cond = main_gate.assign_bit(&mut ctx, Value::known(M::Scalar::ONE))?; + let b = + main_gate.sub_sub_with_constant(&mut ctx, a, a, a, M::Fr::from(2))?; + let cond = main_gate.assign_bit(&mut ctx, Value::known(M::Fr::ONE))?; main_gate.select(&mut ctx, a, &b, &cond)?; Ok(advices)