Skip to content

Commit

Permalink
chore(deps): bump secp256k1 to 0.10
Browse files Browse the repository at this point in the history
- `KeyPair` -> `Keypair`
- `Message::from_slice` -> `Message::from_digest_slice`
  • Loading branch information
DaniPopes committed Mar 6, 2024
1 parent e42fb32 commit 127a7b4
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 45 deletions.
30 changes: 15 additions & 15 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ jsonrpsee-core = { version = "0.20" }
jsonrpsee-types = { version = "0.20" }

# crypto
secp256k1 = { version = "0.27.0", default-features = false, features = [
secp256k1 = { version = "0.28", default-features = false, features = [
"global-context",
"rand-std",
"recovery",
Expand Down
14 changes: 7 additions & 7 deletions crates/interfaces/src/test_utils/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reth_primitives::{
SealedHeader, StorageEntry, Transaction, TransactionKind, TransactionSigned, TxLegacy, B256,
U256,
};
use secp256k1::{KeyPair, Secp256k1};
use secp256k1::{Keypair, Secp256k1};
use std::{
cmp::{max, min},
collections::{hash_map::DefaultHasher, BTreeMap},
Expand Down Expand Up @@ -92,22 +92,22 @@ pub fn random_tx<R: Rng>(rng: &mut R) -> Transaction {
/// - There is no guarantee that the nonce is not used twice for the same account
pub fn random_signed_tx<R: Rng>(rng: &mut R) -> TransactionSigned {
let secp = Secp256k1::new();
let key_pair = KeyPair::new(&secp, rng);
let key_pair = Keypair::new(&secp, rng);
let tx = random_tx(rng);
sign_tx_with_key_pair(key_pair, tx)
}

/// Signs the [Transaction] with the given key pair.
pub fn sign_tx_with_key_pair(key_pair: KeyPair, tx: Transaction) -> TransactionSigned {
pub fn sign_tx_with_key_pair(key_pair: Keypair, tx: Transaction) -> TransactionSigned {
let signature =
sign_message(B256::from_slice(&key_pair.secret_bytes()[..]), tx.signature_hash()).unwrap();
TransactionSigned::from_transaction_and_signature(tx, signature)
}

/// Generates a set of [KeyPair]s based on the desired count.
pub fn generate_keys<R: Rng>(rng: &mut R, count: usize) -> Vec<KeyPair> {
/// 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(&secp, rng)).collect()
}

/// Generate a random block filled with signed transactions (generated using
Expand Down Expand Up @@ -405,7 +405,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(&secp, &mut rand::thread_rng());

let signature =
sign_message(B256::from_slice(&key_pair.secret_bytes()[..]), signature_hash)
Expand Down
15 changes: 9 additions & 6 deletions crates/net/ecies/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl ECIES {
let msg = x ^ self.nonce;
let (rec_id, sig) = SECP256K1
.sign_ecdsa_recoverable(
&secp256k1::Message::from_slice(msg.as_slice()).unwrap(),
&secp256k1::Message::from_digest_slice(msg.as_slice()).unwrap(),
&self.ephemeral_secret_key,
)
.serialize_compact();
Expand Down Expand Up @@ -324,10 +324,13 @@ impl ECIES {
self.remote_nonce = Some(data.get_next()?.ok_or(ECIESErrorImpl::InvalidAuthData)?);

let x = ecdh_x(&self.remote_public_key.unwrap(), &self.secret_key);
self.remote_ephemeral_public_key = Some(SECP256K1.recover_ecdsa(
&secp256k1::Message::from_slice((x ^ self.remote_nonce.unwrap()).as_ref()).unwrap(),
&signature,
)?);
self.remote_ephemeral_public_key = Some(
SECP256K1.recover_ecdsa(
&secp256k1::Message::from_digest_slice((x ^ self.remote_nonce.unwrap()).as_ref())
.unwrap(),
&signature,
)?,
);
self.ephemeral_shared_secret =
Some(ecdh_x(&self.remote_ephemeral_public_key.unwrap(), &self.ephemeral_secret_key));

Expand Down Expand Up @@ -483,7 +486,7 @@ impl ECIES {
let tag = self.egress_mac.as_mut().unwrap().digest();

out.reserve(ECIES::header_len());
out.extend_from_slice(&header);
out.extend_from_slice(&header[..]);
out.extend_from_slice(tag.as_slice());
}

Expand Down
24 changes: 12 additions & 12 deletions crates/primitives/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod allocator {
use alloy_genesis::GenesisAccount;
use secp256k1::{
rand::{thread_rng, RngCore},
KeyPair, Secp256k1,
Keypair, Secp256k1,
};
use std::collections::{hash_map::Entry, HashMap};

Expand Down Expand Up @@ -73,9 +73,9 @@ mod allocator {
/// Add a funded account to the genesis alloc.
///
/// Returns the key pair for the account and the account's address.
pub fn new_funded_account(&mut self, balance: U256) -> (KeyPair, 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(&secp, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(address, GenesisAccount::default().with_balance(balance));
Expand All @@ -90,9 +90,9 @@ mod allocator {
&mut self,
balance: U256,
code: Bytes,
) -> (KeyPair, Address) {
) -> (Keypair, Address) {
let secp = Secp256k1::new();
let pair = KeyPair::new(&secp, &mut self.rng);
let pair = Keypair::new(&secp, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(
Expand All @@ -110,9 +110,9 @@ mod allocator {
&mut self,
balance: U256,
storage: HashMap<B256, B256>,
) -> (KeyPair, Address) {
) -> (Keypair, Address) {
let secp = Secp256k1::new();
let pair = KeyPair::new(&secp, &mut self.rng);
let pair = Keypair::new(&secp, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(
Expand All @@ -130,9 +130,9 @@ mod allocator {
&mut self,
code: Bytes,
storage: HashMap<B256, B256>,
) -> (KeyPair, Address) {
) -> (Keypair, Address) {
let secp = Secp256k1::new();
let pair = KeyPair::new(&secp, &mut self.rng);
let pair = Keypair::new(&secp, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(
Expand All @@ -146,9 +146,9 @@ mod allocator {
/// Adds an account with code to the genesis alloc.
///
/// Returns the key pair for the account and the account's address.
pub fn new_account_with_code(&mut self, code: Bytes) -> (KeyPair, 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(&secp, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(address, GenesisAccount::default().with_code(Some(code)));
Expand All @@ -169,7 +169,7 @@ mod allocator {
/// 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(&secp, &mut self.rng);
let address = public_key_to_address(pair.public_key());

self.alloc.insert(address, account);
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ mod tests {
use alloy_primitives::{address, b256, bytes};
use alloy_rlp::{Decodable, Encodable, Error as RlpError};
use bytes::BytesMut;
use secp256k1::{KeyPair, Secp256k1};
use secp256k1::{Keypair, Secp256k1};
use std::str::FromStr;

#[test]
Expand Down Expand Up @@ -1907,7 +1907,7 @@ mod tests {
tx.set_chain_id(chain_id % (u64::MAX / 2 - 36));
}

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

let signature =
sign_message(B256::from_slice(&key_pair.secret_bytes()[..]), tx.signature_hash()).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/transaction/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ pub(crate) mod secp256k1 {
let sig =
RecoverableSignature::from_compact(&sig[0..64], RecoveryId::from_i32(sig[64] as i32)?)?;

let public = SECP256K1.recover_ecdsa(&Message::from_slice(&msg[..32])?, &sig)?;
let public = SECP256K1.recover_ecdsa(&Message::from_digest_slice(&msg[..32])?, &sig)?;
Ok(public_key_to_address(public))
}

/// Signs message with the given secret key.
/// Returns the corresponding signature.
pub fn sign_message(secret: B256, message: B256) -> Result<Signature, secp256k1::Error> {
let sec = SecretKey::from_slice(secret.as_ref())?;
let s = SECP256K1.sign_ecdsa_recoverable(&Message::from_slice(&message[..])?, &sec);
let s = SECP256K1.sign_ecdsa_recoverable(&Message::from_digest_slice(&message[..])?, &sec);
let (rec_id, data) = s.serialize_compact();

let signature = Signature {
Expand Down

0 comments on commit 127a7b4

Please sign in to comment.