Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 3d6e79a

Browse files
refactor: update the default sign_data (#560)
* refactor: update the default sign_data * clean keccak_inputs_tx_circuit * chore: clippy fix * add sanity check * fix --------- Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
1 parent 89e7511 commit 3d6e79a

File tree

3 files changed

+65
-81
lines changed

3 files changed

+65
-81
lines changed

bus-mapping/src/circuit_input_builder.rs

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ use eth_types::{
3030
evm_types::OpcodeId,
3131
geth_types,
3232
sign_types::{pk_bytes_le, pk_bytes_swap_endianness, SignData},
33-
Address, GethExecStep, GethExecTrace, ToBigEndian, ToWord, Word, H256, U256,
34-
};
35-
use ethers_core::{
36-
k256::ecdsa::SigningKey,
37-
types::{Bytes, Signature, TransactionRequest},
33+
Address, GethExecStep, GethExecTrace, ToBigEndian, ToWord, Word, H256,
3834
};
3935
use ethers_providers::JsonRpcClient;
4036
pub use execution::{
@@ -44,6 +40,7 @@ pub use execution::{
4440
};
4541
use hex::decode_to_slice;
4642

43+
use eth_types::sign_types::get_dummy_tx;
4744
use ethers_core::utils::keccak256;
4845
pub use input_state_ref::CircuitInputStateRef;
4946
use itertools::Itertools;
@@ -692,45 +689,16 @@ pub fn keccak_inputs(block: &Block, code_db: &CodeDB) -> Result<Vec<Vec<u8>>, Er
692689
/// signature datas.
693690
pub fn keccak_inputs_sign_verify(sigs: &[SignData]) -> Vec<Vec<u8>> {
694691
let mut inputs = Vec::new();
695-
for sig in sigs {
692+
let dummy_sign_data = SignData::default();
693+
for sig in sigs.iter().chain(iter::once(&dummy_sign_data)) {
696694
let pk_le = pk_bytes_le(&sig.pk);
697695
let pk_be = pk_bytes_swap_endianness(&pk_le);
698696
inputs.push(pk_be.to_vec());
699697
inputs.push(sig.msg.to_vec());
700698
}
701-
// Padding signature
702-
let pk_le = pk_bytes_le(&SignData::default().pk);
703-
let pk_be = pk_bytes_swap_endianness(&pk_le);
704-
inputs.push(pk_be.to_vec());
705699
inputs
706700
}
707701

708-
/// Generate a dummy pre-eip155 tx in which
709-
/// (nonce=0, gas=0, gas_price=0, to=0, value=0, data="")
710-
/// using the dummy private key = 1
711-
pub fn get_dummy_tx() -> (TransactionRequest, Signature) {
712-
let mut sk_be_scalar = [0u8; 32];
713-
sk_be_scalar[31] = 1_u8;
714-
715-
let sk = SigningKey::from_bytes(&sk_be_scalar).expect("sign key = 1");
716-
let wallet = ethers_signers::Wallet::from(sk);
717-
718-
let tx = TransactionRequest::new()
719-
.nonce(0)
720-
.gas(0)
721-
.gas_price(U256::zero())
722-
.to(Address::zero())
723-
.value(U256::zero())
724-
.data(Bytes::default());
725-
let sighash: H256 = keccak256(tx.rlp_unsigned()).into();
726-
727-
// FIXME: need to check if this is deterministic which means sig is fixed.
728-
let sig = wallet.sign_hash(sighash);
729-
assert_eq!(sig.v, 28);
730-
731-
(tx, sig)
732-
}
733-
734702
/// Get the tx hash of the dummy tx (nonce=0, gas=0, gas_price=0, to=0, value=0,
735703
/// data="")
736704
pub fn get_dummy_tx_hash() -> H256 {
@@ -857,16 +825,6 @@ pub fn keccak_inputs_tx_circuit(txs: &[geth_types::Transaction]) -> Result<Vec<V
857825
let sign_verify_inputs = keccak_inputs_sign_verify(&sign_datas);
858826
inputs.extend_from_slice(&sign_verify_inputs);
859827

860-
// Since the SignData::default() already includes pk = [1]G which is also the
861-
// one that we use in get_dummy_tx, so we only need to include the tx sign
862-
// hash of the dummy tx.
863-
let dummy_sign_input = {
864-
let (dummy_tx, _) = get_dummy_tx();
865-
// dummy tx is of type pre-eip155
866-
dummy_tx.rlp_unsigned().to_vec()
867-
};
868-
inputs.push(dummy_sign_input);
869-
870828
Ok(inputs)
871829
}
872830

eth-types/src/sign_types.rs

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
//! secp256k1 signature types and helper functions.
22
3-
use crate::{ToBigEndian, Word};
3+
use crate::{
4+
address,
5+
geth_types::{Transaction, TxType},
6+
word, ToBigEndian, Word, H256,
7+
};
48
use ethers_core::{
5-
types::{Address, Bytes},
9+
k256::ecdsa::SigningKey,
10+
types::{Address, Bytes, Signature, TransactionRequest, U256},
611
utils::keccak256,
712
};
813
use halo2_proofs::{
@@ -18,7 +23,6 @@ use halo2_proofs::{
1823
};
1924
use lazy_static::lazy_static;
2025
use num_bigint::BigUint;
21-
use sha3::{Digest, Keccak256};
2226
use subtle::CtOption;
2327

2428
/// Do a secp256k1 signature with a given randomness value.
@@ -61,6 +65,39 @@ pub struct SignData {
6165
pub msg_hash: secp256k1::Fq,
6266
}
6367

68+
/// Generate a dummy pre-eip155 tx in which
69+
/// (nonce=0, gas=0, gas_price=0, to=0, value=0, data="")
70+
/// using the dummy private key = 1
71+
pub fn get_dummy_tx() -> (TransactionRequest, Signature) {
72+
let mut sk_be_scalar = [0u8; 32];
73+
sk_be_scalar[31] = 1_u8;
74+
75+
let sk = SigningKey::from_bytes(&sk_be_scalar).expect("sign key = 1");
76+
let wallet = ethers_signers::Wallet::from(sk);
77+
78+
let tx = TransactionRequest::new()
79+
.nonce(0)
80+
.gas(0)
81+
.gas_price(U256::zero())
82+
.to(Address::zero())
83+
.value(U256::zero())
84+
.data(Bytes::default());
85+
let sighash: H256 = keccak256(tx.rlp_unsigned()).into();
86+
87+
let sig = wallet.sign_hash(sighash);
88+
assert_eq!(sig.v, 28);
89+
assert_eq!(
90+
sig.r,
91+
word!("4faabf49beea23083894651a6f34baaf3dc29b396fb5baf8b8454773f328df61")
92+
);
93+
assert_eq!(
94+
sig.s,
95+
word!("0x75ae2dd5e4e688c9dbc6db7e75bafcb04ea141ca20332be9809a444d541272c1")
96+
);
97+
98+
(tx, sig)
99+
}
100+
64101
impl SignData {
65102
/// Recover address of the signature
66103
pub fn get_addr(&self) -> Address {
@@ -74,37 +111,24 @@ impl SignData {
74111
}
75112

76113
lazy_static! {
77-
// FIXME: use Transaction::dummy().sign_data() instead when we merged the develop branch
114+
/// This is the sign data of default padding tx
78115
static ref SIGN_DATA_DEFAULT: SignData = {
79-
let generator = Secp256k1Affine::generator();
80-
let sk = secp256k1::Fq::one();
81-
let pk = generator * sk;
82-
let pk = pk.to_affine();
83-
let msg = b"1";
84-
// let msg = TransactionRequest::new()
85-
// .nonce(0)
86-
// .gas(0)
87-
// .gas_price(U256::zero())
88-
// .to(Address::zero())
89-
// .value(U256::zero())
90-
// .data(Bytes::default())
91-
// .chain_id(1)
92-
// .rlp().to_vec();
93-
let msg_hash: [u8; 32] = Keccak256::digest(msg)
94-
.as_slice()
95-
.to_vec()
96-
.try_into()
97-
.expect("hash length isn't 32 bytes");
98-
let msg_hash = secp256k1::Fq::from_bytes(&msg_hash).unwrap();
99-
let randomness = secp256k1::Fq::one();
100-
let (sig_r, sig_s, v) = sign(randomness, sk, msg_hash);
101-
102-
SignData {
103-
signature: (sig_r, sig_s, v),
104-
pk,
105-
msg: msg.into(),
106-
msg_hash,
107-
}
116+
let (tx_req, sig) = get_dummy_tx();
117+
let tx = Transaction {
118+
tx_type: TxType::PreEip155,
119+
rlp_unsigned_bytes: tx_req.rlp_unsigned().to_vec(),
120+
rlp_bytes: tx_req.rlp_signed(&sig).to_vec(),
121+
v: sig.v,
122+
r: sig.r,
123+
s: sig.s,
124+
// other fields are irrelevant to get the sign_data()
125+
..Default::default()
126+
};
127+
128+
let sign_data = tx.sign_data().unwrap();
129+
assert_eq!(sign_data.get_addr(), address!("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"));
130+
131+
sign_data
108132
};
109133
}
110134

zkevm-circuits/src/witness/tx.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ use crate::{
1515
Tag::{EndList, EndVector},
1616
},
1717
};
18-
use bus_mapping::circuit_input_builder::{self, get_dummy_tx, get_dummy_tx_hash, TxL1Fee};
18+
use bus_mapping::circuit_input_builder::{self, get_dummy_tx_hash, TxL1Fee};
1919
use eth_types::{
2020
evm_types::gas_utils::tx_data_gas_cost,
2121
geth_types::{TxType, TxType::PreEip155},
22-
sign_types::{biguint_to_32bytes_le, ct_option_ok_or, recover_pk, SignData, SECP256K1_Q},
22+
sign_types::{
23+
biguint_to_32bytes_le, ct_option_ok_or, get_dummy_tx, recover_pk, SignData, SECP256K1_Q,
24+
},
2325
Address, Error, Field, Signature, ToBigEndian, ToLittleEndian, ToScalar, ToWord, Word, H256,
2426
};
2527
use ethers_core::{types::TransactionRequest, utils::keccak256};

0 commit comments

Comments
 (0)