Skip to main content

jam_primitives/
crypto.rs

1//! # Cryptographic Primitives for JAM Protocol
2//!
3//! This module provides cryptographic types and operations used throughout the JAM protocol.
4
5use crate::types::Hash;
6use crate::utils::codec::{Decode, Encode};
7use serde::{Deserialize, Serialize};
8
9/// Ed25519 public key (32 bytes)
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
11pub struct Ed25519PublicKey(pub [u8; 32]);
12
13impl From<[u8; 32]> for Ed25519PublicKey {
14    fn from(bytes: [u8; 32]) -> Self {
15        Self(bytes)
16    }
17}
18
19impl AsRef<[u8]> for Ed25519PublicKey {
20    fn as_ref(&self) -> &[u8] {
21        &self.0
22    }
23}
24
25/// Ed25519 signature (64 bytes)
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encode, Decode)]
27pub struct Ed25519Signature(pub [u8; 64]);
28
29impl From<[u8; 64]> for Ed25519Signature {
30    fn from(bytes: [u8; 64]) -> Self {
31        Self(bytes)
32    }
33}
34
35impl serde::Serialize for Ed25519Signature {
36    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
37    where
38        S: serde::Serializer,
39    {
40        serde_bytes::serialize(&self.0, serializer)
41    }
42}
43
44impl<'de> serde::Deserialize<'de> for Ed25519Signature {
45    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
46    where
47        D: serde::Deserializer<'de>,
48    {
49        let bytes: Vec<u8> = serde_bytes::deserialize(deserializer)?;
50        if bytes.len() != 64 {
51            return Err(serde::de::Error::custom("Invalid signature length"));
52        }
53        let mut array = [0u8; 64];
54        array.copy_from_slice(&bytes);
55        Ok(Self(array))
56    }
57}
58
59/// Bandersnatch cryptographic operations module
60pub mod bandersnatch {
61    use super::*;
62
63    /// Bandersnatch public key (32 bytes)
64    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
65    pub struct PublicKey(pub [u8; 32]);
66
67    impl From<[u8; 32]> for PublicKey {
68        fn from(bytes: [u8; 32]) -> Self {
69            Self(bytes)
70        }
71    }
72
73    impl AsRef<[u8]> for PublicKey {
74        fn as_ref(&self) -> &[u8] {
75            &self.0
76        }
77    }
78
79    /// Bandersnatch signature (64 bytes)
80    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encode, Decode)]
81    pub struct Signature(pub [u8; 64]);
82
83    impl From<[u8; 64]> for Signature {
84        fn from(bytes: [u8; 64]) -> Self {
85            Self(bytes)
86        }
87    }
88
89    impl serde::Serialize for Signature {
90        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
91        where
92            S: serde::Serializer,
93        {
94            serde_bytes::serialize(&self.0, serializer)
95        }
96    }
97
98    impl<'de> serde::Deserialize<'de> for Signature {
99        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
100        where
101            D: serde::Deserializer<'de>,
102        {
103            let bytes: Vec<u8> = serde_bytes::deserialize(deserializer)?;
104            if bytes.len() != 64 {
105                return Err(serde::de::Error::custom("Invalid signature length"));
106            }
107            let mut array = [0u8; 64];
108            array.copy_from_slice(&bytes);
109            Ok(Self(array))
110        }
111    }
112}
113
114/// Hashing operations module
115pub mod hashing {
116    use super::*;
117
118    /// Trait for cryptographic hashers
119    pub trait Hasher {
120        fn new() -> Self;
121        fn hash(&self, data: &[u8]) -> Hash;
122        fn update(&mut self, data: &[u8]);
123        fn finalize(self) -> Hash;
124    }
125
126    /// Blake3 hasher implementation
127    pub struct Blake3Hasher {
128        hasher: blake3::Hasher,
129    }
130
131    impl Hasher for Blake3Hasher {
132        fn new() -> Self {
133            Self {
134                hasher: blake3::Hasher::new(),
135            }
136        }
137
138        fn hash(&self, data: &[u8]) -> Hash {
139            let hash = blake3::hash(data);
140            Hash(*hash.as_bytes())
141        }
142
143        fn update(&mut self, data: &[u8]) {
144            self.hasher.update(data);
145        }
146
147        fn finalize(self) -> Hash {
148            let hash = self.hasher.finalize();
149            Hash(*hash.as_bytes())
150        }
151    }
152
153    impl Default for Blake3Hasher {
154        fn default() -> Self {
155            Self::new()
156        }
157    }
158}
159
160/// Signature verification operations
161pub mod signature {
162    use super::*;
163
164    /// Trait for signature verification
165    pub trait Verify {
166        type PublicKey;
167        type Signature;
168
169        fn verify(
170            public_key: &Self::PublicKey,
171            signature: &Self::Signature,
172            message: &[u8],
173        ) -> bool;
174    }
175
176    /// Ed25519 signature verification
177    pub struct Ed25519Verifier;
178
179    impl Verify for Ed25519Verifier {
180        type PublicKey = Ed25519PublicKey;
181        type Signature = Ed25519Signature;
182
183        fn verify(
184            _public_key: &Self::PublicKey,
185            _signature: &Self::Signature,
186            _message: &[u8],
187        ) -> bool {
188            // TODO: Implement actual Ed25519 signature verification
189            // For now, return false as a placeholder
190            // This should be implemented properly with the correct ed25519-dalek API
191            false
192        }
193    }
194}
195
196#[cfg(test)]
197mod tests {
198    use super::*;
199    use hashing::*;
200
201    #[test]
202    fn test_blake3_hasher() {
203        let hasher = Blake3Hasher::new();
204        let data = b"hello world";
205        let hash1 = hasher.hash(data);
206        let hash2 = hasher.hash(data);
207        assert_eq!(hash1, hash2);
208    }
209
210    #[test]
211    fn test_ed25519_public_key() {
212        let bytes = [1u8; 32];
213        let key = Ed25519PublicKey::from(bytes);
214        assert_eq!(key.as_ref(), &bytes);
215    }
216
217    #[test]
218    fn test_bandersnatch_public_key() {
219        let bytes = [2u8; 32];
220        let key = bandersnatch::PublicKey::from(bytes);
221        assert_eq!(key.as_ref(), &bytes);
222    }
223}