Skip to content

Commit

Permalink
refactor: fixing issues with networking
Browse files Browse the repository at this point in the history
  • Loading branch information
tbraun96 committed Dec 17, 2024
1 parent 48c99a6 commit 4f1f56f
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 1,020 deletions.
774 changes: 26 additions & 748 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ dashmap = { version = "6.1.0", default-features = false }
lru-mem = { version = "0.3.0", default-features = false }

# Metrics
metrics = { version = "0.24" }
metrics = { version = "0.24.1", default-features = false }
metrics-exporter-prometheus = { version = "0.16" }
prometheus = { version = "0.13.4", default-features = false }

Expand Down
5 changes: 3 additions & 2 deletions crates/crypto/bls/src/w3f_bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ macro_rules! impl_w3f_serde {
&self,
serializer: S,
) -> core::result::Result<S::Ok, S::Error> {
serializer.serialize_bytes(&to_bytes(self.0.clone()))
let bytes = to_bytes(self.0.clone());
Vec::serialize(&bytes, serializer)
}
}

Expand All @@ -45,7 +46,7 @@ macro_rules! impl_w3f_serde {
where
D: serde::Deserializer<'de>,
{
// Deserialize as bytes
// Deserialize as Vec
let bytes = <gadget_std::vec::Vec<u8>>::deserialize(deserializer)?;

// Convert bytes back to inner type
Expand Down
88 changes: 88 additions & 0 deletions crates/crypto/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,91 @@ pub trait KeyType: Sized + 'static {
) -> Result<Self::Signature, Self::Error>;
fn verify(public: &Self::Public, msg: &[u8], signature: &Self::Signature) -> bool;
}

#[macro_export]
macro_rules! impl_crypto_tests {
($ecdsa_type:ty, $signing_key:ty, $signature:ty) => {
use $crate::KeyType;
#[test]
fn test_key_generation() {
// Test random key generation
let secret = <$ecdsa_type>::generate_with_seed(None).unwrap();
let _public = <$ecdsa_type>::public_from_secret(&secret);

// Test deterministic key generation with seed
let seed = [1u8; 32];
let secret1 = <$ecdsa_type>::generate_with_seed(Some(&seed)).unwrap();
let secret2 = <$ecdsa_type>::generate_with_seed(Some(&seed)).unwrap();
assert_eq!(secret1, secret2, "Deterministic key generation should produce same keys");

// Test key generation from string
let hex_string = hex::encode(&seed);
let secret3 = <$ecdsa_type>::generate_with_string(hex_string).unwrap();
assert_eq!(secret1, secret3, "String-based key generation should match seed-based");
}

#[test]
fn test_signing_and_verification() {
let mut secret = <$ecdsa_type>::generate_with_seed(None).unwrap();
let public = <$ecdsa_type>::public_from_secret(&secret);

// Test normal signing
let message = b"Hello, world!";
let signature = <$ecdsa_type>::sign_with_secret(&mut secret, message).unwrap();
assert!(<$ecdsa_type>::verify(&public, message, &signature),
"Signature verification failed");

// Test pre-hashed signing
let hashed_msg = [42u8; 32];
let signature = <$ecdsa_type>::sign_with_secret_pre_hashed(&mut secret, &hashed_msg).unwrap();

// Verify with wrong message should fail
let wrong_message = b"Wrong message";
assert!(!<$ecdsa_type>::verify(&public, wrong_message, &signature),
"Verification should fail with wrong message");
}

#[test]
fn test_key_serialization() {
let secret = <$ecdsa_type>::generate_with_seed(None).unwrap();
let public = <$ecdsa_type>::public_from_secret(&secret);

// Test signing key serialization
let serialized = serde_json::to_string(&secret).unwrap();
let deserialized: $signing_key = serde_json::from_str(&serialized).unwrap();
assert_eq!(secret, deserialized, "SigningKey serialization roundtrip failed");

// Test verifying key serialization
let serialized = serde_json::to_string(&public).unwrap();
let deserialized = serde_json::from_str(&serialized).unwrap();
assert_eq!(public, deserialized, "VerifyingKey serialization roundtrip failed");
}

#[test]
fn test_signature_serialization() {
let mut secret = <$ecdsa_type>::generate_with_seed(None).unwrap();
let message = b"Test message";
let signature = <$ecdsa_type>::sign_with_secret(&mut secret, message).unwrap();

// Test signature serialization
let serialized = serde_json::to_string(&signature).unwrap();
let deserialized: $signature = serde_json::from_str(&serialized).unwrap();
assert_eq!(signature, deserialized, "Signature serialization roundtrip failed");
}

#[test]
fn test_key_comparison() {
let secret1 = <$ecdsa_type>::generate_with_seed(None).unwrap();
let secret2 = <$ecdsa_type>::generate_with_seed(None).unwrap();
let public1 = <$ecdsa_type>::public_from_secret(&secret1);
let public2 = <$ecdsa_type>::public_from_secret(&secret2);

// Test Ord implementation
assert!(public1 != public2, "Different keys should not be equal");
assert_eq!(public1.cmp(&public1), gadget_std::cmp::Ordering::Equal);

// Verify consistency between PartialOrd and Ord
assert_eq!(public1.partial_cmp(&public2), Some(public1.cmp(&public2)));
}
};
}
13 changes: 10 additions & 3 deletions crates/crypto/ed25519/src/zebra_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ macro_rules! impl_zebra_serde {
S: serde::Serializer,
{
// Get the raw bytes
let bytes = self.0.as_ref();
serializer.serialize_bytes(bytes)
let bytes = self.0.as_ref().to_vec();
Vec::serialize(&bytes, serializer)
}
}

Expand Down Expand Up @@ -75,7 +75,7 @@ pub struct Ed25519Signature(pub ed25519_zebra::Signature);

impl PartialOrd for Ed25519Signature {
fn partial_cmp(&self, other: &Self) -> Option<gadget_std::cmp::Ordering> {
self.0.to_bytes().partial_cmp(&other.0.to_bytes())
Some(self.cmp(other))
}
}

Expand Down Expand Up @@ -159,3 +159,10 @@ impl KeyType for Ed25519Zebra {
public.0.verify(&signature.0, msg).is_ok()
}
}

#[cfg(test)]
mod tests {
use super::*;
// Generate tests for K256 ECDSA
gadget_crypto_core::impl_crypto_tests!(Ed25519Zebra, Ed25519SigningKey, Ed25519Signature);
}
4 changes: 2 additions & 2 deletions crates/crypto/k256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ edition = "2021"
[dependencies]
gadget-crypto-core = { workspace = true, features = ["k256"] }
gadget-std = { workspace = true }
k256 = { workspace = true, features = ["ecdsa", "alloc"] }
k256 = { workspace = true, features = ["ecdsa", "alloc", "serde", "pem"] }
serde = { workspace = true }
serde_json = { workspace = true, features = ["alloc"] }
serde_json = { workspace = true }
thiserror = { workspace = true }
hex = { workspace = true, features = ["alloc"] }

Expand Down
40 changes: 12 additions & 28 deletions crates/crypto/k256/src/k256_ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ use gadget_crypto_core::{KeyType, KeyTypeId};
use gadget_std::UniformRand;
use gadget_std::{
string::{String, ToString},
vec::Vec,
};
use k256::ecdsa::signature::SignerMut;

use serde::{Deserialize, Serialize};
use crate::error::{K256Error, Result};

/// ECDSA key type
pub struct K256Ecdsa;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct K256VerifyingKey(pub k256::ecdsa::VerifyingKey);

impl PartialOrd for K256VerifyingKey {
Expand All @@ -26,28 +25,6 @@ impl Ord for K256VerifyingKey {
}
}

impl serde::Serialize for K256VerifyingKey {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let bytes = self.0.to_sec1_bytes();
serializer.serialize_bytes(&bytes)
}
}

impl<'de> serde::Deserialize<'de> for K256VerifyingKey {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes = <Vec<u8>>::deserialize(deserializer)?;
let verifying_key = k256::ecdsa::VerifyingKey::from_sec1_bytes(&bytes)
.map_err(|e| serde::de::Error::custom(e.to_string()))?;
Ok(K256VerifyingKey(verifying_key))
}
}

macro_rules! impl_serde_bytes {
($wrapper:ident, $inner:path) => {
#[derive(Clone, PartialEq, Eq, Debug)]
Expand All @@ -70,8 +47,8 @@ macro_rules! impl_serde_bytes {
where
S: serde::Serializer,
{
let bytes = self.0.to_bytes();
serializer.serialize_bytes(&bytes)
let bytes = self.0.to_bytes().to_vec();
Vec::serialize(&bytes, serializer)
}
}

Expand All @@ -93,8 +70,8 @@ impl_serde_bytes!(K256SigningKey, k256::ecdsa::SigningKey);
impl_serde_bytes!(K256Signature, k256::ecdsa::Signature);

impl KeyType for K256Ecdsa {
type Public = K256VerifyingKey;
type Secret = K256SigningKey;
type Public = K256VerifyingKey;
type Signature = K256Signature;
type Error = K256Error;

Expand Down Expand Up @@ -159,3 +136,10 @@ impl K256SigningKey {
self.verifying_key()
}
}

#[cfg(test)]
mod tests {
use super::*;
// Generate tests for K256 ECDSA
gadget_crypto_core::impl_crypto_tests!(K256Ecdsa, K256SigningKey, K256Signature);
}
23 changes: 1 addition & 22 deletions crates/crypto/sp-core/src/sp_core_bls_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{impl_sp_core_key_type, impl_sp_core_pair_public};
macro_rules! impl_sp_core_bls_signature {
($key_type:ident, $signature:ty) => {
paste::paste! {
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct [<$key_type Signature>](pub $signature);

impl PartialOrd for [<$key_type Signature>] {
Expand All @@ -37,27 +37,6 @@ macro_rules! impl_sp_core_bls_signature {
write!(f, "{:?}", bytes)
}
}

impl serde::Serialize for [<$key_type Signature>] {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_bytes(self.0.as_ref())
}
}

impl<'de> serde::Deserialize<'de> for [<$key_type Signature>] {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes = <Vec<u8>>::deserialize(deserializer)?;
let sig = <$signature>::from_slice(&bytes)
.map_err(|_| serde::de::Error::custom("Invalid signature length"))?;
Ok([<$key_type Signature>](sig))
}
}
}
};
}
Expand Down
49 changes: 3 additions & 46 deletions crates/crypto/sp-core/src/sp_core_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ macro_rules! impl_sp_core_pair_public {
S: serde::Serializer,
{
let bytes = self.0.to_raw_vec();
serializer.serialize_bytes(&bytes)
Vec::serialize(&bytes, serializer)
}
}

Expand All @@ -69,7 +69,7 @@ macro_rules! impl_sp_core_pair_public {
}

/// Wrapper struct for the cryptographic public key.
#[derive(Clone)]
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct [<$key_type Public>](pub <$pair_type as sp_core::Pair>::Public);

impl PartialEq for [<$key_type Public>]{
Expand Down Expand Up @@ -97,28 +97,6 @@ macro_rules! impl_sp_core_pair_public {
write!(f, "{:?}", self.0.to_raw_vec())
}
}

impl serde::Serialize for [<$key_type Public>]{
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let bytes = self.0.to_raw_vec();
serializer.serialize_bytes(&bytes)
}
}

impl<'de> serde::Deserialize<'de> for [<$key_type Public>]{
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes = <Vec<u8>>::deserialize(deserializer)?;
let public = <$pair_type as sp_core::Pair>::Public::from_slice(&bytes)
.map_err(|_| serde::de::Error::custom("Invalid public key length"))?;
Ok([<$key_type Public>](public))
}
}
}
};
}
Expand All @@ -127,7 +105,7 @@ macro_rules! impl_sp_core_pair_public {
macro_rules! impl_sp_core_signature {
($key_type:ident, $pair_type:ty) => {
paste::paste! {
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct [<$key_type Signature>](pub <$pair_type as sp_core::Pair>::Signature);

impl PartialOrd for [<$key_type Signature>] {
Expand All @@ -147,27 +125,6 @@ macro_rules! impl_sp_core_signature {
write!(f, "{:?}", self.0.0)
}
}

impl serde::Serialize for [<$key_type Signature>] {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_bytes(self.0.as_ref())
}
}

impl<'de> serde::Deserialize<'de> for [<$key_type Signature>] {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes = <Vec<u8>>::deserialize(deserializer)?;
let sig = <$pair_type as sp_core::Pair>::Signature::from_slice(&bytes)
.ok_or_else(|| serde::de::Error::custom("Invalid signature length"))?;
Ok([<$key_type Signature>](sig))
}
}
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion crates/crypto/sr25519/src/schnorrkel_sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ macro_rules! impl_schnorrkel_serde {
&self,
serializer: S,
) -> core::result::Result<S::Ok, S::Error> {
serializer.serialize_bytes(&self.0.to_bytes())
let bytes = self.0.to_bytes().into();
Vec::serialize(&bytes, serializer)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/metrics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "metrics"
name = "gadget-metrics"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
Expand Down
1 change: 0 additions & 1 deletion crates/networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ serde = { workspace = true }
serde_json = { workspace = true, features = ["alloc"] }
hex = { workspace = true }
itertools = { workspace = true, features = ["use_alloc"] }
matchbox_socket = { workspace = true }
parking_lot = { workspace = true }
thiserror = { workspace = true }

Expand Down
Loading

0 comments on commit 4f1f56f

Please sign in to comment.