Skip to content

Commit

Permalink
Use global secp256k1 context for tests (#12339)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonatan Chaverri <[email protected]>
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent f034bf2 commit cefc99b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 32 deletions.
9 changes: 3 additions & 6 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ mod tests {
};
use reth_testing_utils::generators::{self, sign_tx_with_key_pair};
use revm_primitives::BLOCKHASH_SERVE_WINDOW;
use secp256k1::{Keypair, Secp256k1};
use secp256k1::Keypair;
use std::collections::HashMap;

fn create_state_provider_with_beacon_root_contract() -> StateProviderTest {
Expand Down Expand Up @@ -1053,8 +1053,7 @@ mod tests {

let mut db = create_state_provider_with_withdrawal_requests_contract();

let secp = Secp256k1::new();
let sender_key_pair = Keypair::new(&secp, &mut generators::rng());
let sender_key_pair = Keypair::new(secp256k1::SECP256K1, &mut generators::rng());
let sender_address = public_key_to_address(sender_key_pair.public_key());

db.insert_account(
Expand Down Expand Up @@ -1131,10 +1130,8 @@ mod tests {
// Create a state provider with the withdrawal requests contract pre-deployed
let mut db = create_state_provider_with_withdrawal_requests_contract();

// Initialize Secp256k1 for key pair generation
let secp = Secp256k1::new();
// Generate a new key pair for the sender
let sender_key_pair = Keypair::new(&secp, &mut generators::rng());
let sender_key_pair = Keypair::new(secp256k1::SECP256K1, &mut generators::rng());
// Get the sender's address from the public key
let sender_address = public_key_to_address(sender_key_pair.public_key());

Expand Down
6 changes: 2 additions & 4 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,8 +1599,7 @@ impl<'a> arbitrary::Arbitrary<'a> for TransactionSigned {
#[allow(unused_mut)]
let mut transaction = Transaction::arbitrary(u)?;

let secp = secp256k1::Secp256k1::new();
let key_pair = secp256k1::Keypair::new(&secp, &mut rand::thread_rng());
let key_pair = secp256k1::Keypair::new(secp256k1::SECP256K1, &mut rand::thread_rng());
let signature = crate::sign_message(
B256::from_slice(&key_pair.secret_bytes()[..]),
transaction.signature_hash(),
Expand Down Expand Up @@ -2244,14 +2243,13 @@ mod tests {
*crate::transaction::PARALLEL_SENDER_RECOVERY_THRESHOLD * 5
)) {
let mut rng =rand::thread_rng();
let secp = secp256k1::Secp256k1::new();
let txes: Vec<TransactionSigned> = txes.into_iter().map(|mut tx| {
if let Some(chain_id) = tx.chain_id() {
// Otherwise we might overflow when calculating `v` on `recalculate_hash`
tx.set_chain_id(chain_id % (u64::MAX / 2 - 36));
}

let key_pair = secp256k1::Keypair::new(&secp, &mut rng);
let key_pair = secp256k1::Keypair::new(secp256k1::SECP256K1, &mut rng);

let signature =
crate::sign_message(B256::from_slice(&key_pair.secret_bytes()[..]), tx.signature_hash()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion testing/testing-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ alloy-consensus.workspace = true
alloy-eips.workspace = true

rand.workspace = true
secp256k1 = { workspace = true, features = ["rand"] }
secp256k1 = { workspace = true, features = ["rand", "global-context"] }

[dev-dependencies]
alloy-eips.workspace = true
12 changes: 4 additions & 8 deletions testing/testing-utils/src/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use reth_primitives::{
proofs, sign_message, Account, BlockBody, Header, Log, Receipt, SealedBlock, SealedHeader,
StorageEntry, Transaction, TransactionSigned, Withdrawals,
};
use secp256k1::{Keypair, Secp256k1};
use secp256k1::Keypair;
use std::{
cmp::{max, min},
collections::{hash_map::DefaultHasher, BTreeMap},
Expand Down Expand Up @@ -141,8 +141,7 @@ pub fn random_signed_tx<R: Rng>(rng: &mut R) -> TransactionSigned {

/// Signs the [Transaction] with a random key pair.
pub fn sign_tx_with_random_key_pair<R: Rng>(rng: &mut R, tx: Transaction) -> TransactionSigned {
let secp = Secp256k1::new();
let key_pair = Keypair::new(&secp, rng);
let key_pair = Keypair::new(secp256k1::SECP256K1, rng);
sign_tx_with_key_pair(key_pair, tx)
}

Expand All @@ -156,8 +155,7 @@ pub fn sign_tx_with_key_pair(key_pair: Keypair, tx: Transaction) -> TransactionS

/// Generates a set of [Keypair]s based on the desired count.
pub fn generate_keys<R: Rng>(rng: &mut R, count: usize) -> Vec<Keypair> {
let secp = Secp256k1::new();
(0..count).map(|_| Keypair::new(&secp, rng)).collect()
(0..count).map(|_| Keypair::new(secp256k1::SECP256K1, rng)).collect()
}

/// Generate a random block filled with signed transactions (generated using
Expand Down Expand Up @@ -462,8 +460,6 @@ mod tests {

#[test]
fn test_sign_message() {
let secp = Secp256k1::new();

let tx = Transaction::Eip1559(TxEip1559 {
chain_id: 1,
nonce: 0x42,
Expand All @@ -478,7 +474,7 @@ mod tests {
let signature_hash = tx.signature_hash();

for _ in 0..100 {
let key_pair = Keypair::new(&secp, &mut rand::thread_rng());
let key_pair = Keypair::new(secp256k1::SECP256K1, &mut rand::thread_rng());

let signature =
sign_message(B256::from_slice(&key_pair.secret_bytes()[..]), signature_hash)
Expand Down
20 changes: 7 additions & 13 deletions testing/testing-utils/src/genesis_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloy_primitives::{Address, Bytes, B256, U256};
use reth_primitives::public_key_to_address;
use secp256k1::{
rand::{thread_rng, RngCore},
Keypair, Secp256k1,
Keypair,
};
use std::{
collections::{hash_map::Entry, BTreeMap, HashMap},
Expand Down Expand Up @@ -70,8 +70,7 @@ impl<'a> GenesisAllocator<'a> {
///
/// Returns the key pair for the account and the account's address.
pub fn new_funded_account(&mut self, balance: U256) -> (Keypair, Address) {
let secp = Secp256k1::new();
let pair = Keypair::new(&secp, &mut self.rng);
let pair = Keypair::new(secp256k1::SECP256K1, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(address, GenesisAccount::default().with_balance(balance));
Expand All @@ -87,8 +86,7 @@ impl<'a> GenesisAllocator<'a> {
balance: U256,
code: Bytes,
) -> (Keypair, Address) {
let secp = Secp256k1::new();
let pair = Keypair::new(&secp, &mut self.rng);
let pair = Keypair::new(secp256k1::SECP256K1, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc
Expand All @@ -105,8 +103,7 @@ impl<'a> GenesisAllocator<'a> {
balance: U256,
storage: BTreeMap<B256, B256>,
) -> (Keypair, Address) {
let secp = Secp256k1::new();
let pair = Keypair::new(&secp, &mut self.rng);
let pair = Keypair::new(secp256k1::SECP256K1, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(
Expand All @@ -125,8 +122,7 @@ impl<'a> GenesisAllocator<'a> {
code: Bytes,
storage: BTreeMap<B256, B256>,
) -> (Keypair, Address) {
let secp = Secp256k1::new();
let pair = Keypair::new(&secp, &mut self.rng);
let pair = Keypair::new(secp256k1::SECP256K1, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(
Expand All @@ -141,8 +137,7 @@ impl<'a> GenesisAllocator<'a> {
///
/// Returns the key pair for the account and the account's address.
pub fn new_account_with_code(&mut self, code: Bytes) -> (Keypair, Address) {
let secp = Secp256k1::new();
let pair = Keypair::new(&secp, &mut self.rng);
let pair = Keypair::new(secp256k1::SECP256K1, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(address, GenesisAccount::default().with_code(Some(code)));
Expand All @@ -162,8 +157,7 @@ impl<'a> GenesisAllocator<'a> {
///
/// Returns the key pair for the account and the account's address.
pub fn add_account(&mut self, account: GenesisAccount) -> Address {
let secp = Secp256k1::new();
let pair = Keypair::new(&secp, &mut self.rng);
let pair = Keypair::new(secp256k1::SECP256K1, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(address, account);
Expand Down

0 comments on commit cefc99b

Please sign in to comment.