Skip to content

Commit

Permalink
Take messages, bytes, and signatures by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Dec 12, 2024
1 parent 6d7f5a7 commit 274f723
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 44 deletions.
12 changes: 6 additions & 6 deletions crates/chia-secp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(())
}
Expand All @@ -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"
Expand All @@ -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(())
}
Expand Down
10 changes: 5 additions & 5 deletions crates/chia-secp/src/secp256k1/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand All @@ -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<Self, Error> {
Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?))
pub fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Error> {
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 {
Expand Down
10 changes: 5 additions & 5 deletions crates/chia-secp/src/secp256k1/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand All @@ -34,17 +34,17 @@ impl K1SecretKey {
self.0.to_bytes().into()
}

pub fn from_bytes(bytes: [u8; 32]) -> Result<Self, Error> {
Ok(Self(SigningKey::from_bytes((&bytes).into())?))
pub fn from_bytes(bytes: &[u8; 32]) -> Result<Self, Error> {
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<K1Signature, Error> {
pub fn sign_prehashed(&self, message_hash: &[u8; 32]) -> Result<K1Signature, Error> {
Ok(K1Signature(
self.0.sign_prehash_recoverable(&message_hash)?.0,
self.0.sign_prehash_recoverable(message_hash)?.0,
))
}
}
6 changes: 3 additions & 3 deletions crates/chia-secp/src/secp256k1/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand All @@ -40,7 +40,7 @@ impl K1Signature {
self.0.to_bytes().into()
}

pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, Error> {
Ok(Self(Signature::from_slice(&bytes)?))
pub fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Error> {
Ok(Self(Signature::from_slice(bytes)?))
}
}
10 changes: 5 additions & 5 deletions crates/chia-secp/src/secp256r1/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand All @@ -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<Self, Error> {
Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?))
pub fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Error> {
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 {
Expand Down
10 changes: 5 additions & 5 deletions crates/chia-secp/src/secp256r1/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand All @@ -34,17 +34,17 @@ impl R1SecretKey {
self.0.to_bytes().into()
}

pub fn from_bytes(bytes: [u8; 32]) -> Result<Self, Error> {
Ok(Self(SigningKey::from_bytes((&bytes).into())?))
pub fn from_bytes(bytes: &[u8; 32]) -> Result<Self, Error> {
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<R1Signature, Error> {
pub fn sign_prehashed(&self, message_hash: &[u8; 32]) -> Result<R1Signature, Error> {
Ok(R1Signature(
self.0.sign_prehash_recoverable(&message_hash)?.0,
self.0.sign_prehash_recoverable(message_hash)?.0,
))
}
}
6 changes: 3 additions & 3 deletions crates/chia-secp/src/secp256r1/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
Self::from_bytes(&u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand All @@ -40,7 +40,7 @@ impl R1Signature {
self.0.to_bytes().into()
}

pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, Error> {
Ok(Self(Signature::from_slice(&bytes)?))
pub fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Error> {
Ok(Self(Signature::from_slice(bytes)?))
}
}
16 changes: 8 additions & 8 deletions crates/clvm-traits/src/from_clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}

Expand All @@ -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()))
}
}

Expand All @@ -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()))
}
}

Expand All @@ -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()))
}
}

Expand Down Expand Up @@ -461,7 +461,7 @@ mod tests {
a,
"a102827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4"
),
Ok(K1PublicKey::from_bytes(bytes).unwrap())
Ok(K1PublicKey::from_bytes(&bytes).unwrap())
);
assert_eq!(
decode::<K1PublicKey>(a, "8568656c6c6f"),
Expand All @@ -478,7 +478,7 @@ mod tests {
a,
"a1037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4"
),
Ok(R1PublicKey::from_bytes(bytes).unwrap())
Ok(R1PublicKey::from_bytes(&bytes).unwrap())
);
assert_eq!(
decode::<R1PublicKey>(a, "8568656c6c6f"),
Expand Down Expand Up @@ -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::<K1Signature>(a, "8568656c6c6f"),
Expand All @@ -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::<K1Signature>(a, "8568656c6c6f"),
Expand Down
8 changes: 4 additions & 4 deletions crates/clvm-traits/src/to_clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 274f723

Please sign in to comment.