Skip to main content

qualia_core_db/net/qdnf/contracts/
identity.rs

1//! Entity / claim / handle / instrument / person planes for contract authority.
2//!
3//! OWL `sameAs` in modalities is a separate engine. This module only guards
4//! network/contract authority consumption: identifiers cannot stand in for
5//! personhood, and sameAs/IFP cannot transfer authority.
6
7use crate::net::qdnf::errors::QdnfError;
8use crate::net::qdnf::types::StrongDigest;
9
10/// Referent at a contract consumption boundary. Distinct from fabric planes.
11#[repr(u8)]
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub enum ReferentKind {
14    Entity = 1,
15    Claim = 2,
16    Handle = 3,
17    Instrument = 4,
18    NaturalPerson = 5,
19}
20
21/// Consume this referent as a network/contract principal.
22///
23/// Handle and Instrument cannot be used as personhood. Entity and Claim may
24/// consume their own plane. NaturalPerson is explicit and is never inferred.
25pub fn consume_authority(kind: ReferentKind) -> Result<(), QdnfError> {
26    match kind {
27        ReferentKind::Entity | ReferentKind::Claim | ReferentKind::NaturalPerson => Ok(()),
28        ReferentKind::Handle | ReferentKind::Instrument => Err(QdnfError::Unauthorized),
29    }
30}
31
32/// Handle/Instrument/Entity/Claim cannot be treated as a natural person.
33#[inline]
34pub fn consume_as_person(kind: ReferentKind) -> Result<(), QdnfError> {
35    match kind {
36        ReferentKind::NaturalPerson => Ok(()),
37        ReferentKind::Entity
38        | ReferentKind::Claim
39        | ReferentKind::Handle
40        | ReferentKind::Instrument => Err(QdnfError::Unauthorized),
41    }
42}
43
44/// Network/contract sameAs never transfers authority. Always false.
45#[inline]
46pub fn same_as_transfers_authority() -> bool {
47    false
48}
49
50/// A handle never infers NaturalPerson. Always false.
51#[inline]
52pub fn infer_natural_person_from_handle() -> bool {
53    false
54}
55
56/// Inverse-functional properties never merge persons or transfer authority.
57#[inline]
58pub fn inverse_functional_transfers_authority() -> bool {
59    false
60}
61
62/// Alias collision does not merge persons or transfer authority.
63pub fn refuse_alias_merge(
64    left: ReferentKind,
65    right: ReferentKind,
66    left_id: StrongDigest,
67    right_id: StrongDigest,
68) -> Result<(), QdnfError> {
69    let _ = same_as_transfers_authority();
70    let _ = inverse_functional_transfers_authority();
71    if left == ReferentKind::Handle || left == ReferentKind::Instrument {
72        return consume_as_person(left);
73    }
74    if right == ReferentKind::Handle || right == ReferentKind::Instrument {
75        return consume_as_person(right);
76    }
77    if left == ReferentKind::NaturalPerson || right == ReferentKind::NaturalPerson {
78        if left != right || left_id != right_id {
79            return Err(QdnfError::Conflict);
80        }
81    }
82    Ok(())
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    fn digest(b: u8) -> StrongDigest {
90        StrongDigest([b; 48])
91    }
92
93    #[test]
94    fn handle_cannot_consume_as_personhood() {
95        assert_eq!(
96            consume_authority(ReferentKind::Handle),
97            Err(QdnfError::Unauthorized)
98        );
99        assert_eq!(
100            consume_as_person(ReferentKind::Handle),
101            Err(QdnfError::Unauthorized)
102        );
103    }
104
105    #[test]
106    fn instrument_cannot_consume_as_personhood() {
107        assert_eq!(
108            consume_authority(ReferentKind::Instrument),
109            Err(QdnfError::Unauthorized)
110        );
111        assert_eq!(
112            consume_as_person(ReferentKind::Instrument),
113            Err(QdnfError::Unauthorized)
114        );
115    }
116
117    #[test]
118    fn entity_and_claim_consume_their_plane() {
119        assert!(consume_authority(ReferentKind::Entity).is_ok());
120        assert!(consume_authority(ReferentKind::Claim).is_ok());
121        assert_eq!(
122            consume_as_person(ReferentKind::Entity),
123            Err(QdnfError::Unauthorized)
124        );
125        assert_eq!(
126            consume_as_person(ReferentKind::Claim),
127            Err(QdnfError::Unauthorized)
128        );
129    }
130
131    #[test]
132    fn explicit_natural_person_consumes() {
133        assert!(consume_authority(ReferentKind::NaturalPerson).is_ok());
134        assert!(consume_as_person(ReferentKind::NaturalPerson).is_ok());
135    }
136
137    #[test]
138    fn same_as_never_transfers_authority() {
139        assert!(!same_as_transfers_authority());
140        assert!(!inverse_functional_transfers_authority());
141    }
142
143    #[test]
144    fn handle_does_not_infer_natural_person() {
145        assert!(!infer_natural_person_from_handle());
146    }
147
148    #[test]
149    fn alias_collision_does_not_merge_persons() {
150        assert_eq!(
151            refuse_alias_merge(
152                ReferentKind::Handle,
153                ReferentKind::NaturalPerson,
154                digest(1),
155                digest(2),
156            ),
157            Err(QdnfError::Unauthorized)
158        );
159        assert_eq!(
160            refuse_alias_merge(
161                ReferentKind::NaturalPerson,
162                ReferentKind::NaturalPerson,
163                digest(1),
164                digest(2),
165            ),
166            Err(QdnfError::Conflict)
167        );
168    }
169}