From 21525552f8e5b22c11448335f8fbbd3d39d9ff21 Mon Sep 17 00:00:00 2001 From: Eduard S Date: Tue, 29 Nov 2022 14:14:59 +0100 Subject: [PATCH] Address some comments from pinkie --- testool/src/statetest/executor.rs | 3 ++ .../src/bytecode_circuit/bytecode_unroller.rs | 7 +++++ zkevm-circuits/src/super_circuit.rs | 2 ++ zkevm-circuits/src/tx_circuit.rs | 31 +++---------------- zkevm-circuits/src/tx_circuit/sign_verify.rs | 27 ++++++++++++++-- zkevm-circuits/src/witness/block.rs | 2 +- 6 files changed, 42 insertions(+), 30 deletions(-) diff --git a/testool/src/statetest/executor.rs b/testool/src/statetest/executor.rs index 7d54da19d48..a59e1791fec 100644 --- a/testool/src/statetest/executor.rs +++ b/testool/src/statetest/executor.rs @@ -262,6 +262,9 @@ pub fn run_test( nonce: tx.nonce, gas: tx.gas_limit, transaction_index: Some(U64::from(index)), + r: tx.r, + s: tx.s, + v: U64::from(tx.v), ..eth_types::Transaction::default() }) .collect(); diff --git a/zkevm-circuits/src/bytecode_circuit/bytecode_unroller.rs b/zkevm-circuits/src/bytecode_circuit/bytecode_unroller.rs index 32a0d1cae28..02f6ea6c92f 100644 --- a/zkevm-circuits/src/bytecode_circuit/bytecode_unroller.rs +++ b/zkevm-circuits/src/bytecode_circuit/bytecode_unroller.rs @@ -727,6 +727,13 @@ impl SubCircuit for BytecodeCircuit { type Config = BytecodeCircuitConfig; fn new_from_block(block: &witness::Block) -> Self { + // TODO: Find a nicer way to add the extra `128`. Is this to account for + // unusable rows? Then it could be calculated like this: + // fn unusable_rows>() -> usize { + // let mut cs = ConstraintSystem::default(); + // C::configure(&mut cs); + // cs.blinding_factors() + // } let bytecode_size = block.circuits_params.max_bytecode + 128; let bytecodes: Vec> = block .bytecodes diff --git a/zkevm-circuits/src/super_circuit.rs b/zkevm-circuits/src/super_circuit.rs index 8472976e9dc..3a77ab459ee 100644 --- a/zkevm-circuits/src/super_circuit.rs +++ b/zkevm-circuits/src/super_circuit.rs @@ -278,6 +278,8 @@ impl { impl TxCircuit { /// Return a new TxCircuit - pub fn new( - max_txs: usize, - max_calldata: usize, - aux_generator: Secp256k1Affine, - chain_id: u64, - txs: Vec, - ) -> Self { + pub fn new(max_txs: usize, max_calldata: usize, chain_id: u64, txs: Vec) -> Self { TxCircuit:: { max_txs, max_calldata, - sign_verify: SignVerifyChip { - aux_generator, - window_size: 2, - max_verif: max_txs, - _marker: PhantomData, - }, + sign_verify: SignVerifyChip::new(max_txs), txs, chain_id, } @@ -321,13 +307,9 @@ impl SubCircuit for TxCircuit { type Config = TxCircuitConfig; fn new_from_block(block: &witness::Block) -> Self { - let mut rng = ChaCha20Rng::seed_from_u64(42); - let aux_generator = - ::CurveExt::random(&mut rng).to_affine(); Self::new( block.circuits_params.max_txs, block.circuits_params.max_calldata, - aux_generator, block.context.chain_id.as_u64(), block .eth_block @@ -418,9 +400,8 @@ mod tx_circuit_tests { use super::*; use eth_types::address; use halo2_proofs::{ - arithmetic::CurveAffine, dev::{MockProver, VerifyFailure}, - halo2curves::{bn256::Fr, group::Group}, + halo2curves::bn256::Fr, }; use mock::AddrOrWallet; use pretty_assertions::assert_eq; @@ -432,12 +413,8 @@ mod tx_circuit_tests { max_txs: usize, max_calldata: usize, ) -> Result<(), Vec> { - let mut rng = ChaCha20Rng::seed_from_u64(2); - let aux_generator = - ::CurveExt::random(&mut rng).to_affine(); - // SignVerifyChip -> ECDSAChip -> MainGate instance column - let circuit = TxCircuit::::new(max_txs, max_calldata, aux_generator, chain_id, txs); + let circuit = TxCircuit::::new(max_txs, max_calldata, chain_id, txs); let prover = match MockProver::run(k, &circuit, vec![vec![]]) { Ok(prover) => prover, diff --git a/zkevm-circuits/src/tx_circuit/sign_verify.rs b/zkevm-circuits/src/tx_circuit/sign_verify.rs index 1e1d20add01..832618f21fe 100644 --- a/zkevm-circuits/src/tx_circuit/sign_verify.rs +++ b/zkevm-circuits/src/tx_circuit/sign_verify.rs @@ -14,14 +14,19 @@ use ecdsa::ecdsa::{AssignedEcdsaSig, AssignedPublicKey, EcdsaChip}; use eth_types::sign_types::{pk_bytes_le, pk_bytes_swap_endianness, SignData}; use eth_types::{self, Field}; use halo2_proofs::{ - arithmetic::FieldExt, + arithmetic::{CurveAffine, FieldExt}, circuit::{AssignedCell, Cell, Layouter, Value}, - halo2curves::secp256k1, halo2curves::secp256k1::Secp256k1Affine, + halo2curves::{ + group::{Curve, Group}, + secp256k1, + }, plonk::{Advice, Column, ConstraintSystem, Error, Expression, SecondPhase, Selector}, poly::Rotation, }; use integer::{AssignedInteger, IntegerChip, IntegerConfig, IntegerInstructions, Range}; +use rand::SeedableRng; +use rand_chacha::ChaCha20Rng; use itertools::Itertools; use keccak256::plain::Keccak; @@ -47,6 +52,24 @@ pub struct SignVerifyChip { pub _marker: PhantomData, } +impl SignVerifyChip { + /// Return a new SignVerifyChip + pub fn new(max_verif: usize) -> Self { + // TODO: Investigate if it is safe to use a random point as aux generator that + // is choosen by the prover. If this is unsafe, we will need to update the + // EccChip to calculate an aux generator using the challange API. + let mut rng = ChaCha20Rng::seed_from_u64(0); + let aux_generator = + ::CurveExt::random(&mut rng).to_affine(); + Self { + aux_generator, + window_size: 2, + max_verif, + _marker: PhantomData, + } + } +} + const NUMBER_OF_LIMBS: usize = 4; const BIT_LEN_LIMB: usize = 72; const BIT_LEN_LAST_LIMB: usize = 256 - (NUMBER_OF_LIMBS - 1) * BIT_LEN_LIMB; diff --git a/zkevm-circuits/src/witness/block.rs b/zkevm-circuits/src/witness/block.rs index 3b6449a37cd..0b22e1e5172 100644 --- a/zkevm-circuits/src/witness/block.rs +++ b/zkevm-circuits/src/witness/block.rs @@ -47,7 +47,7 @@ pub struct Block { /// Inputs to the SHA3 opcode pub sha3_inputs: Vec>, /// State root of the previous block - pub prev_state_root: Word, // TODO: Make this U256 + pub prev_state_root: Word, // TODO: Make this H256 /// Keccak inputs pub keccak_inputs: Vec>, /// Original Block from geth