Documentation
¶
Overview ¶
Package riblt implements Rateless Invertible Bloom Lookup Tables (Rateless IBLTs), a family of rateless codes that reconcile set differences.
Rateless IBLTs solve the "set reconciliation" problem. Imagine that two computers, Alice and Bob, each holds a set of fixed-length bit strings. We call the bit strings "source symbols". Alice and Bob wish to distributedly compute the symmetric difference of their sets, i.e., source symbols that are exclusively present on either Alice or Bob but not both computers.
Rateless IBLTs operate by defining an infinite sequence of "coded symbols" for any set. The coded symbol sequences have two strong properties. First, for any two sets, prefixes of their coded symbol sequences alone are sufficient for computing their symmetric difference. Second, the number of coded symbols, i.e., the length of the prefixes, required is linear to the size of the symmetric difference, where the coefficient converges to 1.35.
In practice, Alice and Bob locally generate the coded symbol sequences for their respective sets. Alice streams an increasingly long prefix of her sequence to Bob, who keeps trying to use the received prefix along with his locally generated sequence to recover the symmetric difference, which will happen after Bob receives a sufficiently long prefix.
To use this library, the user needs to define the source symbol being reconciled. See type Symbol. Then, the user instantiates an Encoder for Alice, and a Decoder for Bob. Alice and Bob's sets should be imported into the Encoder and the Decoder, respectively. The user should program Alice to stream coded symbols over a reliable transport to Bob, and program Bob to decode the symbols and signal Alice to stop when successful. See the example.
Example ¶
package main
import (
"encoding/binary"
"fmt"
"github.com/dchest/siphash"
"github.com/yangl1996/riblt"
)
// item is the type of set elements we will reconcile. It implements
// riblt.Symbol.
type item uint64
// XOR implements the group operation. It is simply the bitwise exclusive-or of
// the operands.
func (t item) XOR(t2 item) item {
return t ^ t2
}
// Hash hashes t using SipHash.
func (t item) Hash() uint64 {
buf := [8]byte{}
binary.LittleEndian.PutUint64(buf[0:8], uint64(t))
return siphash.Hash(123, 456, buf[:])
}
func main() {
// Alice and Bob each holds a set of items. Bob wishes to know the items
// that Alice has but he does not, as well as items that he has but Alice
// does not. Their sets are mostly the same.
alice := []item{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} // only Alice has 2
bob := []item{1, 3, 4, 5, 6, 7, 8, 9, 10, 11} // only Bob has 11
// Alice creates an encoder and gives it her set.
enc := riblt.Encoder[item]{}
for _, v := range alice {
enc.AddSymbol(v)
}
// Bob creates a decoder and gives it his set.
dec := riblt.Decoder[item]{}
for _, v := range bob {
dec.AddSymbol(v)
}
cost := 0
for {
// Alice generates the next coded symbol and sends to Bob.
s := enc.ProduceNextCodedSymbol()
cost += 1
// Bob receives the next coded symbol from Alice, and tries to decode.
dec.AddCodedSymbol(s)
dec.TryDecode()
if dec.Decoded() {
break
}
}
fmt.Println(dec.Remote()[0].Symbol, "is exclusive to Alice")
fmt.Println(dec.Local()[0].Symbol, "is exclusive to Bob")
fmt.Println(cost, "coded symbols sent")
}
Output: 2 is exclusive to Alice 11 is exclusive to Bob 2 coded symbols sent
Index ¶
- type CodedSymbol
- type Decoder
- func (d *Decoder[T]) AddCodedSymbol(c CodedSymbol[T])
- func (d *Decoder[T]) AddHashedSymbol(s HashedSymbol[T])
- func (d *Decoder[T]) AddSymbol(s T)
- func (d *Decoder[T]) Decoded() bool
- func (d *Decoder[T]) Local() []HashedSymbol[T]
- func (d *Decoder[T]) Remote() []HashedSymbol[T]
- func (d *Decoder[T]) Reset()
- func (d *Decoder[T]) TryDecode()
- type Encoder
- type HashedSymbol
- type Sketch
- func (s Sketch[T]) AddHashedSymbol(t HashedSymbol[T])
- func (s Sketch[T]) AddSymbol(t T)
- func (s Sketch[T]) Decode() (fwd []HashedSymbol[T], rev []HashedSymbol[T], succ bool)
- func (s Sketch[T]) RemoveHashedSymbol(t HashedSymbol[T])
- func (s Sketch[T]) RemoveSymbol(t T)
- func (s Sketch[T]) Subtract(s2 Sketch[T])
- type Symbol
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CodedSymbol ¶
type CodedSymbol[T Symbol[T]] struct { HashedSymbol[T] Count int64 }
CodedSymbol is a coded symbol produced by a Rateless IBLT encoder.
type Decoder ¶
type Decoder[T Symbol[T]] struct { // contains filtered or unexported fields }
Decoder computes the symmetric difference between two sets A, B. The Decoder knows B (the local set) and expects coded symbols for A (the remote set).
func (*Decoder[T]) AddCodedSymbol ¶
func (d *Decoder[T]) AddCodedSymbol(c CodedSymbol[T])
AddCodedSymbol passes the next coded symbol in A's sequence to the Decoder. Coded symbols must be passed in the same ordering as they are generated by A's Encoder.
func (*Decoder[T]) AddHashedSymbol ¶
func (d *Decoder[T]) AddHashedSymbol(s HashedSymbol[T])
AddHashedSymbol adds a source symbol to B, the Decoder's local set. It is undefined behavior to call AddHashedSymbol after AddCodedSymbol has been called one or multiple times.
func (*Decoder[T]) AddSymbol ¶
func (d *Decoder[T]) AddSymbol(s T)
AddSymbol adds a source symbol to B, the Decoder's local set. It is undefined behavior to call AddSymbol after AddCodedSymbol has been called one or multiple times.
func (*Decoder[T]) Decoded ¶
Decoded returns true if and only if every existing coded symbols d received so far have been decoded.
func (*Decoder[T]) Local ¶
func (d *Decoder[T]) Local() []HashedSymbol[T]
Local returns the list of source symbols that are present in B but not in A.
func (*Decoder[T]) Remote ¶
func (d *Decoder[T]) Remote() []HashedSymbol[T]
Remote returns the list of source symbols that are present in A but not in B.
type Encoder ¶
type Encoder[T Symbol[T]] codingWindow[T]
Encoder is an incremental encoder of Rateless IBLTs. Once initialized with a set of source symbols by calling AddSymbol or AddHashedSymbol, a Encoder can incrementally generate coded symbols in the infinite sequence defined for the set. The set must not change after one or multiple coded symbols have been generated by calling ProduceNextCodedSymbol.
func (*Encoder[T]) AddHashedSymbol ¶
func (e *Encoder[T]) AddHashedSymbol(s HashedSymbol[T])
AddHashedSymbol adds source symbol s to e. It is undefined behavior to call AddHashedSymbol after calling ProduceNextCodedSymbol.
func (*Encoder[T]) AddSymbol ¶
func (e *Encoder[T]) AddSymbol(s T)
AddSymbol adds source symbol s to e. It is undefined behavior to call AddSymbol after calling ProduceNextCodedSymbol.
func (*Encoder[T]) ProduceNextCodedSymbol ¶
func (e *Encoder[T]) ProduceNextCodedSymbol() CodedSymbol[T]
ProduceNextCodedSymbol returns the next coded symbol in the sequence.
type HashedSymbol ¶
HashedSymbol is the bundle of a symbol and its hash computed using its Hash method.
type Sketch ¶
type Sketch[T Symbol[T]] []CodedSymbol[T]
Sketch is a prefix of the coded symbol sequence for a set of source symbols. When generating a prefix of predetermined length, compared to generating the prefix incrementally using an Encoder, it is more efficient to use Sketch. Sketch also allows inserting or deleting source symbols from the set after it has been created.
func (Sketch[T]) AddHashedSymbol ¶
func (s Sketch[T]) AddHashedSymbol(t HashedSymbol[T])
AddHashedSymbol inserts source symbol t to the set of which s is a sketch.
func (Sketch[T]) AddSymbol ¶
func (s Sketch[T]) AddSymbol(t T)
AddSymbol inserts source symbol t to the set of which s is a sketch.
func (Sketch[T]) Decode ¶
func (s Sketch[T]) Decode() (fwd []HashedSymbol[T], rev []HashedSymbol[T], succ bool)
Decode tries to decode s, where s can be one of the following
- A sketch of set S.
- Content of s after calling s.Subtract(s2), where s is a sketch of set S, and s2 is a sketch of set S2.
When successful, indicated by succ being true, fwd contains all source symbols in S in case 1, or S \ S2 in case 2 (\ is the set subtraction operation). rev is empty in case 1, or S2 \ S in case 2.
func (Sketch[T]) RemoveHashedSymbol ¶
func (s Sketch[T]) RemoveHashedSymbol(t HashedSymbol[T])
RemoveHashedSymbol deletes source symbol t from the set of which s is a sketch.
func (Sketch[T]) RemoveSymbol ¶
func (s Sketch[T]) RemoveSymbol(t T)
RemoveSymbol deletes source symbol t from the set of which s is a sketch.
type Symbol ¶
type Symbol[T any] interface { // XOR returns t $ t2, where t is the method receiver. XOR is allowed to // modify the method receiver in-place (when T is a pointer) and return the // modified t. Although the method is called XOR (because the bitwise // exclusive-or operation is a valid group operation for groups of // fixed-length byte strings), it can implement any operation that satisfy // the aforementioned properties. XOR(t2 T) T // Hash returns the hash of the method receiver. It must not modify the // method receiver. It must not be homomorphic over the group operation. // That is, the probability that // (a $ b).Hash() == a.Hash() ^ b.Hash() // must be negligible. Here, ^ on the right-hand side is the bitwise // exclusive-or operation. Hash() uint64 }
Symbol is the interface that source symbols (set elements being reconciled) should implement. It specifies a Boolean group, where type T (or its subset) is the underlying set, and $ is the group operation. It should satisfy the following properties:
- For all a, b, c in the group, (a $ b) $ c = a $ (b $ c).
- Let e be the default value of T. For every a in the group, e $ a = a and a $ e = a.
- For every a in the group, a $ a = e.
As an example, when source symbols are plain byte strings of length 32, T is [32]byte. $ can be the bitwise exclusive-or (XOR) operation. e is the byte string where every byte is zero.