rustywallet-bloom
Fast and memory-efficient Bloom Filter implementation optimized for cryptocurrency address matching in rustywallet.
Features
- Memory efficient: ~1.2 bytes per item at 1% false positive rate
- Fast: Uses FNV-1a hash with double hashing technique (only 2 hash computations regardless of k)
- No dependencies: Pure Rust implementation with zero external dependencies
- Optimized parameters: Automatically calculates optimal bits and hash functions
- Thread-safe: Safe for concurrent read operations
- Cryptocurrency optimized: Designed for Bitcoin and other cryptocurrency address filtering
- Configurable: Adjustable false positive rates from 0.001% to 10%
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
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
Bloom Filter Creation
Basic Creation
use BloomFilter;
// Create with expected items and false positive rate
let bloom = new; // 1M items, 1% FPR
// Create with custom parameters
let bloom = with_params; // items, k, m
Recommended Configurations
// High precision (0.1% FPR) - for critical applications
let precise = new;
// Balanced (1% FPR) - recommended for most use cases
let balanced = new;
// Memory optimized (5% FPR) - for memory-constrained environments
let compact = new;
Membership Testing
let mut bloom = new;
// Insert addresses
bloom.insert;
bloom.insert;
// Test membership
match bloom.contains
// Batch testing
let addresses = vec!;
for addr in addresses
False Positive Rates
Understanding false positives is crucial for proper Bloom filter usage:
| False Positive Rate | Use Case | Memory Overhead |
|---|---|---|
| 0.001% (0.00001) | Critical applications | ~2.4x |
| 0.01% (0.0001) | High precision needed | ~2.0x |
| 0.1% (0.001) | Balanced precision | ~1.7x |
| 1% (0.01) | Recommended | ~1.2x |
| 5% (0.05) | Memory constrained | ~0.8x |
| 10% (0.1) | Very memory constrained | ~0.6x |
// Calculate actual false positive rate
let bloom = new;
println!;
Use Cases
Address Matching in Cryptocurrency Wallets
use BloomFilter;
// Usage
let mut wallet = new;
wallet.add_address;
// Fast pre-filtering
if wallet.might_own
Transaction Filtering
// Filter relevant transactions from blockchain data
let mut relevant_addresses = new;
// Add all wallet addresses
for address in wallet_addresses
// Process blockchain transactions
for transaction in blockchain_transactions
Memory Usage
| Items | FPR | Memory | Bits per Item |
|---|---|---|---|
| 100K | 1% | ~120 KB | ~9.6 |
| 1M | 1% | ~1.2 MB | ~9.6 |
| 10M | 1% | ~12 MB | ~9.6 |
| 37M | 1% | ~44 MB | ~9.6 |
| 100M | 1% | ~120 MB | ~9.6 |
// Check memory usage
let bloom = new;
println!;
println!;
Performance
- Insert: O(k) where k = number of hash functions (typically 7 for 1% FPR)
- Lookup: O(k)
- Space: O(m) where m = number of bits
- Hash computations: Only 2 per operation (uses double hashing)
Benchmarks
On a modern CPU (Intel i7-10700K):
- Insert: ~50ns per operation
- Lookup: ~45ns per operation
- Throughput: ~20M operations/second
API Reference
BloomFilter
Utility Functions
// Calculate optimal parameters
Examples
See the examples/ directory for complete examples:
basic_usage.rs- Simple address filteringwallet_integration.rs- Integration with wallet softwareperformance_test.rs- Performance benchmarking
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.