An open API service indexing awesome lists of open source software.

https://github.com/dirvine/saorsa-gossip

PQC-secure gossip overlay network for Communitas
https://github.com/dirvine/saorsa-gossip

Last synced: 8 months ago
JSON representation

PQC-secure gossip overlay network for Communitas

Awesome Lists containing this project

README

          

# Saorsa Gossip Overlay

[![CI](https://github.com/dirvine/saorsa-gossip/workflows/CI/badge.svg)](https://github.com/dirvine/saorsa-gossip/actions)
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](LICENSE)
[![Rust Version](https://img.shields.io/badge/rust-1.75%2B-blue.svg)](https://www.rust-lang.org)

## ๐Ÿ“ฆ Published Crates

| Crate | Version | Docs | Downloads |
|-------|---------|------|-----------|
| [saorsa-gossip-types] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-types.svg)][types-crate] | [![Docs](https://docs.rs/saorsa-gossip-types/badge.svg)][types-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-types.svg)][types-crate] |
| [saorsa-gossip-identity] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-identity.svg)][identity-crate] | [![Docs](https://docs.rs/saorsa-gossip-identity/badge.svg)][identity-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-identity.svg)][identity-crate] |
| [saorsa-gossip-transport] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-transport.svg)][transport-crate] | [![Docs](https://docs.rs/saorsa-gossip-transport/badge.svg)][transport-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-transport.svg)][transport-crate] |
| [saorsa-gossip-membership] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-membership.svg)][membership-crate] | [![Docs](https://docs.rs/saorsa-gossip-membership/badge.svg)][membership-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-membership.svg)][membership-crate] |
| [saorsa-gossip-pubsub] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-pubsub.svg)][pubsub-crate] | [![Docs](https://docs.rs/saorsa-gossip-pubsub/badge.svg)][pubsub-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-pubsub.svg)][pubsub-crate] |
| [saorsa-gossip-coordinator] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-coordinator.svg)][coordinator-crate] | [![Docs](https://docs.rs/saorsa-gossip-coordinator/badge.svg)][coordinator-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-coordinator.svg)][coordinator-crate] |
| [saorsa-gossip-rendezvous] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-rendezvous.svg)][rendezvous-crate] | [![Docs](https://docs.rs/saorsa-gossip-rendezvous/badge.svg)][rendezvous-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-rendezvous.svg)][rendezvous-crate] |
| [saorsa-gossip-groups] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-groups.svg)][groups-crate] | [![Docs](https://docs.rs/saorsa-gossip-groups/badge.svg)][groups-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-groups.svg)][groups-crate] |
| [saorsa-gossip-presence] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-presence.svg)][presence-crate] | [![Docs](https://docs.rs/saorsa-gossip-presence/badge.svg)][presence-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-presence.svg)][presence-crate] |
| [saorsa-gossip-crdt-sync] | [![Crates.io](https://img.shields.io/crates/v/saorsa-gossip-crdt-sync.svg)][crdt-crate] | [![Docs](https://docs.rs/saorsa-gossip-crdt-sync/badge.svg)][crdt-docs] | [![Downloads](https://img.shields.io/crates/d/saorsa-gossip-crdt-sync.svg)][crdt-crate] |

[saorsa-gossip-types]: #core-crates
[saorsa-gossip-identity]: #core-crates
[saorsa-gossip-transport]: #core-crates
[saorsa-gossip-membership]: #core-crates
[saorsa-gossip-pubsub]: #core-crates
[saorsa-gossip-coordinator]: #core-crates
[saorsa-gossip-rendezvous]: #core-crates
[saorsa-gossip-groups]: #core-crates
[saorsa-gossip-presence]: #core-crates
[saorsa-gossip-crdt-sync]: #core-crates

[types-crate]: https://crates.io/crates/saorsa-gossip-types
[types-docs]: https://docs.rs/saorsa-gossip-types
[identity-crate]: https://crates.io/crates/saorsa-gossip-identity
[identity-docs]: https://docs.rs/saorsa-gossip-identity
[transport-crate]: https://crates.io/crates/saorsa-gossip-transport
[transport-docs]: https://docs.rs/saorsa-gossip-transport
[membership-crate]: https://crates.io/crates/saorsa-gossip-membership
[membership-docs]: https://docs.rs/saorsa-gossip-membership
[pubsub-crate]: https://crates.io/crates/saorsa-gossip-pubsub
[pubsub-docs]: https://docs.rs/saorsa-gossip-pubsub
[coordinator-crate]: https://crates.io/crates/saorsa-gossip-coordinator
[coordinator-docs]: https://docs.rs/saorsa-gossip-coordinator
[rendezvous-crate]: https://crates.io/crates/saorsa-gossip-rendezvous
[rendezvous-docs]: https://docs.rs/saorsa-gossip-rendezvous
[groups-crate]: https://crates.io/crates/saorsa-gossip-groups
[groups-docs]: https://docs.rs/saorsa-gossip-groups
[presence-crate]: https://crates.io/crates/saorsa-gossip-presence
[presence-docs]: https://docs.rs/saorsa-gossip-presence
[crdt-crate]: https://crates.io/crates/saorsa-gossip-crdt-sync
[crdt-docs]: https://docs.rs/saorsa-gossip-crdt-sync

A **post-quantum secure gossip overlay network** for decentralized peer-to-peer communication. Designed to replace DHT-based discovery with a contact-graph-aware gossip protocol, providing low-latency broadcast, partition tolerance, and quantum-resistant cryptography.

## ๐ŸŽฏ Overview

Saorsa Gossip implements a complete gossip overlay with:

- **Post-Quantum Cryptography**: ML-KEM-768 + ML-DSA-65 + ChaCha20-Poly1305 (FIPS 203/204)
- **QUIC Transport**: Low-latency, NAT-traversal with connection migration
- **MLS Group Security**: RFC 9420 compliant end-to-end encryption with ChaCha20-Poly1305
- **Gossip Protocols**: HyParView, SWIM, Plumtree for robust dissemination
- **Local-First CRDTs**: Delta-CRDTs with anti-entropy synchronization
- **No DHT**: Contact-graph-based discovery, no global directory

**Status**: โœ… **Production-Ready v0.1.6** - Complete post-quantum cryptography, deployable coordinator binary, 192 tests passing, zero compilation warnings (see [DESIGN.md](DESIGN.md))

## ๐Ÿ—๏ธ Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Saorsa Gossip โ”‚
โ”‚ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚Presence โ”‚ โ”‚ PubSub โ”‚ โ”‚ CRDT โ”‚ โ”‚ Groups โ”‚ โ”‚
โ”‚ โ”‚(Beacons)โ”‚ โ”‚(Plumtree)โ”‚ โ”‚ Sync โ”‚ โ”‚ (MLS) โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚ Membership Layer โ”‚ โ”‚
โ”‚ โ”‚ (HyParView + SWIM) โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”‚ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚ Transport Layer (ant-quic) โ”‚ โ”‚
โ”‚ โ”‚ QUIC + PQC TLS 1.3 + NAT Traversal โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

### Core Crates

All crates are published on [crates.io](https://crates.io) at version **0.1.6** with production-ready post-quantum cryptography.

| Crate | Purpose | Why It's Important |
|-------|---------|-------------------|
| [**types**](https://crates.io/crates/saorsa-gossip-types) | Core types (TopicId, PeerId, MessageHeader, wire formats) | **Foundation** - Defines all fundamental data structures and message formats used across the entire network. Includes BLAKE3-based message ID generation and CBOR wire serialization. |
| [**identity**](https://crates.io/crates/saorsa-gossip-identity) | ML-DSA-65 key generation, signing, and verification | **Security Core** - Provides quantum-resistant digital signatures for all messages. Every peer identity is backed by ML-DSA-65 keypairs, ensuring authenticity in a post-quantum world. |
| [**transport**](https://crates.io/crates/saorsa-gossip-transport) | QUIC transport with ant-quic, NAT traversal | **Network Layer** - Handles all peer-to-peer communication with low-latency QUIC streams. Includes hole-punching for NAT traversal and connection migration for mobile nodes. |
| [**membership**](https://crates.io/crates/saorsa-gossip-membership) | HyParView partial views + SWIM failure detection | **Peer Discovery** - Maintains partial views of the network (8-12 active peers, 64-128 passive). SWIM detects failures in <5s, HyParView heals partitions through periodic shuffles. Critical for network connectivity. |
| [**pubsub**](https://crates.io/crates/saorsa-gossip-pubsub) | Plumtree epidemic broadcast with EAGER/IHAVE/IWANT | **Message Dissemination** - Efficiently broadcasts messages to all topic subscribers. Uses spanning tree (EAGER) for low latency and lazy links (IHAVE) for redundancy. Achieves <500ms P50 broadcast latency. |
| [**coordinator**](https://crates.io/crates/saorsa-gossip-coordinator) | Bootstrap node discovery, address reflection, relay | **Network Bootstrap** - Enables new peers to join the network. Publishes Coordinator Adverts (ML-DSA signed), provides FOAF (friends-of-friends) discovery, and optional relay services for NAT-restricted peers. |
| [**rendezvous**](https://crates.io/crates/saorsa-gossip-rendezvous) | k=16 rendezvous sharding for global findability | **Global Discovery** - Implements 65,536 content-addressed shards (BLAKE3-based) for finding peers without DHTs. Providers publish signed summaries to deterministic shards, enabling discovery through capability queries. |
| [**groups**](https://crates.io/crates/saorsa-gossip-groups) | MLS group key derivation with BLAKE3 KDF | **Group Security** - Wraps MLS (RFC 9420) for end-to-end encrypted group messaging. Derives presence beaconing secrets from MLS exporter contexts using BLAKE3 keyed hashing. Essential for private group communication. |
| [**presence**](https://crates.io/crates/saorsa-gossip-presence) | MLS-derived beacon broadcasting, FOAF queries | **Online Detection** - Broadcasts encrypted presence beacons (10-15 min TTL) derived from group secrets. Enables "who's online" queries within groups and FOAF discovery (3-4 hop TTL). Privacy-preserving through MLS encryption. |
| [**crdt-sync**](https://crates.io/crates/saorsa-gossip-crdt-sync) | Delta-CRDTs (OR-Set, LWW-Register) with anti-entropy | **Local-First Data** - Provides conflict-free replicated data types for distributed state. OR-Set tracks membership, LWW-Register for scalar values. Delta-based sync minimizes bandwidth. Anti-entropy every 30s ensures eventual consistency. |

**Why these crates matter together**: They form a complete decentralized gossip network stack - from quantum-resistant identities and QUIC transport, through membership and broadcast protocols, to group encryption and local-first data sync. No DHT, no central servers, pure peer-to-peer with post-quantum security.

## ๐ŸŽฎ Running a Test Network

Saorsa Gossip provides two production-ready binaries for testing and deployment:

### ๐Ÿ“ฆ Deployable Binaries

| Binary | Crate | Purpose |
|--------|-------|---------|
| `saorsa-gossip-coordinator` | [saorsa-coordinator](https://crates.io/crates/saorsa-coordinator) | Bootstrap/coordinator node for network discovery |
| `saorsa-gossip` | [saorsa-gossip](https://crates.io/crates/saorsa-gossip) | CLI tool for testing all network features |

### Installation

Install both binaries from crates.io:

```bash
# Install coordinator binary (provides saorsa-gossip-coordinator command)
cargo install saorsa-coordinator

# Install CLI tool (provides saorsa-gossip command)
cargo install saorsa-gossip
```

Or build from source:

```bash
# Clone repository
git clone https://github.com/dirvine/saorsa-gossip.git
cd saorsa-gossip

# Build both binaries
cargo build --release -p saorsa-coordinator -p saorsa-gossip

# Binaries available at:
# - target/release/saorsa-gossip-coordinator
# - target/release/saorsa-gossip
```

### ๐Ÿš€ Starting a Coordinator Node

Coordinators provide bootstrap discovery for new peers joining the network:

```bash
# Start a coordinator on port 7000 with verbose logging
saorsa-gossip-coordinator \
--verbose \
--bind 0.0.0.0:7000 \
--roles coordinator,reflector,relay \
--publish-interval 60
```

**Options:**
- `--bind ` - Address to bind to (default: `0.0.0.0:7000`)
- `--roles ` - Comma-separated roles: `coordinator`, `reflector`, `relay`, `rendezvous`
- `--publish-interval ` - Advert publish interval in seconds (default: 300)
- `--identity-path ` - Path to ML-DSA identity file (default: `~/.saorsa-gossip/coordinator.identity`)
- `--verbose` - Enable verbose DEBUG logging

**Roles Explained:**
- **coordinator**: Publishes signed coordinator adverts for bootstrap discovery
- **reflector**: Provides address reflection for NAT traversal (observes peers' public IPs)
- **relay**: Relays messages for NAT-restricted peers (optional, bandwidth-intensive)
- **rendezvous**: Provides rendezvous sharding for global peer discovery (future)

**What the coordinator does:**
1. Generates or loads an ML-DSA-65 identity (32-byte PeerId)
2. Publishes signed coordinator adverts every N seconds (~3.5KB CBOR messages)
3. Provides address reflection for peers behind NAT
4. Logs all activity with timestamps (INFO + DEBUG levels)

**Example output:**
```
INFO Starting Saorsa Gossip Coordinator
INFO Bind address: 0.0.0.0:7000
INFO Roles: coordinator,reflector,relay
INFO Loaded identity: c6333dcf4207a805989f9743e8b42d8e38ea35b085b2d54e80103f2c9725d41f
INFO Coordinator advert publisher started (interval: 60s)
DEBUG Published coordinator advert (3551 bytes)
```

### ๐Ÿงช Using the CLI Tool

The `saorsa-gossip` CLI exercises all library features:

#### Identity Management

```bash
# Create a new ML-DSA identity
saorsa-gossip identity create --alias Alice

# List all identities in keystore
saorsa-gossip identity list

# Show identity details
saorsa-gossip identity show Alice

# Delete an identity
saorsa-gossip identity delete Alice
```

**Output example:**
```
โœ“ Created identity: Alice
PeerId: e4338043f8a848e62110892ca8321f25fad745a615f9dd30f7515aba93988d7a
Saved to: /Users/you/.saorsa-gossip/keystore
```

#### Network Operations

```bash
# Join the gossip network via coordinator
saorsa-gossip network join \
--coordinator 127.0.0.1:7000 \
--identity Alice \
--bind 0.0.0.0:0

# Show network status
saorsa-gossip network status

# List known peers
saorsa-gossip network peers
```

#### PubSub Messaging

```bash
# Subscribe to a topic
saorsa-gossip pubsub subscribe --topic news

# Publish a message
saorsa-gossip pubsub publish --topic news --message "Hello, gossip!"

# List subscriptions
saorsa-gossip pubsub list
```

#### Presence Beacons

```bash
# Start broadcasting presence
saorsa-gossip presence start --topic general

# Check who's online
saorsa-gossip presence online --topic general

# Stop broadcasting
saorsa-gossip presence stop --topic general
```

### ๐ŸŒ Local Test Network Setup

Run a multi-node test network on your local machine:

**Terminal 1 - Start Coordinator:**
```bash
saorsa-coordinator --verbose --bind 127.0.0.1:7000 --roles coordinator,reflector --publish-interval 10
```

**Terminal 2 - Start Second Coordinator:**
```bash
saorsa-coordinator --verbose --bind 127.0.0.1:7001 --roles coordinator,relay --publish-interval 15 \
--identity-path ~/.saorsa-gossip/coordinator2.identity
```

**Terminal 3 - Create Test Identities:**
```bash
# Create 3 test node identities
saorsa-gossip identity create --alias Node1
saorsa-gossip identity create --alias Node2
saorsa-gossip identity create --alias Node3

# Verify they were created
saorsa-gossip identity list
```

**What you'll see:**
- **Coordinator 1 (port 7000)**: Publishing 3551-byte adverts every 10s with unique PeerId
- **Coordinator 2 (port 7001)**: Publishing 3552-byte adverts every 15s with different PeerId
- **CLI Tool**: Successfully creating ML-DSA identities and saving to keystore
- **Persistence**: Coordinators remember their identities across restarts

**Test Results from Local Validation:**
- โœ… 2 coordinators ran simultaneously without conflicts
- โœ… Identity persistence verified (same PeerId after restart)
- โœ… Precise timing: 10s and 15s intervals maintained perfectly
- โœ… Verbose logging showing all operations (INFO + DEBUG)
- โœ… Zero compilation warnings, zero runtime errors

### ๐Ÿ“Š Logging and Monitoring

All binaries use structured logging with the `tracing` crate:

**Log Levels:**
- `INFO` - Operational events (startup, identity loading, service status)
- `DEBUG` - Detailed activity (advert publications, message counts)

**Enable verbose logging:**
```bash
# For coordinator
saorsa-coordinator --verbose ...

# For CLI tool
saorsa-gossip --verbose identity create --alias Test
```

**Log format:**
```
2025-10-05T13:34:34.486139Z INFO Starting Saorsa Gossip Coordinator
2025-10-05T13:34:34.486960Z INFO Loaded identity: c6333dcf...725d41f
2025-10-05T13:34:34.488876Z DEBUG Published coordinator advert (3551 bytes)
```

### ๐Ÿงช Testing Checklist

Before deploying to production, verify:

- [ ] Coordinator generates unique ML-DSA identity
- [ ] Coordinator publishes adverts at configured interval
- [ ] Identity persists across coordinator restarts (same PeerId)
- [ ] Multiple coordinators can run on different ports
- [ ] CLI can create and list identities
- [ ] All logging shows timestamps and correct levels
- [ ] No warnings or errors in logs

### ๐Ÿ” Troubleshooting

**Issue: "Address already in use"**
- Another process is using the port
- Solution: Use `--bind 127.0.0.1:PORT` with a different PORT

**Issue: "Failed to read keystore file"**
- Identity file doesn't exist yet (expected on first run)
- Solution: Let the binary create it automatically

**Issue: Coordinator not publishing adverts**
- Check logs for ERROR messages
- Verify `--roles` includes `coordinator`
- Ensure `--publish-interval` is reasonable (>5s)

## ๐Ÿš€ Quick Start (Library Usage)

### Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
saorsa-gossip-types = "0.1.3"
saorsa-gossip-identity = "0.1.3"
saorsa-gossip-transport = "0.1.3"
saorsa-gossip-membership = "0.1.3"
saorsa-gossip-pubsub = "0.1.3"
saorsa-gossip-coordinator = "0.1.3"
saorsa-gossip-rendezvous = "0.1.3"
saorsa-gossip-groups = "0.1.3"
saorsa-gossip-presence = "0.1.3"
saorsa-gossip-crdt-sync = "0.1.3"
```

### Basic Usage

```rust
use saorsa_gossip_types::{TopicId, PeerId};
use saorsa_gossip_membership::{Membership, HyParViewMembership};
use saorsa_gossip_pubsub::{PubSub, PlumtreePubSub};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Create a topic for your group
let topic = TopicId::new([1u8; 32]);

// Initialize membership layer
let membership = HyParViewMembership::default();
membership.join(vec!["127.0.0.1:8080".to_string()]).await?;

// Initialize pub/sub
let pubsub = PlumtreePubSub::new();
let mut rx = pubsub.subscribe(topic);

// Publish a message
pubsub.publish(topic, bytes::Bytes::from("Hello, gossip!")).await?;

// Receive messages
while let Some((peer_id, data)) = rx.recv().await {
println!("Received from {}: {:?}", peer_id, data);
}

Ok(())
}
```

## ๐Ÿ“š Protocol Specifications

### Membership (HyParView + SWIM)

- **HyParView**: Partial views for connectivity
- Active view: 8-12 peers (routing)
- Passive view: 64-128 peers (healing)
- Periodic shuffle: every 30s

- **SWIM**: Failure detection
- Probe interval: 1s
- Suspect timeout: 3s
- Piggyback membership deltas

### Dissemination (Plumtree)

- **EAGER** push along spanning tree
- **IHAVE** digests to non-tree links (batch โ‰ค 1024)
- **IWANT** pull on demand
- **Anti-entropy**: every 30s with message-ID sketches
- **Peer scoring**: mesh gating for quality

### Presence & Discovery

- **Beacons**: MLS exporter-derived tags, ML-DSA signed
- TTL: 10-15 minutes
- Encrypted to group with ChaCha20-Poly1305

- **FOAF Queries**: Friends-of-friends discovery
- Fanout: 3
- TTL: 3-4 hops
- No DHT, no global directory

### CRDTs

- **OR-Set**: For membership tracking
- **LWW-Register**: For scalar values
- **Delta-CRDTs**: Bandwidth-efficient synchronization
- **IBLT**: Reconciliation for large sets

## ๐Ÿ” Security

### Post-Quantum Cryptography

- **ML-KEM-768**: Key encapsulation (FIPS 203)
- **ML-DSA-65**: Digital signatures (FIPS 204) - default
- **SLH-DSA**: Hash-based signatures (FIPS 205 / SPHINCS+) - available for long-term security
- 12 parameter sets: SHA2/SHAKE variants at 128/192/256-bit security
- Trade-offs: fast (larger sigs) vs small (smaller sigs)
- **ChaCha20-Poly1305**: AEAD symmetric encryption (quantum-resistant)
- **MLS**: Group messaging (RFC 9420)

Provided by:
- [`saorsa-pqc`](https://crates.io/crates/saorsa-pqc) v0.3.14+ - PQC primitives including ML-KEM, ML-DSA, SLH-DSA, and ChaCha20-Poly1305
- [`saorsa-mls`](https://crates.io/crates/saorsa-mls) - MLS protocol

### Threat Model

| Attack | Mitigation |
|--------|-----------|
| Spam/Sybil | Invited joins, capability checks, scoring |
| Eclipse | HyParView shuffles, passive diversity |
| Replay | Per-topic nonces, signature checks, expiry |
| Partition | Plumtree lazy links, anti-entropy |

## ๐Ÿ› ๏ธ Development

### Building

```bash
# Build all crates
cargo build --release

# Run tests
cargo test --all

# Run with all features
cargo build --all-features
```

### Testing

```bash
# Unit tests
cargo test --all

# Integration tests
cargo test --test integration_tests

# Performance benchmarks
cargo bench --bench performance

# Code coverage report
./scripts/coverage.sh

# Run all medium priority improvements
./scripts/medium-priority-improvements.sh
```

### Code Quality

```bash
# Format code
cargo fmt --all

# Lint with Clippy (zero warnings enforced)
cargo clippy --all-features --all-targets -- -D warnings

# Generate documentation
cargo doc --all-features --no-deps --open
```

## ๐Ÿ“– Documentation

- [**SPEC.md**](SPEC.md) - Complete protocol specification
- [**API Docs**](https://docs.rs/saorsa-gossip) - Rust API documentation
- [**Examples**](examples/) - Usage examples (coming soon)

### Crate Documentation

- [saorsa-gossip-types](https://docs.rs/saorsa-gossip-types) - Core types and wire format
- [saorsa-gossip-transport](https://docs.rs/saorsa-gossip-transport) - QUIC transport
- [saorsa-gossip-membership](https://docs.rs/saorsa-gossip-membership) - HyParView + SWIM
- [saorsa-gossip-pubsub](https://docs.rs/saorsa-gossip-pubsub) - Plumtree broadcast
- [saorsa-gossip-presence](https://docs.rs/saorsa-gossip-presence) - Presence beacons
- [saorsa-gossip-crdt-sync](https://docs.rs/saorsa-gossip-crdt-sync) - CRDT synchronization
- [saorsa-gossip-groups](https://docs.rs/saorsa-gossip-groups) - MLS groups
- [saorsa-gossip-identity](https://docs.rs/saorsa-gossip-identity) - Identity management

## ๐Ÿ—บ๏ธ Roadmap

### โœ… Phase 1: Foundation (Complete - v0.1.0)
- [x] Core types and traits
- [x] CRDT implementations (OR-Set, LWW)
- [x] MLS group wrapper
- [x] PQC identity management

### โœ… Phase 2: Protocols (Complete - v0.1.2)
- [x] HyParView trait definitions
- [x] SWIM trait definitions
- [x] Plumtree trait definitions
- [x] Membership wired to transport
- [x] Broadcast dissemination wired to transport
- [x] Delta-CRDT anti-entropy

### โœ… Phase 3: Transport (Complete - v0.1.2)
- [x] ant-quic 0.10.1 QUIC integration
- [x] NAT traversal with hole punching
- [x] Ed25519 keypair generation
- [x] Stream multiplexing (mship, pubsub, bulk)
- [x] Message send/receive with routing

### โœ… Phase 4: Production Crypto (Complete - v0.1.3)
- [x] Real ML-DSA-65 message signing/verification
- [x] BLAKE3 KDF for MLS exporter secrets
- [x] Coordinator binary with full CLI
- [x] Rendezvous shard implementation
- [x] Zero compilation warnings
- [x] 192 tests passing across all crates
- [x] Published to crates.io

### ๐Ÿ“‹ Phase 5: Advanced Features (In Progress)
- [x] Presence beacon broadcasting (basic)
- [x] FOAF query framework
- [ ] Complete IBLT reconciliation
- [ ] Peer scoring and mesh gating
- [ ] Saorsa Sites (website publishing)
- [ ] Complete anti-entropy with message sketches

### ๐Ÿ“‹ Phase 6: Production Hardening (Planned)
- [ ] 100-node test harness
- [ ] Performance benchmarks
- [ ] Security audit
- [ ] Production deployment guide
- [ ] Chaos engineering tests
- [ ] Load testing framework

## ๐Ÿ“Š Performance Benchmarks

### Transport Layer Performance (Real-World Results)

Comprehensive benchmarks on localhost (ant-quic 0.10.3 with direct stream acceptance):

| Message Size | Throughput (Mbps) | Throughput (MB/s) | Latency | Notes |
|--------------|-------------------|-------------------|---------|-------|
| 1 KB | 281 | 33.5 | <1ms | โœ… Low-latency messaging |
| 10 KB | 2,759 | 328.9 | <1ms | โœ… Optimal for small payloads |
| 100 KB | 22,230 | 2,650 | <10ms | ๐Ÿš€ Excellent throughput |
| 1 MB | 79,875 | 9,522 | ~10ms | ๐Ÿš€๐Ÿš€ Outstanding performance |
| 10 MB | 1,471 | 175.4 | ~57ms | โœ… Sustained bulk transfer |
| 50 MB | 1,392 | 166.0 | ~300ms | โœ… Large file transfer |
| 100 MB | 1,400+ | 167+ | ~600ms | โœ… Consistent large transfers |

**Test Environment:**
- Platform: macOS Darwin 25.0.0
- Network: Localhost (127.0.0.1)
- Transport: QUIC with post-quantum TLS 1.3 (ML-DSA-65 signatures)
- NAT Traversal: Enabled with hole-punching capability
- Stream Type: Unidirectional (open_uni)
- Encryption: ChaCha20-Poly1305 AEAD
- Connection Time: 4ms (excellent)

**Key Achievements:**
- โœ… **Bidirectional data transfer working** - Direct stream acceptance implementation
- โœ… **Zero timeout issues** - Continuous stream acceptance without 100ms limits
- โœ… **Large message support** - Successfully transfers 100MB+ messages
- โœ… **Consistent performance** - Stable throughput across message sizes
- โœ… **Low latency** - Sub-millisecond for small messages, <1s for 100MB

**Technical Implementation:**
- Direct connection access via `nat_endpoint.list_connections()`
- Per-peer dedicated stream handlers (unidirectional + bidirectional)
- 100MB read limit per stream (configurable)
- Proper async/await handling with tokio runtime
- Dynamic peer discovery without polling delays

### Network Performance Targets

| Metric | Target | Status |
|--------|--------|--------|
| Broadcast P50 latency | < 500ms | ๐Ÿ”„ Testing |
| Broadcast P95 latency | < 2s | ๐Ÿ”„ Testing |
| Failure detection | < 5s | ๐Ÿ”„ Testing |
| Memory per node | < 50MB | ๐Ÿ”„ Testing |
| Messages/sec/node | > 100 | โœ… **Achieved** (>2000 small msgs/sec) |
| Transport latency | < 10ms | โœ… **Achieved** (4ms connection, <1ms for 1KB) |

## ๐Ÿค Contributing

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

### Development Priorities

**High Priority** (blocking):
1. Complete QUIC transport implementation
2. Implement Plumtree EAGER/IHAVE/IWANT
3. Implement proper message ID derivation

**Medium Priority** (important):
4. Complete HyParView join/shuffle
5. Complete SWIM probe/suspect
6. Add anti-entropy protocols

**Low Priority** (enhancement):
7. Performance optimization
8. Comprehensive testing
9. Example applications

## ๐Ÿ“œ License

Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

## ๐Ÿ™ Acknowledgments

Built on top of:
- [`ant-quic`](https://crates.io/crates/ant-quic) - QUIC transport with NAT traversal
- [`saorsa-pqc`](https://crates.io/crates/saorsa-pqc) - Post-quantum cryptography
- [`saorsa-mls`](https://crates.io/crates/saorsa-mls) - MLS group messaging

Inspired by:
- **Plumtree** - Efficient epidemic broadcast
- **HyParView** - Partial view membership protocol
- **SWIM** - Scalable failure detection
- **GossipSub** - Libp2p's gossip protocol

## ๐Ÿ“ž Contact

- **Project**: [github.com/dirvine/saorsa-gossip](https://github.com/dirvine/saorsa-gossip)
- **Issues**: [github.com/dirvine/saorsa-gossip/issues](https://github.com/dirvine/saorsa-gossip/issues)
- **Author**: David Irvine ([@dirvine](https://github.com/dirvine))

---

**โœ… Status v0.1.6**: Production-ready foundation with complete post-quantum cryptography. Core gossip protocols implemented with real ML-DSA-65 signatures, BLAKE3 KDF, and deployable coordinator binary. All 192 tests passing with zero warnings. Published to crates.io.

**Next Steps**: Advanced features (IBLT reconciliation, peer scoring), production hardening (security audit, 100-node testing), and Saorsa Sites implementation.

See [DESIGN.md](DESIGN.md) for the complete technical specification and implementation roadmap.