pub struct BloomFilter { /* private fields */ }Expand description
A simple Bloom filter for sync negotiation.
Used during sync to quickly determine which entries a peer likely has.
False positives are expected (~1% with default parameters); false negatives
never occur. Subsequent sync rounds resolve false positives via explicit
need lists.
Parameters: num_bits total bits in the bitvec, num_hashes hash functions
(derived from BLAKE3 by slicing the 32-byte hash into segments).
Implementations§
Source§impl BloomFilter
impl BloomFilter
Sourcepub fn validate(&self) -> Result<(), String>
pub fn validate(&self) -> Result<(), String>
S-05: Validate bloom filter dimensions after deserialization. Prevents panics from malformed sync offers (division by zero, out of bounds).
Sourcepub fn new(expected_items: usize, fp_rate: f64) -> Self
pub fn new(expected_items: usize, fp_rate: f64) -> Self
Create a new Bloom filter sized for expected_items with the given
false positive rate.
Computes optimal bit count and hash count from the standard formulas:
- m = -n * ln(p) / (ln(2)^2)
- k = (m/n) * ln(2)
Sourcepub fn contains(&self, hash: &Hash) -> bool
pub fn contains(&self, hash: &Hash) -> bool
Check if a hash might be in the filter.
Returns true if the item is probably present (with false positive rate),
false if the item is definitely NOT present.
Sourcepub fn merge(&mut self, other: &BloomFilter)
pub fn merge(&mut self, other: &BloomFilter)
Merge another filter into this one (union).
Both filters must have the same dimensions (num_bits, num_hashes).
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
Deserialize from MessagePack bytes.
Trait Implementations§
Source§impl Clone for BloomFilter
impl Clone for BloomFilter
Source§fn clone(&self) -> BloomFilter
fn clone(&self) -> BloomFilter
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more