From 274f7237d86f7ef1a5bd8a36bed17d77a41716e3 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 12 Dec 2024 10:23:17 -0500 Subject: [PATCH] Take messages, bytes, and signatures by reference --- crates/chia-secp/src/lib.rs | 12 ++++++------ crates/chia-secp/src/secp256k1/public_key.rs | 10 +++++----- crates/chia-secp/src/secp256k1/secret_key.rs | 10 +++++----- crates/chia-secp/src/secp256k1/signature.rs | 6 +++--- crates/chia-secp/src/secp256r1/public_key.rs | 10 +++++----- crates/chia-secp/src/secp256r1/secret_key.rs | 10 +++++----- crates/chia-secp/src/secp256r1/signature.rs | 6 +++--- crates/clvm-traits/src/from_clvm.rs | 16 ++++++++-------- crates/clvm-traits/src/to_clvm.rs | 8 ++++---- 9 files changed, 44 insertions(+), 44 deletions(-) diff --git a/crates/chia-secp/src/lib.rs b/crates/chia-secp/src/lib.rs index 4914e1ace..886fc8869 100644 --- a/crates/chia-secp/src/lib.rs +++ b/crates/chia-secp/src/lib.rs @@ -15,7 +15,7 @@ mod tests { fn test_secp256k1_key() -> anyhow::Result<()> { let mut rng = ChaCha8Rng::seed_from_u64(1337); - let sk = K1SecretKey::from_bytes(rng.gen())?; + let sk = K1SecretKey::from_bytes(&rng.gen())?; assert_eq!( hex::encode(sk.to_bytes()), "ae491886341a539a1ccfaffcc9c78650ad1adc6270620c882b8d29bf6b9bc4cd" @@ -28,13 +28,13 @@ mod tests { ); let message_hash: [u8; 32] = rng.gen(); - let sig = sk.sign_prehashed(message_hash)?; + let sig = sk.sign_prehashed(&message_hash)?; assert_eq!( hex::encode(sig.to_bytes()), "6f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591" ); - assert!(pk.verify_prehashed(message_hash, sig)); + assert!(pk.verify_prehashed(&message_hash, &sig)); Ok(()) } @@ -43,7 +43,7 @@ mod tests { fn test_secp256r1_key() -> anyhow::Result<()> { let mut rng = ChaCha8Rng::seed_from_u64(1337); - let sk = R1SecretKey::from_bytes(rng.gen())?; + let sk = R1SecretKey::from_bytes(&rng.gen())?; assert_eq!( hex::encode(sk.to_bytes()), "ae491886341a539a1ccfaffcc9c78650ad1adc6270620c882b8d29bf6b9bc4cd" @@ -56,13 +56,13 @@ mod tests { ); let message_hash: [u8; 32] = rng.gen(); - let sig = sk.sign_prehashed(message_hash)?; + let sig = sk.sign_prehashed(&message_hash)?; assert_eq!( hex::encode(sig.to_bytes()), "550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228" ); - assert!(pk.verify_prehashed(message_hash, sig)); + assert!(pk.verify_prehashed(&message_hash, &sig)); Ok(()) } diff --git a/crates/chia-secp/src/secp256k1/public_key.rs b/crates/chia-secp/src/secp256k1/public_key.rs index f212e4987..8d9e9f7dd 100644 --- a/crates/chia-secp/src/secp256k1/public_key.rs +++ b/crates/chia-secp/src/secp256k1/public_key.rs @@ -31,7 +31,7 @@ impl fmt::Display for K1PublicKey { #[cfg(feature = "arbitrary")] impl<'a> arbitrary::Arbitrary<'a> for K1PublicKey { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) } } @@ -42,12 +42,12 @@ impl K1PublicKey { self.0.to_encoded_point(true).as_ref().try_into().unwrap() } - pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { - Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?)) + pub fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result { + Ok(Self(VerifyingKey::from_sec1_bytes(bytes)?)) } - pub fn verify_prehashed(&self, message_hash: [u8; 32], signature: K1Signature) -> bool { - self.0.verify_prehash(&message_hash, &signature.0).is_ok() + pub fn verify_prehashed(&self, message_hash: &[u8; 32], signature: &K1Signature) -> bool { + self.0.verify_prehash(message_hash, &signature.0).is_ok() } pub fn fingerprint(&self) -> u32 { diff --git a/crates/chia-secp/src/secp256k1/secret_key.rs b/crates/chia-secp/src/secp256k1/secret_key.rs index 2c286c098..515cdf269 100644 --- a/crates/chia-secp/src/secp256k1/secret_key.rs +++ b/crates/chia-secp/src/secp256k1/secret_key.rs @@ -25,7 +25,7 @@ impl fmt::Debug for K1SecretKey { #[cfg(feature = "arbitrary")] impl<'a> arbitrary::Arbitrary<'a> for K1SecretKey { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) } } @@ -34,17 +34,17 @@ impl K1SecretKey { self.0.to_bytes().into() } - pub fn from_bytes(bytes: [u8; 32]) -> Result { - Ok(Self(SigningKey::from_bytes((&bytes).into())?)) + pub fn from_bytes(bytes: &[u8; 32]) -> Result { + Ok(Self(SigningKey::from_bytes(bytes.into())?)) } pub fn public_key(&self) -> K1PublicKey { K1PublicKey(*self.0.verifying_key()) } - pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result { + pub fn sign_prehashed(&self, message_hash: &[u8; 32]) -> Result { Ok(K1Signature( - self.0.sign_prehash_recoverable(&message_hash)?.0, + self.0.sign_prehash_recoverable(message_hash)?.0, )) } } diff --git a/crates/chia-secp/src/secp256k1/signature.rs b/crates/chia-secp/src/secp256k1/signature.rs index cfa3991c2..18635bb43 100644 --- a/crates/chia-secp/src/secp256k1/signature.rs +++ b/crates/chia-secp/src/secp256k1/signature.rs @@ -29,7 +29,7 @@ impl fmt::Display for K1Signature { #[cfg(feature = "arbitrary")] impl<'a> arbitrary::Arbitrary<'a> for K1Signature { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) } } @@ -40,7 +40,7 @@ impl K1Signature { self.0.to_bytes().into() } - pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { - Ok(Self(Signature::from_slice(&bytes)?)) + pub fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result { + Ok(Self(Signature::from_slice(bytes)?)) } } diff --git a/crates/chia-secp/src/secp256r1/public_key.rs b/crates/chia-secp/src/secp256r1/public_key.rs index 490590432..183774ac9 100644 --- a/crates/chia-secp/src/secp256r1/public_key.rs +++ b/crates/chia-secp/src/secp256r1/public_key.rs @@ -31,7 +31,7 @@ impl fmt::Display for R1PublicKey { #[cfg(feature = "arbitrary")] impl<'a> arbitrary::Arbitrary<'a> for R1PublicKey { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) } } @@ -42,12 +42,12 @@ impl R1PublicKey { self.0.to_encoded_point(true).as_ref().try_into().unwrap() } - pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { - Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?)) + pub fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result { + Ok(Self(VerifyingKey::from_sec1_bytes(bytes)?)) } - pub fn verify_prehashed(&self, message_hash: [u8; 32], signature: R1Signature) -> bool { - self.0.verify_prehash(&message_hash, &signature.0).is_ok() + pub fn verify_prehashed(&self, message_hash: &[u8; 32], signature: &R1Signature) -> bool { + self.0.verify_prehash(message_hash, &signature.0).is_ok() } pub fn fingerprint(&self) -> u32 { diff --git a/crates/chia-secp/src/secp256r1/secret_key.rs b/crates/chia-secp/src/secp256r1/secret_key.rs index 6eb668b98..eb6d10558 100644 --- a/crates/chia-secp/src/secp256r1/secret_key.rs +++ b/crates/chia-secp/src/secp256r1/secret_key.rs @@ -25,7 +25,7 @@ impl fmt::Debug for R1SecretKey { #[cfg(feature = "arbitrary")] impl<'a> arbitrary::Arbitrary<'a> for R1SecretKey { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) } } @@ -34,17 +34,17 @@ impl R1SecretKey { self.0.to_bytes().into() } - pub fn from_bytes(bytes: [u8; 32]) -> Result { - Ok(Self(SigningKey::from_bytes((&bytes).into())?)) + pub fn from_bytes(bytes: &[u8; 32]) -> Result { + Ok(Self(SigningKey::from_bytes(bytes.into())?)) } pub fn public_key(&self) -> R1PublicKey { R1PublicKey(*self.0.verifying_key()) } - pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result { + pub fn sign_prehashed(&self, message_hash: &[u8; 32]) -> Result { Ok(R1Signature( - self.0.sign_prehash_recoverable(&message_hash)?.0, + self.0.sign_prehash_recoverable(message_hash)?.0, )) } } diff --git a/crates/chia-secp/src/secp256r1/signature.rs b/crates/chia-secp/src/secp256r1/signature.rs index cfb4b3b35..e619897ba 100644 --- a/crates/chia-secp/src/secp256r1/signature.rs +++ b/crates/chia-secp/src/secp256r1/signature.rs @@ -29,7 +29,7 @@ impl fmt::Display for R1Signature { #[cfg(feature = "arbitrary")] impl<'a> arbitrary::Arbitrary<'a> for R1Signature { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) } } @@ -40,7 +40,7 @@ impl R1Signature { self.0.to_bytes().into() } - pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { - Ok(Self(Signature::from_slice(&bytes)?)) + pub fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result { + Ok(Self(Signature::from_slice(bytes)?)) } } diff --git a/crates/clvm-traits/src/from_clvm.rs b/crates/clvm-traits/src/from_clvm.rs index 2b34d9e5d..bc57ab10a 100644 --- a/crates/clvm-traits/src/from_clvm.rs +++ b/crates/clvm-traits/src/from_clvm.rs @@ -234,7 +234,7 @@ where expected: Self::SIZE, found: atom.len(), })?; - Self::from_bytes(bytes).map_err(|error| FromClvmError::Custom(error.to_string())) + Self::from_bytes(&bytes).map_err(|error| FromClvmError::Custom(error.to_string())) } } @@ -252,7 +252,7 @@ where expected: Self::SIZE, found: atom.len(), })?; - Self::from_bytes(bytes).map_err(|error| FromClvmError::Custom(error.to_string())) + Self::from_bytes(&bytes).map_err(|error| FromClvmError::Custom(error.to_string())) } } @@ -270,7 +270,7 @@ where expected: Self::SIZE, found: atom.len(), })?; - Self::from_bytes(bytes).map_err(|error| FromClvmError::Custom(error.to_string())) + Self::from_bytes(&bytes).map_err(|error| FromClvmError::Custom(error.to_string())) } } @@ -288,7 +288,7 @@ where expected: Self::SIZE, found: atom.len(), })?; - Self::from_bytes(bytes).map_err(|error| FromClvmError::Custom(error.to_string())) + Self::from_bytes(&bytes).map_err(|error| FromClvmError::Custom(error.to_string())) } } @@ -461,7 +461,7 @@ mod tests { a, "a102827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4" ), - Ok(K1PublicKey::from_bytes(bytes).unwrap()) + Ok(K1PublicKey::from_bytes(&bytes).unwrap()) ); assert_eq!( decode::(a, "8568656c6c6f"), @@ -478,7 +478,7 @@ mod tests { a, "a1037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4" ), - Ok(R1PublicKey::from_bytes(bytes).unwrap()) + Ok(R1PublicKey::from_bytes(&bytes).unwrap()) ); assert_eq!( decode::(a, "8568656c6c6f"), @@ -506,7 +506,7 @@ mod tests { assert_eq!( decode(a, "c0406f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591"), - Ok(K1Signature::from_bytes(bytes).unwrap()) + Ok(K1Signature::from_bytes(&bytes).unwrap()) ); assert_eq!( decode::(a, "8568656c6c6f"), @@ -525,7 +525,7 @@ mod tests { assert_eq!( decode(a, "c040550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228"), - Ok(K1Signature::from_bytes(bytes).unwrap()) + Ok(K1Signature::from_bytes(&bytes).unwrap()) ); assert_eq!( decode::(a, "8568656c6c6f"), diff --git a/crates/clvm-traits/src/to_clvm.rs b/crates/clvm-traits/src/to_clvm.rs index bfb03c008..1ef642d5c 100644 --- a/crates/clvm-traits/src/to_clvm.rs +++ b/crates/clvm-traits/src/to_clvm.rs @@ -394,7 +394,7 @@ mod tests { let a = &mut Allocator::new(); - let k1_pk = K1PublicKey::from_bytes(hex!( + let k1_pk = K1PublicKey::from_bytes(&hex!( "02827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4" )) .unwrap(); @@ -403,7 +403,7 @@ mod tests { Ok("a102827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4".to_string()) ); - let r1_pk = R1PublicKey::from_bytes(hex!( + let r1_pk = R1PublicKey::from_bytes(&hex!( "037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4" )) .unwrap(); @@ -421,7 +421,7 @@ mod tests { let a = &mut Allocator::new(); - let k1_sig = K1Signature::from_bytes(hex!( + let k1_sig = K1Signature::from_bytes(&hex!( "6f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591" )) .unwrap(); @@ -430,7 +430,7 @@ mod tests { Ok("c0406f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591".to_string()) ); - let r1_sig = R1Signature::from_bytes(hex!( + let r1_sig = R1Signature::from_bytes(&hex!( "550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228" )) .unwrap();