Skip to content

Commit

Permalink
General refactor for further integration (#13)
Browse files Browse the repository at this point in the history
* feat: remove dev-dependency `foundry` and vendor necessary part of it

* refactor: simplify traits and remove unused stuff

* refactor: much less clone

* feat: generalized `AccumulatorEncoding` for `EccInstructions`
  • Loading branch information
han0110 authored Nov 8, 2022
1 parent 2cd8b9d commit 25dbaf5
Show file tree
Hide file tree
Showing 29 changed files with 1,488 additions and 550 deletions.
15 changes: 9 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ num-bigint = "0.4.3"
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.0", package = "halo2curves" }

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

# loader_evm
ethereum_types = { package = "ethereum-types", version = "0.13.1", default-features = false, features = ["std"], optional = true }
sha3 = { version = "0.10.1", optional = true }
ethereum_types = { package = "ethereum-types", version = "0.13", default-features = false, features = ["std"], optional = true }
sha3 = { version = "0.10", optional = true }
revm = { version = "2.1.0", optional = true }
bytes = { version = "1.2", optional = true }
rlp = { version = "0.5", default-features = false, features = ["std"], optional = true }

# loader_halo2
halo2_wrong_ecc = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2022_10_22", package = "ecc", optional = true }
Expand All @@ -31,14 +35,13 @@ paste = "1.0.7"
halo2_wrong_ecc = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2022_10_22", package = "ecc" }

# loader_evm
foundry_evm = { git = "https://github.com/foundry-rs/foundry", package = "foundry-evm", rev = "6b1ee60e" }
crossterm = { version = "0.22.1" }
tui = { version = "0.16.0", default-features = false, features = ["crossterm"] }
crossterm = { version = "0.25" }
tui = { version = "0.19", default-features = false, features = ["crossterm"] }

[features]
default = ["loader_evm", "loader_halo2", "system_halo2"]

loader_evm = ["dep:ethereum_types", "dep:sha3"]
loader_evm = ["dep:ethereum_types", "dep:sha3", "dep:revm", "dep:bytes", "dep:rlp"]
loader_halo2 = ["dep:halo2_proofs", "dep:halo2_wrong_ecc", "dep:poseidon"]

system_halo2 = ["dep:halo2_proofs"]
Expand Down
48 changes: 25 additions & 23 deletions examples/evm-verifier-with-accumulator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ethereum_types::Address;
use foundry_evm::executor::{fork::MultiFork, Backend, ExecutorBuilder};
use halo2_curves::bn256::{Bn256, Fq, Fr, G1Affine};
use halo2_proofs::{
dev::MockProver,
Expand All @@ -18,7 +17,7 @@ use halo2_proofs::{
use itertools::Itertools;
use plonk_verifier::{
loader::{
evm::{encode_calldata, EvmLoader},
evm::{encode_calldata, EvmLoader, ExecutorBuilder},
native::NativeLoader,
},
pcs::kzg::{Gwc19, Kzg, KzgAs, LimbsEncoding},
Expand Down Expand Up @@ -167,7 +166,7 @@ mod aggregation {
use halo2_curves::bn256::{Bn256, Fq, Fr, G1Affine};
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
plonk::{self, Circuit, ConstraintSystem},
plonk::{self, Circuit, ConstraintSystem, Error},
poly::{commitment::ParamsProver, kzg::commitment::ParamsKZG},
};
use halo2_wrong_ecc::{
Expand All @@ -182,7 +181,7 @@ mod aggregation {
use plonk_verifier::{
loader::{self, native::NativeLoader},
pcs::{
kzg::{KzgAccumulator, KzgSuccinctVerifyingKey},
kzg::{KzgAccumulator, KzgSuccinctVerifyingKey, LimbsEncodingInstructions},
AccumulationScheme, AccumulationSchemeProver,
},
system,
Expand All @@ -191,7 +190,7 @@ mod aggregation {
Protocol,
};
use rand::rngs::OsRng;
use std::{iter, rc::Rc};
use std::rc::Rc;

const T: usize = 5;
const RATE: usize = 4;
Expand Down Expand Up @@ -434,28 +433,33 @@ mod aggregation {

range_chip.load_table(&mut layouter)?;

let (lhs, rhs) = layouter.assign_region(
let accumulator_limbs = layouter.assign_region(
|| "",
|region| {
let ctx = RegionCtx::new(region, 0);

let ecc_chip = config.ecc_chip();
let loader = Halo2Loader::new(ecc_chip, ctx);
let KzgAccumulator { lhs, rhs } =
aggregate(&self.svk, &loader, &self.snarks, self.as_proof());
let accumulator = aggregate(&self.svk, &loader, &self.snarks, self.as_proof());

Ok((lhs.assigned(), rhs.assigned()))
let accumulator_limbs = [accumulator.lhs, accumulator.rhs]
.iter()
.map(|ec_point| {
loader.ecc_chip().assign_ec_point_to_limbs(
&mut loader.ctx_mut(),
ec_point.assigned(),
)
})
.collect::<Result<Vec<_>, Error>>()?
.into_iter()
.flatten();

Ok(accumulator_limbs)
},
)?;

for (limb, row) in iter::empty()
.chain(lhs.x().limbs())
.chain(lhs.y().limbs())
.chain(rhs.x().limbs())
.chain(rhs.y().limbs())
.zip(0..)
{
main_gate.expose_public(layouter.namespace(|| ""), limb.into(), row)?;
for (row, limb) in accumulator_limbs.enumerate() {
main_gate.expose_public(layouter.namespace(|| ""), limb, row)?;
}

Ok(())
Expand Down Expand Up @@ -574,16 +578,14 @@ fn evm_verify(deployment_code: Vec<u8>, instances: Vec<Vec<Fr>>, proof: Vec<u8>)
let success = {
let mut evm = ExecutorBuilder::default()
.with_gas_limit(u64::MAX.into())
.build(Backend::new(MultiFork::new().0, None));
.build();

let caller = Address::from_low_u64_be(0xfe);
let verifier = evm
.deploy(caller, deployment_code.into(), 0.into(), None)
.unwrap()
.address;
let result = evm
.call_raw(caller, verifier, calldata.into(), 0.into())
.deploy(caller, deployment_code.into(), 0.into())
.address
.unwrap();
let result = evm.call_raw(caller, verifier, calldata.into(), 0.into());

dbg!(result.gas_used);

Expand Down
13 changes: 5 additions & 8 deletions examples/evm-verifier.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ethereum_types::Address;
use foundry_evm::executor::{fork::MultiFork, Backend, ExecutorBuilder};
use halo2_curves::bn256::{Bn256, Fq, Fr, G1Affine};
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
Expand All @@ -21,7 +20,7 @@ use halo2_proofs::{
};
use itertools::Itertools;
use plonk_verifier::{
loader::evm::{encode_calldata, EvmLoader},
loader::evm::{encode_calldata, EvmLoader, ExecutorBuilder},
pcs::kzg::{Gwc19, Kzg},
system::halo2::{compile, transcript::evm::EvmTranscript, Config},
verifier::{self, PlonkVerifier},
Expand Down Expand Up @@ -231,16 +230,14 @@ fn evm_verify(deployment_code: Vec<u8>, instances: Vec<Vec<Fr>>, proof: Vec<u8>)
let success = {
let mut evm = ExecutorBuilder::default()
.with_gas_limit(u64::MAX.into())
.build(Backend::new(MultiFork::new().0, None));
.build();

let caller = Address::from_low_u64_be(0xfe);
let verifier = evm
.deploy(caller, deployment_code.into(), 0.into(), None)
.unwrap()
.address;
let result = evm
.call_raw(caller, verifier, calldata.into(), 0.into())
.deploy(caller, deployment_code.into(), 0.into())
.address
.unwrap();
let result = evm.call_raw(caller, verifier, calldata.into(), 0.into());

dbg!(result.gas_used);

Expand Down
13 changes: 7 additions & 6 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
},
Error,
};
use std::{fmt::Debug, iter};
use std::{borrow::Cow, fmt::Debug, iter, ops::Deref};

pub mod native;

Expand Down Expand Up @@ -86,7 +86,7 @@ pub trait EcPointLoader<C: CurveAffine> {
) -> Result<(), Error>;

fn multi_scalar_multiplication(
pairs: &[(Self::LoadedScalar, Self::LoadedEcPoint)],
pairs: &[(&Self::LoadedScalar, &Self::LoadedEcPoint)],
) -> Self::LoadedEcPoint
where
Self: ScalarLoader<C::ScalarExt>;
Expand Down Expand Up @@ -126,17 +126,18 @@ pub trait ScalarLoader<F: PrimeField> {
.chain(if constant == F::zero() {
None
} else {
Some(loader.load_const(&constant))
Some(Cow::Owned(loader.load_const(&constant)))
})
.chain(values.iter().map(|&(coeff, value)| {
if coeff == F::one() {
value.clone()
Cow::Borrowed(value)
} else {
loader.load_const(&coeff) * value
Cow::Owned(loader.load_const(&coeff) * value)
}
}))
.reduce(|acc, term| acc + term)
.reduce(|acc, term| Cow::Owned(acc.into_owned() + term.deref()))
.unwrap()
.into_owned()
}

fn sum_products_with_coeff_and_const(
Expand Down
4 changes: 3 additions & 1 deletion src/loader/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ mod util;
mod test;

pub use loader::{EcPoint, EvmLoader, Scalar};
pub use util::{encode_calldata, estimate_gas, fe_to_u256, modulus, u256_to_fe, MemoryChunk};
pub use util::{
encode_calldata, estimate_gas, fe_to_u256, modulus, u256_to_fe, ExecutorBuilder, MemoryChunk,
};

pub use ethereum_types::U256;

Expand Down
11 changes: 6 additions & 5 deletions src/loader/evm/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<T: Debug> PartialEq for Value<T> {

impl<T: Debug> Value<T> {
fn identifier(&self) -> String {
match &self {
match self {
Value::Constant(_) | Value::Memory(_) => format!("{:?}", self),
Value::Negated(value) => format!("-({:?})", value),
Value::Sum(lhs, rhs) => format!("({:?} + {:?})", lhs, rhs),
Expand Down Expand Up @@ -222,13 +222,13 @@ impl EvmLoader {

pub fn ec_point_from_limbs<const LIMBS: usize, const BITS: usize>(
self: &Rc<Self>,
x_limbs: [Scalar; LIMBS],
y_limbs: [Scalar; LIMBS],
x_limbs: [&Scalar; LIMBS],
y_limbs: [&Scalar; LIMBS],
) -> EcPoint {
let ptr = self.allocate(0x40);
for (ptr, limbs) in [(ptr, x_limbs), (ptr + 0x20, y_limbs)] {
for (idx, limb) in limbs.into_iter().enumerate() {
self.push(&limb);
self.push(limb);
// [..., success, acc]
if idx > 0 {
self.code
Expand Down Expand Up @@ -769,10 +769,11 @@ where
}

fn multi_scalar_multiplication(
pairs: &[(<Self as ScalarLoader<C::Scalar>>::LoadedScalar, EcPoint)],
pairs: &[(&<Self as ScalarLoader<C::Scalar>>::LoadedScalar, &EcPoint)],
) -> EcPoint {
pairs
.iter()
.cloned()
.map(|(scalar, ec_point)| match scalar.value {
Value::Constant(constant) if U256::one() == constant => ec_point.clone(),
_ => ec_point.loader.ec_point_scalar_mul(ec_point, scalar),
Expand Down
22 changes: 9 additions & 13 deletions src/loader/evm/test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::{loader::evm::test::tui::Tui, util::Itertools};
use foundry_evm::{
executor::{backend::Backend, fork::MultiFork, ExecutorBuilder},
revm::{AccountInfo, Bytecode},
utils::h256_to_u256_be,
Address,
use crate::{
loader::evm::{test::tui::Tui, util::ExecutorBuilder},
util::Itertools,
};
use ethereum_types::{Address, U256};
use revm::{AccountInfo, Bytecode};
use std::env::var_os;

mod tui;
Expand All @@ -29,23 +28,20 @@ pub fn execute(code: Vec<u8>, calldata: Vec<u8>) -> (bool, u64, Vec<u64>) {

let mut evm = ExecutorBuilder::default()
.with_gas_limit(u64::MAX.into())
.set_tracing(debug)
.set_debugger(debug)
.build(Backend::new(MultiFork::new().0, None));
.build();

evm.backend_mut().insert_account_info(
evm.db_mut().insert_account_info(
callee,
AccountInfo::new(0.into(), 1, Bytecode::new_raw(code.into())),
);

let result = evm
.call_raw(caller, callee, calldata.into(), 0.into())
.unwrap();
let result = evm.call_raw(caller, callee, calldata.into(), 0.into());

let costs = result
.logs
.into_iter()
.map(|log| h256_to_u256_be(log.topics[0]).as_u64())
.map(|log| U256::from_big_endian(log.topics[0].as_bytes()).as_u64())
.collect_vec();

if debug {
Expand Down
Loading

0 comments on commit 25dbaf5

Please sign in to comment.