From 308ef71fa2de0544d2d3b04aa93aa12f6c71ae4b Mon Sep 17 00:00:00 2001 From: Rigidity Date: Tue, 10 Dec 2024 19:25:46 -0500 Subject: [PATCH] Move keys --- Cargo.lock | 5 +- crates/chia-sdk-signer/Cargo.toml | 1 - crates/chia-sdk-signer/src/secp.rs | 76 ------------------ .../src/secp/required_secp_signature.rs | 3 +- .../chia-sdk-signer/src/secp/secp_dialect.rs | 6 +- crates/chia-sdk-types/Cargo.toml | 4 + crates/chia-sdk-types/src/lib.rs | 2 + crates/chia-sdk-types/src/secp.rs | 77 +++++++++++++++++++ .../src/secp/secp256k1_public_key.rs | 6 +- .../src/secp/secp256k1_secret_key.rs | 11 +-- .../src/secp/secp256k1_signature.rs | 6 +- .../src/secp/secp256r1_public_key.rs | 6 +- .../src/secp/secp256r1_secret_key.rs | 11 +-- .../src/secp/secp256r1_signature.rs | 6 +- 14 files changed, 105 insertions(+), 115 deletions(-) create mode 100644 crates/chia-sdk-types/src/secp.rs rename crates/{chia-sdk-signer => chia-sdk-types}/src/secp/secp256k1_public_key.rs (91%) rename crates/{chia-sdk-signer => chia-sdk-types}/src/secp/secp256k1_secret_key.rs (68%) rename crates/{chia-sdk-signer => chia-sdk-types}/src/secp/secp256k1_signature.rs (89%) rename crates/{chia-sdk-signer => chia-sdk-types}/src/secp/secp256r1_public_key.rs (91%) rename crates/{chia-sdk-signer => chia-sdk-types}/src/secp/secp256r1_secret_key.rs (68%) rename crates/{chia-sdk-signer => chia-sdk-types}/src/secp/secp256r1_signature.rs (89%) diff --git a/Cargo.lock b/Cargo.lock index a811716..d5249df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -487,7 +487,6 @@ dependencies = [ "hex", "hex-literal", "k256", - "p256", "rand", "rand_chacha", "thiserror", @@ -539,7 +538,11 @@ dependencies = [ "clvmr", "hex", "hex-literal", + "k256", "once_cell", + "p256", + "rand", + "rand_chacha", "rstest", ] diff --git a/crates/chia-sdk-signer/Cargo.toml b/crates/chia-sdk-signer/Cargo.toml index 840bf61..8b18d0a 100644 --- a/crates/chia-sdk-signer/Cargo.toml +++ b/crates/chia-sdk-signer/Cargo.toml @@ -23,7 +23,6 @@ clvmr = { workspace = true } thiserror = { workspace = true } chia-sdk-types = { workspace = true } k256 = { workspace = true } -p256 = { workspace = true } [dev-dependencies] chia-puzzles = { workspace = true } diff --git a/crates/chia-sdk-signer/src/secp.rs b/crates/chia-sdk-signer/src/secp.rs index e31a85a..d8f5317 100644 --- a/crates/chia-sdk-signer/src/secp.rs +++ b/crates/chia-sdk-signer/src/secp.rs @@ -1,81 +1,5 @@ mod required_secp_signature; -mod secp256k1_public_key; -mod secp256k1_secret_key; -mod secp256k1_signature; -mod secp256r1_public_key; -mod secp256r1_secret_key; -mod secp256r1_signature; mod secp_dialect; pub use required_secp_signature::*; -pub use secp256k1_public_key::*; -pub use secp256k1_secret_key::*; -pub use secp256k1_signature::*; -pub use secp256r1_public_key::*; -pub use secp256r1_secret_key::*; -pub use secp256r1_signature::*; pub use secp_dialect::*; - -#[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-sdk-signer/src/secp/required_secp_signature.rs b/crates/chia-sdk-signer/src/secp/required_secp_signature.rs index 79e8eb5..8a906e9 100644 --- a/crates/chia-sdk-signer/src/secp/required_secp_signature.rs +++ b/crates/chia-sdk-signer/src/secp/required_secp_signature.rs @@ -1,7 +1,6 @@ +use chia_sdk_types::{Secp256k1PublicKey, Secp256r1PublicKey}; use clvmr::NodePtr; -use super::{Secp256k1PublicKey, Secp256r1PublicKey}; - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SecpPublicKey { K1(Secp256k1PublicKey), diff --git a/crates/chia-sdk-signer/src/secp/secp_dialect.rs b/crates/chia-sdk-signer/src/secp/secp_dialect.rs index 1db3f45..105192f 100644 --- a/crates/chia-sdk-signer/src/secp/secp_dialect.rs +++ b/crates/chia-sdk-signer/src/secp/secp_dialect.rs @@ -1,6 +1,7 @@ use std::cell::RefCell; use chia_protocol::Bytes32; +use chia_sdk_types::{Secp256k1PublicKey, Secp256r1PublicKey}; use clvm_traits::FromClvm; use clvmr::{ cost::Cost, @@ -10,7 +11,7 @@ use clvmr::{ Allocator, NodePtr, }; -use super::{RequiredSecpSignature, Secp256k1PublicKey, Secp256r1PublicKey, SecpPublicKey}; +use super::{RequiredSecpSignature, SecpPublicKey}; const SECP256R1_VERIFY_COST: Cost = 1_850_000; const SECP256K1_VERIFY_COST: Cost = 1_300_000; @@ -112,13 +113,12 @@ where #[cfg(test)] mod tests { use chia_protocol::Bytes; + use chia_sdk_types::Secp256k1SecretKey; use clvm_traits::{clvm_list, clvm_quote, ToClvm}; use clvmr::{run_program, ChiaDialect}; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; - use crate::Secp256k1SecretKey; - use super::*; #[test] diff --git a/crates/chia-sdk-types/Cargo.toml b/crates/chia-sdk-types/Cargo.toml index ecbd6d3..c30569d 100644 --- a/crates/chia-sdk-types/Cargo.toml +++ b/crates/chia-sdk-types/Cargo.toml @@ -29,8 +29,12 @@ clvm-utils = { workspace = true } clvmr = { workspace = true } hex-literal = { workspace = true } once_cell = { workspace = true } +k256 = { workspace = true } +p256 = { workspace = true } [dev-dependencies] hex = { workspace = true } anyhow = { workspace = true } rstest = { workspace = true } +rand = { workspace = true } +rand_chacha = { workspace = true } diff --git a/crates/chia-sdk-types/src/lib.rs b/crates/chia-sdk-types/src/lib.rs index 9f81ac2..2700947 100644 --- a/crates/chia-sdk-types/src/lib.rs +++ b/crates/chia-sdk-types/src/lib.rs @@ -5,6 +5,7 @@ mod merkle_tree; mod puzzle_mod; mod puzzles; mod run_puzzle; +mod secp; pub use condition::*; pub use conditions::*; @@ -13,3 +14,4 @@ pub use merkle_tree::*; pub use puzzle_mod::*; pub use puzzles::*; pub use run_puzzle::*; +pub use secp::*; diff --git a/crates/chia-sdk-types/src/secp.rs b/crates/chia-sdk-types/src/secp.rs new file mode 100644 index 0000000..a0a2d83 --- /dev/null +++ b/crates/chia-sdk-types/src/secp.rs @@ -0,0 +1,77 @@ +mod secp256k1_public_key; +mod secp256k1_secret_key; +mod secp256k1_signature; +mod secp256r1_public_key; +mod secp256r1_secret_key; +mod secp256r1_signature; + +pub use secp256k1_public_key::*; +pub use secp256k1_secret_key::*; +pub use secp256k1_signature::*; +pub use secp256r1_public_key::*; +pub use secp256r1_secret_key::*; +pub use secp256r1_signature::*; + +#[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-sdk-signer/src/secp/secp256k1_public_key.rs b/crates/chia-sdk-types/src/secp/secp256k1_public_key.rs similarity index 91% rename from crates/chia-sdk-signer/src/secp/secp256k1_public_key.rs rename to crates/chia-sdk-types/src/secp/secp256k1_public_key.rs index 0117a3f..ff79f8f 100644 --- a/crates/chia-sdk-signer/src/secp/secp256k1_public_key.rs +++ b/crates/chia-sdk-types/src/secp/secp256k1_public_key.rs @@ -1,9 +1,7 @@ use clvm_traits::{ClvmDecoder, ClvmEncoder, FromClvm, FromClvmError, ToClvm, ToClvmError}; use clvmr::Atom; use k256::ecdsa::signature::hazmat::PrehashVerifier; -use k256::ecdsa::VerifyingKey; - -use crate::SignerError; +use k256::ecdsa::{Error, VerifyingKey}; use super::Secp256k1Signature; @@ -17,7 +15,7 @@ impl Secp256k1PublicKey { self.0.to_encoded_point(true).as_ref().try_into().unwrap() } - pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { + pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?)) } diff --git a/crates/chia-sdk-signer/src/secp/secp256k1_secret_key.rs b/crates/chia-sdk-types/src/secp/secp256k1_secret_key.rs similarity index 68% rename from crates/chia-sdk-signer/src/secp/secp256k1_secret_key.rs rename to crates/chia-sdk-types/src/secp/secp256k1_secret_key.rs index ab2d4a2..27ace0b 100644 --- a/crates/chia-sdk-signer/src/secp/secp256k1_secret_key.rs +++ b/crates/chia-sdk-types/src/secp/secp256k1_secret_key.rs @@ -1,6 +1,4 @@ -use k256::ecdsa::SigningKey; - -use crate::SignerError; +use k256::ecdsa::{Error, SigningKey}; use super::{Secp256k1PublicKey, Secp256k1Signature}; @@ -12,7 +10,7 @@ impl Secp256k1SecretKey { self.0.to_bytes().into() } - pub fn from_bytes(bytes: [u8; 32]) -> Result { + pub fn from_bytes(bytes: [u8; 32]) -> Result { Ok(Self(SigningKey::from_bytes((&bytes).into())?)) } @@ -20,10 +18,7 @@ impl Secp256k1SecretKey { Secp256k1PublicKey(*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(Secp256k1Signature( self.0.sign_prehash_recoverable(&message_hash)?.0, )) diff --git a/crates/chia-sdk-signer/src/secp/secp256k1_signature.rs b/crates/chia-sdk-types/src/secp/secp256k1_signature.rs similarity index 89% rename from crates/chia-sdk-signer/src/secp/secp256k1_signature.rs rename to crates/chia-sdk-types/src/secp/secp256k1_signature.rs index 6ac2869..955a5ee 100644 --- a/crates/chia-sdk-signer/src/secp/secp256k1_signature.rs +++ b/crates/chia-sdk-types/src/secp/secp256k1_signature.rs @@ -1,8 +1,6 @@ use clvm_traits::{ClvmDecoder, ClvmEncoder, FromClvm, FromClvmError, ToClvm, ToClvmError}; use clvmr::Atom; -use k256::ecdsa::Signature; - -use crate::SignerError; +use k256::ecdsa::{Error, Signature}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Secp256k1Signature(pub(crate) Signature); @@ -14,7 +12,7 @@ impl Secp256k1Signature { self.0.to_bytes().into() } - pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { + pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { Ok(Self(Signature::from_slice(&bytes)?)) } } diff --git a/crates/chia-sdk-signer/src/secp/secp256r1_public_key.rs b/crates/chia-sdk-types/src/secp/secp256r1_public_key.rs similarity index 91% rename from crates/chia-sdk-signer/src/secp/secp256r1_public_key.rs rename to crates/chia-sdk-types/src/secp/secp256r1_public_key.rs index 14daeb5..5a117f0 100644 --- a/crates/chia-sdk-signer/src/secp/secp256r1_public_key.rs +++ b/crates/chia-sdk-types/src/secp/secp256r1_public_key.rs @@ -1,9 +1,7 @@ use clvm_traits::{ClvmDecoder, ClvmEncoder, FromClvm, FromClvmError, ToClvm, ToClvmError}; use clvmr::Atom; use p256::ecdsa::signature::hazmat::PrehashVerifier; -use p256::ecdsa::VerifyingKey; - -use crate::SignerError; +use p256::ecdsa::{Error, VerifyingKey}; use super::Secp256r1Signature; @@ -17,7 +15,7 @@ impl Secp256r1PublicKey { self.0.to_encoded_point(true).as_ref().try_into().unwrap() } - pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { + pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?)) } diff --git a/crates/chia-sdk-signer/src/secp/secp256r1_secret_key.rs b/crates/chia-sdk-types/src/secp/secp256r1_secret_key.rs similarity index 68% rename from crates/chia-sdk-signer/src/secp/secp256r1_secret_key.rs rename to crates/chia-sdk-types/src/secp/secp256r1_secret_key.rs index f545e12..ce90d19 100644 --- a/crates/chia-sdk-signer/src/secp/secp256r1_secret_key.rs +++ b/crates/chia-sdk-types/src/secp/secp256r1_secret_key.rs @@ -1,6 +1,4 @@ -use p256::ecdsa::SigningKey; - -use crate::SignerError; +use p256::ecdsa::{Error, SigningKey}; use super::{Secp256r1PublicKey, Secp256r1Signature}; @@ -12,7 +10,7 @@ impl Secp256r1SecretKey { self.0.to_bytes().into() } - pub fn from_bytes(bytes: [u8; 32]) -> Result { + pub fn from_bytes(bytes: [u8; 32]) -> Result { Ok(Self(SigningKey::from_bytes((&bytes).into())?)) } @@ -20,10 +18,7 @@ impl Secp256r1SecretKey { Secp256r1PublicKey(*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(Secp256r1Signature( self.0.sign_prehash_recoverable(&message_hash)?.0, )) diff --git a/crates/chia-sdk-signer/src/secp/secp256r1_signature.rs b/crates/chia-sdk-types/src/secp/secp256r1_signature.rs similarity index 89% rename from crates/chia-sdk-signer/src/secp/secp256r1_signature.rs rename to crates/chia-sdk-types/src/secp/secp256r1_signature.rs index 112eb04..2d54b81 100644 --- a/crates/chia-sdk-signer/src/secp/secp256r1_signature.rs +++ b/crates/chia-sdk-types/src/secp/secp256r1_signature.rs @@ -1,8 +1,6 @@ use clvm_traits::{ClvmDecoder, ClvmEncoder, FromClvm, FromClvmError, ToClvm, ToClvmError}; use clvmr::Atom; -use p256::ecdsa::Signature; - -use crate::SignerError; +use p256::ecdsa::{Error, Signature}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Secp256r1Signature(pub(crate) Signature); @@ -14,7 +12,7 @@ impl Secp256r1Signature { self.0.to_bytes().into() } - pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { + pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result { Ok(Self(Signature::from_slice(&bytes)?)) } }