From c5cc8cb0552a3aeb77335c7791d5931b814a7da9 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 12 Dec 2024 09:23:31 -0500 Subject: [PATCH 1/5] Add secp256k1 and secp256r1 types --- Cargo.lock | 16 +++ Cargo.toml | 9 +- crates/chia-secp/Cargo.toml | 27 ++++ crates/chia-secp/src/lib.rs | 69 ++++++++++ crates/chia-secp/src/secp256k1.rs | 7 ++ crates/chia-secp/src/secp256k1/public_key.rs | 59 +++++++++ crates/chia-secp/src/secp256k1/secret_key.rs | 50 ++++++++ crates/chia-secp/src/secp256k1/signature.rs | 46 +++++++ crates/chia-secp/src/secp256r1.rs | 7 ++ crates/chia-secp/src/secp256r1/public_key.rs | 59 +++++++++ crates/chia-secp/src/secp256r1/secret_key.rs | 50 ++++++++ crates/chia-secp/src/secp256r1/signature.rs | 46 +++++++ crates/clvm-traits/Cargo.toml | 2 + crates/clvm-traits/src/from_clvm.rs | 126 +++++++++++++++++++ crates/clvm-traits/src/to_clvm.rs | 94 ++++++++++++++ 15 files changed, 666 insertions(+), 1 deletion(-) create mode 100644 crates/chia-secp/Cargo.toml create mode 100644 crates/chia-secp/src/lib.rs create mode 100644 crates/chia-secp/src/secp256k1.rs create mode 100644 crates/chia-secp/src/secp256k1/public_key.rs create mode 100644 crates/chia-secp/src/secp256k1/secret_key.rs create mode 100644 crates/chia-secp/src/secp256k1/signature.rs create mode 100644 crates/chia-secp/src/secp256r1.rs create mode 100644 crates/chia-secp/src/secp256r1/public_key.rs create mode 100644 crates/chia-secp/src/secp256r1/secret_key.rs create mode 100644 crates/chia-secp/src/secp256r1/signature.rs diff --git a/Cargo.lock b/Cargo.lock index 326f26e17..58d9bd5c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -278,6 +278,7 @@ dependencies = [ "chia-consensus", "chia-protocol", "chia-puzzles", + "chia-secp", "chia-sha2", "chia-ssl", "chia-traits 0.15.0", @@ -444,6 +445,20 @@ dependencies = [ "libfuzzer-sys", ] +[[package]] +name = "chia-secp" +version = "0.16.0" +dependencies = [ + "anyhow", + "arbitrary", + "chia-sha2", + "hex", + "k256", + "p256", + "rand", + "rand_chacha", +] + [[package]] name = "chia-sha2" version = "0.15.0" @@ -641,6 +656,7 @@ name = "clvm-traits" version = "0.16.0" dependencies = [ "chia-bls 0.16.0", + "chia-secp", "clvm-derive", "clvmr", "hex", diff --git a/Cargo.toml b/Cargo.toml index 9246114f0..15def5a9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ implicit_hasher = "allow" [dependencies] chia-bls = { workspace = true, optional = true } +chia-secp = { workspace = true, optional = true } chia-client = { workspace = true, optional = true } chia-consensus = { workspace = true, optional = true } chia-protocol = { workspace = true, optional = true } @@ -71,6 +72,7 @@ ignored = ["clvmr"] [features] default = [ "bls", + "secp", "client", "consensus", "protocol", @@ -82,7 +84,8 @@ default = [ "clvm-utils" ] -bls = ["dep:chia-bls"] +bls = ["dep:chia-bls", "clvm-traits/chia-bls"] +secp = ["dep:chia-secp", "clvm-traits/chia-secp"] client = ["dep:chia-client"] consensus = ["dep:chia-consensus"] protocol = ["dep:chia-protocol"] @@ -105,6 +108,7 @@ chia-bls = { path = "./crates/chia-bls", version = "0.16.0" } chia-client = { path = "./crates/chia-client", version = "0.16.0" } chia-consensus = { path = "./crates/chia-consensus", version = "0.16.0" } chia-protocol = { path = "./crates/chia-protocol", version = "0.16.0" } +chia-secp = { path = "./crates/chia-secp", version = "0.16.0" } chia-ssl = { path = "./crates/chia-ssl", version = "0.11.0" } chia-traits = { path = "./crates/chia-traits", version = "0.15.0" } chia-puzzles = { path = "./crates/chia-puzzles", version = "0.16.0" } @@ -153,3 +157,6 @@ blocking-threadpool = "1.0.1" libfuzzer-sys = "0.4" wasm-bindgen = "0.2.95" openssl = "0.10.68" +k256 = "0.13.4" +p256 = "0.13.2" +rand_chacha = "0.3.1" diff --git a/crates/chia-secp/Cargo.toml b/crates/chia-secp/Cargo.toml new file mode 100644 index 000000000..484a8c757 --- /dev/null +++ b/crates/chia-secp/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "chia-secp" +version = "0.16.0" +edition = "2021" +license = "Apache-2.0" +description = "Secp256k1 and secp256r1 types for Chia" +authors = ["Brandon Haggstrom "] +homepage = "https://github.com/Chia-Network/chia_rs" +repository = "https://github.com/Chia-Network/chia_rs" + +[lints] +workspace = true + +[features] +arbitrary = ["dep:arbitrary"] + +[dependencies] +arbitrary = { workspace = true, optional = true } +k256 = { workspace = true } +p256 = { workspace = true } +hex = { workspace = true } +chia-sha2= { workspace = true } + +[dev-dependencies] +rand = { workspace = true } +rand_chacha = { workspace = true } +anyhow = { workspace = true } diff --git a/crates/chia-secp/src/lib.rs b/crates/chia-secp/src/lib.rs new file mode 100644 index 000000000..25ec6e548 --- /dev/null +++ b/crates/chia-secp/src/lib.rs @@ -0,0 +1,69 @@ +mod secp256k1; +mod secp256r1; + +pub use secp256k1::*; +pub use secp256r1::*; + +#[cfg(test)] +mod tests { + use rand::{Rng, SeedableRng}; + use rand_chacha::ChaCha8Rng; + + use super::*; + + #[test] + fn test_secp256k1_key() -> anyhow::Result<()> { + let mut rng = ChaCha8Rng::seed_from_u64(1337); + + let sk = Secp256k1SecretKey::from_bytes(rng.gen())?; + assert_eq!( + hex::encode(sk.to_bytes()), + "ae491886341a539a1ccfaffcc9c78650ad1adc6270620c882b8d29bf6b9bc4cd" + ); + + let pk = sk.public_key(); + assert_eq!( + hex::encode(pk.to_bytes()), + "02827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4" + ); + + let message_hash: [u8; 32] = rng.gen(); + let sig = sk.sign_prehashed(message_hash)?; + assert_eq!( + hex::encode(sig.to_bytes()), + "6f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591" + ); + + assert!(pk.verify_prehashed(message_hash, sig)); + + Ok(()) + } + + #[test] + fn test_secp256r1_key() -> anyhow::Result<()> { + let mut rng = ChaCha8Rng::seed_from_u64(1337); + + let sk = Secp256r1SecretKey::from_bytes(rng.gen())?; + assert_eq!( + hex::encode(sk.to_bytes()), + "ae491886341a539a1ccfaffcc9c78650ad1adc6270620c882b8d29bf6b9bc4cd" + ); + + let pk = sk.public_key(); + assert_eq!( + hex::encode(pk.to_bytes()), + "037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4" + ); + + let message_hash: [u8; 32] = rng.gen(); + let sig = sk.sign_prehashed(message_hash)?; + assert_eq!( + hex::encode(sig.to_bytes()), + "550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228" + ); + + assert!(pk.verify_prehashed(message_hash, sig)); + + Ok(()) + } +} diff --git a/crates/chia-secp/src/secp256k1.rs b/crates/chia-secp/src/secp256k1.rs new file mode 100644 index 000000000..bff7b3cd2 --- /dev/null +++ b/crates/chia-secp/src/secp256k1.rs @@ -0,0 +1,7 @@ +mod public_key; +mod secret_key; +mod signature; + +pub use public_key::*; +pub use secret_key::*; +pub use signature::*; diff --git a/crates/chia-secp/src/secp256k1/public_key.rs b/crates/chia-secp/src/secp256k1/public_key.rs new file mode 100644 index 000000000..9d4e877fb --- /dev/null +++ b/crates/chia-secp/src/secp256k1/public_key.rs @@ -0,0 +1,59 @@ +use std::fmt; +use std::hash::{Hash, Hasher}; + +use chia_sha2::Sha256; +use k256::ecdsa::signature::hazmat::PrehashVerifier; +use k256::ecdsa::{Error, VerifyingKey}; + +use super::Secp256k1Signature; + +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct Secp256k1PublicKey(pub(crate) VerifyingKey); + +impl Hash for Secp256k1PublicKey { + fn hash(&self, state: &mut H) { + self.to_bytes().hash(state); + } +} + +impl fmt::Debug for Secp256k1PublicKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Secp256k1PublicKey({self})") + } +} + +impl fmt::Display for Secp256k1PublicKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", hex::encode(self.to_bytes())) + } +} + +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for Secp256k1PublicKey { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + } +} + +impl Secp256k1PublicKey { + pub const SIZE: usize = 33; + + pub fn to_bytes(&self) -> [u8; Self::SIZE] { + 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 verify_prehashed(&self, message_hash: [u8; 32], signature: Secp256k1Signature) -> bool { + self.0.verify_prehash(&message_hash, &signature.0).is_ok() + } + + pub fn fingerprint(&self) -> u32 { + let mut hasher = Sha256::new(); + hasher.update(self.to_bytes()); + let hash = hasher.finalize(); + u32::from_be_bytes(hash[0..4].try_into().unwrap()) + } +} diff --git a/crates/chia-secp/src/secp256k1/secret_key.rs b/crates/chia-secp/src/secp256k1/secret_key.rs new file mode 100644 index 000000000..813612bb0 --- /dev/null +++ b/crates/chia-secp/src/secp256k1/secret_key.rs @@ -0,0 +1,50 @@ +use std::{ + fmt, + hash::{Hash, Hasher}, +}; + +use k256::ecdsa::{Error, SigningKey}; + +use super::{Secp256k1PublicKey, Secp256k1Signature}; + +#[derive(Clone, PartialEq, Eq)] +pub struct Secp256k1SecretKey(SigningKey); + +impl Hash for Secp256k1SecretKey { + fn hash(&self, state: &mut H) { + self.to_bytes().hash(state); + } +} + +impl fmt::Debug for Secp256k1SecretKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Secp256k1SecretKey(...)") + } +} + +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for Secp256k1SecretKey { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + } +} + +impl Secp256k1SecretKey { + pub fn to_bytes(&self) -> [u8; 32] { + self.0.to_bytes().into() + } + + pub fn from_bytes(bytes: [u8; 32]) -> Result { + Ok(Self(SigningKey::from_bytes((&bytes).into())?)) + } + + pub fn public_key(&self) -> Secp256k1PublicKey { + Secp256k1PublicKey(*self.0.verifying_key()) + } + + pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result { + Ok(Secp256k1Signature( + 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 new file mode 100644 index 000000000..05194902c --- /dev/null +++ b/crates/chia-secp/src/secp256k1/signature.rs @@ -0,0 +1,46 @@ +use std::{ + fmt, + hash::{Hash, Hasher}, +}; + +use k256::ecdsa::{Error, Signature}; + +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct Secp256k1Signature(pub(crate) Signature); + +impl Hash for Secp256k1Signature { + fn hash(&self, state: &mut H) { + self.to_bytes().hash(state); + } +} + +impl fmt::Debug for Secp256k1Signature { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Secp256k1Signature({self})") + } +} + +impl fmt::Display for Secp256k1Signature { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", hex::encode(self.to_bytes())) + } +} + +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for Secp256k1Signature { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + } +} + +impl Secp256k1Signature { + pub const SIZE: usize = 64; + + pub fn to_bytes(&self) -> [u8; Self::SIZE] { + self.0.to_bytes().into() + } + + pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { + Ok(Self(Signature::from_slice(&bytes)?)) + } +} diff --git a/crates/chia-secp/src/secp256r1.rs b/crates/chia-secp/src/secp256r1.rs new file mode 100644 index 000000000..bff7b3cd2 --- /dev/null +++ b/crates/chia-secp/src/secp256r1.rs @@ -0,0 +1,7 @@ +mod public_key; +mod secret_key; +mod signature; + +pub use public_key::*; +pub use secret_key::*; +pub use signature::*; diff --git a/crates/chia-secp/src/secp256r1/public_key.rs b/crates/chia-secp/src/secp256r1/public_key.rs new file mode 100644 index 000000000..9426699f0 --- /dev/null +++ b/crates/chia-secp/src/secp256r1/public_key.rs @@ -0,0 +1,59 @@ +use std::fmt; +use std::hash::{Hash, Hasher}; + +use chia_sha2::Sha256; +use p256::ecdsa::signature::hazmat::PrehashVerifier; +use p256::ecdsa::{Error, VerifyingKey}; + +use super::Secp256r1Signature; + +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct Secp256r1PublicKey(pub(crate) VerifyingKey); + +impl Hash for Secp256r1PublicKey { + fn hash(&self, state: &mut H) { + self.to_bytes().hash(state); + } +} + +impl fmt::Debug for Secp256r1PublicKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Secp256r1PublicKey({self})") + } +} + +impl fmt::Display for Secp256r1PublicKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", hex::encode(self.to_bytes())) + } +} + +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for Secp256r1PublicKey { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + } +} + +impl Secp256r1PublicKey { + pub const SIZE: usize = 33; + + pub fn to_bytes(&self) -> [u8; Self::SIZE] { + 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 verify_prehashed(&self, message_hash: [u8; 32], signature: Secp256r1Signature) -> bool { + self.0.verify_prehash(&message_hash, &signature.0).is_ok() + } + + pub fn fingerprint(&self) -> u32 { + let mut hasher = Sha256::new(); + hasher.update(self.to_bytes()); + let hash = hasher.finalize(); + u32::from_be_bytes(hash[0..4].try_into().unwrap()) + } +} diff --git a/crates/chia-secp/src/secp256r1/secret_key.rs b/crates/chia-secp/src/secp256r1/secret_key.rs new file mode 100644 index 000000000..79a318985 --- /dev/null +++ b/crates/chia-secp/src/secp256r1/secret_key.rs @@ -0,0 +1,50 @@ +use std::{ + fmt, + hash::{Hash, Hasher}, +}; + +use p256::ecdsa::{Error, SigningKey}; + +use super::{Secp256r1PublicKey, Secp256r1Signature}; + +#[derive(Clone, PartialEq, Eq)] +pub struct Secp256r1SecretKey(SigningKey); + +impl Hash for Secp256r1SecretKey { + fn hash(&self, state: &mut H) { + self.to_bytes().hash(state); + } +} + +impl fmt::Debug for Secp256r1SecretKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Secp256r1SecretKey(...)") + } +} + +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for Secp256r1SecretKey { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + } +} + +impl Secp256r1SecretKey { + pub fn to_bytes(&self) -> [u8; 32] { + self.0.to_bytes().into() + } + + pub fn from_bytes(bytes: [u8; 32]) -> Result { + Ok(Self(SigningKey::from_bytes((&bytes).into())?)) + } + + pub fn public_key(&self) -> Secp256r1PublicKey { + Secp256r1PublicKey(*self.0.verifying_key()) + } + + pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result { + Ok(Secp256r1Signature( + 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 new file mode 100644 index 000000000..3457cc52e --- /dev/null +++ b/crates/chia-secp/src/secp256r1/signature.rs @@ -0,0 +1,46 @@ +use std::{ + fmt, + hash::{Hash, Hasher}, +}; + +use p256::ecdsa::{Error, Signature}; + +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct Secp256r1Signature(pub(crate) Signature); + +impl Hash for Secp256r1Signature { + fn hash(&self, state: &mut H) { + self.to_bytes().hash(state); + } +} + +impl fmt::Debug for Secp256r1Signature { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Secp256r1Signature({self})") + } +} + +impl fmt::Display for Secp256r1Signature { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", hex::encode(self.to_bytes())) + } +} + +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for Secp256r1Signature { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + Self::from_bytes(u.arbitrary()?).map_err(|_| arbitrary::Error::IncorrectFormat) + } +} + +impl Secp256r1Signature { + pub const SIZE: usize = 64; + + pub fn to_bytes(&self) -> [u8; Self::SIZE] { + self.0.to_bytes().into() + } + + pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { + Ok(Self(Signature::from_slice(&bytes)?)) + } +} diff --git a/crates/clvm-traits/Cargo.toml b/crates/clvm-traits/Cargo.toml index bada6d170..3b32a7981 100644 --- a/crates/clvm-traits/Cargo.toml +++ b/crates/clvm-traits/Cargo.toml @@ -17,6 +17,7 @@ workspace = true [features] derive = ["dep:clvm-derive"] chia-bls = ["dep:chia-bls"] +chia-secp = ["dep:chia-secp"] py-bindings = ["dep:pyo3"] [dependencies] @@ -24,6 +25,7 @@ pyo3 = { workspace = true, optional = true } clvmr = { workspace = true } clvm-derive = { workspace = true, optional = true } chia-bls = { workspace = true, optional = true } +chia-secp = { workspace = true, optional = true } num-bigint = { workspace = true } thiserror = { workspace = true } diff --git a/crates/clvm-traits/src/from_clvm.rs b/crates/clvm-traits/src/from_clvm.rs index 897ffe8e9..4b82f9f45 100644 --- a/crates/clvm-traits/src/from_clvm.rs +++ b/crates/clvm-traits/src/from_clvm.rs @@ -220,6 +220,78 @@ impl> FromClvm for chia_bls::Signature { } } +#[cfg(feature = "chia-secp")] +impl FromClvm for chia_secp::Secp256k1PublicKey +where + D: ClvmDecoder, +{ + fn from_clvm(decoder: &D, node: D::Node) -> Result { + let atom = decoder.decode_atom(&node)?; + let bytes: [u8; Self::SIZE] = + atom.as_ref() + .try_into() + .map_err(|_| FromClvmError::WrongAtomLength { + expected: Self::SIZE, + found: atom.len(), + })?; + Self::from_bytes(bytes).map_err(|error| FromClvmError::Custom(error.to_string())) + } +} + +#[cfg(feature = "chia-secp")] +impl FromClvm for chia_secp::Secp256k1Signature +where + D: ClvmDecoder, +{ + fn from_clvm(decoder: &D, node: D::Node) -> Result { + let atom = decoder.decode_atom(&node)?; + let bytes: [u8; Self::SIZE] = + atom.as_ref() + .try_into() + .map_err(|_| FromClvmError::WrongAtomLength { + expected: Self::SIZE, + found: atom.len(), + })?; + Self::from_bytes(bytes).map_err(|error| FromClvmError::Custom(error.to_string())) + } +} + +#[cfg(feature = "chia-secp")] +impl FromClvm for chia_secp::Secp256r1PublicKey +where + D: ClvmDecoder, +{ + fn from_clvm(decoder: &D, node: D::Node) -> Result { + let atom = decoder.decode_atom(&node)?; + let bytes: [u8; Self::SIZE] = + atom.as_ref() + .try_into() + .map_err(|_| FromClvmError::WrongAtomLength { + expected: Self::SIZE, + found: atom.len(), + })?; + Self::from_bytes(bytes).map_err(|error| FromClvmError::Custom(error.to_string())) + } +} + +#[cfg(feature = "chia-secp")] +impl FromClvm for chia_secp::Secp256r1Signature +where + D: ClvmDecoder, +{ + fn from_clvm(decoder: &D, node: D::Node) -> Result { + let atom = decoder.decode_atom(&node)?; + let bytes: [u8; Self::SIZE] = + atom.as_ref() + .try_into() + .map_err(|_| FromClvmError::WrongAtomLength { + expected: Self::SIZE, + found: atom.len(), + })?; + Self::from_bytes(bytes).map_err(|error| FromClvmError::Custom(error.to_string())) + } +} + #[cfg(test)] mod tests { use clvmr::{serde::node_from_bytes, Allocator}; @@ -373,4 +445,58 @@ mod tests { }) ); } + + #[cfg(feature = "chia-secp")] + #[test] + fn test_secp_public_key() { + use chia_secp::Secp256k1PublicKey; + use hex_literal::hex; + + let a = &mut Allocator::new(); + + let bytes = hex!("02827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4"); + + assert_eq!( + decode( + a, + "a102827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4" + ), + Ok(Secp256k1PublicKey::from_bytes(bytes).unwrap()) + ); + assert_eq!( + decode::(a, "8568656c6c6f"), + Err(FromClvmError::WrongAtomLength { + expected: 33, + found: 5 + }) + ); + } + + #[cfg(feature = "chia-secp")] + #[test] + fn test_secp_signature() { + use chia_secp::Secp256k1Signature; + use hex_literal::hex; + + let a = &mut Allocator::new(); + + let bytes = hex!( + " + 6f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e865 + 3ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591 + " + ); + + assert_eq!( + decode(a, "c0406f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591"), + Ok(Secp256k1Signature::from_bytes(bytes).unwrap()) + ); + assert_eq!( + decode::(a, "8568656c6c6f"), + Err(FromClvmError::WrongAtomLength { + expected: 64, + found: 5 + }) + ); + } } diff --git a/crates/clvm-traits/src/to_clvm.rs b/crates/clvm-traits/src/to_clvm.rs index c2672b0f0..37eb5c7d1 100644 --- a/crates/clvm-traits/src/to_clvm.rs +++ b/crates/clvm-traits/src/to_clvm.rs @@ -173,6 +173,46 @@ impl> ToClvm for chia_bls::Signature { } } +#[cfg(feature = "chia-secp")] +impl ToClvm for chia_secp::Secp256k1PublicKey +where + E: ClvmEncoder, +{ + fn to_clvm(&self, encoder: &mut E) -> Result { + encoder.encode_atom(Atom::Borrowed(&self.to_bytes())) + } +} + +#[cfg(feature = "chia-secp")] +impl ToClvm for chia_secp::Secp256k1Signature +where + E: ClvmEncoder, +{ + fn to_clvm(&self, encoder: &mut E) -> Result { + encoder.encode_atom(Atom::Borrowed(&self.to_bytes())) + } +} + +#[cfg(feature = "chia-secp")] +impl ToClvm for chia_secp::Secp256r1PublicKey +where + E: ClvmEncoder, +{ + fn to_clvm(&self, encoder: &mut E) -> Result { + encoder.encode_atom(Atom::Borrowed(&self.to_bytes())) + } +} + +#[cfg(feature = "chia-secp")] +impl ToClvm for chia_secp::Secp256r1Signature +where + E: ClvmEncoder, +{ + fn to_clvm(&self, encoder: &mut E) -> Result { + encoder.encode_atom(Atom::Borrowed(&self.to_bytes())) + } +} + #[cfg(test)] mod tests { use clvmr::{serde::node_to_bytes, Allocator}; @@ -345,4 +385,58 @@ mod tests { Ok("c060a3994dc9c0ef41a903d3335f0afe42ba16c88e7881706798492da4a1653cd10c69c841eeb56f44ae005e2bad27fb7ebb16ce8bbfbd708ea91dd4ff24f030497b50e694a8270eccd07dbc206b8ffe0c34a9ea81291785299fae8206a1e1bbc1d1".to_string()) ); } + + #[cfg(feature = "chia-secp")] + #[test] + fn test_secp_public_key() { + use chia_secp::{Secp256k1PublicKey, Secp256r1PublicKey}; + use hex_literal::hex; + + let a = &mut Allocator::new(); + + let k1_pk = Secp256k1PublicKey::from_bytes(hex!( + "02827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4" + )) + .unwrap(); + assert_eq!( + encode(a, k1_pk), + Ok("a102827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4".to_string()) + ); + + let r1_pk = Secp256r1PublicKey::from_bytes(hex!( + "037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4" + )) + .unwrap(); + assert_eq!( + encode(a, r1_pk), + Ok("a1037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4".to_string()) + ); + } + + #[cfg(feature = "chia-secp")] + #[test] + fn test_secp_signature() { + use chia_secp::{Secp256k1Signature, Secp256r1Signature}; + use hex_literal::hex; + + let a = &mut Allocator::new(); + + let k1_sig = Secp256k1Signature::from_bytes(hex!( + "6f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591" + )) + .unwrap(); + assert_eq!( + encode(a, k1_sig), + Ok("c0406f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591".to_string()) + ); + + let r1_sig = Secp256r1Signature::from_bytes(hex!( + "550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228" + )) + .unwrap(); + assert_eq!( + encode(a, r1_sig), + Ok("c040550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228".to_string()) + ); + } } From 6d7f5a708a75bab4fac469ec39a8551cb45e9d01 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 12 Dec 2024 10:20:37 -0500 Subject: [PATCH 2/5] Rename and add R1 fromclvm tests --- crates/chia-secp/src/lib.rs | 4 +- crates/chia-secp/src/secp256k1/public_key.rs | 18 +++---- crates/chia-secp/src/secp256k1/secret_key.rs | 22 ++++---- crates/chia-secp/src/secp256k1/signature.rs | 14 ++--- crates/chia-secp/src/secp256r1/public_key.rs | 18 +++---- crates/chia-secp/src/secp256r1/secret_key.rs | 22 ++++---- crates/chia-secp/src/secp256r1/signature.rs | 14 ++--- crates/clvm-traits/src/from_clvm.rs | 56 ++++++++++++++++---- crates/clvm-traits/src/to_clvm.rs | 20 +++---- 9 files changed, 112 insertions(+), 76 deletions(-) diff --git a/crates/chia-secp/src/lib.rs b/crates/chia-secp/src/lib.rs index 25ec6e548..4914e1ace 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 = Secp256k1SecretKey::from_bytes(rng.gen())?; + let sk = K1SecretKey::from_bytes(rng.gen())?; assert_eq!( hex::encode(sk.to_bytes()), "ae491886341a539a1ccfaffcc9c78650ad1adc6270620c882b8d29bf6b9bc4cd" @@ -43,7 +43,7 @@ mod tests { fn test_secp256r1_key() -> anyhow::Result<()> { let mut rng = ChaCha8Rng::seed_from_u64(1337); - let sk = Secp256r1SecretKey::from_bytes(rng.gen())?; + let sk = R1SecretKey::from_bytes(rng.gen())?; assert_eq!( hex::encode(sk.to_bytes()), "ae491886341a539a1ccfaffcc9c78650ad1adc6270620c882b8d29bf6b9bc4cd" diff --git a/crates/chia-secp/src/secp256k1/public_key.rs b/crates/chia-secp/src/secp256k1/public_key.rs index 9d4e877fb..f212e4987 100644 --- a/crates/chia-secp/src/secp256k1/public_key.rs +++ b/crates/chia-secp/src/secp256k1/public_key.rs @@ -5,37 +5,37 @@ use chia_sha2::Sha256; use k256::ecdsa::signature::hazmat::PrehashVerifier; use k256::ecdsa::{Error, VerifyingKey}; -use super::Secp256k1Signature; +use super::K1Signature; #[derive(Clone, Copy, PartialEq, Eq)] -pub struct Secp256k1PublicKey(pub(crate) VerifyingKey); +pub struct K1PublicKey(pub(crate) VerifyingKey); -impl Hash for Secp256k1PublicKey { +impl Hash for K1PublicKey { fn hash(&self, state: &mut H) { self.to_bytes().hash(state); } } -impl fmt::Debug for Secp256k1PublicKey { +impl fmt::Debug for K1PublicKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Secp256k1PublicKey({self})") + write!(f, "K1PublicKey({self})") } } -impl fmt::Display for Secp256k1PublicKey { +impl fmt::Display for K1PublicKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", hex::encode(self.to_bytes())) } } #[cfg(feature = "arbitrary")] -impl<'a> arbitrary::Arbitrary<'a> for Secp256k1PublicKey { +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) } } -impl Secp256k1PublicKey { +impl K1PublicKey { pub const SIZE: usize = 33; pub fn to_bytes(&self) -> [u8; Self::SIZE] { @@ -46,7 +46,7 @@ impl Secp256k1PublicKey { Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?)) } - pub fn verify_prehashed(&self, message_hash: [u8; 32], signature: Secp256k1Signature) -> bool { + pub fn verify_prehashed(&self, message_hash: [u8; 32], signature: K1Signature) -> bool { self.0.verify_prehash(&message_hash, &signature.0).is_ok() } diff --git a/crates/chia-secp/src/secp256k1/secret_key.rs b/crates/chia-secp/src/secp256k1/secret_key.rs index 813612bb0..2c286c098 100644 --- a/crates/chia-secp/src/secp256k1/secret_key.rs +++ b/crates/chia-secp/src/secp256k1/secret_key.rs @@ -5,31 +5,31 @@ use std::{ use k256::ecdsa::{Error, SigningKey}; -use super::{Secp256k1PublicKey, Secp256k1Signature}; +use super::{K1PublicKey, K1Signature}; #[derive(Clone, PartialEq, Eq)] -pub struct Secp256k1SecretKey(SigningKey); +pub struct K1SecretKey(SigningKey); -impl Hash for Secp256k1SecretKey { +impl Hash for K1SecretKey { fn hash(&self, state: &mut H) { self.to_bytes().hash(state); } } -impl fmt::Debug for Secp256k1SecretKey { +impl fmt::Debug for K1SecretKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Secp256k1SecretKey(...)") + write!(f, "K1SecretKey(...)") } } #[cfg(feature = "arbitrary")] -impl<'a> arbitrary::Arbitrary<'a> for Secp256k1SecretKey { +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) } } -impl Secp256k1SecretKey { +impl K1SecretKey { pub fn to_bytes(&self) -> [u8; 32] { self.0.to_bytes().into() } @@ -38,12 +38,12 @@ impl Secp256k1SecretKey { Ok(Self(SigningKey::from_bytes((&bytes).into())?)) } - pub fn public_key(&self) -> Secp256k1PublicKey { - Secp256k1PublicKey(*self.0.verifying_key()) + pub fn public_key(&self) -> K1PublicKey { + K1PublicKey(*self.0.verifying_key()) } - pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result { - Ok(Secp256k1Signature( + pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result { + Ok(K1Signature( 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 05194902c..cfa3991c2 100644 --- a/crates/chia-secp/src/secp256k1/signature.rs +++ b/crates/chia-secp/src/secp256k1/signature.rs @@ -6,34 +6,34 @@ use std::{ use k256::ecdsa::{Error, Signature}; #[derive(Clone, Copy, PartialEq, Eq)] -pub struct Secp256k1Signature(pub(crate) Signature); +pub struct K1Signature(pub(crate) Signature); -impl Hash for Secp256k1Signature { +impl Hash for K1Signature { fn hash(&self, state: &mut H) { self.to_bytes().hash(state); } } -impl fmt::Debug for Secp256k1Signature { +impl fmt::Debug for K1Signature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Secp256k1Signature({self})") + write!(f, "K1Signature({self})") } } -impl fmt::Display for Secp256k1Signature { +impl fmt::Display for K1Signature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", hex::encode(self.to_bytes())) } } #[cfg(feature = "arbitrary")] -impl<'a> arbitrary::Arbitrary<'a> for Secp256k1Signature { +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) } } -impl Secp256k1Signature { +impl K1Signature { pub const SIZE: usize = 64; pub fn to_bytes(&self) -> [u8; Self::SIZE] { diff --git a/crates/chia-secp/src/secp256r1/public_key.rs b/crates/chia-secp/src/secp256r1/public_key.rs index 9426699f0..490590432 100644 --- a/crates/chia-secp/src/secp256r1/public_key.rs +++ b/crates/chia-secp/src/secp256r1/public_key.rs @@ -5,37 +5,37 @@ use chia_sha2::Sha256; use p256::ecdsa::signature::hazmat::PrehashVerifier; use p256::ecdsa::{Error, VerifyingKey}; -use super::Secp256r1Signature; +use super::R1Signature; #[derive(Clone, Copy, PartialEq, Eq)] -pub struct Secp256r1PublicKey(pub(crate) VerifyingKey); +pub struct R1PublicKey(pub(crate) VerifyingKey); -impl Hash for Secp256r1PublicKey { +impl Hash for R1PublicKey { fn hash(&self, state: &mut H) { self.to_bytes().hash(state); } } -impl fmt::Debug for Secp256r1PublicKey { +impl fmt::Debug for R1PublicKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Secp256r1PublicKey({self})") + write!(f, "R1PublicKey({self})") } } -impl fmt::Display for Secp256r1PublicKey { +impl fmt::Display for R1PublicKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", hex::encode(self.to_bytes())) } } #[cfg(feature = "arbitrary")] -impl<'a> arbitrary::Arbitrary<'a> for Secp256r1PublicKey { +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) } } -impl Secp256r1PublicKey { +impl R1PublicKey { pub const SIZE: usize = 33; pub fn to_bytes(&self) -> [u8; Self::SIZE] { @@ -46,7 +46,7 @@ impl Secp256r1PublicKey { Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?)) } - pub fn verify_prehashed(&self, message_hash: [u8; 32], signature: Secp256r1Signature) -> bool { + pub fn verify_prehashed(&self, message_hash: [u8; 32], signature: R1Signature) -> bool { self.0.verify_prehash(&message_hash, &signature.0).is_ok() } diff --git a/crates/chia-secp/src/secp256r1/secret_key.rs b/crates/chia-secp/src/secp256r1/secret_key.rs index 79a318985..6eb668b98 100644 --- a/crates/chia-secp/src/secp256r1/secret_key.rs +++ b/crates/chia-secp/src/secp256r1/secret_key.rs @@ -5,31 +5,31 @@ use std::{ use p256::ecdsa::{Error, SigningKey}; -use super::{Secp256r1PublicKey, Secp256r1Signature}; +use super::{R1PublicKey, R1Signature}; #[derive(Clone, PartialEq, Eq)] -pub struct Secp256r1SecretKey(SigningKey); +pub struct R1SecretKey(SigningKey); -impl Hash for Secp256r1SecretKey { +impl Hash for R1SecretKey { fn hash(&self, state: &mut H) { self.to_bytes().hash(state); } } -impl fmt::Debug for Secp256r1SecretKey { +impl fmt::Debug for R1SecretKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Secp256r1SecretKey(...)") + write!(f, "R1SecretKey(...)") } } #[cfg(feature = "arbitrary")] -impl<'a> arbitrary::Arbitrary<'a> for Secp256r1SecretKey { +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) } } -impl Secp256r1SecretKey { +impl R1SecretKey { pub fn to_bytes(&self) -> [u8; 32] { self.0.to_bytes().into() } @@ -38,12 +38,12 @@ impl Secp256r1SecretKey { Ok(Self(SigningKey::from_bytes((&bytes).into())?)) } - pub fn public_key(&self) -> Secp256r1PublicKey { - Secp256r1PublicKey(*self.0.verifying_key()) + pub fn public_key(&self) -> R1PublicKey { + R1PublicKey(*self.0.verifying_key()) } - pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result { - Ok(Secp256r1Signature( + pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result { + Ok(R1Signature( 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 3457cc52e..cfb4b3b35 100644 --- a/crates/chia-secp/src/secp256r1/signature.rs +++ b/crates/chia-secp/src/secp256r1/signature.rs @@ -6,34 +6,34 @@ use std::{ use p256::ecdsa::{Error, Signature}; #[derive(Clone, Copy, PartialEq, Eq)] -pub struct Secp256r1Signature(pub(crate) Signature); +pub struct R1Signature(pub(crate) Signature); -impl Hash for Secp256r1Signature { +impl Hash for R1Signature { fn hash(&self, state: &mut H) { self.to_bytes().hash(state); } } -impl fmt::Debug for Secp256r1Signature { +impl fmt::Debug for R1Signature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Secp256r1Signature({self})") + write!(f, "R1Signature({self})") } } -impl fmt::Display for Secp256r1Signature { +impl fmt::Display for R1Signature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", hex::encode(self.to_bytes())) } } #[cfg(feature = "arbitrary")] -impl<'a> arbitrary::Arbitrary<'a> for Secp256r1Signature { +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) } } -impl Secp256r1Signature { +impl R1Signature { pub const SIZE: usize = 64; pub fn to_bytes(&self) -> [u8; Self::SIZE] { diff --git a/crates/clvm-traits/src/from_clvm.rs b/crates/clvm-traits/src/from_clvm.rs index 4b82f9f45..2b34d9e5d 100644 --- a/crates/clvm-traits/src/from_clvm.rs +++ b/crates/clvm-traits/src/from_clvm.rs @@ -221,7 +221,7 @@ impl> FromClvm for chia_bls::Signature { } #[cfg(feature = "chia-secp")] -impl FromClvm for chia_secp::Secp256k1PublicKey +impl FromClvm for chia_secp::K1PublicKey where D: ClvmDecoder, { @@ -239,7 +239,7 @@ where } #[cfg(feature = "chia-secp")] -impl FromClvm for chia_secp::Secp256k1Signature +impl FromClvm for chia_secp::K1Signature where D: ClvmDecoder, { @@ -257,7 +257,7 @@ where } #[cfg(feature = "chia-secp")] -impl FromClvm for chia_secp::Secp256r1PublicKey +impl FromClvm for chia_secp::R1PublicKey where D: ClvmDecoder, { @@ -275,7 +275,7 @@ where } #[cfg(feature = "chia-secp")] -impl FromClvm for chia_secp::Secp256r1Signature +impl FromClvm for chia_secp::R1Signature where D: ClvmDecoder, { @@ -449,7 +449,7 @@ mod tests { #[cfg(feature = "chia-secp")] #[test] fn test_secp_public_key() { - use chia_secp::Secp256k1PublicKey; + use chia_secp::{K1PublicKey, R1PublicKey}; use hex_literal::hex; let a = &mut Allocator::new(); @@ -461,10 +461,27 @@ mod tests { a, "a102827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4" ), - Ok(Secp256k1PublicKey::from_bytes(bytes).unwrap()) + Ok(K1PublicKey::from_bytes(bytes).unwrap()) ); assert_eq!( - decode::(a, "8568656c6c6f"), + decode::(a, "8568656c6c6f"), + Err(FromClvmError::WrongAtomLength { + expected: 33, + found: 5 + }) + ); + + let bytes = hex!("037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4"); + + assert_eq!( + decode( + a, + "a1037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4" + ), + Ok(R1PublicKey::from_bytes(bytes).unwrap()) + ); + assert_eq!( + decode::(a, "8568656c6c6f"), Err(FromClvmError::WrongAtomLength { expected: 33, found: 5 @@ -475,7 +492,7 @@ mod tests { #[cfg(feature = "chia-secp")] #[test] fn test_secp_signature() { - use chia_secp::Secp256k1Signature; + use chia_secp::K1Signature; use hex_literal::hex; let a = &mut Allocator::new(); @@ -489,10 +506,29 @@ mod tests { assert_eq!( decode(a, "c0406f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591"), - Ok(Secp256k1Signature::from_bytes(bytes).unwrap()) + Ok(K1Signature::from_bytes(bytes).unwrap()) + ); + assert_eq!( + decode::(a, "8568656c6c6f"), + Err(FromClvmError::WrongAtomLength { + expected: 64, + found: 5 + }) + ); + + let bytes = hex!( + " + 550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b4053 + 7915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228 + " + ); + + assert_eq!( + decode(a, "c040550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228"), + Ok(K1Signature::from_bytes(bytes).unwrap()) ); assert_eq!( - decode::(a, "8568656c6c6f"), + decode::(a, "8568656c6c6f"), Err(FromClvmError::WrongAtomLength { expected: 64, found: 5 diff --git a/crates/clvm-traits/src/to_clvm.rs b/crates/clvm-traits/src/to_clvm.rs index 37eb5c7d1..bfb03c008 100644 --- a/crates/clvm-traits/src/to_clvm.rs +++ b/crates/clvm-traits/src/to_clvm.rs @@ -174,7 +174,7 @@ impl> ToClvm for chia_bls::Signature { } #[cfg(feature = "chia-secp")] -impl ToClvm for chia_secp::Secp256k1PublicKey +impl ToClvm for chia_secp::K1PublicKey where E: ClvmEncoder, { @@ -184,7 +184,7 @@ where } #[cfg(feature = "chia-secp")] -impl ToClvm for chia_secp::Secp256k1Signature +impl ToClvm for chia_secp::K1Signature where E: ClvmEncoder, { @@ -194,7 +194,7 @@ where } #[cfg(feature = "chia-secp")] -impl ToClvm for chia_secp::Secp256r1PublicKey +impl ToClvm for chia_secp::R1PublicKey where E: ClvmEncoder, { @@ -204,7 +204,7 @@ where } #[cfg(feature = "chia-secp")] -impl ToClvm for chia_secp::Secp256r1Signature +impl ToClvm for chia_secp::R1Signature where E: ClvmEncoder, { @@ -389,12 +389,12 @@ mod tests { #[cfg(feature = "chia-secp")] #[test] fn test_secp_public_key() { - use chia_secp::{Secp256k1PublicKey, Secp256r1PublicKey}; + use chia_secp::{K1PublicKey, R1PublicKey}; use hex_literal::hex; let a = &mut Allocator::new(); - let k1_pk = Secp256k1PublicKey::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 = Secp256r1PublicKey::from_bytes(hex!( + let r1_pk = R1PublicKey::from_bytes(hex!( "037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4" )) .unwrap(); @@ -416,12 +416,12 @@ mod tests { #[cfg(feature = "chia-secp")] #[test] fn test_secp_signature() { - use chia_secp::{Secp256k1Signature, Secp256r1Signature}; + use chia_secp::{K1Signature, R1Signature}; use hex_literal::hex; let a = &mut Allocator::new(); - let k1_sig = Secp256k1Signature::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 = Secp256r1Signature::from_bytes(hex!( + let r1_sig = R1Signature::from_bytes(hex!( "550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228" )) .unwrap(); From 274f7237d86f7ef1a5bd8a36bed17d77a41716e3 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 12 Dec 2024 10:23:17 -0500 Subject: [PATCH 3/5] 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(); From 6fd365bbc69e04d5acff38e4e4aa635fa16789c4 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 12 Dec 2024 10:30:53 -0500 Subject: [PATCH 4/5] Add debug and display tests --- crates/chia-secp/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/chia-secp/src/lib.rs b/crates/chia-secp/src/lib.rs index 886fc8869..d37355c35 100644 --- a/crates/chia-secp/src/lib.rs +++ b/crates/chia-secp/src/lib.rs @@ -20,12 +20,21 @@ mod tests { hex::encode(sk.to_bytes()), "ae491886341a539a1ccfaffcc9c78650ad1adc6270620c882b8d29bf6b9bc4cd" ); + assert_eq!(format!("{sk:?}"), "K1SecretKey(...)"); let pk = sk.public_key(); assert_eq!( hex::encode(pk.to_bytes()), "02827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4" ); + assert_eq!( + format!("{pk:?}"), + "K1PublicKey(02827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4)" + ); + assert_eq!( + format!("{pk}"), + "02827cdbbed87e45683d448be2ea15fb72ba3732247bda18474868cf5456123fb4" + ); let message_hash: [u8; 32] = rng.gen(); let sig = sk.sign_prehashed(&message_hash)?; @@ -33,6 +42,14 @@ mod tests { hex::encode(sig.to_bytes()), "6f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591" ); + assert_eq!( + format!("{sig:?}"), + "K1Signature(6f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591)" + ); + assert_eq!( + format!("{sig}"), + "6f07897d1d28b8698af5dec5ca06907b1304b227dc9f740b8c4065cf04d5e8653ae66aa17063e7120ee7f22fae54373b35230e259244b90400b65cf00d86c591" + ); assert!(pk.verify_prehashed(&message_hash, &sig)); @@ -48,12 +65,21 @@ mod tests { hex::encode(sk.to_bytes()), "ae491886341a539a1ccfaffcc9c78650ad1adc6270620c882b8d29bf6b9bc4cd" ); + assert_eq!(format!("{sk:?}"), "R1SecretKey(...)"); let pk = sk.public_key(); assert_eq!( hex::encode(pk.to_bytes()), "037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4" ); + assert_eq!( + format!("{pk:?}"), + "R1PublicKey(037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4)" + ); + assert_eq!( + format!("{pk}"), + "037dc85102f5eb7867b9580fea8b242c774173e1a47db320c798242d3a7a7579e4" + ); let message_hash: [u8; 32] = rng.gen(); let sig = sk.sign_prehashed(&message_hash)?; @@ -61,6 +87,14 @@ mod tests { hex::encode(sig.to_bytes()), "550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228" ); + assert_eq!( + format!("{sig:?}"), + "R1Signature(550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228)" + ); + assert_eq!( + format!("{sig}"), + "550e83da8cf9b2d407ed093ae213869ebd7ceaea603920f87d535690e52b40537915d8fe3d5a96c87e700c56dc638c32f7a2954f2ba409367d1a132000cc2228" + ); assert!(pk.verify_prehashed(&message_hash, &sig)); From 04a5edfd318240f2a20178a76f5976541e04748c Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 12 Dec 2024 10:34:27 -0500 Subject: [PATCH 5/5] Add secp --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 1df6d0b21..26eb6956c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub use chia_client as client; pub use chia_consensus as consensus; pub use chia_protocol as protocol; pub use chia_puzzles as puzzles; +pub use chia_secp as secp; pub use chia_sha2 as sha2; pub use chia_ssl as ssl; pub use chia_traits as traits;