Metadata-Version: 2.4
Name: rriblt
Version: 0.1.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: System :: Distributed Computing
Classifier: Topic :: Software Development :: Libraries
Requires-Dist: pytest ; extra == 'dev'
Requires-Dist: hypothesis ; extra == 'dev'
Requires-Dist: ruff==0.15.21 ; extra == 'dev'
Provides-Extra: dev
License-File: LICENSE
Summary: Rust-powered Rateless IBLT: compute the difference between two remote sets with communication proportional to the difference
Keywords: set reconciliation,iblt,rateless,riblt,sync,invertible bloom lookup table
License: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/f4hy/rriblt
Project-URL: Issues, https://github.com/f4hy/rriblt/issues
Project-URL: Repository, https://github.com/f4hy/rriblt

# rriblt

**Rateless Invertible Bloom Lookup Tables** for Python, implemented in Rust
via [PyO3](https://pyo3.rs).

Two machines each hold a large set, and they want to learn exactly which
items differ. `rriblt` does this with communication proportional to the
**size of the difference**, not the size of the sets, and neither side has
to know or estimate that difference in advance.

It implements Yang, Gilad & Alizadeh,
[*Practical Rateless Set Reconciliation*](https://arxiv.org/abs/2402.02668)
(SIGCOMM 2024). As far as I can tell, this is the first Python
implementation. The authors' reference implementation is in Go.

## Install

```sh
pip install rriblt
```

## Usage

The sender (Alice) streams coded symbols. The receiver (Bob) feeds them to a
decoder until it reports success, then tells Alice to stop.

```python
from rriblt import Encoder, Decoder

SIZE = 16  # every symbol is exactly SIZE bytes

# Alice
enc = Encoder(SIZE)
enc.add_many(alice_symbols)          # iterable of bytes, or one flat buffer

# Bob
dec = Decoder(SIZE)
dec.add_many(bob_symbols)

while not dec.decoded:
    chunk = enc.produce(64)          # 64 coded symbols, `64 * enc.cell_size` bytes
    dec.add_coded(chunk)             # ...sent over the network in real life

dec.remote_only()   # list[bytes]: Alice has these, Bob doesn't
dec.local_only()    # list[bytes]: Bob has these, Alice doesn't
```

### Variable-size records

Symbols have a fixed size. To reconcile rows, files, or other records,
reconcile their digests and keep a local map back to the records:

```python
from rriblt import digest  # BLAKE2b, 16 bytes by default

by_digest = {digest(row): row for row in my_rows}
dec.add_many(by_digest)
...
missing_here = dec.remote_only()           # digests to fetch from the peer
extra_here = [by_digest[d] for d in dec.local_only()]
```

Integers can be encoded directly, e.g. `x.to_bytes(8, "little")` with
`symbol_size=8`. A numpy `uint64` array can be passed straight to `add_many`
as a flat buffer.

### Fixed-size sketches

When you'd rather compute a one-shot summary (for example, to store with each
replica), use `Sketch`. Sketches support `add`, `remove`, and subtraction:

```python
from rriblt import Sketch

a = Sketch(16, num_cells=1000); a.add_many(set_a)
b = Sketch(16, num_cells=1000); b.add_many(set_b)
only_a, only_b = (a - b).decode()   # ValueError if the difference is too big
```

`Sketch.to_bytes()` is byte-identical to the first `num_cells` symbols an
`Encoder` would produce for the same set. A stored sketch can therefore be fed
to a `Decoder` as the start of a stream, and the encoder can continue from
there if more cells are needed.

## Benchmarks

16-byte symbols (32-byte cells). Each party loads its set from a flat
buffer, then cells are streamed in batches of d/20 until decoding succeeds.
Single thread. Source: [`benchmarks/bench.py`](./benchmarks/bench.py).

| set size | difference d | load (both sides) | reconcile | cells sent | bytes sent | vs. sending every digest |
|---|---|---|---|---|---|---|
| 100,000 | 10 | 0.01s | 0.15s | 24 (2.4x d) | 0.8 kB | 2,083x less |
| 100,000 | 1,000 | 0.01s | 0.38s | 1,400 (1.40x d) | 44.8 kB | 36x less |
| 1,000,000 | 100 | 0.08s | 3.9s | 135 (1.35x d) | 4.3 kB | 3,704x less |
| 1,000,000 | 10,000 | 0.07s | 6.7s | 14,000 (1.40x d) | 448 kB | 36x less |

The number of cells converges to the paper's **≈1.35x the difference**. For
very small differences the constant is larger, and batching adds some
overshoot. Reconcile time is dominated by applying each local symbol to the
~2·ln(cells) cells it maps to. The paper's own measurements show the same
trade-off: very little bandwidth, in exchange for CPU proportional to set
size × log(difference).

## How it works

Every symbol `s` is XOR-ed into an infinite sequence of cells: cell 0
always, and cell `i` with probability `1/(1+i/2)`, at indices determined by
`hash(s)`. Each cell stores `(xor of symbols, count, xor of hashes)`.
Subtracting Bob's cells from Alice's leaves a sketch of the symmetric
difference. That sketch is *peeled*: a cell with count ±1 whose stored hash
matches the hash of its sum holds exactly one symbol, which is removed
everywhere it appears, and that can expose more pure cells. Because the
sequence is infinite, Alice just keeps sending until Bob succeeds.

Implementation notes:

- The index mapping follows the authors' Go `randomMapping.nextIndex`, except
  that the step is clamped to at least 1. The Go code can produce a zero step
  (with probability about 2^-31 per step), which maps a symbol into the same
  cell twice.
- The hash is `xxh3_64` with a configurable `seed`, and both sides must use
  the same `seed`. Pick a secret random seed if adversaries could choose your
  set elements, since crafted collisions can stall decoding.
- Wire format per cell: `sum[symbol_size] | count: i64 LE | hash: u64 LE`.
  It is not compatible with the Go library's encoding.
- `Encoder.add` raises after `produce` has been called, because symbols added
  late would be missing from cells already sent. The Go version accepts them
  silently. `Decoder.add` can be called at any time.
- Encoding is batched: each `produce(n)` / `add_coded(...)` call does one
  heap operation per symbol touching that block of cells, not one per cell.

## Development

```sh
cargo test --no-default-features
pip install -e '.[dev]' && maturin develop --release
pytest
python benchmarks/bench.py
```

