saorsa-core 0.20.0

Saorsa - Core P2P networking library with DHT, QUIC transport, and post-quantum cryptography
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright (c) 2025 Saorsa Labs Limited

// This file is part of the Saorsa P2P network.

// Licensed under the AGPL-3.0 license:
// <https://www.gnu.org/licenses/agpl-3.0.html>

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// Copyright 2024 P2P Foundation
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Peer Identity
//!
//! Implements the core identity system for P2P nodes with:
//! - ML-DSA-65 post-quantum cryptographic keys
//! - Four-word human-readable addresses
//! - Deterministic generation from seeds

use crate::error::IdentityError;
use crate::{P2PError, Result};
use saorsa_pqc::HkdfSha3_256;
use saorsa_pqc::api::sig::{MlDsa, MlDsaVariant};
use saorsa_pqc::api::traits::Kdf;
use serde::{Deserialize, Serialize};
use std::fmt;

// Import PQC types from saorsa_transport via quantum_crypto module
use crate::quantum_crypto::saorsa_transport_integration::{
    MlDsaPublicKey, MlDsaSecretKey, MlDsaSignature,
};

// Re-export canonical PeerId from the peer_id module.
pub use super::peer_id::{PEER_ID_BYTE_LEN, PeerId, PeerIdParseError};

/// Create a [`PeerId`] from an ML-DSA public key.
///
/// This is a standalone function because it depends on `MlDsaPublicKey`
/// from `saorsa-pqc`, which `saorsa-types` does not (and should not)
/// depend on.
pub fn peer_id_from_public_key(public_key: &MlDsaPublicKey) -> PeerId {
    let hash = blake3::hash(public_key.as_bytes());
    PeerId(*hash.as_bytes())
}

/// ML-DSA-65 public key length in bytes.
const ML_DSA_PUB_KEY_LEN: usize = 1952;

/// Create a [`PeerId`] from raw ML-DSA public key bytes.
///
/// # Errors
///
/// Returns an error if the byte slice is not exactly 1952 bytes or
/// cannot be parsed as a valid ML-DSA-65 public key.
pub fn peer_id_from_public_key_bytes(bytes: &[u8]) -> Result<PeerId> {
    if bytes.len() != ML_DSA_PUB_KEY_LEN {
        return Err(P2PError::Identity(IdentityError::InvalidFormat(
            "Invalid ML-DSA public key length".to_string().into(),
        )));
    }

    let public_key = MlDsaPublicKey::from_bytes(bytes).map_err(|e| {
        IdentityError::InvalidFormat(format!("Invalid ML-DSA public key: {:?}", e).into())
    })?;

    Ok(peer_id_from_public_key(&public_key))
}

/// Public node identity information (without secret keys) - safe to clone
#[derive(Clone)]
pub struct PublicNodeIdentity {
    /// ML-DSA public key
    public_key: MlDsaPublicKey,
    /// Peer ID derived from public key
    peer_id: PeerId,
}

impl PublicNodeIdentity {
    /// Get peer ID
    pub fn peer_id(&self) -> &PeerId {
        &self.peer_id
    }

    /// Get public key
    pub fn public_key(&self) -> &MlDsaPublicKey {
        &self.public_key
    }

    // Word addresses are not part of identity; use bootstrap/transport layers
}

/// Core node identity with cryptographic keys
///
/// `Debug` is manually implemented to redact secret key material.
pub struct NodeIdentity {
    /// ML-DSA-65 secret key (private)
    secret_key: MlDsaSecretKey,
    /// ML-DSA-65 public key
    public_key: MlDsaPublicKey,
    /// Peer ID derived from public key
    peer_id: PeerId,
}

impl fmt::Debug for NodeIdentity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("NodeIdentity")
            .field("peer_id", &self.peer_id)
            .field("secret_key", &"[REDACTED]")
            .finish()
    }
}

impl NodeIdentity {
    /// Generate new identity
    pub fn generate() -> Result<Self> {
        // Generate ML-DSA-65 key pair (saorsa-transport integration)
        let (public_key, secret_key) =
            crate::quantum_crypto::generate_ml_dsa_keypair().map_err(|e| {
                P2PError::Identity(IdentityError::InvalidFormat(
                    format!("Failed to generate ML-DSA key pair: {}", e).into(),
                ))
            })?;

        let peer_id = peer_id_from_public_key(&public_key);

        Ok(Self {
            secret_key,
            public_key,
            peer_id,
        })
    }

    /// Generate from seed (deterministic)
    pub fn from_seed(seed: &[u8; 32]) -> Result<Self> {
        // Derive a 32-byte ML-DSA seed from the input via HKDF-SHA3
        let mut xi = [0u8; 32];
        HkdfSha3_256::derive(seed, None, b"saorsa-node-identity-seed", &mut xi).map_err(|_| {
            P2PError::Identity(IdentityError::InvalidFormat("HKDF expand failed".into()))
        })?;

        // Generate a real ML-DSA-65 keypair deterministically from the seed
        let dsa = MlDsa::new(MlDsaVariant::MlDsa65);
        let (pk, sk) = dsa.generate_keypair_from_seed(&xi);

        let public_key = MlDsaPublicKey::from_bytes(&pk.to_bytes()).map_err(|e| {
            P2PError::Identity(IdentityError::InvalidFormat(
                format!("Invalid ML-DSA public key bytes: {e}").into(),
            ))
        })?;
        let secret_key = MlDsaSecretKey::from_bytes(&sk.to_bytes()).map_err(|e| {
            P2PError::Identity(IdentityError::InvalidFormat(
                format!("Invalid ML-DSA secret key bytes: {e}").into(),
            ))
        })?;

        let peer_id = peer_id_from_public_key(&public_key);

        Ok(Self {
            secret_key,
            public_key,
            peer_id,
        })
    }

    /// Get peer ID
    pub fn peer_id(&self) -> &PeerId {
        &self.peer_id
    }

    /// Get public key
    pub fn public_key(&self) -> &MlDsaPublicKey {
        &self.public_key
    }

    // No Proof-of-Work in this crate

    /// Get secret key bytes (for raw key authentication)
    pub fn secret_key_bytes(&self) -> &[u8] {
        self.secret_key.as_bytes()
    }

    /// Sign a message
    pub fn sign(&self, message: &[u8]) -> Result<MlDsaSignature> {
        crate::quantum_crypto::ml_dsa_sign(&self.secret_key, message).map_err(|e| {
            P2PError::Identity(IdentityError::InvalidFormat(
                format!("ML-DSA signing failed: {:?}", e).into(),
            ))
        })
    }

    /// Verify a signature
    pub fn verify(&self, message: &[u8], signature: &MlDsaSignature) -> Result<bool> {
        crate::quantum_crypto::ml_dsa_verify(&self.public_key, message, signature).map_err(|e| {
            P2PError::Identity(IdentityError::InvalidFormat(
                format!("ML-DSA verification failed: {:?}", e).into(),
            ))
        })
    }

    /// Create a public version of this identity (safe to clone)
    pub fn to_public(&self) -> PublicNodeIdentity {
        PublicNodeIdentity {
            public_key: self.public_key.clone(),
            peer_id: self.peer_id,
        }
    }
}

impl NodeIdentity {
    /// Create an identity from an existing secret key
    /// Note: Currently not supported as saorsa-transport doesn't provide public key derivation from secret key
    /// This would require storing both keys together
    pub fn from_secret_key(_secret_key: MlDsaSecretKey) -> Result<Self> {
        Err(P2PError::Identity(IdentityError::InvalidFormat(
            "Creating identity from secret key alone is not supported"
                .to_string()
                .into(),
        )))
    }
}

impl NodeIdentity {
    /// Save identity to a JSON file (async)
    pub async fn save_to_file(&self, path: &std::path::Path) -> Result<()> {
        use tokio::fs;
        let data = self.export();
        let json = serde_json::to_string_pretty(&data).map_err(|e| {
            P2PError::Identity(crate::error::IdentityError::InvalidFormat(
                format!("Failed to serialize identity: {}", e).into(),
            ))
        })?;

        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).await.map_err(|e| {
                P2PError::Identity(crate::error::IdentityError::InvalidFormat(
                    format!("Failed to create directory: {}", e).into(),
                ))
            })?;
        }

        tokio::fs::write(path, json).await.map_err(|e| {
            P2PError::Identity(crate::error::IdentityError::InvalidFormat(
                format!("Failed to write identity file: {}", e).into(),
            ))
        })?;
        Ok(())
    }

    /// Load identity from a JSON file (async)
    pub async fn load_from_file(path: &std::path::Path) -> Result<Self> {
        let json = tokio::fs::read_to_string(path).await.map_err(|e| {
            P2PError::Identity(crate::error::IdentityError::InvalidFormat(
                format!("Failed to read identity file: {}", e).into(),
            ))
        })?;
        let data: IdentityData = serde_json::from_str(&json).map_err(|e| {
            P2PError::Identity(crate::error::IdentityError::InvalidFormat(
                format!("Failed to deserialize identity: {}", e).into(),
            ))
        })?;
        Self::import(&data)
    }
}

/// Serializable identity data for persistence
#[derive(Serialize, Deserialize)]
pub struct IdentityData {
    /// ML-DSA secret key bytes (4032 bytes for ML-DSA-65)
    pub secret_key: Vec<u8>,
    /// ML-DSA public key bytes (1952 bytes for ML-DSA-65)
    pub public_key: Vec<u8>,
}

impl NodeIdentity {
    /// Export identity for persistence
    pub fn export(&self) -> IdentityData {
        IdentityData {
            secret_key: self.secret_key.as_bytes().to_vec(),
            public_key: self.public_key.as_bytes().to_vec(),
        }
    }

    /// Import identity from persisted data
    pub fn import(data: &IdentityData) -> Result<Self> {
        // Reconstruct keys from bytes
        let secret_key =
            crate::quantum_crypto::saorsa_transport_integration::MlDsaSecretKey::from_bytes(
                &data.secret_key,
            )
            .map_err(|e| {
                P2PError::Identity(IdentityError::InvalidFormat(
                    format!("Invalid ML-DSA secret key: {e}").into(),
                ))
            })?;
        let public_key =
            crate::quantum_crypto::saorsa_transport_integration::MlDsaPublicKey::from_bytes(
                &data.public_key,
            )
            .map_err(|e| {
                P2PError::Identity(IdentityError::InvalidFormat(
                    format!("Invalid ML-DSA public key: {e}").into(),
                ))
            })?;

        let peer_id = peer_id_from_public_key(&public_key);

        Ok(Self {
            secret_key,
            public_key,
            peer_id,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_peer_id_generation() {
        let (public_key, _secret_key) = crate::quantum_crypto::generate_ml_dsa_keypair()
            .expect("ML-DSA key generation should succeed");
        let peer_id = peer_id_from_public_key(&public_key);

        // Should be 32 bytes
        assert_eq!(peer_id.to_bytes().len(), 32);

        // Should be deterministic
        let peer_id2 = peer_id_from_public_key(&public_key);
        assert_eq!(peer_id, peer_id2);
    }

    #[test]
    fn test_xor_distance() {
        let id1 = PeerId([0u8; 32]);
        let mut id2_bytes = [0u8; 32];
        id2_bytes[0] = 0xFF;
        let id2 = PeerId(id2_bytes);

        let distance = id1.xor_distance(&id2);
        assert_eq!(distance[0], 0xFF);
        for byte in &distance[1..] {
            assert_eq!(*byte, 0);
        }
    }

    #[test]
    fn test_proof_of_work() {
        // PoW removed: this test no longer applicable
    }

    #[test]
    fn test_identity_generation() {
        let identity = NodeIdentity::generate().expect("Identity generation should succeed");

        // Test signing and verification
        let message = b"Hello, P2P!";
        let signature = identity.sign(message).unwrap();
        assert!(identity.verify(message, &signature).unwrap());

        // Wrong message should fail with original signature
        assert!(!identity.verify(b"Wrong message", &signature).unwrap());
    }

    #[test]
    fn test_deterministic_generation() {
        let seed = [0x42; 32];
        let identity1 = NodeIdentity::from_seed(&seed).expect("Identity from seed should succeed");
        let identity2 = NodeIdentity::from_seed(&seed).expect("Identity from seed should succeed");

        // Should generate same identity
        assert_eq!(identity1.peer_id, identity2.peer_id);
        assert_eq!(
            identity1.public_key().as_bytes(),
            identity2.public_key().as_bytes()
        );
    }

    #[test]
    fn test_identity_persistence() {
        let identity = NodeIdentity::generate().expect("Identity generation should succeed");

        // Export
        let data = identity.export();

        // Import
        let imported = NodeIdentity::import(&data).expect("Import should succeed with valid data");

        // Should be the same
        assert_eq!(identity.peer_id, imported.peer_id);
        assert_eq!(
            identity.public_key().as_bytes(),
            imported.public_key().as_bytes()
        );

        // Should be able to sign with imported identity
        let message = b"Test message";
        let signature = imported.sign(message);
        assert!(identity.verify(message, &signature.unwrap()).unwrap());
    }

    #[test]
    fn test_peer_id_display_full_hex() {
        let id = PeerId([0xAB; 32]);
        let display = format!("{}", id);
        assert_eq!(display.len(), 64);
        assert_eq!(display, "ab".repeat(32));
    }

    #[test]
    fn test_peer_id_ord() {
        let a = PeerId([0x00; 32]);
        let b = PeerId([0xFF; 32]);
        assert!(a < b);
    }

    #[test]
    fn test_peer_id_from_str() {
        let hex = "ab".repeat(32);
        let id: PeerId = hex.parse().expect("should parse valid hex");
        assert_eq!(id.0, [0xAB; 32]);
    }

    #[test]
    fn test_peer_id_json_roundtrip() {
        let id = PeerId([0xAB; 32]);
        let json = serde_json::to_string(&id).expect("serialize");
        assert_eq!(json, format!("\"{}\"", "ab".repeat(32)));
        let deserialized: PeerId = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(id, deserialized);
    }

    #[test]
    fn test_peer_id_postcard_roundtrip() {
        let id = PeerId([0xAB; 32]);
        let bytes = postcard::to_stdvec(&id).expect("serialize");
        let deserialized: PeerId = postcard::from_bytes(&bytes).expect("deserialize");
        assert_eq!(id, deserialized);
    }
}