Skip to main content

zeitgeist_protocol/
confidence.rs

1//! Confidence tracking via bloom filter + parity
2
3use serde::{Deserialize, Serialize};
4
5/// Confidence state captures certainty about a proposition
6/// using a bloom filter hash, XOR parity (Euler characteristic), and scalar certainty.
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8pub struct ConfidenceState {
9    /// Bloom filter hash (32 bytes)
10    pub bloom: [u8; 32],
11    /// XOR parity (Euler χ)
12    pub parity: u8,
13    /// Certainty scalar 0.0-1.0
14    pub certainty: f64,
15}
16
17impl ConfidenceState {
18    pub fn new(bloom: [u8; 32], parity: u8, certainty: f64) -> Self {
19        Self { bloom, parity, certainty }
20    }
21
22    pub fn default() -> Self {
23        Self {
24            bloom: [0u8; 32],
25            parity: 0,
26            certainty: 0.0,
27        }
28    }
29
30    /// Check alignment: certainty must be 0-1
31    pub fn check_alignment(&self) -> Vec<String> {
32        let mut violations = Vec::new();
33        if !(0.0..=1.0).contains(&self.certainty) {
34            violations.push("confidence.certainty must be 0-1".into());
35        }
36        violations
37    }
38
39    /// Merge: OR the bloom bits, OR the parities, take max certainty
40    /// All semilattice operations (OR is idempotent, commutative, associative)
41    pub fn merge(&self, other: &Self) -> Self {
42        let mut bloom = [0u8; 32];
43        for i in 0..32 {
44            bloom[i] = self.bloom[i] | other.bloom[i];
45        }
46        Self {
47            bloom,
48            parity: self.parity | other.parity,
49            certainty: self.certainty.max(other.certainty),
50        }
51    }
52}