Skip to main content

qualia_core_db/net/qdnf/contracts/
provenance.rs

1//! Claim provenance, issuer scope and disagreement.
2//!
3//! Relationship confidence and a caller-selected NaturalAgent enum cannot
4//! certify identity.
5
6use crate::net::qdnf::errors::QdnfError;
7use crate::net::qdnf::types::StrongDigest;
8
9/// Tracked claim. Confidence is observational; it never becomes identity.
10#[repr(C)]
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub struct ClaimRecord {
13    pub issuer: StrongDigest,
14    pub scope: u64,
15    pub disagreement: bool,
16    pub confidence: u8,
17}
18
19impl ClaimRecord {
20    /// `ClaimRecord::blank`.
21    pub const fn blank() -> Self {
22        Self {
23            issuer: StrongDigest::ZERO,
24            scope: 0,
25            disagreement: false,
26            confidence: 0,
27        }
28    }
29}
30
31/// Confidence and a NaturalAgent enum never certify identity.
32///
33/// Always [`QdnfError::Unauthorized`]: neither argument is an identity proof.
34pub fn certify_identity(_confidence: u8, _natural_agent_enum: bool) -> Result<(), QdnfError> {
35    Err(QdnfError::Unauthorized)
36}
37
38/// Admit a claim as a scoped assertion, never as certified identity.
39pub fn admit_claim(record: &ClaimRecord) -> Result<(), QdnfError> {
40    if record.issuer.is_zero() {
41        return Err(QdnfError::Unauthorized);
42    }
43    if record.scope == 0 {
44        return Err(QdnfError::Unauthorized);
45    }
46    if record.disagreement {
47        return Err(QdnfError::Conflict);
48    }
49    let _ = certify_identity(record.confidence, false);
50    Ok(())
51}
52
53/// Contradictory claims in the same scope cannot collapse to one person.
54pub fn contradicting_claims(a: &ClaimRecord, b: &ClaimRecord) -> Result<(), QdnfError> {
55    if a.disagreement || b.disagreement {
56        return Err(QdnfError::Conflict);
57    }
58    if a.scope == b.scope && a.issuer != b.issuer {
59        return Err(QdnfError::Conflict);
60    }
61    Ok(())
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    fn issuer(b: u8) -> StrongDigest {
69        StrongDigest([b; 48])
70    }
71
72    fn claim(iss: u8, scope: u64, confidence: u8) -> ClaimRecord {
73        ClaimRecord {
74            issuer: issuer(iss),
75            scope,
76            disagreement: false,
77            confidence,
78        }
79    }
80
81    #[test]
82    fn max_confidence_does_not_certify_identity() {
83        assert_eq!(certify_identity(255, false), Err(QdnfError::Unauthorized));
84    }
85
86    #[test]
87    fn natural_agent_enum_does_not_certify_identity() {
88        assert_eq!(certify_identity(255, true), Err(QdnfError::Unauthorized));
89        assert_eq!(certify_identity(0, true), Err(QdnfError::Unauthorized));
90    }
91
92    #[test]
93    fn zero_issuer_claim_is_unauthorized() {
94        let mut rec = claim(1, 7, 200);
95        rec.issuer = StrongDigest::ZERO;
96        assert_eq!(admit_claim(&rec), Err(QdnfError::Unauthorized));
97    }
98
99    #[test]
100    fn disagreement_is_conflict() {
101        let mut rec = claim(1, 7, 200);
102        rec.disagreement = true;
103        assert_eq!(admit_claim(&rec), Err(QdnfError::Conflict));
104    }
105
106    #[test]
107    fn scoped_claim_admits_without_certifying() {
108        let rec = claim(3, 11, 255);
109        assert!(admit_claim(&rec).is_ok());
110        assert_eq!(
111            certify_identity(rec.confidence, true),
112            Err(QdnfError::Unauthorized)
113        );
114    }
115
116    #[test]
117    fn contradictory_issuers_in_scope_conflict() {
118        assert_eq!(
119            contradicting_claims(&claim(1, 4, 10), &claim(2, 4, 10)),
120            Err(QdnfError::Conflict)
121        );
122    }
123}