Skip to main content

qualia_core_db/net/qdnf/biometrics/
remote.rs

1//! Authorised remote processing (E15.3, E15.5, E15.6).
2//!
3//! Remote matching is unselectable until a complete qualification contract
4//! exists *and* independent measurement passes. This crate has no operational
5//! biometric corpus. The linear-algebra privacy engine (BFV packed integer
6//! arithmetic and DP releases) is not a qualified biometric matcher.
7
8use crate::net::qdnf::errors::QdnfError;
9use crate::net::qdnf::types::StrongDigest;
10
11/// Bound fields required on an encrypted remote object (E15.3).
12#[repr(C)]
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub struct RemoteBoundFields {
15    pub modality: StrongDigest,
16    pub algorithm_version: StrongDigest,
17    pub quality: u16,
18    pub provenance: StrongDigest,
19    pub purpose: StrongDigest,
20    pub recipient: StrongDigest,
21    pub retention_unix: u64,
22}
23
24impl RemoteBoundFields {
25    #[inline]
26    /// `RemoteBoundFields::missing_any`.
27    pub fn missing_any(&self) -> bool {
28        self.modality.is_zero()
29            || self.algorithm_version.is_zero()
30            || self.quality == 0
31            || self.provenance.is_zero()
32            || self.purpose.is_zero()
33            || self.recipient.is_zero()
34            || self.retention_unix == 0
35    }
36}
37
38/// Encrypted artifact for a selected recipient/worker. Ciphertext only.
39#[repr(C)]
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub struct EncryptedRemoteObject {
42    pub ciphertext_digest: StrongDigest,
43    pub bound: RemoteBoundFields,
44    pub explicitly_authorised: bool,
45}
46
47/// Admit remote *processing* only when every bound field is present on an
48/// encrypted, explicitly authorised object. Admission is not qualification
49/// and does not make remote matching selectable.
50pub fn admit_remote_processing(obj: &EncryptedRemoteObject) -> Result<(), QdnfError> {
51    if !obj.explicitly_authorised {
52        return Err(QdnfError::Denied);
53    }
54    if obj.ciphertext_digest.is_zero() {
55        return Err(QdnfError::Denied);
56    }
57    if obj.bound.missing_any() {
58        return Err(QdnfError::Denied);
59    }
60    Ok(())
61}
62
63/// Owner-approved measurement contract required before remote qualification.
64#[repr(C)]
65#[derive(Clone, Copy, Debug, PartialEq, Eq)]
66pub struct QualificationCriteria {
67    pub threshold: u16,
68    pub sample_size: u32,
69    pub spoof_limit: u16,
70    pub leakage_limit: u16,
71}
72
73impl QualificationCriteria {
74    #[inline]
75    /// `QualificationCriteria::any_zero`.
76    pub const fn any_zero(self) -> bool {
77        self.threshold == 0
78            || self.sample_size == 0
79            || self.spoof_limit == 0
80            || self.leakage_limit == 0
81    }
82}
83
84/// Check that a criteria document is present and non-zero.
85///
86/// `Ok` means the contract fields are filled. It is not operational
87/// qualification: [`remote_matching_selectable`] stays false, and
88/// [`synthetic_accuracy_is_operational`] is false.
89pub fn qualify_remote(c: Option<&QualificationCriteria>) -> Result<(), QdnfError> {
90    let c = match c {
91        None => return Err(QdnfError::UnknownProfile),
92        Some(c) => c,
93    };
94    if c.any_zero() {
95        return Err(QdnfError::Denied);
96    }
97    Ok(())
98}
99
100/// Remote matching remains unselectable: E15.6 independent measurement has
101/// not been performed, and E15.5 SMPC is not qualified.
102#[inline]
103pub const fn remote_matching_selectable() -> bool {
104    false
105}
106
107/// Existing privacy engine is packed BFV / DP, not a biometric matcher.
108#[inline]
109pub const fn smpc_matching_qualified() -> bool {
110    false
111}
112
113/// Synthetic fixtures prove protocol wiring, not operational accuracy.
114#[inline]
115pub const fn synthetic_accuracy_is_operational() -> bool {
116    false
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122
123    fn d(tag: u8) -> StrongDigest {
124        let mut x = StrongDigest::ZERO;
125        x.0[0] = tag;
126        x.0[47] = 0xC4;
127        x
128    }
129
130    fn complete_fields() -> RemoteBoundFields {
131        RemoteBoundFields {
132            modality: d(1),
133            algorithm_version: d(2),
134            quality: 7,
135            provenance: d(3),
136            purpose: d(4),
137            recipient: d(5),
138            retention_unix: 1_700_000_000,
139        }
140    }
141
142    fn complete_object() -> EncryptedRemoteObject {
143        EncryptedRemoteObject {
144            ciphertext_digest: d(9),
145            bound: complete_fields(),
146            explicitly_authorised: true,
147        }
148    }
149
150    fn complete_criteria() -> QualificationCriteria {
151        QualificationCriteria {
152            threshold: 10,
153            sample_size: 1000,
154            spoof_limit: 5,
155            leakage_limit: 3,
156        }
157    }
158
159    #[test]
160    fn missing_authorisation_is_denied() {
161        let mut obj = complete_object();
162        obj.explicitly_authorised = false;
163        assert_eq!(
164            admit_remote_processing(&obj).unwrap_err(),
165            QdnfError::Denied
166        );
167    }
168
169    #[test]
170    fn missing_any_bound_field_is_denied() {
171        let cases: [RemoteBoundFields; 7] = [
172            RemoteBoundFields {
173                modality: StrongDigest::ZERO,
174                ..complete_fields()
175            },
176            RemoteBoundFields {
177                algorithm_version: StrongDigest::ZERO,
178                ..complete_fields()
179            },
180            RemoteBoundFields {
181                quality: 0,
182                ..complete_fields()
183            },
184            RemoteBoundFields {
185                provenance: StrongDigest::ZERO,
186                ..complete_fields()
187            },
188            RemoteBoundFields {
189                purpose: StrongDigest::ZERO,
190                ..complete_fields()
191            },
192            RemoteBoundFields {
193                recipient: StrongDigest::ZERO,
194                ..complete_fields()
195            },
196            RemoteBoundFields {
197                retention_unix: 0,
198                ..complete_fields()
199            },
200        ];
201        for bound in cases {
202            let obj = EncryptedRemoteObject {
203                ciphertext_digest: d(9),
204                bound,
205                explicitly_authorised: true,
206            };
207            assert_eq!(
208                admit_remote_processing(&obj).unwrap_err(),
209                QdnfError::Denied
210            );
211        }
212    }
213
214    #[test]
215    fn unencrypted_object_is_denied() {
216        let mut obj = complete_object();
217        obj.ciphertext_digest = StrongDigest::ZERO;
218        assert_eq!(
219            admit_remote_processing(&obj).unwrap_err(),
220            QdnfError::Denied
221        );
222    }
223
224    #[test]
225    fn complete_authorised_encrypted_object_admits_processing() {
226        admit_remote_processing(&complete_object()).unwrap();
227        assert!(!remote_matching_selectable());
228    }
229
230    #[test]
231    fn qualify_remote_none_is_unknown_profile() {
232        assert_eq!(qualify_remote(None).unwrap_err(), QdnfError::UnknownProfile);
233        assert!(!remote_matching_selectable());
234    }
235
236    #[test]
237    fn qualify_remote_zeros_are_denied() {
238        let zeros = QualificationCriteria {
239            threshold: 0,
240            sample_size: 0,
241            spoof_limit: 0,
242            leakage_limit: 0,
243        };
244        assert_eq!(qualify_remote(Some(&zeros)).unwrap_err(), QdnfError::Denied);
245        let partial = QualificationCriteria {
246            threshold: 10,
247            sample_size: 0,
248            spoof_limit: 5,
249            leakage_limit: 3,
250        };
251        assert_eq!(
252            qualify_remote(Some(&partial)).unwrap_err(),
253            QdnfError::Denied
254        );
255        assert!(!remote_matching_selectable());
256    }
257
258    #[test]
259    fn complete_criteria_do_not_select_remote_or_claim_accuracy() {
260        qualify_remote(Some(&complete_criteria())).unwrap();
261        assert!(!remote_matching_selectable());
262        assert!(!smpc_matching_qualified());
263        assert!(!synthetic_accuracy_is_operational());
264    }
265}