Skip to content

Commit

Permalink
Upgrade halo2curves and halo2_proofs (#35)
Browse files Browse the repository at this point in the history
* feat: upgrade `halo2curves` to `0.3.2` and `halo2_proofs` to `v2023_04_20`

* feat: support `circuit-params` feature flag
  • Loading branch information
han0110 authored Apr 26, 2023
1 parent 54452c7 commit e5d5e4a
Show file tree
Hide file tree
Showing 29 changed files with 206 additions and 138 deletions.
15 changes: 10 additions & 5 deletions snark-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ num-integer = "0.1.45"
num-traits = "0.2.15"
rand = "0.8"
hex = "0.4"
halo2_curves = { git = "https://github.com/privacy-scaling-explorations/halo2curves", tag = "0.3.1", package = "halo2curves" }
halo2_curves = { git = "https://github.com/privacy-scaling-explorations/halo2curves", tag = "0.3.2", package = "halo2curves" }

# parallel
rayon = { version = "1.5.3", optional = true }

# system_halo2
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2", tag = "v2023_02_02", optional = true }
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2", tag = "v2023_04_20", optional = true }

# loader_evm
sha3 = { version = "0.10", optional = true }
Expand All @@ -27,15 +27,15 @@ rlp = { version = "0.5.2", default-features = false, features = ["std"], optiona
revm = { version = "= 2.3.1", optional = true }

# loader_halo2
halo2_wrong_ecc = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2023_02_02", package = "ecc", optional = true }
poseidon = { git = "https://github.com/privacy-scaling-explorations/poseidon", tag = "v2022_10_22", optional = true }
halo2_wrong_ecc = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2023_04_20", package = "ecc", optional = true }
poseidon = { git = "https://github.com/privacy-scaling-explorations/poseidon", tag = "v2023_04_20", optional = true }

[dev-dependencies]
rand_chacha = "0.3.1"
paste = "1.0.7"

# system_halo2
halo2_wrong_ecc = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2023_02_02", package = "ecc" }
halo2_wrong_ecc = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2023_04_20", package = "ecc" }

# loader_evm
crossterm = { version = "0.25" }
Expand All @@ -46,11 +46,16 @@ default = ["loader_evm", "loader_halo2", "system_halo2"]

parallel = ["dep:rayon"]

# loaders
loader_evm = ["dep:bytes", "dep:sha3", "dep:primitive-types", "dep:rlp", "dep:revm"]
loader_halo2 = ["dep:halo2_proofs", "dep:halo2_wrong_ecc", "dep:poseidon"]

# systems
system_halo2 = ["dep:halo2_proofs"]

# features of halo2
halo2_circuit_params = ["halo2_proofs?/circuit-params", "halo2_wrong_ecc?/circuit-params"]

[[example]]
name = "evm-verifier"
required-features = ["loader_evm", "system_halo2"]
Expand Down
14 changes: 9 additions & 5 deletions snark-verifier/examples/evm-verifier-with-accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type PlonkSuccinctVerifier = verifier::plonk::PlonkSuccinctVerifier<As, LimbsEnc
type PlonkVerifier = verifier::plonk::PlonkVerifier<As, LimbsEncoding<LIMBS, BITS>>;

mod application {
use halo2_curves::bn256::Fr;
use halo2_curves::{bn256::Fr, ff::Field};
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
plonk::{Advice, Circuit, Column, ConstraintSystem, Error, Fixed, Instance},
Expand Down Expand Up @@ -117,6 +117,8 @@ mod application {
impl Circuit<Fr> for StandardPlonk {
type Config = StandardPlonkConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "halo2_circuit_params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand All @@ -136,7 +138,7 @@ mod application {
|| "",
|mut region| {
region.assign_advice(|| "", config.a, 0, || Value::known(self.0))?;
region.assign_fixed(|| "", config.q_a, 0, || Value::known(-Fr::one()))?;
region.assign_fixed(|| "", config.q_a, 0, || Value::known(-Fr::ONE))?;

region.assign_advice(|| "", config.a, 1, || Value::known(-Fr::from(5)))?;
for (idx, column) in (1..).zip([
Expand All @@ -149,7 +151,7 @@ mod application {
region.assign_fixed(|| "", column, 1, || Value::known(Fr::from(idx)))?;
}

let a = region.assign_advice(|| "", config.a, 2, || Value::known(Fr::one()))?;
let a = region.assign_advice(|| "", config.a, 2, || Value::known(Fr::ONE))?;
a.copy_advice(|| "", &mut region, config.b, 3)?;
a.copy_advice(|| "", &mut region, config.c, 4)?;

Expand Down Expand Up @@ -185,7 +187,7 @@ mod aggregation {
AccumulationScheme, AccumulationSchemeProver,
},
system,
util::arithmetic::{fe_to_limbs, FieldExt},
util::arithmetic::{fe_to_limbs, PrimeField},
verifier::{plonk::PlonkProtocol, SnarkVerifier},
};
use std::rc::Rc;
Expand Down Expand Up @@ -309,7 +311,7 @@ mod aggregation {
}

impl AggregationConfig {
pub fn configure<F: FieldExt>(
pub fn configure<F: PrimeField>(
meta: &mut ConstraintSystem<F>,
composition_bits: Vec<usize>,
overflow_bits: Vec<usize>,
Expand Down Expand Up @@ -410,6 +412,8 @@ mod aggregation {
impl Circuit<Fr> for AggregationCircuit {
type Config = AggregationConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "halo2_circuit_params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self {
Expand Down
11 changes: 8 additions & 3 deletions snark-verifier/examples/evm-verifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use halo2_curves::bn256::{Bn256, Fq, Fr, G1Affine};
use halo2_curves::{
bn256::{Bn256, Fq, Fr, G1Affine},
ff::Field,
};
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
dev::MockProver,
Expand Down Expand Up @@ -103,6 +106,8 @@ impl StandardPlonk {
impl Circuit<Fr> for StandardPlonk {
type Config = StandardPlonkConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "halo2_circuit_params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand All @@ -122,7 +127,7 @@ impl Circuit<Fr> for StandardPlonk {
|| "",
|mut region| {
region.assign_advice(|| "", config.a, 0, || Value::known(self.0))?;
region.assign_fixed(|| "", config.q_a, 0, || Value::known(-Fr::one()))?;
region.assign_fixed(|| "", config.q_a, 0, || Value::known(-Fr::ONE))?;

region.assign_advice(|| "", config.a, 1, || Value::known(-Fr::from(5)))?;
for (idx, column) in (1..).zip([
Expand All @@ -135,7 +140,7 @@ impl Circuit<Fr> for StandardPlonk {
region.assign_fixed(|| "", column, 1, || Value::known(Fr::from(idx)))?;
}

let a = region.assign_advice(|| "", config.a, 2, || Value::known(Fr::one()))?;
let a = region.assign_advice(|| "", config.a, 2, || Value::known(Fr::ONE))?;
a.copy_advice(|| "", &mut region, config.b, 3)?;
a.copy_advice(|| "", &mut region, config.c, 4)?;

Expand Down
24 changes: 12 additions & 12 deletions snark-verifier/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ pub trait ScalarLoader<F: PrimeField> {

/// Load `zero` as constant.
fn load_zero(&self) -> Self::LoadedScalar {
self.load_const(&F::zero())
self.load_const(&F::ZERO)
}

/// Load `one` as constant.
fn load_one(&self) -> Self::LoadedScalar {
self.load_const(&F::one())
self.load_const(&F::ONE)
}

/// Assert lhs and rhs field elements are equal.
Expand All @@ -150,13 +150,13 @@ pub trait ScalarLoader<F: PrimeField> {

let loader = values.first().unwrap().1.loader();
iter::empty()
.chain(if constant == F::zero() {
.chain(if constant == F::ZERO {
None
} else {
Some(Cow::Owned(loader.load_const(&constant)))
})
.chain(values.iter().map(|&(coeff, value)| {
if coeff == F::one() {
if coeff == F::ONE {
Cow::Borrowed(value)
} else {
Cow::Owned(loader.load_const(&coeff) * value)
Expand All @@ -179,13 +179,13 @@ pub trait ScalarLoader<F: PrimeField> {

let loader = values.first().unwrap().1.loader();
iter::empty()
.chain(if constant == F::zero() {
.chain(if constant == F::ZERO {
None
} else {
Some(loader.load_const(&constant))
})
.chain(values.iter().map(|&(coeff, lhs, rhs)| {
if coeff == F::one() {
if coeff == F::ONE {
lhs.clone() * rhs
} else {
loader.load_const(&coeff) * lhs * rhs
Expand All @@ -197,28 +197,28 @@ pub trait ScalarLoader<F: PrimeField> {

/// Sum field elements with coefficients.
fn sum_with_coeff(&self, values: &[(F, &Self::LoadedScalar)]) -> Self::LoadedScalar {
self.sum_with_coeff_and_const(values, F::zero())
self.sum_with_coeff_and_const(values, F::ZERO)
}

/// Sum field elements and constant.
fn sum_with_const(&self, values: &[&Self::LoadedScalar], constant: F) -> Self::LoadedScalar {
self.sum_with_coeff_and_const(
&values.iter().map(|&value| (F::one(), value)).collect_vec(),
&values.iter().map(|&value| (F::ONE, value)).collect_vec(),
constant,
)
}

/// Sum field elements.
fn sum(&self, values: &[&Self::LoadedScalar]) -> Self::LoadedScalar {
self.sum_with_const(values, F::zero())
self.sum_with_const(values, F::ZERO)
}

/// Sum product of field elements with coefficients.
fn sum_products_with_coeff(
&self,
values: &[(F, &Self::LoadedScalar, &Self::LoadedScalar)],
) -> Self::LoadedScalar {
self.sum_products_with_coeff_and_const(values, F::zero())
self.sum_products_with_coeff_and_const(values, F::ZERO)
}

/// Sum product of field elements and constant.
Expand All @@ -230,7 +230,7 @@ pub trait ScalarLoader<F: PrimeField> {
self.sum_products_with_coeff_and_const(
&values
.iter()
.map(|&(lhs, rhs)| (F::one(), lhs, rhs))
.map(|&(lhs, rhs)| (F::ONE, lhs, rhs))
.collect_vec(),
constant,
)
Expand All @@ -241,7 +241,7 @@ pub trait ScalarLoader<F: PrimeField> {
&self,
values: &[(&Self::LoadedScalar, &Self::LoadedScalar)],
) -> Self::LoadedScalar {
self.sum_products_with_const(values, F::zero())
self.sum_products_with_const(values, F::ZERO)
}

/// Product of field elements.
Expand Down
12 changes: 6 additions & 6 deletions snark-verifier/src/loader/evm/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,8 @@ impl<F: PrimeField<Repr = [u8; 0x20]>> ScalarLoader<F> for Rc<EvmLoader> {
}

let push_addend = |(coeff, value): &(F, &Scalar)| {
assert_ne!(*coeff, F::zero());
match (*coeff == F::one(), &value.value) {
assert_ne!(*coeff, F::ZERO);
match (*coeff == F::ONE, &value.value) {
(true, _) => self.push(value),
(false, Value::Constant(value)) => self.push(&self.scalar(Value::Constant(
fe_to_u256(*coeff * u256_to_fe::<F>(*value)),
Expand All @@ -736,7 +736,7 @@ impl<F: PrimeField<Repr = [u8; 0x20]>> ScalarLoader<F> for Rc<EvmLoader> {
};

let mut values = values.iter();
let initial_value = if constant == F::zero() {
let initial_value = if constant == F::ZERO {
push_addend(values.next().unwrap())
} else {
self.push(&self.scalar(Value::Constant(fe_to_u256(constant))))
Expand Down Expand Up @@ -770,8 +770,8 @@ impl<F: PrimeField<Repr = [u8; 0x20]>> ScalarLoader<F> for Rc<EvmLoader> {
}

let push_addend = |(coeff, lhs, rhs): &(F, &Scalar, &Scalar)| {
assert_ne!(*coeff, F::zero());
match (*coeff == F::one(), &lhs.value, &rhs.value) {
assert_ne!(*coeff, F::ZERO);
match (*coeff == F::ONE, &lhs.value, &rhs.value) {
(_, Value::Constant(lhs), Value::Constant(rhs)) => {
self.push(&self.scalar(Value::Constant(fe_to_u256(
*coeff * u256_to_fe::<F>(*lhs) * u256_to_fe::<F>(*rhs),
Expand Down Expand Up @@ -800,7 +800,7 @@ impl<F: PrimeField<Repr = [u8; 0x20]>> ScalarLoader<F> for Rc<EvmLoader> {
};

let mut values = values.iter();
let initial_value = if constant == F::zero() {
let initial_value = if constant == F::ZERO {
push_addend(values.next().unwrap())
} else {
self.push(&self.scalar(Value::Constant(fe_to_u256(constant))))
Expand Down
2 changes: 1 addition & 1 deletion snark-verifier/src/loader/evm/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn modulus<F>() -> U256
where
F: PrimeField<Repr = [u8; 32]>,
{
U256::from_little_endian((-F::one()).to_repr().as_ref()) + 1
U256::from_little_endian((-F::ONE).to_repr().as_ref()) + 1
}

/// Encode instances and proof into calldata.
Expand Down
18 changes: 9 additions & 9 deletions snark-verifier/src/loader/halo2/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<'a, C: CurveAffine, EccChip: EccInstructions<'a, C>> Halo2Loader<'a, C, Ecc
.scalar_chip()
.sum_with_coeff_and_const(
&mut self.ctx_mut(),
&[(C::Scalar::one(), assigned)],
&[(C::Scalar::ONE, assigned)],
*constant,
)
.map(Value::Assigned)
Expand All @@ -168,8 +168,8 @@ impl<'a, C: CurveAffine, EccChip: EccInstructions<'a, C>> Halo2Loader<'a, C, Ecc
.scalar_chip()
.sum_with_coeff_and_const(
&mut self.ctx_mut(),
&[(C::Scalar::one(), lhs), (C::Scalar::one(), rhs)],
C::Scalar::zero(),
&[(C::Scalar::ONE, lhs), (C::Scalar::ONE, rhs)],
C::Scalar::ZERO,
)
.map(Value::Assigned)
.unwrap(),
Expand All @@ -188,7 +188,7 @@ impl<'a, C: CurveAffine, EccChip: EccInstructions<'a, C>> Halo2Loader<'a, C, Ecc
.scalar_chip()
.sum_with_coeff_and_const(
&mut self.ctx_mut(),
&[(-C::Scalar::one(), assigned)],
&[(-C::Scalar::ONE, assigned)],
*constant,
)
.map(Value::Assigned)
Expand All @@ -197,7 +197,7 @@ impl<'a, C: CurveAffine, EccChip: EccInstructions<'a, C>> Halo2Loader<'a, C, Ecc
.scalar_chip()
.sum_with_coeff_and_const(
&mut self.ctx_mut(),
&[(C::Scalar::one(), assigned)],
&[(C::Scalar::ONE, assigned)],
-*constant,
)
.map(Value::Assigned)
Expand All @@ -224,16 +224,16 @@ impl<'a, C: CurveAffine, EccChip: EccInstructions<'a, C>> Halo2Loader<'a, C, Ecc
.sum_with_coeff_and_const(
&mut self.ctx_mut(),
&[(*constant, assigned)],
C::Scalar::zero(),
C::Scalar::ZERO,
)
.map(Value::Assigned)
.unwrap(),
(Value::Assigned(lhs), Value::Assigned(rhs)) => self
.scalar_chip()
.sum_products_with_coeff_and_const(
&mut self.ctx_mut(),
&[(C::Scalar::one(), lhs, rhs)],
C::Scalar::zero(),
&[(C::Scalar::ONE, lhs, rhs)],
C::Scalar::ZERO,
)
.map(Value::Assigned)
.unwrap(),
Expand Down Expand Up @@ -651,7 +651,7 @@ impl<'a, C: CurveAffine, EccChip: EccInstructions<'a, C>> EcPointLoader<C>
fixed_base.push((scalar, *base))
}
(Value::Constant(scalar), Value::Assigned(_))
if scalar.eq(&C::Scalar::one()) =>
if scalar.eq(&C::Scalar::ONE) =>
{
variable_base_non_scaled.push(base);
}
Expand Down
Loading

0 comments on commit e5d5e4a

Please sign in to comment.