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

Commit

Permalink
test: all testing passed in sig_circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiWu123 committed Jan 27, 2024
1 parent 3dc90c3 commit 09dbea8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 61 deletions.
12 changes: 6 additions & 6 deletions zkevm-circuits/src/sig_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ where
// in the ecdsa_chip.
let q_keccak = meta.complex_selector();

// FIXME fix the layout
meta.lookup_any("keccak lookup table", |meta| {
// When address is 0, we disable the signature verification by using a dummy pk,
// msg_hash and signature which is not constrained to match msg_hash_rlc nor
Expand All @@ -177,17 +176,18 @@ where
// | -------- | --------------- |
// | 1 | is_address_zero |
// | | pk_rlc |
// | | pk_hash_rlc |
// | | pk_hash_lo |
// | | pk_hash_hi |
let q_keccak = meta.query_selector(q_keccak);
let is_address_zero = meta.query_advice(rlc_column, Rotation::cur());
let is_enable = q_keccak * not::expr(is_address_zero);

let input = [
is_enable.clone(),
is_enable.clone(),
is_enable.clone() * meta.query_advice(rlc_column, Rotation(1)),
is_enable.clone() * 64usize.expr(),
is_enable * meta.query_advice(rlc_column, Rotation(2)),
is_enable.clone() * meta.query_advice(rlc_column, Rotation(2)),
is_enable * meta.query_advice(rlc_column, Rotation(3)),
];
let table = [
meta.query_advice(keccak_table.is_enabled, Rotation::cur()),
Expand Down Expand Up @@ -505,7 +505,7 @@ impl<F: Field + halo2_base::utils::ScalarField> SigCircuit<F> {
|| "pk_hash_hi",
config.rlc_column,
offset + 3,
|| pk_hash.lo().value,
|| pk_hash.hi().value,
)?;
ctx.region
.constrain_equal(pk_hash.hi().cell, pk_cell_hi.cell())?;
Expand Down Expand Up @@ -914,7 +914,7 @@ impl<F: Field + halo2_base::utils::ScalarField> SigCircuit<F> {
for (i, [is_address_zero, pk_rlc, pk_hash_lo, pk_hash_hi]) in
assigned_keccak_values.iter().enumerate()
{
let offset = i * 3;
let offset = i * 4;
self.enable_keccak_lookup(
config,
&mut ctx,
Expand Down
113 changes: 58 additions & 55 deletions zkevm-circuits/src/sig_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ use std::marker::PhantomData;

use crate::sig_circuit::SigCircuit;

use super::utils::LOG_TOTAL_NUM_ROWS;

Check warning on line 19 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Various lints

unused import: `super::utils::LOG_TOTAL_NUM_ROWS`

Check warning on line 19 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Linux Build

unused import: `super::utils::LOG_TOTAL_NUM_ROWS`
use crate::sig_circuit::utils::MAX_NUM_SIG;

Check warning on line 20 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Various lints

unused import: `crate::sig_circuit::utils::MAX_NUM_SIG`

Check warning on line 20 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Linux Build

unused import: `crate::sig_circuit::utils::MAX_NUM_SIG`
use eth_types::{
sign_types::{biguint_to_32bytes_le, recover_pk, SECP256K1_Q},

Check warning on line 22 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Various lints

unused imports: `SECP256K1_Q`, `ToBigEndian`, `ToLittleEndian`, `Word`, `biguint_to_32bytes_le`, `recover_pk`, `word`

Check warning on line 22 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Linux Build

unused imports: `SECP256K1_Q`, `ToBigEndian`, `ToLittleEndian`, `Word`, `biguint_to_32bytes_le`, `recover_pk`, `word`
word, ToBigEndian, ToLittleEndian, Word,
};
use ethers_core::k256::elliptic_curve::PrimeField;

Check warning on line 25 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Various lints

unused import: `ethers_core::k256::elliptic_curve::PrimeField`

Check warning on line 25 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Linux Build

unused import: `ethers_core::k256::elliptic_curve::PrimeField`
use halo2_proofs::halo2curves::secp256k1::Fq;

Check warning on line 26 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Various lints

unused import: `halo2_proofs::halo2curves::secp256k1::Fq`

Check warning on line 26 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Linux Build

unused import: `halo2_proofs::halo2curves::secp256k1::Fq`
use num::{BigUint, Integer};

Check warning on line 27 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Various lints

unused imports: `BigUint`, `Integer`

Check warning on line 27 in zkevm-circuits/src/sig_circuit/test.rs

View workflow job for this annotation

GitHub Actions / Linux Build

unused imports: `BigUint`, `Integer`
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use sha3::{Digest, Keccak256};
use snark_verifier::util::arithmetic::PrimeCurveAffine;

#[test]
fn test_edge_cases() {
use super::utils::LOG_TOTAL_NUM_ROWS;
use eth_types::{
sign_types::{biguint_to_32bytes_le, recover_pk, SECP256K1_Q},
word, ToBigEndian, ToLittleEndian, Word,
};
use ethers_core::k256::elliptic_curve::PrimeField;
use halo2_proofs::halo2curves::secp256k1::Fq;
use num::{BigUint, Integer};
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use snark_verifier::util::arithmetic::PrimeCurveAffine;

let mut rng = XorShiftRng::seed_from_u64(1);

// helper
Expand Down Expand Up @@ -133,58 +135,59 @@ fn test_edge_cases() {
}

#[test]
fn sign_verify() {
use super::utils::LOG_TOTAL_NUM_ROWS;
use crate::sig_circuit::utils::MAX_NUM_SIG;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use sha3::{Digest, Keccak256};
fn sign_verify_zero_msg_hash() {
let mut rng = XorShiftRng::seed_from_u64(1);

// msg_hash == 0
{
log::debug!("testing for msg_hash = 0");
let mut signatures = Vec::new();
log::debug!("testing for msg_hash = 0");
let mut signatures = Vec::new();

let (sk, pk) = gen_key_pair(&mut rng);
let msg = gen_msg(&mut rng);
let msg_hash = secp256k1::Fq::zero();
let (r, s, v) = sign_with_rng(&mut rng, sk, msg_hash);
signatures.push(SignData {
signature: (r, s, v),
pk,
msg: msg.into(),
msg_hash,
});
let (sk, pk) = gen_key_pair(&mut rng);
let msg = gen_msg(&mut rng);
let msg_hash = secp256k1::Fq::zero();
let (r, s, v) = sign_with_rng(&mut rng, sk, msg_hash);
signatures.push(SignData {
signature: (r, s, v),
pk,
msg: msg.into(),
msg_hash,
});

let k = LOG_TOTAL_NUM_ROWS as u32;
run::<Fr>(k, 1, signatures);
let k = LOG_TOTAL_NUM_ROWS as u32;
run::<Fr>(k, 1, signatures);

log::debug!("end of testing for msg_hash = 0");
}
// msg_hash == 1
{
log::debug!("testing for msg_hash = 1");
let mut signatures = Vec::new();
log::debug!("end of testing for msg_hash = 0");
}

let (sk, pk) = gen_key_pair(&mut rng);
let msg = gen_msg(&mut rng);
let msg_hash = secp256k1::Fq::one();
let (r, s, v) = sign_with_rng(&mut rng, sk, msg_hash);
signatures.push(SignData {
signature: (r, s, v),
pk,
msg: msg.into(),
msg_hash,
});
#[test]
fn sign_verify_nonzero_msg_hash() {
let mut rng = XorShiftRng::seed_from_u64(1);

let k = LOG_TOTAL_NUM_ROWS as u32;
run::<Fr>(k, 1, signatures);
log::debug!("testing for msg_hash = 1");
let mut signatures = Vec::new();

let (sk, pk) = gen_key_pair(&mut rng);
let msg = gen_msg(&mut rng);
let msg_hash = secp256k1::Fq::one();
let (r, s, v) = sign_with_rng(&mut rng, sk, msg_hash);
signatures.push(SignData {
signature: (r, s, v),
pk,
msg: msg.into(),
msg_hash,
});

let k = LOG_TOTAL_NUM_ROWS as u32;
run::<Fr>(k, 1, signatures);

log::debug!("end of testing for msg_hash = 1");
}

#[test]
fn sign_verify() {
let mut rng = XorShiftRng::seed_from_u64(1);

log::debug!("end of testing for msg_hash = 1");
}
// random msg_hash
let max_sigs = [1];
let max_sigs = [1, 16, MAX_NUM_SIG];
for max_sig in max_sigs.iter() {
log::debug!("testing for {} signatures", max_sig);
let mut signatures = Vec::new();
Expand Down

0 comments on commit 09dbea8

Please sign in to comment.