saorsa-core 0.23.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# Saorsa Core API Reference

This document provides a comprehensive guide to the saorsa-core public API.

## Table of Contents

- [Phonebook & Trust Signals]#phonebook--trust-signals
- [DHT Operations]#dht-operations
- [Network & Transport]#network--transport
- [Cryptography]#cryptography
- [Trust & Reputation]#trust--reputation
- [Bootstrap & Discovery]#bootstrap--discovery
- [Configuration]#configuration

---

## Phonebook & Trust Signals

saorsa-core uses the DHT strictly as a **peer phonebook** (routing + peer records).
Application data storage is handled in **saorsa-node** via `send_message`-style APIs.

To keep reputation accurate, saorsa-node reports data availability outcomes back to
saorsa-core’s trust engine:

```rust
use saorsa_core::adaptive::{EigenTrustEngine, NodeStatisticsUpdate};

// On successful data fetch:
trust_engine
    .update_node_stats(&peer_id, NodeStatisticsUpdate::CorrectResponse)
    .await;

// On failure:
trust_engine
    .update_node_stats(&peer_id, NodeStatisticsUpdate::FailedResponse)
    .await;
```

---

## Data Replication Flow (saorsa-node)

saorsa-core does **not** replicate application data. saorsa-node is responsible for:
1. Storing chunks locally and tracking replica sets.
2. Selecting target peers using saorsa-core’s adaptive routing outputs.
3. Replicating via `send_message` and updating trust based on outcomes.
4. Reacting to churn and re‑replicating when peers drop.

Recommended wiring (using `ReplicaPlanner`):
```rust
use saorsa_core::{
    adaptive::ReplicaPlanner,
    DhtNetworkManager,
};

// 1) Subscribe to churn signals
let planner = ReplicaPlanner::new(adaptive_dht, dht_manager);
let mut events = planner.subscribe_churn();
tokio::spawn(async move {
    while let Ok(event) = events.recv().await {
        if let saorsa_core::DhtNetworkEvent::PeerDisconnected { peer_id } = event {
            // saorsa-node should re-replicate any data that had replicas on peer_id
        }
    }
});

// 2) Choose replica targets (routing-only)
let targets = planner
    .select_replica_targets(content_hash, replica_count)
    .await?;

// 3) Replicate over send_message (saorsa-node chunk protocol)
// 4) Report success/failure back to EigenTrust
```

---

## DHT Operations

### DHT Network Manager

High-level DHT operations with network integration. Use this for **peer discovery**
and routing. Application data should travel over `send_message` in saorsa-node.

```rust
use saorsa_core::{DhtNetworkManager, DhtNetworkConfig, Key};

// Create manager
let config = DhtNetworkConfig::default();
let manager = DhtNetworkManager::new(config).await?;

// Find closest peers to a key (peer routing / phonebook lookups)
let key: Key = *blake3::hash(b\"peer-id\").as_bytes();
let peers = manager.find_closest_nodes(&key, 8).await?;
```

### AdaptiveDHT (Recommended)

Adaptive DHT for peer routing that enforces layered scoring (trust, geo, churn,
hyperbolic, SOM). Use this for **phonebook/routing**, not application data storage.

```rust
use saorsa_core::adaptive::{AdaptiveDHT, AdaptiveDhtConfig, AdaptiveDhtDependencies};
use saorsa_core::{DhtNetworkConfig, P2PNode};
use std::sync::Arc;

// Create your P2P node and DHT network config
let node = Arc::new(P2PNode::new(node_config).await?);
let dht_net = DhtNetworkConfig::default();

// Dependencies can be provided from your adaptive stack
let deps = AdaptiveDhtDependencies::with_defaults(identity, trust_provider);

// Attach AdaptiveDHT to the running node
let dht = AdaptiveDHT::attach_to_node(node, dht_net, AdaptiveDhtConfig::default(), deps).await?;

// Store and retrieve
let key = *blake3::hash(b\"example-key\").as_bytes();
dht.put(key, b\"example-value\".to_vec()).await?;
let value = dht.get(key).await?;
```

### Low-Level DHT

Direct DHT operations.

```rust
use saorsa_core::dht::{Key, Record, DHTConfig};

// Create key from bytes
let key: Key = *blake3::hash(b\"content-hash\").as_bytes();

// Create record
let record = Record::new(key, data, "peer-id".to_string());
```

### DHT Subscriptions

Watch for changes to DHT keys.

```rust
use saorsa_core::dht_watch;

let mut subscription = dht_watch(&key).await?;

while let Some(event) = subscription.recv().await {
    match event {
        DhtEvent::ValueChanged(new_value) => println!("Updated: {:?}", new_value),
        DhtEvent::Expired => println!("Key expired"),
    }
}
```

---

## Network & Transport

### P2P Node

Create and run a P2P node.

```rust
use saorsa_core::{P2PNode, NodeConfig};

// Using builder pattern
let config = NodeConfig::builder()
    .port(9000)
    .bootstrap_peer("192.168.1.1:9000".parse()?)
    .build()?;
let node = P2PNode::new(config).await?;

// Start the node
node.start().await?;
```

### Connection Events

Subscribe to connection events.

```rust
use saorsa_core::{subscribe_topology, TopologyEvent};

let mut subscription = subscribe_topology().await?;

while let Some(event) = subscription.recv().await {
    match event {
        TopologyEvent::PeerConnected(peer_id) => {
            println!("Connected: {}", peer_id);
        }
        TopologyEvent::PeerDisconnected(peer_id) => {
            println!("Disconnected: {}", peer_id);
        }
    }
}
```

---

## Cryptography

### Post-Quantum Key Generation

Generate ML-DSA-65 and ML-KEM-768 key pairs.

```rust
use saorsa_core::{MlDsa65, MlKem768, MlDsaOperations, MlKemOperations};

// Signature keypair (ML-DSA-65)
let (signing_pk, signing_sk) = MlDsa65::generate_keypair()?;

// Key exchange keypair (ML-KEM-768)
let (kem_pk, kem_sk) = MlKem768::generate_keypair()?;
```

### Digital Signatures

Sign and verify with ML-DSA-65.

```rust
use saorsa_core::{MlDsa65, MlDsaOperations};

// Sign message
let message = b"Hello, quantum-safe world!";
let signature = MlDsa65::sign(&signing_sk, message)?;

// Verify signature
let valid = MlDsa65::verify(&signing_pk, message, &signature)?;
assert!(valid);
```

### Key Encapsulation

Establish shared secrets with ML-KEM-768.

```rust
use saorsa_core::{MlKem768, MlKemOperations};

// Sender encapsulates
let (ciphertext, shared_secret_sender) = MlKem768::encapsulate(&recipient_pk)?;

// Recipient decapsulates
let shared_secret_recipient = MlKem768::decapsulate(&recipient_sk, &ciphertext)?;

// Both have the same shared secret
assert_eq!(shared_secret_sender, shared_secret_recipient);
```

### Symmetric Encryption

Encrypt data with ChaCha20-Poly1305.

```rust
use saorsa_core::{ChaCha20Poly1305Cipher, SymmetricKey};

// Create cipher with key
let key = SymmetricKey::generate();
let cipher = ChaCha20Poly1305Cipher::new(&key);

// Encrypt
let plaintext = b"Secret message";
let encrypted = cipher.encrypt(plaintext)?;

// Decrypt
let decrypted = cipher.decrypt(&encrypted)?;
assert_eq!(plaintext, &decrypted[..]);
```

### Secure Memory

Protect sensitive data in memory.

```rust
use saorsa_core::{SecureVec, SecureString, secure_vec_with_capacity};

// Secure vector (zeroed on drop)
let mut secret_key = secure_vec_with_capacity(32);
secret_key.extend_from_slice(&key_bytes);

// Secure string
let password = SecureString::from("my-secret-password");

// Memory is automatically zeroed when dropped
```

---

## Trust & Reputation

### EigenTrust Scores

Query reputation scores for peers via P2PNode.

```rust
// Get trust score (0.0 - 1.0)
let score = node.peer_trust(&peer_id);
```

### Node Age Verification

Check node age for privilege levels.

```rust
use saorsa_core::{NodeAgeVerifier, NodeAgeConfig, OperationType};

let verifier = NodeAgeVerifier::new(NodeAgeConfig::default());

// Check if node can perform operation
let result = verifier.verify_operation(&peer_id, OperationType::FullRouting)?;

match result {
    AgeVerificationResult::Allowed => println!("Operation permitted"),
    AgeVerificationResult::TooYoung { required_age } => {
        println!("Node must wait {} more seconds", required_age.as_secs());
    }
}
```

### IP Diversity Enforcement

Ensure geographic diversity.

```rust
use saorsa_core::{IPDiversityEnforcer, IPDiversityConfig};

let config = IPDiversityConfig {
    max_per_slash8: 0.25,   // Max 25% from any /8 subnet
    max_per_slash16: 0.10,  // Max 10% from any /16 subnet
    min_distinct_slash16: 5, // At least 5 distinct /16 subnets
};

let enforcer = IPDiversityEnforcer::new(config);

// Check if IP can be added
if enforcer.check_diversity(ip_addr) {
    // IP meets diversity requirements
}
```

---

## Bootstrap & Discovery

### Bootstrap Manager

Manage peer discovery cache.

```rust
use saorsa_core::{BootstrapManager, CacheConfig};
use std::path::PathBuf;

// Create with default config
let manager = BootstrapManager::new(PathBuf::from("~/.cache/saorsa")).await?;

// Add contact (with Sybil protection)
manager.add_contact("192.168.1.100:9000".parse()?).await?;

// Get bootstrap contacts
let contacts = manager.get_contacts(10).await;

// Record connection result
manager.record_connection_result(addr, true, Some(Duration::from_millis(50))).await;
```

### Bootstrap Configuration

Configure cache behavior.

```rust
use saorsa_core::bootstrap::CacheConfig;

let config = CacheConfig {
    cache_dir: PathBuf::from("~/.cache/saorsa"),
    max_contacts: 30_000,
    merge_interval: Duration::from_secs(60),
    cleanup_interval: Duration::from_secs(300),
    quality_update_interval: Duration::from_secs(60),
    stale_threshold: Duration::from_secs(86400),
    ..Default::default()
};
```

---

## Configuration

### Production Configuration

Configure for production deployment.

```rust
use saorsa_core::{ProductionConfig, Config};

let config = ProductionConfig {
    max_connections: 1000,
    max_memory_mb: 512,
    enable_metrics: true,
    metrics_port: 9090,
    ..Default::default()
};
```

### Health Monitoring

Enable health endpoints.

```rust
use saorsa_core::{HealthManager, HealthServer, PrometheusExporter};

// Create health manager
let health = HealthManager::new();

// Start health server
let server = HealthServer::new(health.clone());
server.start("0.0.0.0:8080").await?;

// Export Prometheus metrics
let exporter = PrometheusExporter::new(health);
let metrics = exporter.export()?;
```

### Rate Limiting

Configure join rate limits.

```rust
use saorsa_core::{JoinRateLimiter, JoinRateLimiterConfig};

let config = JoinRateLimiterConfig {
    per_ip_per_minute: 5,
    per_subnet24_per_minute: 20,
    per_subnet16_per_hour: 100,
    ..Default::default()
};

let limiter = JoinRateLimiter::new(config);

// Check rate limit
match limiter.check_rate(ip_addr) {
    Ok(()) => println!("Rate OK"),
    Err(e) => println!("Rate limited: {}", e),
}
```

---

## Error Handling

All operations return `Result<T, P2PError>`:

```rust
use saorsa_core::{DhtNetworkManager, P2PError, Result};

async fn example(manager: &DhtNetworkManager) -> Result<()> {
    let key = *blake3::hash(b"peer-id").as_bytes();
    let _peers = manager.find_closest_nodes(&key, 8).await.map_err(|e| {
        match e {
            P2PError::Timeout(_) => println!("Operation timed out"),
            P2PError::Network(e) => println!("Network error: {}", e),
            _ => println!("Other error: {}", e),
        }
        e
    })?;
    Ok(())
}
```

---

## Feature Flags

Enable optional features in `Cargo.toml`:

```toml
[dependencies]
saorsa-core = { version = "0.11", features = ["metrics"] }
```

| Feature | Description |
|---------|-------------|
| `metrics` | Prometheus metrics integration |

---

## Thread Safety

Most types are `Send + Sync` and can be shared across threads:

```rust
use std::sync::Arc;
use tokio::spawn;

let manager = Arc::new(DhtNetworkManager::new(config).await?);

let manager_clone = manager.clone();
spawn(async move {
    manager_clone.store(key, record).await?;
});
```

---

## Version Compatibility

| saorsa-core | saorsa-transport | Rust | Features |
|-------------|----------|------|----------|
| 0.11.x | 0.21.x | 1.75+ | Full PQC, placement system, threshold crypto |
| 0.10.x | 0.20.x | 1.75+ | Full PQC, unified config |
| 0.5.x | 0.14.x | 1.75+ | Legacy stable |

---

## See Also

- [Architecture Decision Records]./adr/ - Design decisions
- [Security Model]./SECURITY_MODEL.md - Security architecture
- [Auto-Upgrade System]./AUTO_UPGRADE.md - Binary updates