Skip to content

Commit

Permalink
Split light and heavy tests; run heavy tests serially
Browse files Browse the repository at this point in the history
  • Loading branch information
ed255 committed May 17, 2022
1 parent f48ea3f commit 5a6a4dd
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 115 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
83 changes: 4 additions & 79 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
22 changes: 13 additions & 9 deletions zkevm-circuits/src/tx_circuit.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -74,12 +78,7 @@ fn random_linear_combine<F: Field>(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<u8, U32>,
) -> Result<Secp256k1Affine, Error> {
fn recover_pk(v: u8, r: &Word, s: &Word, msg_hash: &[u8; 32]) -> Result<Secp256k1Affine, Error> {
let r_be = r.to_be_bytes();
let s_be = s.to_be_bytes();
let mut r = libsecp256k1::curve::Scalar::from_int(0);
Expand Down Expand Up @@ -154,7 +153,11 @@ fn tx_to_sign_data(tx: &Transaction, chain_id: u64) -> Result<SignData, Error> {
.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
Expand Down Expand Up @@ -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;
Expand Down
40 changes: 19 additions & 21 deletions zkevm-circuits/src/tx_circuit/sign_verify.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Naming notes:
// - *_be: Big-Endian bytes
// - *_le: Little-Endian bytes

use crate::{
evm_circuit::util::{not, RandomLinearCombination, Word},
util::Expr,
Expand Down Expand Up @@ -227,7 +231,6 @@ impl<F: FieldExt> SignVerifyConfig<F> {
.collect::<Vec<Expression<F>>>()
.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 =
Expand Down Expand Up @@ -473,11 +476,6 @@ impl<F: FieldExt, const MAX_VERIF: usize> SignVerifyChip<F, MAX_VERIF> {
&self,
ctx: &mut RegionCtx<F>,
chips: &ChipsRef<F, NUMBER_OF_LIMBS, BIT_LEN_LIMB>,
// main_gate: &MainGate<F>,
// range_chip: &RangeChip<F>,
// ecc_chip: &GeneralEccChip<Secp256k1Affine, F, NUMBER_OF_LIMBS, BIT_LEN_LIMB>,
// scalar_chip: &IntegerChip<secp256k1::Fq, F, NUMBER_OF_LIMBS, BIT_LEN_LIMB>,
// ecdsa_chip: &EcdsaChip<Secp256k1Affine, F, NUMBER_OF_LIMBS, BIT_LEN_LIMB>,
sign_data: &SignData,
) -> Result<AssignedECDSA<F>, Error> {
let SignData {
Expand Down Expand Up @@ -940,7 +938,7 @@ mod sign_verify_tests {
fn gen_key_pair(rng: impl RngCore) -> (secp256k1::Fq, Secp256k1Affine) {
// generate a valid signature
let generator = <Secp256k1Affine as PrimeCurveAffine>::generator();
let sk = <Secp256k1Affine as CurveAffine>::ScalarExt::random(rng);
let sk = secp256k1::Fq::random(rng);
let pk = generator * sk;
let pk = pk.to_affine();

Expand All @@ -949,7 +947,7 @@ mod sign_verify_tests {

// Generate a test message hash
fn gen_msg_hash(rng: impl RngCore) -> secp256k1::Fq {
<Secp256k1Affine as CurveAffine>::ScalarExt::random(rng)
secp256k1::Fq::random(rng)
}

// Returns (r, s)
Expand All @@ -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);
Expand All @@ -979,16 +986,7 @@ mod sign_verify_tests {
});
}

let k = 20;
let k = 19;
run::<Fr, MAX_VERIF>(k, txs);
}
}

// Vectors using `XorShiftRng::seed_from_u64(1)`
// sk: 0x771bd7bf6c6414b9370bb8559d46e1cedb479b1836ea3c2e59a54c343b0d0495
// pk: (
// 0x8e31a3586d4c8de89d4e0131223ecfefa4eb76215f68a691ae607757d6256ede,
// 0xc76fdd462294a7eeb8ff3f0f698eb470f32085ba975801dbe446ed8e0b05400b
// )
// pk_hash: d90e2e9d267cbcfd94de06fa7adbe6857c2c733025c0b8938a76beeefc85d6c7
// addr: 0x7adbe6857c2c733025c0b8938a76beeefc85d6c7

0 comments on commit 5a6a4dd

Please sign in to comment.