Skip to content

Commit

Permalink
Move keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Dec 11, 2024
1 parent b077cee commit 308ef71
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 115 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/chia-sdk-signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
76 changes: 0 additions & 76 deletions crates/chia-sdk-signer/src/secp.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
3 changes: 1 addition & 2 deletions crates/chia-sdk-signer/src/secp/required_secp_signature.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
6 changes: 3 additions & 3 deletions crates/chia-sdk-signer/src/secp/secp_dialect.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions crates/chia-sdk-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 2 additions & 0 deletions crates/chia-sdk-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod merkle_tree;
mod puzzle_mod;
mod puzzles;
mod run_puzzle;
mod secp;

pub use condition::*;
pub use conditions::*;
Expand All @@ -13,3 +14,4 @@ pub use merkle_tree::*;
pub use puzzle_mod::*;
pub use puzzles::*;
pub use run_puzzle::*;
pub use secp::*;
77 changes: 77 additions & 0 deletions crates/chia-sdk-types/src/secp.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,7 +15,7 @@ impl Secp256k1PublicKey {
self.0.to_encoded_point(true).as_ref().try_into().unwrap()

Check warning on line 15 in crates/chia-sdk-types/src/secp/secp256k1_public_key.rs

View check run for this annotation

Codecov / codecov/patch

crates/chia-sdk-types/src/secp/secp256k1_public_key.rs#L15

Added line #L15 was not covered by tests
}

pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, SignerError> {
pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, Error> {
Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use k256::ecdsa::SigningKey;

use crate::SignerError;
use k256::ecdsa::{Error, SigningKey};

use super::{Secp256k1PublicKey, Secp256k1Signature};

Expand All @@ -12,18 +10,15 @@ impl Secp256k1SecretKey {
self.0.to_bytes().into()
}

pub fn from_bytes(bytes: [u8; 32]) -> Result<Self, SignerError> {
pub fn from_bytes(bytes: [u8; 32]) -> Result<Self, Error> {
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<Secp256k1Signature, SignerError> {
pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result<Secp256k1Signature, Error> {
Ok(Secp256k1Signature(
self.0.sign_prehash_recoverable(&message_hash)?.0,
))
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -14,7 +12,7 @@ impl Secp256k1Signature {
self.0.to_bytes().into()
}

pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, SignerError> {
pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, Error> {
Ok(Self(Signature::from_slice(&bytes)?))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,7 +15,7 @@ impl Secp256r1PublicKey {
self.0.to_encoded_point(true).as_ref().try_into().unwrap()

Check warning on line 15 in crates/chia-sdk-types/src/secp/secp256r1_public_key.rs

View check run for this annotation

Codecov / codecov/patch

crates/chia-sdk-types/src/secp/secp256r1_public_key.rs#L15

Added line #L15 was not covered by tests
}

pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, SignerError> {
pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, Error> {
Ok(Self(VerifyingKey::from_sec1_bytes(&bytes)?))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use p256::ecdsa::SigningKey;

use crate::SignerError;
use p256::ecdsa::{Error, SigningKey};

use super::{Secp256r1PublicKey, Secp256r1Signature};

Expand All @@ -12,18 +10,15 @@ impl Secp256r1SecretKey {
self.0.to_bytes().into()
}

pub fn from_bytes(bytes: [u8; 32]) -> Result<Self, SignerError> {
pub fn from_bytes(bytes: [u8; 32]) -> Result<Self, Error> {
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<Secp256r1Signature, SignerError> {
pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result<Secp256r1Signature, Error> {
Ok(Secp256r1Signature(
self.0.sign_prehash_recoverable(&message_hash)?.0,
))
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -14,7 +12,7 @@ impl Secp256r1Signature {
self.0.to_bytes().into()
}

pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, SignerError> {
pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Result<Self, Error> {
Ok(Self(Signature::from_slice(&bytes)?))
}
}
Expand Down

0 comments on commit 308ef71

Please sign in to comment.