Skip to content

Commit

Permalink
[revert] undo cargo fmt
Browse files Browse the repository at this point in the history
This reverts commit 77c9bd4.
  • Loading branch information
zhenfeizhang committed Apr 8, 2023
1 parent b4686f2 commit f829da6
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 99 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ incremental = true
inherits = "release"
debug = true


[patch."https://github.com/privacy-scaling-explorations/halo2.git"]
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "halo2-ecc-snark-verifier-0323" }
[patch."https://github.com/privacy-scaling-explorations/poseidon.git"]
Expand Down
7 changes: 5 additions & 2 deletions snark-verifier-sdk/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use super::{CircuitExt, Plonk};
use ark_std::{end_timer, start_timer};
use ethereum_types::Address;
use halo2_base::halo2_proofs::{
dev::MockProver,
halo2curves::bn256::{Bn256, Fq, Fr, G1Affine},
plonk::{create_proof, verify_proof, Circuit, ProvingKey, VerifyingKey},
poly::{
commitment::{Params, ParamsProver, Prover, Verifier},
commitment::{ParamsProver, Prover, Verifier},
kzg::{
commitment::{KZGCommitmentScheme, ParamsKZG},
msm::DualMSM,
Expand Down Expand Up @@ -52,6 +51,10 @@ where
{
#[cfg(debug_assertions)]
{
use halo2_base::halo2_proofs::{
dev::MockProver,
poly::commitment::Params,
};
MockProver::run(params.k(), &circuit, instances.clone()).unwrap().assert_satisfied();
}

Expand Down
7 changes: 6 additions & 1 deletion snark-verifier/src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ impl Cost {
num_evaluation: usize,
num_msm: usize,
) -> Self {
Self { num_instance, num_commitment, num_evaluation, num_msm }
Self {
num_instance,
num_commitment,
num_evaluation,
num_msm,
}
}
}

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

let loader = values.first().unwrap().1.loader();
iter::empty()
.chain(if constant == F::zero() { None } else { Some(loader.load_const(&constant)) })
.chain(if constant == F::zero() {
None
} else {
Some(loader.load_const(&constant))
})
.chain(values.iter().map(|&(coeff, lhs, rhs)| {
if coeff == F::one() {
lhs.clone() * rhs
Expand Down Expand Up @@ -191,7 +195,10 @@ pub trait ScalarLoader<F: PrimeField> {
constant: F,
) -> Self::LoadedScalar {
self.sum_products_with_coeff_and_const(
&values.iter().map(|&(lhs, rhs)| (F::one(), lhs, rhs)).collect_vec(),
&values
.iter()
.map(|&(lhs, rhs)| (F::one(), lhs, rhs))
.collect_vec(),
constant,
)
}
Expand All @@ -204,7 +211,9 @@ pub trait ScalarLoader<F: PrimeField> {
}

fn product(&self, values: &[&Self::LoadedScalar]) -> Self::LoadedScalar {
values.iter().fold(self.load_one(), |acc, value| acc * *value)
values
.iter()
.fold(self.load_one(), |acc, value| acc * *value)
}

fn batch_invert<'a>(values: impl IntoIterator<Item = &'a mut Self::LoadedScalar>)
Expand Down
4 changes: 3 additions & 1 deletion snark-verifier/src/loader/evm/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub struct YulCode {

impl YulCode {
pub fn new() -> Self {
YulCode { runtime: String::new() }
YulCode {
runtime: String::new(),
}
}

pub fn code(&self, base_modulus: String, scalar_modulus: String) -> String {
Expand Down
94 changes: 67 additions & 27 deletions snark-verifier/src/loader/evm/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ impl EvmLoader {
return(0, 0)"
.to_string();
self.code.borrow_mut().runtime_append(code);
self.code
.borrow()
.code(hex_encode_u256(&self.base_modulus), hex_encode_u256(&self.scalar_modulus))
self.code.borrow().code(
hex_encode_u256(&self.base_modulus),
hex_encode_u256(&self.scalar_modulus),
)
}

pub fn allocate(self: &Rc<Self>, size: usize) -> usize {
Expand Down Expand Up @@ -209,27 +210,41 @@ impl EvmLoader {
}

pub(crate) fn scalar(self: &Rc<Self>, value: Value<U256>) -> Scalar {
let value = if matches!(value, Value::Constant(_) | Value::Memory(_) | Value::Negated(_)) {
let value = if matches!(
value,
Value::Constant(_) | Value::Memory(_) | Value::Negated(_)
) {
value
} else {
let identifier = value.identifier();
let some_ptr = self.cache.borrow().get(&identifier).cloned();
let ptr = if let Some(ptr) = some_ptr {
ptr
} else {
let v = self.push(&Scalar { loader: self.clone(), value });
let v = self.push(&Scalar {
loader: self.clone(),
value,
});
let ptr = self.allocate(0x20);
self.code.borrow_mut().runtime_append(format!("mstore({ptr:#x}, {v})"));
self.code
.borrow_mut()
.runtime_append(format!("mstore({ptr:#x}, {v})"));
self.cache.borrow_mut().insert(identifier, ptr);
ptr
};
Value::Memory(ptr)
};
Scalar { loader: self.clone(), value }
Scalar {
loader: self.clone(),
value,
}
}

fn ec_point(self: &Rc<Self>, value: Value<(U256, U256)>) -> EcPoint {
EcPoint { loader: self.clone(), value }
EcPoint {
loader: self.clone(),
value,
}
}

pub fn keccak256(self: &Rc<Self>, ptr: usize, len: usize) -> usize {
Expand All @@ -241,7 +256,9 @@ impl EvmLoader {

pub fn copy_scalar(self: &Rc<Self>, scalar: &Scalar, ptr: usize) {
let scalar = self.push(scalar);
self.code.borrow_mut().runtime_append(format!("mstore({ptr:#x}, {scalar})"));
self.code
.borrow_mut()
.runtime_append(format!("mstore({ptr:#x}, {scalar})"));
}

pub fn dup_scalar(self: &Rc<Self>, scalar: &Scalar) -> Scalar {
Expand Down Expand Up @@ -375,7 +392,10 @@ impl EvmLoader {
return self.scalar(Value::Constant(out.try_into().unwrap()));
}

self.scalar(Value::Sum(Box::new(lhs.value.clone()), Box::new(rhs.value.clone())))
self.scalar(Value::Sum(
Box::new(lhs.value.clone()),
Box::new(rhs.value.clone()),
))
}

fn sub(self: &Rc<Self>, lhs: &Scalar, rhs: &Scalar) -> Scalar {
Expand All @@ -395,7 +415,10 @@ impl EvmLoader {
return self.scalar(Value::Constant(out.try_into().unwrap()));
}

self.scalar(Value::Product(Box::new(lhs.value.clone()), Box::new(rhs.value.clone())))
self.scalar(Value::Product(
Box::new(lhs.value.clone()),
Box::new(rhs.value.clone()),
))
}

fn neg(self: &Rc<Self>, scalar: &Scalar) -> Scalar {
Expand All @@ -410,14 +433,18 @@ impl EvmLoader {
#[cfg(test)]
impl EvmLoader {
fn start_gas_metering(self: &Rc<Self>, identifier: &str) {
self.gas_metering_ids.borrow_mut().push(identifier.to_string());
self.gas_metering_ids
.borrow_mut()
.push(identifier.to_string());
let code = format!("let {identifier} := gas()");
self.code.borrow_mut().runtime_append(code);
}

fn end_gas_metering(self: &Rc<Self>) {
let code =
format!("log1(0, 0, sub({}, gas()))", self.gas_metering_ids.borrow().last().unwrap());
let code = format!(
"log1(0, 0, sub({}, gas()))",
self.gas_metering_ids.borrow().last().unwrap()
);
self.code.borrow_mut().runtime_append(code);
}

Expand Down Expand Up @@ -453,7 +480,9 @@ impl EcPoint {

impl Debug for EcPoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EcPoint").field("value", &self.value).finish()
f.debug_struct("EcPoint")
.field("value", &self.value)
.finish()
}
}

Expand Down Expand Up @@ -497,14 +526,21 @@ impl Scalar {
pub(crate) fn ptr(&self) -> usize {
match self.value {
Value::Memory(ptr) => ptr,
_ => *self.loader.cache.borrow().get(&self.value.identifier()).unwrap(),
_ => *self
.loader
.cache
.borrow()
.get(&self.value.identifier())
.unwrap(),
}
}
}

impl Debug for Scalar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Scalar").field("value", &self.value).finish()
f.debug_struct("Scalar")
.field("value", &self.value)
.finish()
}
}

Expand Down Expand Up @@ -673,9 +709,9 @@ impl<F: PrimeField<Repr = [u8; 0x20]>> ScalarLoader<F> for Rc<EvmLoader> {
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)))),
),
(false, Value::Constant(value)) => self.push(&self.scalar(Value::Constant(
fe_to_u256(*coeff * u256_to_fe::<F>(*value)),
))),
(false, _) => {
let value = self.push(value);
let coeff = self.push(&self.scalar(Value::Constant(fe_to_u256(*coeff))));
Expand Down Expand Up @@ -729,10 +765,9 @@ impl<F: PrimeField<Repr = [u8; 0x20]>> ScalarLoader<F> for Rc<EvmLoader> {
(_, value @ Value::Memory(_), Value::Constant(constant))
| (_, Value::Constant(constant), value @ Value::Memory(_)) => {
let v1 = self.push(&self.scalar(value.clone()));
let v2 =
self.push(&self.scalar(Value::Constant(fe_to_u256(
*coeff * u256_to_fe::<F>(*constant),
))));
let v2 = self.push(&self.scalar(Value::Constant(fe_to_u256(
*coeff * u256_to_fe::<F>(*constant),
))));
format!("mulmod({v1}, {v2}, f_q)")
}
(true, _, _) => {
Expand Down Expand Up @@ -823,9 +858,14 @@ impl<F: PrimeField<Repr = [u8; 0x20]>> ScalarLoader<F> for Rc<EvmLoader> {
let v
"
);
for (value, product) in
values.iter().rev().zip(products.iter().rev().skip(1).map(Some).chain(iter::once(None)))
{
for (value, product) in values.iter().rev().zip(
products
.iter()
.rev()
.skip(1)
.map(Some)
.chain(iter::once(None)),
) {
if let Some(product) = product {
let val_ptr = value.ptr();
let prod_ptr = product.ptr();
Expand Down
13 changes: 9 additions & 4 deletions snark-verifier/src/loader/evm/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ pub fn execute(deployment_code: Vec<u8>, calldata: Vec<u8>) -> (bool, u64, Vec<u
let debug = debug();
let caller = Address::from_low_u64_be(0xfe);

let mut evm =
ExecutorBuilder::default().with_gas_limit(u64::MAX.into()).set_debugger(debug).build();

let contract = evm.deploy(caller, deployment_code.into(), 0.into()).address.unwrap();
let mut evm = ExecutorBuilder::default()
.with_gas_limit(u64::MAX.into())
.set_debugger(debug)
.build();

let contract = evm
.deploy(caller, deployment_code.into(), 0.into())
.address
.unwrap();
let result = evm.call_raw(caller, contract, calldata.into(), 0.into());

let costs = result
Expand Down
Loading

0 comments on commit f829da6

Please sign in to comment.