# The Ghost Mesh Protocol Specification (v0.1)

## 1. Packet Structure (Binary)

All transmissions over BLE/Wi-Fi/Audio use the following binary frame format to ensure minimal overhead.

### 1.1. Header (Fixed 12 Bytes)
| Offset | Size | Field | Description |
| :--- | :--- | :--- | :--- |
| 0 | 1B | `VER` | Protocol Version (0x01) |
| 1 | 1B | `TYPE` | Packet Type (0x01: Hello, 0x02: Data, 0x03: Ack) |
| 2 | 2B | `LEN` | Payload Length (Big Endian, Max 64KB) |
| 4 | 4B | `CRC` | CRC32 Checksum of Header+Payload |
| 8 | 4B | `TS` | Unix Timestamp (Creation Time) |

### 1.2. Packet Types

#### Type 0x01: HELLO (Discovery Beacon)
Broadcasted via BLE Advertising.
*   **Payload:**
    *   `NodeID_Hash` (16 Bytes): Truncated SHA-256 of Public Key.
    *   `Bloom_Filter` (64 Bytes): Compact representation of stored Message IDs.

#### Type 0x02: DATA (Encrypted Payload)
*   **Payload:**
    *   `Recipient_Hash` (16 Bytes)
    *   `Nonce` (24 Bytes): For XSalsa20.
    *   `Ciphertext` (Variable): The actual encrypted message.

#### Type 0x03: ACK (Anti-Entropy)
*   **Payload:**
    *   `MessageID_List` (Variable): List of SHA-256 hashes of received messages.

## 2. Cryptography Standards

### 2.1. Identity Generation
```rust
// Rust Pseudo-code using Ed25519
let (secret_key, public_key) = ed25519::generate_keypair();
let node_id = blake3::hash(public_key);
```

### 2.2. Forward Secrecy (Double Ratchet)
Every session between two nodes uses a new ephemeral key derived from the master secret, ensuring that if a device is captured, past messages cannot be decrypted.

## 3. Storage Strategy (LRU + TTL)
To prevent storage flooding:
1.  **TTL (Time-To-Live):** Every message has a hard expiry (default 24h). Expired messages are `unlink()`ed immediately.
2.  **LRU (Least Recently Used):** If storage > 1GB, delete the oldest messages first.
3.  **Priority:** "Direct Messages" (DM) have higher retention priority than "Broadcasts".
