From bb0681ccb3d1b33ec771271caea7319b00b54c85 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Fri, 19 Jul 2024 12:37:20 -0400 Subject: [PATCH] fix(eips): make SignedAuthorizationList arbitrary less fallible --- crates/eips/Cargo.toml | 2 ++ crates/eips/src/eip7702/auth_list.rs | 21 +++++++++++++++------ crates/eips/src/lib.rs | 5 +++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/crates/eips/Cargo.toml b/crates/eips/Cargo.toml index 4c2a1866ca5..aaff60fa33d 100644 --- a/crates/eips/Cargo.toml +++ b/crates/eips/Cargo.toml @@ -41,6 +41,7 @@ arbitrary = { workspace = true, features = ["derive"], optional = true } # for signed authorization list arbitrary k256 = { workspace = true, optional = true } +rand = { workspace = true, optional = true } [dev-dependencies] alloy-primitives = { workspace = true, features = [ @@ -80,6 +81,7 @@ arbitrary = [ "std", "kzg-sidecar", "dep:arbitrary", + "dep:rand", "alloy-primitives/arbitrary", "alloy-serde?/arbitrary", ] diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index d7ef4142639..64280012b5b 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -177,10 +177,15 @@ impl Deref for SignedAuthorization { #[cfg(all(any(test, feature = "arbitrary"), feature = "k256"))] impl<'a> arbitrary::Arbitrary<'a> for SignedAuthorization { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - use k256::ecdsa::{signature::hazmat::PrehashSigner, SigningKey}; - let key_bytes = u.arbitrary::<[u8; 32]>()?; - let signing_key = SigningKey::from_bytes(&key_bytes.into()) - .map_err(|_| arbitrary::Error::IncorrectFormat)?; + use k256::{ + ecdsa::{signature::hazmat::PrehashSigner, SigningKey}, + NonZeroScalar, + }; + use rand::{rngs::StdRng, SeedableRng}; + + let rng_seed = u.arbitrary::<[u8; 32]>()?; + let mut rand_gen = StdRng::from_seed(rng_seed); + let signing_key: SigningKey = NonZeroScalar::random(&mut rand_gen).into(); let inner = u.arbitrary::()?; let signature_hash = inner.signature_hash(); @@ -307,7 +312,6 @@ impl Deref for OptionalNonce { mod tests { use super::*; use alloy_primitives::{hex, Signature}; - use arbitrary::Arbitrary; use core::str::FromStr; fn test_encode_decode_roundtrip(auth: Authorization) { @@ -367,10 +371,15 @@ mod tests { assert_eq!(decoded, auth); } - #[cfg(feature = "k256")] + #[cfg(all(feature = "arbitrary", feature = "k256"))] #[test] fn test_arbitrary_auth() { + use arbitrary::Arbitrary; let mut unstructured = arbitrary::Unstructured::new(b"unstructured auth"); + // try this multiple times + let _auth = SignedAuthorization::arbitrary(&mut unstructured).unwrap(); + let _auth = SignedAuthorization::arbitrary(&mut unstructured).unwrap(); + let _auth = SignedAuthorization::arbitrary(&mut unstructured).unwrap(); let _auth = SignedAuthorization::arbitrary(&mut unstructured).unwrap(); } } diff --git a/crates/eips/src/lib.rs b/crates/eips/src/lib.rs index 393ba4f343e..b1111f8eeb5 100644 --- a/crates/eips/src/lib.rs +++ b/crates/eips/src/lib.rs @@ -11,6 +11,11 @@ #[macro_use] extern crate alloc; +// To ensure no unused imports, since signed auth list requires arbitrary _and_ k256 features, but +// is only enabled using the `arbitrary` feature. +#[cfg(all(not(feature = "k256"), feature = "arbitrary"))] +use rand as _; + pub mod eip1559; pub use eip1559::calc_next_block_base_fee;