qualia_core_db/net/qdnf/profiles/
cover.rs1use super::budget::{
23 pad_to_next_bucket, record_bucket, P1_PADDING_BUCKET, P2_RECORD_BYTES, P3_CELL_BYTES,
24 P3_COVER_BYTES_PER_SEC,
25};
26use super::catalog::{catalog_entry, ControlPredicate, ControlSet, ProtectionProfile};
27use crate::net::qdnf::errors::QdnfError;
28
29#[repr(C)]
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33pub struct CoverClass {
34 pub pad_bucket: u32,
35 pub batch_max: u8,
36 pub cover_bps: u64,
37}
38
39impl CoverClass {
40 pub const NONE: Self = Self {
41 pad_bucket: 0,
42 batch_max: 1,
43 cover_bps: 0,
44 };
45}
46
47pub fn cover_mandatory(profile: ProtectionProfile) -> bool {
50 catalog_entry(profile).contains_predicate(ControlPredicate::CoverTraffic)
51}
52
53pub fn cover_mandatory_controls(controls: &ControlSet) -> bool {
55 controls.contains_predicate(ControlPredicate::CoverTraffic)
56}
57
58pub fn drop_cover_for_cost(profile: ProtectionProfile) -> Result<(), QdnfError> {
61 if cover_mandatory(profile) {
62 Err(QdnfError::Downgrade)
63 } else {
64 Ok(())
65 }
66}
67
68pub fn drop_cover_for_controls(controls: &ControlSet) -> Result<(), QdnfError> {
70 if cover_mandatory_controls(controls) {
71 Err(QdnfError::Downgrade)
72 } else {
73 Ok(())
74 }
75}
76
77pub fn cover_class(profile: ProtectionProfile) -> CoverClass {
79 let pad_bucket = record_bucket(profile) as u32;
80 match profile {
81 ProtectionProfile::P0 => CoverClass::NONE,
82 ProtectionProfile::P1 => CoverClass {
83 pad_bucket,
84 batch_max: 1,
85 cover_bps: 0,
86 },
87 ProtectionProfile::P2 => CoverClass {
88 pad_bucket,
89 batch_max: 4,
90 cover_bps: 0,
91 },
92 ProtectionProfile::P3 | ProtectionProfile::P4 => CoverClass {
93 pad_bucket,
94 batch_max: 8,
95 cover_bps: P3_COVER_BYTES_PER_SEC,
96 },
97 }
98}
99
100pub fn approved_relay_required(profile: ProtectionProfile) -> bool {
102 catalog_entry(profile).contains_predicate(ControlPredicate::ApprovedRelay)
103}
104
105pub fn isolated_bearer_required(profile: ProtectionProfile) -> bool {
107 catalog_entry(profile).contains_predicate(ControlPredicate::IsolatedBearer)
108}
109
110pub fn correlation_resistance_measured() -> bool {
112 false
113}
114
115pub fn cover_oracle_observed_bytes(
122 profile: ProtectionProfile,
123 real_len: u64,
124) -> Result<u64, QdnfError> {
125 let class = cover_class(profile);
126 if class.pad_bucket == 0 {
127 return Ok(real_len);
128 }
129 let padded = pad_to_next_bucket(real_len, class.pad_bucket as u64)?;
130 if class.cover_bps == 0 {
131 return Ok(padded);
132 }
133 if real_len <= class.pad_bucket as u64 {
134 Ok(class.pad_bucket as u64)
135 } else {
136 Ok(padded)
137 }
138}
139
140pub const fn p1_padding_bucket() -> u64 {
142 P1_PADDING_BUCKET
143}
144
145pub const fn p3_cell_bytes() -> u64 {
147 P3_CELL_BYTES
148}
149
150pub const fn p2_record_bytes() -> u64 {
152 P2_RECORD_BYTES
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158 use crate::net::qdnf::profiles::budget::estimate_overhead;
159 use crate::net::qdnf::profiles::catalog::control_set_from_predicates;
160
161 #[test]
162 fn catalog_profiles_do_not_mandate_cover() {
163 assert!(!cover_mandatory(ProtectionProfile::P0));
164 assert!(!cover_mandatory(ProtectionProfile::P1));
165 assert!(!cover_mandatory(ProtectionProfile::P2));
166 assert!(!cover_mandatory(ProtectionProfile::P3));
167 assert!(!cover_mandatory(ProtectionProfile::P4));
168 assert_eq!(drop_cover_for_cost(ProtectionProfile::P3), Ok(()));
169 }
170
171 #[test]
172 fn mandatory_cover_cannot_be_dropped_for_cost() {
173 let controls = control_set_from_predicates(
174 ProtectionProfile::P3,
175 &[
176 ControlPredicate::Authenticity,
177 ControlPredicate::CoverTraffic,
178 ],
179 )
180 .unwrap();
181 assert!(cover_mandatory_controls(&controls));
182 assert_eq!(
183 drop_cover_for_controls(&controls),
184 Err(QdnfError::Downgrade)
185 );
186 assert_eq!(
187 estimate_overhead(ProtectionProfile::P3, &controls, 1, 1000, 0, true),
188 Err(QdnfError::Downgrade)
189 );
190 }
191
192 #[test]
193 fn cover_class_reuses_budget_buckets() {
194 let p1 = cover_class(ProtectionProfile::P1);
195 assert_eq!(p1.pad_bucket, P1_PADDING_BUCKET as u32);
196 assert_eq!(p1.batch_max, 1);
197 assert_eq!(p1.cover_bps, 0);
198 let p3 = cover_class(ProtectionProfile::P3);
199 assert_eq!(p3.pad_bucket, P3_CELL_BYTES as u32);
200 assert_eq!(p3.batch_max, 8);
201 assert_eq!(p3.cover_bps, P3_COVER_BYTES_PER_SEC);
202 let p4 = cover_class(ProtectionProfile::P4);
203 assert_eq!(p4.pad_bucket, p3.pad_bucket);
204 assert_eq!(p4.cover_bps, P3_COVER_BYTES_PER_SEC);
205 }
206
207 #[test]
208 fn p3_requires_approved_relay_p4_isolated() {
209 assert!(approved_relay_required(ProtectionProfile::P3));
210 assert!(!isolated_bearer_required(ProtectionProfile::P3));
211 assert!(!approved_relay_required(ProtectionProfile::P4));
212 assert!(isolated_bearer_required(ProtectionProfile::P4));
213 }
214
215 #[test]
216 fn correlation_resistance_is_unmeasured() {
217 assert!(!correlation_resistance_measured());
218 }
219
220 #[test]
221 fn cover_oracle_does_not_leak_real_length_class() {
222 let a = 17u64;
223 let b = 200u64;
224 assert_ne!(a, b);
225 let p1_a = cover_oracle_observed_bytes(ProtectionProfile::P1, a).unwrap();
226 let p1_b = cover_oracle_observed_bytes(ProtectionProfile::P1, b).unwrap();
227 assert_eq!(p1_a, p1_b);
228 assert_eq!(p1_a, P1_PADDING_BUCKET);
229 let p3_a = cover_oracle_observed_bytes(ProtectionProfile::P3, 1).unwrap();
230 let p3_b = cover_oracle_observed_bytes(ProtectionProfile::P3, 4000).unwrap();
231 assert_eq!(p3_a, p3_b);
232 assert_eq!(p3_a, P3_CELL_BYTES);
233 assert_ne!(
234 cover_oracle_observed_bytes(ProtectionProfile::P1, 17).unwrap(),
235 cover_oracle_observed_bytes(ProtectionProfile::P1, 300).unwrap()
236 );
237 }
238}