fips_core/bloom/mod.rs
1//! Bloom Filter Implementation
2//!
3//! 1KB Bloom filters for reachability in FIPS routing. Each node
4//! maintains filters that summarize which destinations are reachable
5//! through each peer, enabling efficient routing decisions without
6//! global network knowledge.
7//!
8//! ## v1 Parameters
9//!
10//! - Size: 1 KB (8,192 bits) - sized for actual ~400-800 entry occupancy
11//! - Hash functions: k=5 - optimal at ~1,200 entries, good for 800-1,600
12//! - Bandwidth: 1 KB/announce (75% reduction from original 4KB design)
13//!
14//! These parameters are right-sized for typical network occupancy of
15//! ~250-800 entries per node.
16
17mod filter;
18mod state;
19
20use thiserror::Error;
21
22pub use filter::BloomFilter;
23pub use state::BloomState;
24
25/// Default filter size in bits (1KB = 8,192 bits).
26///
27/// Sized for ~800-1,600 entries. FPR ~0.05% at 400 entries, ~0.9% at 800.
28/// This is v1 protocol default (size_class=1).
29pub const DEFAULT_FILTER_SIZE_BITS: usize = 8192;
30
31/// Default filter size in bytes (1KB).
32pub const DEFAULT_FILTER_SIZE_BYTES: usize = DEFAULT_FILTER_SIZE_BITS / 8;
33
34/// Default number of hash functions.
35///
36/// k=5 is optimal at ~1,200 entries and a good compromise for 800-1,600.
37/// At 400 entries: FPR ~0.05%. At 800 entries: FPR ~0.9%.
38pub const DEFAULT_HASH_COUNT: u8 = 5;
39
40/// Size class for v1 protocol (1 KB filters).
41pub const V1_SIZE_CLASS: u8 = 1;
42
43/// Filter sizes by size_class: bytes = 512 << size_class
44pub const SIZE_CLASS_BYTES: [usize; 4] = [512, 1024, 2048, 4096];
45
46/// Errors related to Bloom filter operations.
47#[derive(Debug, Error)]
48pub enum BloomError {
49 #[error("invalid filter size: expected {expected} bits, got {got}")]
50 InvalidSize { expected: usize, got: usize },
51
52 #[error("filter size must be a multiple of 8, got {0}")]
53 SizeNotByteAligned(usize),
54
55 #[error("hash count must be positive")]
56 ZeroHashCount,
57}
58
59#[cfg(test)]
60mod tests;