Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit a4b959d

Browse files
authored
Update deps (#35)
* Doc fix * Update secp256k1 * Update k256
1 parent 35a1160 commit a4b959d

File tree

5 files changed

+66
-45
lines changed

5 files changed

+66
-45
lines changed

Cargo.toml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ keywords = ["ethereum", "enr", "record", "EIP778", "node"]
99
repository = "https://github.com/rust-ethereum/enr"
1010
categories = ["cryptography::cryptocurrencies"]
1111
license = "MIT"
12-
exclude = [
13-
".gitignore",
14-
".github/*"
15-
]
12+
exclude = [".gitignore", ".github/*"]
1613

1714
[dependencies]
1815
base64 = "0.13"
@@ -23,20 +20,22 @@ log = "0.4.8"
2320
rand = "0.8"
2421
rlp = "0.5"
2522
zeroize = "1.1.0"
26-
sha3 = "0.9"
27-
k256 = { version = "0.8", features = ["ecdsa", "zeroize"], optional = true }
23+
sha3 = "0.10"
24+
k256 = { version = "0.11", features = ["ecdsa"], optional = true }
2825
serde = { version = "1.0.110", optional = true }
2926
ed25519-dalek = { version = "1.0.0-pre.4", optional = true }
30-
c-secp256k1 = { package = "secp256k1", version = "0.20", optional = true, features = ["global-context"] }
27+
secp256k1 = { version = "0.22", optional = true, default-features = false, features = [
28+
"global-context",
29+
] }
3130

3231
[dev-dependencies]
3332
rand_07 = { package = "rand", version = "0.7" }
34-
c-secp256k1 = { package = "secp256k1", features = ["rand-std"], version = "0.20" }
33+
secp256k1 = { features = ["rand-std"], version = "0.22" }
3534

3635
[features]
37-
default = ["serde", "k256" ]
36+
default = ["serde", "k256"]
3837
ed25519 = ["ed25519-dalek"]
39-
rust-secp256k1 = ["c-secp256k1"]
38+
rust-secp256k1 = ["secp256k1"]
4039

4140
[lib]
4241
name = "enr"

src/keys/k256_key.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ use k256::{
88
signature::{DigestVerifier, RandomizedDigestSigner, Signature as _},
99
Signature, SigningKey, VerifyingKey,
1010
},
11-
elliptic_curve::{generic_array::GenericArray, sec1::UntaggedPointSize},
12-
CompressedPoint, EncodedPoint, Secp256k1,
11+
elliptic_curve::{
12+
sec1::{Coordinates, ToEncodedPoint},
13+
subtle::Choice,
14+
DecompressPoint,
15+
},
16+
AffinePoint, CompressedPoint, EncodedPoint,
1317
};
1418
use rand::rngs::OsRng;
1519
use rlp::DecoderError;
@@ -24,7 +28,7 @@ impl EnrKey for SigningKey {
2428

2529
fn sign_v4(&self, msg: &[u8]) -> Result<Vec<u8>, SigningError> {
2630
// take a keccak256 hash then sign.
27-
let digest = Keccak256::new().chain(msg);
31+
let digest = Keccak256::new().chain_update(msg);
2832
let signature: Signature = self
2933
.try_sign_digest_with_rng(&mut OsRng, digest)
3034
.map_err(|_| SigningError::new("failed to sign"))?;
@@ -33,7 +37,7 @@ impl EnrKey for SigningKey {
3337
}
3438

3539
fn public(&self) -> Self::PublicKey {
36-
self.verify_key()
40+
self.verifying_key()
3741
}
3842

3943
fn enr_to_public(content: &BTreeMap<Key, Bytes>) -> Result<Self::PublicKey, DecoderError> {
@@ -58,12 +62,12 @@ impl EnrKeyUnambiguous for SigningKey {
5862

5963
impl EnrPublicKey for VerifyingKey {
6064
type Raw = CompressedPoint;
61-
type RawUncompressed = GenericArray<u8, UntaggedPointSize<Secp256k1>>;
65+
type RawUncompressed = [u8; 64];
6266

6367
fn verify_v4(&self, msg: &[u8], sig: &[u8]) -> bool {
6468
if let Ok(sig) = k256::ecdsa::Signature::try_from(sig) {
6569
return self
66-
.verify_digest(Keccak256::new().chain(msg), &sig)
70+
.verify_digest(Keccak256::new().chain_update(msg), &sig)
6771
.is_ok();
6872
}
6973
false
@@ -75,7 +79,25 @@ impl EnrPublicKey for VerifyingKey {
7579
}
7680

7781
fn encode_uncompressed(&self) -> Self::RawUncompressed {
78-
EncodedPoint::from(self).to_untagged_bytes().unwrap()
82+
let p = EncodedPoint::from(self);
83+
let (x, y) = match p.coordinates() {
84+
Coordinates::Compact { .. } | Coordinates::Identity => unreachable!(),
85+
Coordinates::Compressed { x, y_is_odd } => (
86+
x,
87+
*AffinePoint::decompress(x, Choice::from(u8::from(y_is_odd)))
88+
.unwrap()
89+
.to_encoded_point(false)
90+
.y()
91+
.unwrap(),
92+
),
93+
Coordinates::Uncompressed { x, y } => (x, *y),
94+
};
95+
96+
let mut coords = [0; 64];
97+
coords[..32].copy_from_slice(x);
98+
coords[32..].copy_from_slice(&y);
99+
100+
coords
79101
}
80102

81103
fn enr_key(&self) -> Key {

src/keys/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
mod combined;
1010
#[cfg(feature = "ed25519")]
1111
mod ed25519;
12-
#[cfg(any(feature = "k256", doc))]
12+
#[cfg(any(feature = "k256"))]
1313
mod k256_key;
1414
#[cfg(feature = "rust-secp256k1")]
1515
mod rust_secp256k1;
1616

17-
#[cfg(feature = "rust-secp256k1")]
18-
pub use c_secp256k1;
1917
#[cfg(all(feature = "ed25519", feature = "k256"))]
2018
pub use combined::{CombinedKey, CombinedPublicKey};
2119
#[cfg(feature = "ed25519")]
2220
pub use ed25519_dalek;
23-
#[cfg(any(feature = "k256", doc))]
21+
#[cfg(any(feature = "k256"))]
2422
pub use k256;
23+
#[cfg(feature = "rust-secp256k1")]
24+
pub use secp256k1;
2525

2626
use crate::Key;
2727
use bytes::Bytes;

src/keys/rust_secp256k1.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
use super::{EnrKey, EnrKeyUnambiguous, EnrPublicKey, SigningError};
22
use crate::{digest, Key};
33
use bytes::Bytes;
4-
use c_secp256k1::SECP256K1;
54
use rlp::DecoderError;
5+
use secp256k1::SECP256K1;
66
use std::collections::BTreeMap;
77

88
/// The ENR key that stores the public key in the ENR record.
99
pub const ENR_KEY: &str = "secp256k1";
1010

11-
impl EnrKey for c_secp256k1::SecretKey {
12-
type PublicKey = c_secp256k1::PublicKey;
11+
impl EnrKey for secp256k1::SecretKey {
12+
type PublicKey = secp256k1::PublicKey;
1313

1414
fn sign_v4(&self, msg: &[u8]) -> Result<Vec<u8>, SigningError> {
1515
// take a keccak256 hash then sign.
1616
let hash = digest(msg);
17-
let m = c_secp256k1::Message::from_slice(&hash)
17+
let m = secp256k1::Message::from_slice(&hash)
1818
.map_err(|_| SigningError::new("failed to parse secp256k1 digest"))?;
1919
// serialize to an uncompressed 64 byte vector
20-
Ok(SECP256K1.sign(&m, self).serialize_compact().to_vec())
20+
Ok(SECP256K1.sign_ecdsa(&m, self).serialize_compact().to_vec())
2121
}
2222

2323
fn public(&self) -> Self::PublicKey {
@@ -35,23 +35,23 @@ impl EnrKey for c_secp256k1::SecretKey {
3535
}
3636
}
3737

38-
impl EnrKeyUnambiguous for c_secp256k1::SecretKey {
38+
impl EnrKeyUnambiguous for secp256k1::SecretKey {
3939
fn decode_public(bytes: &[u8]) -> Result<Self::PublicKey, DecoderError> {
4040
// should be encoded in compressed form, i.e 33 byte raw secp256k1 public key
41-
c_secp256k1::PublicKey::from_slice(bytes)
41+
secp256k1::PublicKey::from_slice(bytes)
4242
.map_err(|_| DecoderError::Custom("Invalid Secp256k1 Signature"))
4343
}
4444
}
4545

46-
impl EnrPublicKey for c_secp256k1::PublicKey {
47-
type Raw = [u8; c_secp256k1::constants::PUBLIC_KEY_SIZE];
48-
type RawUncompressed = [u8; c_secp256k1::constants::UNCOMPRESSED_PUBLIC_KEY_SIZE - 1];
46+
impl EnrPublicKey for secp256k1::PublicKey {
47+
type Raw = [u8; secp256k1::constants::PUBLIC_KEY_SIZE];
48+
type RawUncompressed = [u8; secp256k1::constants::UNCOMPRESSED_PUBLIC_KEY_SIZE - 1];
4949

5050
fn verify_v4(&self, msg: &[u8], sig: &[u8]) -> bool {
5151
let msg = digest(msg);
52-
if let Ok(sig) = c_secp256k1::Signature::from_compact(sig) {
53-
if let Ok(msg) = c_secp256k1::Message::from_slice(&msg) {
54-
return SECP256K1.verify(&msg, &sig, self).is_ok();
52+
if let Ok(sig) = secp256k1::ecdsa::Signature::from_compact(sig) {
53+
if let Ok(msg) = secp256k1::Message::from_slice(&msg) {
54+
return SECP256K1.verify_ecdsa(&msg, &sig, self).is_ok();
5555
}
5656
}
5757
false
@@ -62,7 +62,7 @@ impl EnrPublicKey for c_secp256k1::PublicKey {
6262
}
6363

6464
fn encode_uncompressed(&self) -> Self::RawUncompressed {
65-
let mut out = [0_u8; c_secp256k1::constants::UNCOMPRESSED_PUBLIC_KEY_SIZE - 1];
65+
let mut out = [0_u8; secp256k1::constants::UNCOMPRESSED_PUBLIC_KEY_SIZE - 1];
6666
out.copy_from_slice(&self.serialize_uncompressed()[1..]);
6767
out
6868
}

src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ use std::{
199199

200200
pub use builder::EnrBuilder;
201201

202-
#[cfg(feature = "rust-secp256k1")]
203-
pub use keys::c_secp256k1;
204202
#[cfg(feature = "k256")]
205203
pub use keys::k256;
204+
#[cfg(feature = "rust-secp256k1")]
205+
pub use keys::secp256k1;
206206
#[cfg(all(feature = "ed25519", feature = "k256"))]
207207
pub use keys::{ed25519_dalek, CombinedKey, CombinedPublicKey};
208208

@@ -242,13 +242,13 @@ impl<K: EnrKey> Enr<K> {
242242

243243
/// The `NodeId` for the record.
244244
#[must_use]
245-
pub fn node_id(&self) -> NodeId {
245+
pub const fn node_id(&self) -> NodeId {
246246
self.node_id
247247
}
248248

249249
/// The current sequence number of the ENR record.
250250
#[must_use]
251-
pub fn seq(&self) -> u64 {
251+
pub const fn seq(&self) -> u64 {
252252
self.seq
253253
}
254254

@@ -812,7 +812,7 @@ impl<K: EnrKey> FromStr for Enr<K> {
812812
}
813813
}
814814

815-
#[cfg(any(feature = "serde", doc))]
815+
#[cfg(any(feature = "serde"))]
816816
impl<K: EnrKey> Serialize for Enr<K> {
817817
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
818818
where
@@ -822,7 +822,7 @@ impl<K: EnrKey> Serialize for Enr<K> {
822822
}
823823
}
824824

825-
#[cfg(any(feature = "serde", doc))]
825+
#[cfg(any(feature = "serde"))]
826826
impl<'de, K: EnrKey> Deserialize<'de> for Enr<K> {
827827
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
828828
where
@@ -1068,9 +1068,9 @@ mod tests {
10681068

10691069
#[cfg(feature = "rust-secp256k1")]
10701070
#[test]
1071-
fn test_encode_decode_c_secp256k1() {
1072-
let mut rng = c_secp256k1::rand::thread_rng();
1073-
let key = c_secp256k1::SecretKey::new(&mut rng);
1071+
fn test_encode_decode_secp256k1() {
1072+
let mut rng = secp256k1::rand::thread_rng();
1073+
let key = secp256k1::SecretKey::new(&mut rng);
10741074
let ip = Ipv4Addr::new(127, 0, 0, 1);
10751075
let tcp = 3000;
10761076

@@ -1083,7 +1083,7 @@ mod tests {
10831083

10841084
let encoded_enr = rlp::encode(&enr);
10851085

1086-
let decoded_enr = rlp::decode::<Enr<c_secp256k1::SecretKey>>(&encoded_enr).unwrap();
1086+
let decoded_enr = rlp::decode::<Enr<secp256k1::SecretKey>>(&encoded_enr).unwrap();
10871087

10881088
assert_eq!(decoded_enr.id(), Some("v4".into()));
10891089
assert_eq!(decoded_enr.ip4(), Some(ip));

0 commit comments

Comments
 (0)