Skip to content

Commit

Permalink
Rename and add R1 fromclvm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Dec 12, 2024
1 parent c5cc8cb commit 6d7f5a7
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 76 deletions.
4 changes: 2 additions & 2 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 = Secp256k1SecretKey::from_bytes(rng.gen())?;
let sk = K1SecretKey::from_bytes(rng.gen())?;
assert_eq!(
hex::encode(sk.to_bytes()),
"ae491886341a539a1ccfaffcc9c78650ad1adc6270620c882b8d29bf6b9bc4cd"
Expand Down Expand Up @@ -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"
Expand Down
18 changes: 9 additions & 9 deletions crates/chia-secp/src/secp256k1/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: Hasher>(&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> {
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] {
Expand All @@ -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()
}

Expand Down
22 changes: 11 additions & 11 deletions crates/chia-secp/src/secp256k1/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: Hasher>(&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> {
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()
}
Expand All @@ -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<Secp256k1Signature, Error> {
Ok(Secp256k1Signature(
pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result<K1Signature, Error> {
Ok(K1Signature(
self.0.sign_prehash_recoverable(&message_hash)?.0,
))
}
Expand Down
14 changes: 7 additions & 7 deletions crates/chia-secp/src/secp256k1/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: Hasher>(&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> {
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] {
Expand Down
18 changes: 9 additions & 9 deletions crates/chia-secp/src/secp256r1/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: Hasher>(&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> {
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] {
Expand All @@ -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()
}

Expand Down
22 changes: 11 additions & 11 deletions crates/chia-secp/src/secp256r1/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: Hasher>(&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> {
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()
}
Expand All @@ -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<Secp256r1Signature, Error> {
Ok(Secp256r1Signature(
pub fn sign_prehashed(&self, message_hash: [u8; 32]) -> Result<R1Signature, Error> {
Ok(R1Signature(
self.0.sign_prehash_recoverable(&message_hash)?.0,
))
}
Expand Down
14 changes: 7 additions & 7 deletions crates/chia-secp/src/secp256r1/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: Hasher>(&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> {
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] {
Expand Down
Loading

0 comments on commit 6d7f5a7

Please sign in to comment.