Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
pi circuit more test with real block data
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Apr 6, 2023
1 parent 4d9acf0 commit ddcfef7
Showing 1 changed file with 81 additions and 7 deletions.
88 changes: 81 additions & 7 deletions zkevm-circuits/src/pi_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{iter, marker::PhantomData};
use eth_types::{
geth_types::{BlockConstants, Transaction},
sign_types::SignData,
Address, BigEndianHash, Field, ToBigEndian, ToLittleEndian, ToScalar, Word, H256,
Address, BigEndianHash, Field, ToBigEndian, ToLittleEndian, ToScalar, ToWord, Word, H256,
};
use halo2_proofs::plonk::{Expression, Instance, SecondPhase};
use itertools::Itertools;
Expand Down Expand Up @@ -181,7 +181,10 @@ impl PublicData {
}

fn txs(&self) -> Vec<Transaction> {
self.transactions.iter().map(Transaction::from).collect()
self.transactions
.iter()
.map(Transaction::from)
.collect_vec()
}

fn get_pi_bytes(&self, max_txs: usize, max_calldata: usize) -> Vec<u8> {
Expand Down Expand Up @@ -1456,7 +1459,7 @@ impl<F: Field> PiCircuitConfig<F> {
// difficulty
let difficulty = challenges
.evm_word()
.zip(Value::known(block_values.difficulty.to_le_bytes()))
.zip(Value::known(block_values.difficulty.to_be_bytes()))
.map(|(r, bytes)| rlc(bytes, r));
let block_cell = region.assign_advice(
|| "difficulty",
Expand All @@ -1478,7 +1481,7 @@ impl<F: Field> PiCircuitConfig<F> {
// base_fee
let base_fee = challenges
.evm_word()
.zip(Value::known(block_values.base_fee.to_le_bytes()))
.zip(Value::known(block_values.base_fee.to_be_bytes()))
.map(|(r, bytes)| rlc(bytes, r));
let block_cell = region.assign_advice(
|| "base_fee",
Expand Down Expand Up @@ -1518,7 +1521,7 @@ impl<F: Field> PiCircuitConfig<F> {
for prev_hash in block_values.history_hashes {
let prev_hash_rlc = challenges
.evm_word()
.zip(Value::known(prev_hash.to_fixed_bytes()))
.zip(Value::known(prev_hash.to_word().to_le_bytes()))
.map(|(r, bytes)| rlc(bytes, r));

let block_cell = region.assign_advice(
Expand Down Expand Up @@ -2063,7 +2066,6 @@ impl<F: Field> SubCircuit<F> for PiCircuit<F> {
config.rpi_digest_bytes_rlc,
keccak_offset,
)?;

Ok(vec![hi_lc_cell, lo_lc_cell])
},
)?;
Expand Down Expand Up @@ -2190,13 +2192,22 @@ fn get_rpi_digest_byte_field<F: Field>(

#[cfg(test)]
mod pi_circuit_test {
use std::collections::HashMap;

use crate::witness::block_convert;

use super::*;
use bus_mapping::{circuit_input_builder::CircuitsParams, mock::BlockData};
use eth_types::{bytecode, geth_types::GethData};
use ethers_signers::{LocalWallet, Signer};
use halo2_proofs::{
dev::{MockProver, VerifyFailure},
halo2curves::bn256::Fr,
};
use mock::{CORRECT_MOCK_TXS, MOCK_CHAIN_ID};
use mock::{eth, TestContext, CORRECT_MOCK_TXS, MOCK_ACCOUNTS, MOCK_CHAIN_ID};
use pretty_assertions::assert_eq;
use rand::SeedableRng;
use rand_chacha::ChaChaRng;

fn run<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize>(
k: u32,
Expand Down Expand Up @@ -2247,6 +2258,69 @@ mod pi_circuit_test {
assert_eq!(run::<Fr, MAX_TXS, MAX_CALLDATA>(k, public_data), Ok(()));
}

#[test]
fn test_1tx_1maxtx() {
let mut rng = ChaChaRng::seed_from_u64(2);
let wallet_a = LocalWallet::new(&mut rng).with_chain_id(MOCK_CHAIN_ID.as_u64());

let addr_a = wallet_a.address();
let addr_b = MOCK_ACCOUNTS[0];

let degree = 17;
let calldata = vec![];
let code = bytecode! {
PUSH4(0x1000) // size
PUSH2(0x00) // offset
RETURN
};
let test_ctx = TestContext::<2, 1>::new(
Some(vec![Word::from("0xdeadbeef")]),
|accs| {
accs[0].address(addr_b).balance(eth(10)).code(code);
accs[1].address(addr_a).balance(eth(10));
},
|mut txs, accs| {
txs[0]
.from(accs[1].address)
.to(accs[0].address)
.input(calldata.into())
.gas((1e16 as u64).into());
},
|block, _txs| block.number(0xcafeu64).chain_id(*MOCK_CHAIN_ID),
)
.unwrap();
let mut wallets = HashMap::new();
wallets.insert(wallet_a.address(), wallet_a);

let mut block: GethData = test_ctx.into();
let mut builder = BlockData::new_from_geth_data_with_params(
block.clone(),
CircuitsParams {
max_rws: 1 << (degree - 1),
..Default::default()
},
)
.new_circuit_input_builder();

block.sign(&wallets);

builder
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();

builder.block.eth_block = block.eth_block.clone();
let block = block_convert(&builder.block, &builder.code_db).unwrap();
// MAX_TXS, MAX_TXS align with `CircuitsParams`
let circuit = PiTestCircuit::<Fr, 1, 256>::new_from_block(&block);
let public_inputs = circuit.0.instance();

let prover = match MockProver::run(degree, &circuit, public_inputs) {
Ok(prover) => prover,
Err(e) => panic!("{:#?}", e),
};
assert_eq!(prover.verify(), Ok(()));
}

fn run_size_check<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize>(
public_data: [PublicData; 2],
) {
Expand Down

0 comments on commit ddcfef7

Please sign in to comment.