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

Commit

Permalink
fix: each params will be up to u256
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko committed Jul 6, 2023
1 parent 0eb3159 commit ff710d7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions circuit-benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ethers-signers = "=2.0.0"
mock = { path = "../mock" }
rand_chacha = "0.3"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "*"
hex = "0.4.3"

[features]
Expand Down
46 changes: 43 additions & 3 deletions circuit-benchmarks/src/super_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! SuperCircuit circuit benchmarks

use eth_types::{Bytes, U256};
use halo2_proofs::poly::commitment::Params;
use serde::{Deserialize, Serialize};
use std::{fs, rc::Rc};
use zkevm_circuits::{
root_circuit::{
Expand All @@ -14,16 +16,18 @@ use bus_mapping::circuit_input_builder::CircuitsParams;

use rand::SeedableRng;

use ark_std::{end_timer, start_timer};
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fq, Fr, G1Affine},
halo2curves::{
bn256::{Bn256, Fq, Fr, G1Affine},
ff::PrimeField,
},
plonk::{keygen_pk, keygen_vk, ProvingKey, VerifyingKey},
poly::{
commitment::ParamsProver,
kzg::commitment::{KZGCommitmentScheme, ParamsKZG},
},
};

use ark_std::{end_timer, start_timer};
use std::path::Path;

use snark_verifier_sdk::{
Expand Down Expand Up @@ -57,6 +61,12 @@ pub type PlonkVerifierSHPLONK =

use rand::rngs::StdRng;

#[derive(Serialize, Deserialize, Debug)]
struct BlockProofData {
instances: Vec<U256>,
proof: Bytes,
}

/// Fixed rng for testing purposes
pub fn fixed_rng() -> StdRng {
StdRng::seed_from_u64(9)
Expand Down Expand Up @@ -255,6 +265,21 @@ fn create_root_super_circuit_prover_sdk<const T: u64, AS: AccumulationSchemeSDK>
}
};

let block_proof_data = BlockProofData {
instances: instances
.iter()
.flatten()
.map(|v| U256::from_little_endian(v.to_repr().as_ref()))
.collect(),
proof: proof_calldata.clone().into(),
};

fs::write(
Path::new("./proof.json"),
serde_json::to_vec(&block_proof_data).unwrap(),
)
.unwrap();

let deployment_code = gen_verifier(
&params,
&vk,
Expand Down Expand Up @@ -314,6 +339,21 @@ fn create_1_level_root_super_circuit_prover_sdk<const T: u64, AS: AccumulationSc
}
};

let block_proof_data = BlockProofData {
instances: instances
.iter()
.flatten()
.map(|v| U256::from_little_endian(v.to_repr().as_ref()))
.collect(),
proof: proof_calldata.clone().into(),
};

fs::write(
Path::new("./proof.json"),
serde_json::to_vec(&block_proof_data).unwrap(),
)
.unwrap();

let deployment_code = gen_verifier(
&params,
&vk,
Expand Down
8 changes: 4 additions & 4 deletions zkevm-circuits/src/anchor_tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const BYTE_POW_BASE: u64 = 1 << 8;
// uint64 parentGasUsed
// )
// anchor(bytes32,bytes32,uint64,uint64) =
// method_signature(4B)+l1Hash(32B)+l1SignalRoot(32B)+l1Height(8B)+parentGasUsed(8B)
const ANCHOR_CALL_DATA_LEN: usize = 84;
// method_signature(4B)+l1Hash(32B)+l1SignalRoot(32B)+l1Height(32B)+parentGasUsed(32B)
const ANCHOR_CALL_DATA_LEN: usize = 132;

struct CallData {
start: usize,
Expand Down Expand Up @@ -284,12 +284,12 @@ impl<F: Field> AnchorTxCircuitConfig<F> {
),
(
"l1_height",
&anchor_tx.call_data[68..76],
&anchor_tx.call_data[68..100],
PiFieldTag::L1Height,
),
(
"parent_gas_used",
&anchor_tx.call_data[76..84],
&anchor_tx.call_data[100..132],
PiFieldTag::ParentGasUsed,
),
] {
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/anchor_tx_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn anchor_test() {
anchor_gas_cost: 150000,
..Default::default()
};
let block = gen_block::<1>(2, 100, protocol_instance, sign_tx);
let block = gen_block::<1>(2, 200, protocol_instance, sign_tx);
assert_eq!(run::<Fr>(&block, None), Ok(()));
}

Expand All @@ -252,6 +252,6 @@ fn anchor_test_when_sign_r_is_gx2() {
};
let msg_hash = *N - *GX1_MUL_PRIVATEKEY;
let msg_hash = H256::from(msg_hash.to_le_bytes());
let block = gen_block::<1>(2, 100, protocol_instance, sign_tx_r_is_gx2);
let block = gen_block::<1>(2, 200, protocol_instance, sign_tx_r_is_gx2);
assert_eq!(run::<Fr>(&block, Some(msg_hash)), Ok(()));
}
36 changes: 34 additions & 2 deletions zkevm-circuits/src/witness/protocol_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ impl ProtocolInstance {
result.extend_from_slice(&ANCHOR_TX_METHOD_SIGNATURE.to_be_bytes());
result.extend_from_slice(&self.meta_hash.l1_hash.to_fixed_bytes());
result.extend_from_slice(&self.signal_root.to_fixed_bytes());
result.extend_from_slice(&self.meta_hash.l1_height.to_be_bytes());
result.extend_from_slice(&(self.parent_gas_used as u64).to_be_bytes());
result.extend_from_slice(&self.meta_hash.l1_height.to_word().to_be_bytes());
result.extend_from_slice(&(self.parent_gas_used as u64).to_word().to_be_bytes());
result.into()
}

Expand Down Expand Up @@ -160,3 +160,35 @@ impl ProtocolInstance {
]
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use super::*;
#[test]
fn test_left_shift() {
let _field0 = left_shift(1u64, 192) + left_shift(1688574587u64, 128) + left_shift(9u64, 64);

let _field5 = left_shift(0u32 as u64, 232)
+ left_shift(124u32 as u64, 208)
+ left_shift(21000u32 as u64, 176)
+ left_shift(
Address::from_str("0000777700000000000000000000000000000001").unwrap(),
16,
);
let _field6 = left_shift(
Address::from_str("df09A0afD09a63fb04ab3573922437e1e637dE8b").unwrap(),
96,
);

let _field9 = left_shift(
Address::from_str("70997970C51812dc3A010C7d01b50e0d17dc79C8").unwrap(),
96,
) + left_shift(0u64, 64)
+ left_shift(141003u64, 32);

let _field10 =
left_shift(6000000u64, 192) + left_shift(79u64, 128) + left_shift(120000u64, 64);
}
}

0 comments on commit ff710d7

Please sign in to comment.