rustywallet-bloom
Fast and memory-efficient Bloom Filter implementations optimized for cryptocurrency address matching.
Features
- Standard Bloom Filter: Memory efficient (~1.2 bytes per item at 1% FPR)
- Counting Bloom Filter: Supports removal of items (4x memory of standard)
- Fast: Uses FNV-1a hash with double hashing technique
- No dependencies: Pure Rust implementation
- Optimized parameters: Automatically calculates optimal bits and hash functions
Installation
[]
= "0.2"
Standard Bloom Filter
For when you only need to add items (no removal):
use BloomFilter;
// Create filter for 1 million addresses with 1% false positive rate
let mut bloom = new;
// Insert Bitcoin addresses
bloom.insert;
bloom.insert;
// Test membership
if bloom.contains
// Check memory usage
println!;
Counting Bloom Filter
For when you need to add AND remove items:
use CountingBloomFilter;
let mut filter = new;
// Insert items
filter.insert;
filter.insert;
assert!;
// Remove an item
filter.remove.unwrap;
assert!;
assert!;
// Insert same item multiple times
filter.insert;
filter.insert;
filter.insert;
// Must remove same number of times
filter.remove.unwrap; // Still present
filter.remove.unwrap; // Still present
filter.remove.unwrap; // Now gone
assert!;
Memory Usage
| Items | FPR | Standard | Counting |
|---|---|---|---|
| 100K | 1% | ~120 KB | ~480 KB |
| 1M | 1% | ~1.2 MB | ~4.8 MB |
| 10M | 1% | ~12 MB | ~48 MB |
False Positive Rates
| FPR | Use Case |
|---|---|
| 0.1% | High precision needed |
| 1% | Recommended for most cases |
| 5% | Memory constrained |
API Reference
BloomFilter
// Create new filter
let bloom = new;
// Insert and check
bloom.insert;
bloom.contains; // true or false
// Utilities
bloom.memory_usage; // bytes
bloom.num_bits; // bit count
bloom.num_hashes; // hash function count
bloom.clear; // reset filter
CountingBloomFilter
// Create new filter
let filter = new;
// Insert (always succeeds)
filter.insert;
// Remove (returns Result)
filter.remove?; // Ok(()) or Err(CounterUnderflow)
// Check membership
filter.contains; // true or false
// Get approximate count
filter.count_estimate; // 0-15
// Utilities
filter.memory_usage;
filter.clear;
License
MIT