diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fb8851d8ce..ebf404a9ef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,11 +47,16 @@ jobs: ~/.cargo/git/db/ target/ key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - - name: Run tests + - name: Run light tests # light tests are run in parallel uses: actions-rs/cargo@v1 with: command: test args: --verbose --release --all --all-features --exclude integration-tests --exclude circuit-benchmarks + - name: Run heavy tests # heavy tests are run serially to avoid OOM + uses: actions-rs/cargo@v1 + with: + command: test + args: --verbose --release --all --all-features --exclude integration-tests --exclude circuit-benchmarks serial_ -- --ignored --test-threads 1 build: if: github.event.pull_request.draft == false diff --git a/Cargo.lock b/Cargo.lock index d23c2e5792..81b9d87be2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -544,7 +544,7 @@ dependencies = [ "coins-core", "digest 0.9.0", "hmac 0.11.0", - "k256 0.9.6", + "k256", "lazy_static", "serde", "sha2 0.9.9", @@ -1096,18 +1096,6 @@ dependencies = [ "signature", ] -[[package]] -name = "ecdsa" -version = "0.13.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0d69ae62e0ce582d56380743515fefaf1a8c70cec685d9677636d7e30ae9dc9" -dependencies = [ - "der 0.5.1", - "elliptic-curve 0.11.12", - "rfc6979", - "signature", -] - [[package]] name = "either" version = "1.6.1" @@ -1124,7 +1112,7 @@ dependencies = [ "ff 0.10.1", "generic-array 0.14.5", "group 0.10.0", - "pkcs8 0.7.6", + "pkcs8", "rand_core", "subtle", "zeroize", @@ -1139,11 +1127,8 @@ dependencies = [ "base16ct", "crypto-bigint 0.3.2", "der 0.5.1", - "ff 0.11.1", "generic-array 0.14.5", - "group 0.11.0", "rand_core", - "sec1", "subtle", "zeroize", ] @@ -1337,7 +1322,7 @@ dependencies = [ "ethabi", "generic-array 0.14.5", "hex", - "k256 0.9.6", + "k256", "once_cell", "proc-macro2", "quote", @@ -2242,19 +2227,6 @@ dependencies = [ "sha3 0.9.1", ] -[[package]] -name = "k256" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19c3a5e0a0b8450278feda242592512e09f61c72e018b8cd5c859482802daf2d" -dependencies = [ - "cfg-if 1.0.0", - "ecdsa 0.13.4", - "elliptic-curve 0.11.12", - "sec1", - "sha2 0.9.9", -] - [[package]] name = "keccak" version = "0.1.0" @@ -2845,18 +2817,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee3ef9b64d26bad0536099c816c6734379e45bbd5f14798def6809e5cc350447" dependencies = [ "der 0.4.5", - "spki 0.4.1", -] - -[[package]] -name = "pkcs8" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cabda3fb821068a9a4fab19a683eac3af12edf0f34b94a8be53c4972b8149d0" -dependencies = [ - "der 0.5.1", - "spki 0.5.4", - "zeroize", + "spki", ] [[package]] @@ -3194,17 +3155,6 @@ dependencies = [ "winreg", ] -[[package]] -name = "rfc6979" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96ef608575f6392792f9ecf7890c00086591d29a83910939d430753f7c050525" -dependencies = [ - "crypto-bigint 0.3.2", - "hmac 0.11.0", - "zeroize", -] - [[package]] name = "ring" version = "0.16.20" @@ -3402,19 +3352,6 @@ dependencies = [ "untrusted", ] -[[package]] -name = "sec1" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08da66b8b0965a5555b6bd6639e68ccba85e1e2506f5fbb089e93f8a04e1a2d1" -dependencies = [ - "der 0.5.1", - "generic-array 0.14.5", - "pkcs8 0.8.0", - "subtle", - "zeroize", -] - [[package]] name = "secp256k1" version = "0.0.1" @@ -3702,16 +3639,6 @@ dependencies = [ "der 0.4.5", ] -[[package]] -name = "spki" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d01ac02a6ccf3e07db148d2be087da624fea0221a16152ed01f0496a6b0a27" -dependencies = [ - "base64ct", - "der 0.5.1", -] - [[package]] name = "static_assertions" version = "1.1.0" @@ -4442,13 +4369,11 @@ dependencies = [ "ethers-signers", "ff 0.11.1", "gadgets", - "generic-array 0.12.4", "group 0.11.0", "halo2_proofs 0.1.0-beta.1", "hex", "integer", "itertools", - "k256 0.10.4", "keccak256", "lazy_static", "libsecp256k1", diff --git a/Cargo.toml b/Cargo.toml index 72def5ac02..ac0189884b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,9 +20,6 @@ members = [ # is resolved: https://github.com/bitvecto-rs/bitvec/pull/141 bitvec = { git = "https://github.com/ed255/bitvec.git", rev = "5cfc5fa8496c66872d21905e677120fc3e79693c" } -# [patch."https://github.com/appliedzkp/halo2.git"] -# halo2_proofs = { path = "../halo2/halo2_proofs" } - # Definition of benchmarks profile to use. [profile.bench] opt-level = 3 diff --git a/Makefile b/Makefile index ea755b7aaa..feca1538a0 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,10 @@ fmt: ## Check whether the code is formated correctly @cargo fmt --all -- --check test: ## Run tests for all the workspace members + # Run light tests @cargo test --release --all --all-features --exclude integration-tests --exclude circuit-benchmarks + # Run heavy tests serially to avoid OOM + @cargo test --release --all --all-features --exclude integration-tests --exclude circuit-benchmarks serial_ -- --ignored --test-threads 1 test_benches: ## Compiles the benchmarks @cargo test --verbose --release --all-features -p circuit-benchmarks --no-run diff --git a/zkevm-circuits/Cargo.toml b/zkevm-circuits/Cargo.toml index 4d48ad5bb4..b5b0c202aa 100644 --- a/zkevm-circuits/Cargo.toml +++ b/zkevm-circuits/Cargo.toml @@ -35,8 +35,6 @@ ecc = { git = "https://github.com/appliedzkp/halo2wrong", rev = "92b96893b maingate = { git = "https://github.com/appliedzkp/halo2wrong", rev = "92b96893b5699ff40723e201a2416313aeafd267", features = ["kzg"] } integer = { git = "https://github.com/appliedzkp/halo2wrong", rev = "92b96893b5699ff40723e201a2416313aeafd267", features = ["kzg"] } group = "0.11" -k256 = "0.10.4" -generic-array = "0.12.4" libsecp256k1 = "0.7" rlp = "0.5" num-bigint = { version = "0.4" } diff --git a/zkevm-circuits/src/tx_circuit.rs b/zkevm-circuits/src/tx_circuit.rs index 9892775f5d..bfea50e75b 100644 --- a/zkevm-circuits/src/tx_circuit.rs +++ b/zkevm-circuits/src/tx_circuit.rs @@ -1,5 +1,9 @@ //! The transaction circuit implementation. +// Naming notes: +// - *_be: Big-Endian bytes +// - *_le: Little-Endian bytes + mod sign_verify; use crate::util::Expr; @@ -12,7 +16,6 @@ use halo2_proofs::{ poly::Rotation, }; use itertools::Itertools; -use k256::elliptic_curve::generic_array::{typenum::consts::U32, GenericArray}; use lazy_static::lazy_static; use libsecp256k1; use log::error; @@ -23,6 +26,7 @@ use secp256k1::Secp256k1Affine; use sha3::{Digest, Keccak256}; use sign_verify::{SignData, SignVerifyChip, SignVerifyConfig}; pub use sign_verify::{POW_RAND_SIZE, VERIF_HEIGHT}; +use std::convert::TryInto; use std::marker::PhantomData; use subtle::CtOption; @@ -74,12 +78,7 @@ fn random_linear_combine(bytes: [u8; 32], randomness: F) -> F { crate::evm_circuit::util::Word::random_linear_combine(bytes, randomness) } -fn recover_pk( - v: u8, - r: &Word, - s: &Word, - msg_hash: &GenericArray, -) -> Result { +fn recover_pk(v: u8, r: &Word, s: &Word, msg_hash: &[u8; 32]) -> Result { let r_be = r.to_be_bytes(); let s_be = s.to_be_bytes(); let mut r = libsecp256k1::curve::Scalar::from_int(0); @@ -154,7 +153,11 @@ fn tx_to_sign_data(tx: &Transaction, chain_id: u64) -> Result { .append(&0u32) .append(&0u32); let msg = stream.out(); - let msg_hash = Keccak256::digest(&msg); + let msg_hash: [u8; 32] = Keccak256::digest(&msg) + .as_slice() + .to_vec() + .try_into() + .expect("hash length isn't 32 bytes"); let v = (tx.v - 35 - chain_id * 2) as u8; let pk = recover_pk(v, &tx.r, &tx.s, &msg_hash)?; // msg_hash = msg_hash % q @@ -519,8 +522,9 @@ mod tx_circuit_tests { } } + #[ignore] #[test] - fn test_tx_circuit() { + fn serial_test_tx_circuit() { const NUM_TXS: usize = 2; const MAX_TXS: usize = 2; const MAX_CALLDATA: usize = 32; diff --git a/zkevm-circuits/src/tx_circuit/sign_verify.rs b/zkevm-circuits/src/tx_circuit/sign_verify.rs index a1582444dd..3313d20f33 100644 --- a/zkevm-circuits/src/tx_circuit/sign_verify.rs +++ b/zkevm-circuits/src/tx_circuit/sign_verify.rs @@ -1,3 +1,7 @@ +// Naming notes: +// - *_be: Big-Endian bytes +// - *_le: Little-Endian bytes + use crate::{ evm_circuit::util::{not, RandomLinearCombination, Word}, util::Expr, @@ -227,7 +231,6 @@ impl SignVerifyConfig { .collect::>>() .try_into() .expect("vector to array of size 64"); - // let mut pk_be: [_; 64] = (0..64)pk[0] + pk[1]; pk_be[..32].reverse(); pk_be[32..].reverse(); let pk_rlc = @@ -473,11 +476,6 @@ impl SignVerifyChip { &self, ctx: &mut RegionCtx, chips: &ChipsRef, - // main_gate: &MainGate, - // range_chip: &RangeChip, - // ecc_chip: &GeneralEccChip, - // scalar_chip: &IntegerChip, - // ecdsa_chip: &EcdsaChip, sign_data: &SignData, ) -> Result, Error> { let SignData { @@ -940,7 +938,7 @@ mod sign_verify_tests { fn gen_key_pair(rng: impl RngCore) -> (secp256k1::Fq, Secp256k1Affine) { // generate a valid signature let generator = ::generator(); - let sk = ::ScalarExt::random(rng); + let sk = secp256k1::Fq::random(rng); let pk = generator * sk; let pk = pk.to_affine(); @@ -949,7 +947,7 @@ mod sign_verify_tests { // Generate a test message hash fn gen_msg_hash(rng: impl RngCore) -> secp256k1::Fq { - ::ScalarExt::random(rng) + secp256k1::Fq::random(rng) } // Returns (r, s) @@ -962,11 +960,20 @@ mod sign_verify_tests { sign(randomness, sk, msg_hash) } + #[ignore] #[test] - fn test_sign_verify() { + fn serial_test_sign_verify() { + // Vectors using `XorShiftRng::seed_from_u64(1)` + // sk: 0x771bd7bf6c6414b9370bb8559d46e1cedb479b1836ea3c2e59a54c343b0d0495 + // pk: ( + // 0x8e31a3586d4c8de89d4e0131223ecfefa4eb76215f68a691ae607757d6256ede, + // 0xc76fdd462294a7eeb8ff3f0f698eb470f32085ba975801dbe446ed8e0b05400b + // ) + // pk_hash: d90e2e9d267cbcfd94de06fa7adbe6857c2c733025c0b8938a76beeefc85d6c7 + // addr: 0x7adbe6857c2c733025c0b8938a76beeefc85d6c7 let mut rng = XorShiftRng::seed_from_u64(1); - const MAX_VERIF: usize = 4; - const NUM_TXS: usize = 3; + const MAX_VERIF: usize = 3; + const NUM_TXS: usize = 2; let mut txs = Vec::new(); for _ in 0..NUM_TXS { let (sk, pk) = gen_key_pair(&mut rng); @@ -979,16 +986,7 @@ mod sign_verify_tests { }); } - let k = 20; + let k = 19; run::(k, txs); } } - -// Vectors using `XorShiftRng::seed_from_u64(1)` -// sk: 0x771bd7bf6c6414b9370bb8559d46e1cedb479b1836ea3c2e59a54c343b0d0495 -// pk: ( -// 0x8e31a3586d4c8de89d4e0131223ecfefa4eb76215f68a691ae607757d6256ede, -// 0xc76fdd462294a7eeb8ff3f0f698eb470f32085ba975801dbe446ed8e0b05400b -// ) -// pk_hash: d90e2e9d267cbcfd94de06fa7adbe6857c2c733025c0b8938a76beeefc85d6c7 -// addr: 0x7adbe6857c2c733025c0b8938a76beeefc85d6c7