Skip to content

Commit 6586c7f

Browse files
committed
Merge branch 'main' into fix/global-variables
2 parents 00b15c3 + 5f305da commit 6586c7f

File tree

18 files changed

+602
-149
lines changed

18 files changed

+602
-149
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ sha3 = { version = "0.10.8", default-features = false }
7777
hashbrown = { version = "0.16", default-features = false }
7878
indexmap = { version = "2.5", default-features = false }
7979
foldhash = { version = "0.2", default-features = false }
80+
rapidhash = { version = "4", default-features = false }
8081
rustc-hash = { version = "2.1", default-features = false }
8182

8283
# misc

crates/core/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ asm-keccak = ["alloy-primitives/asm-keccak"]
6060
native-keccak = ["alloy-primitives/native-keccak"]
6161
sha3-keccak = ["alloy-primitives/sha3-keccak"]
6262
tiny-keccak = ["alloy-primitives/tiny-keccak"]
63+
keccak-cache = ["alloy-primitives/keccak-cache"]
64+
keccak-cache-global = ["alloy-primitives/keccak-cache-global"]
6365

6466
map = ["alloy-primitives/map"]
6567
map-hashbrown = ["alloy-primitives/map-hashbrown"]
6668
map-indexmap = ["alloy-primitives/map-indexmap"]
6769
map-foldhash = ["alloy-primitives/map-foldhash"]
70+
map-rapidhash = ["alloy-primitives/map-rapidhash"]
6871
map-fxhash = ["alloy-primitives/map-fxhash"]
6972

7073
getrandom = ["alloy-primitives/getrandom"]

crates/primitives/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ hashbrown = { workspace = true, optional = true, features = [
8686
] }
8787
indexmap = { workspace = true, optional = true }
8888
foldhash = { workspace = true, optional = true }
89+
rapidhash = { workspace = true, optional = true }
8990
rustc-hash = { workspace = true, optional = true }
9091

9192
# arbitrary
@@ -119,6 +120,7 @@ std = [
119120
"ruint/std",
120121
"alloy-rlp?/std",
121122
"foldhash?/std",
123+
"rapidhash?/std",
122124
"indexmap?/std",
123125
"k256?/std",
124126
"keccak-asm?/std",
@@ -131,6 +133,7 @@ std = [
131133
]
132134
nightly = [
133135
"foldhash?/nightly",
136+
"rapidhash?/nightly",
134137
"hashbrown?/nightly",
135138
"hex/nightly",
136139
"ruint/nightly",
@@ -141,16 +144,19 @@ asm-keccak = ["dep:keccak-asm"]
141144
native-keccak = []
142145
sha3-keccak = ["dep:sha3"]
143146
tiny-keccak = []
147+
keccak-cache = ["std", "dep:rapidhash"]
148+
keccak-cache-global = ["keccak-cache"]
144149

145150
map = ["dep:hashbrown"]
146151
map-hashbrown = ["map"]
147152
map-indexmap = ["map", "dep:indexmap"]
148153
map-foldhash = ["map", "dep:foldhash"]
154+
map-rapidhash = ["map", "dep:rapidhash"]
149155
map-fxhash = ["map", "dep:rustc-hash"]
150156

151157
getrandom = ["dep:getrandom"]
152158
k256 = ["dep:k256"]
153-
rand = ["dep:rand", "getrandom", "ruint/rand-09", "rustc-hash?/rand"]
159+
rand = ["dep:rand", "getrandom", "ruint/rand-09", "rapidhash?/rand", "rustc-hash?/rand"]
154160
rayon = ["dep:rayon", "hashbrown?/rayon", "indexmap?/rayon"]
155161
rkyv = ["dep:rkyv", "ruint/rkyv"]
156162
rlp = ["dep:alloy-rlp", "ruint/alloy-rlp"]

crates/primitives/src/bits/address.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ wrap_fixed_bytes!(
4747
extra_derives: [],
4848
/// An Ethereum address, 20 bytes in length.
4949
///
50-
/// This type is separate from [`B160`](crate::B160) / [`FixedBytes<20>`]
50+
/// This type is separate from [`FixedBytes<20>`]
5151
/// and is declared with the [`wrap_fixed_bytes!`] macro. This allows us
5252
/// to implement address-specific functionality.
5353
///

crates/primitives/src/lib.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
77
#![cfg_attr(not(feature = "std"), no_std)]
88
#![cfg_attr(feature = "nightly", feature(hasher_prefixfree_extras))]
9+
#![cfg_attr(feature = "nightly", feature(core_intrinsics))]
10+
#![cfg_attr(feature = "nightly", feature(likely_unlikely))]
11+
#![cfg_attr(feature = "nightly", allow(internal_features))]
912
#![cfg_attr(docsrs, feature(doc_cfg))]
1013

1114
#[macro_use]
@@ -65,12 +68,10 @@ mod signed;
6568
pub use signed::{BigIntConversionError, ParseSignedError, Sign, Signed};
6669

6770
mod signature;
68-
#[allow(deprecated)]
69-
pub use signature::PrimitiveSignature;
7071
pub use signature::{Signature, SignatureError, normalize_v, to_eip155_v};
7172

7273
pub mod utils;
73-
pub use utils::{KECCAK256_EMPTY, Keccak256, eip191_hash_message, keccak256};
74+
pub use utils::{KECCAK256_EMPTY, Keccak256, eip191_hash_message, keccak256, keccak256_uncached};
7475

7576
#[doc(hidden)] // Use `hex` directly instead!
7677
pub mod hex_literal;
@@ -88,22 +89,6 @@ pub use {
8889
#[doc(no_inline)]
8990
pub use ::hex::serde as serde_hex;
9091

91-
/// 20-byte [fixed byte-array][FixedBytes] type.
92-
///
93-
/// You'll likely want to use [`Address`] instead, as it is a different type
94-
/// from `FixedBytes<20>`, and implements methods useful for working with
95-
/// Ethereum addresses.
96-
///
97-
/// If you are sure you want to use this type, and you don't want the
98-
/// deprecation warning, you can use `aliases::B160`.
99-
#[deprecated(
100-
since = "0.3.2",
101-
note = "you likely want to use `Address` instead. \
102-
`B160` and `Address` are different types, \
103-
see this type's documentation for more."
104-
)]
105-
pub type B160 = FixedBytes<20>;
106-
10792
// Not public API.
10893
#[doc(hidden)]
10994
pub mod private {

crates/primitives/src/map/hasher.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ cfg_if! {
2828
#[doc(no_inline)]
2929
pub use foldhash;
3030

31+
#[cfg(feature = "map-rapidhash")]
32+
#[doc(no_inline)]
33+
pub use rapidhash;
34+
3135
// Default hasher.
3236
cfg_if! {
3337
if #[cfg(feature = "map-foldhash")] {
3438
type DefaultHashBuilderInner = foldhash::fast::RandomState;
39+
} else if #[cfg(feature = "map-rapidhash")] {
40+
type DefaultHashBuilderInner = rapidhash::fast::RandomState;
3541
} else if #[cfg(feature = "map-fxhash")] {
3642
type DefaultHashBuilderInner = FxBuildHasher;
3743
} else if #[cfg(any(feature = "map-hashbrown", not(feature = "std")))] {

crates/primitives/src/signature/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,3 @@ pub use utils::{normalize_v, to_eip155_v};
66

77
mod sig;
88
pub use sig::Signature;
9-
10-
/// Deprecated alias for [`Signature`].
11-
#[deprecated(since = "0.9.0", note = "use Signature instead")]
12-
pub type PrimitiveSignature = Signature;

crates/primitives/src/signature/sig.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,6 @@ impl Signature {
226226
self
227227
}
228228

229-
/// Returns the inner ECDSA signature.
230-
#[cfg(feature = "k256")]
231-
#[deprecated(note = "use `Signature::to_k256` instead")]
232-
#[inline]
233-
pub fn into_inner(self) -> k256::ecdsa::Signature {
234-
self.try_into().expect("signature conversion failed")
235-
}
236-
237229
/// Returns the inner ECDSA signature.
238230
#[cfg(feature = "k256")]
239231
#[inline]
@@ -290,13 +282,6 @@ impl Signature {
290282
k256::ecdsa::RecoveryId::new(self.y_parity, false)
291283
}
292284

293-
#[cfg(feature = "k256")]
294-
#[doc(hidden)]
295-
#[deprecated(note = "use `Signature::recid` instead")]
296-
pub fn recovery_id(&self) -> k256::ecdsa::RecoveryId {
297-
self.recid()
298-
}
299-
300285
/// Recovers an [`Address`] from this signature and the given message by first prefixing and
301286
/// hashing the message according to [EIP-191](crate::eip191_hash_message).
302287
#[cfg(feature = "k256")]

crates/primitives/src/signed/conversions.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{BigIntConversionError, ParseSignedError, Sign, Signed, utils::twos_complement};
22
use alloc::string::String;
33
use core::str::FromStr;
4-
use ruint::{ToUintError, Uint, UintTryFrom};
4+
use ruint::{FromUintError, ToUintError, Uint, UintTryFrom, UintTryTo};
55

66
impl<const BITS: usize, const LIMBS: usize> TryFrom<Uint<BITS, LIMBS>> for Signed<BITS, LIMBS> {
77
type Error = BigIntConversionError;
@@ -28,7 +28,7 @@ impl<const BITS: usize, const LIMBS: usize> TryFrom<Signed<BITS, LIMBS>> for Uin
2828
}
2929
}
3030

31-
/// Conversion between `Signed` of different `BITS` or `LIMBS` length.
31+
/// Conversion from `Signed` of different `BITS` or `LIMBS` length.
3232
impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_SRC: usize>
3333
UintTryFrom<Signed<BITS_SRC, LIMBS_SRC>> for Signed<BITS, LIMBS>
3434
{
@@ -48,6 +48,37 @@ impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_S
4848
}
4949
}
5050

51+
/// Conversion to `Signed` of different `BITS` or `LIMBS` length.
52+
impl<const BITS: usize, const LIMBS: usize, const BITS_TARGET: usize, const LIMBS_TARGET: usize>
53+
UintTryTo<Signed<BITS_TARGET, LIMBS_TARGET>> for Signed<BITS, LIMBS>
54+
{
55+
#[inline]
56+
fn uint_try_to(
57+
&self,
58+
) -> Result<Signed<BITS_TARGET, LIMBS_TARGET>, FromUintError<Signed<BITS_TARGET, LIMBS_TARGET>>>
59+
{
60+
let (sign, abs) = self.into_sign_and_abs();
61+
let resized = Signed::<BITS_TARGET, LIMBS_TARGET>::from_raw(
62+
Uint::uint_try_to(&abs).map_err(|e| match e {
63+
FromUintError::Overflow(b, t, v) => {
64+
FromUintError::Overflow(b, Signed(t), Signed(v))
65+
}
66+
})?,
67+
);
68+
if resized.is_negative() {
69+
return Err(FromUintError::Overflow(BITS_TARGET, resized, Signed::MAX));
70+
}
71+
Ok(match sign {
72+
Sign::Negative => resized.checked_neg().ok_or(FromUintError::Overflow(
73+
BITS_TARGET,
74+
resized,
75+
Signed::MAX,
76+
))?,
77+
Sign::Positive => resized,
78+
})
79+
}
80+
}
81+
5182
/// Conversion from positive `Signed` to `Uint` of different `BITS` or `LIMBS` length.
5283
impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_SRC: usize>
5384
UintTryFrom<Signed<BITS_SRC, LIMBS_SRC>> for Uint<BITS, LIMBS>
@@ -61,6 +92,22 @@ impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_S
6192
}
6293
}
6394

95+
/// Conversion to `Uint` from positive `Signed` of different `BITS` or `LIMBS` length.
96+
impl<const BITS: usize, const LIMBS: usize, const BITS_TARGET: usize, const LIMBS_TARGET: usize>
97+
UintTryTo<Uint<BITS_TARGET, LIMBS_TARGET>> for Signed<BITS, LIMBS>
98+
{
99+
#[inline]
100+
fn uint_try_to(
101+
&self,
102+
) -> Result<Uint<BITS_TARGET, LIMBS_TARGET>, FromUintError<Uint<BITS_TARGET, LIMBS_TARGET>>>
103+
{
104+
if self.is_negative() {
105+
return Err(FromUintError::Overflow(BITS_TARGET, Uint::ZERO, Uint::MAX));
106+
}
107+
Uint::uint_try_to(&self.into_raw())
108+
}
109+
}
110+
64111
/// Conversion from `Uint` to positive `Signed` of different `BITS` or `LIMBS` length.
65112
impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_SRC: usize>
66113
UintTryFrom<Uint<BITS_SRC, LIMBS_SRC>> for Signed<BITS, LIMBS>
@@ -76,6 +123,29 @@ impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_S
76123
}
77124
}
78125

126+
/// Conversion to positive `Signed` from `Uint` of different `BITS` or `LIMBS` length.
127+
impl<const BITS: usize, const LIMBS: usize, const BITS_TARGET: usize, const LIMBS_TARGET: usize>
128+
UintTryTo<Signed<BITS_TARGET, LIMBS_TARGET>> for Uint<BITS, LIMBS>
129+
{
130+
#[inline]
131+
fn uint_try_to(
132+
&self,
133+
) -> Result<Signed<BITS_TARGET, LIMBS_TARGET>, FromUintError<Signed<BITS_TARGET, LIMBS_TARGET>>>
134+
{
135+
let resized = Signed::<BITS_TARGET, LIMBS_TARGET>::from_raw(
136+
Self::uint_try_to(self).map_err(|e| match e {
137+
FromUintError::Overflow(b, t, v) => {
138+
FromUintError::Overflow(b, Signed(t), Signed(v))
139+
}
140+
})?,
141+
);
142+
if resized.is_negative() {
143+
return Err(FromUintError::Overflow(BITS_TARGET, resized, Signed::MAX));
144+
}
145+
Ok(resized)
146+
}
147+
}
148+
79149
fn signed_err<const BITS: usize, const LIMBS: usize>(
80150
err: ToUintError<Uint<BITS, LIMBS>>,
81151
) -> ToUintError<Signed<BITS, LIMBS>> {

crates/primitives/src/signed/int.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,28 +1692,39 @@ mod tests {
16921692
let m_i256 = I256::unchecked_from(-4);
16931693
let m_i24 = I24::from(m_i256);
16941694
assert_eq!(m_i24, I24::from_dec_str("-4").unwrap());
1695+
assert_eq!(m_i24.to::<I256>(), m_i256);
16951696
let m_i56 = I56::from(m_i24);
16961697
assert_eq!(m_i56, I56::from_dec_str("-4").unwrap());
1698+
assert_eq!(m_i56.to::<I24>(), m_i24);
16971699
let m_i128 = I128::from(m_i56);
16981700
assert_eq!(m_i128, I128::from_dec_str("-4").unwrap());
1701+
assert_eq!(m_i128.to::<I56>(), m_i56);
16991702
let m_i96 = I96::from(m_i128);
17001703
assert_eq!(m_i96, I96::from_dec_str("-4").unwrap());
1704+
assert_eq!(m_i96.to::<I128>(), m_i128);
17011705

17021706
// convert positive signed to unsigned
17031707
assert_eq!(U24::from(I24::from_hex_str("0x7FFFFF").unwrap()), U24::from(0x7FFFFF));
1708+
assert_eq!(I24::from_hex_str("0x7FFFFF").unwrap().to::<U24>(), U24::from(0x7FFFFF));
17041709

17051710
// convert unsigned to positive signed
17061711
assert_eq!(I24::from(U24::from(0x7FFFFF)), I24::from_hex_str("0x7FFFFF").unwrap());
1712+
assert_eq!(U24::from(0x7FFFFF).to::<I24>(), I24::from_hex_str("0x7FFFFF").unwrap());
17071713
assert_eq!(I24::from(U96::from(0x7FFFFF)), I24::from_hex_str("0x7FFFFF").unwrap());
1714+
assert_eq!(U96::from(0x7FFFFF).to::<I24>(), I24::from_hex_str("0x7FFFFF").unwrap());
17081715

17091716
// can't convert negative signed to unsigned
17101717
assert!(U24::uint_try_from(m_i24).is_err());
1718+
assert!(<I24 as UintTryTo<U24>>::uint_try_to(&m_i24).is_err());
17111719

17121720
// can't convert unsigned to positive signed if too large
17131721
assert!(I24::uint_try_from(U24::from(0x800000)).is_err());
1722+
assert!(<U24 as UintTryTo<I24>>::uint_try_to(&U24::from(0x800000)).is_err());
17141723

17151724
// out-of-bounds conversions
17161725
assert!(I24::uint_try_from(I128::MIN).is_err());
1726+
assert!(<I128 as UintTryTo<I24>>::uint_try_to(&I128::MIN).is_err());
17171727
assert!(I24::uint_try_from(I128::MAX).is_err());
1728+
assert!(<I128 as UintTryTo<I24>>::uint_try_to(&I128::MAX).is_err());
17181729
}
17191730
}

0 commit comments

Comments
 (0)