asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! ATP compression module for policy-driven compression with verification transparency.
//!
//! This module implements compression transforms for ATP objects while maintaining
//! clear verification semantics and proof boundaries. Supports optional compression
//! that can be disabled per object type/path without affecting verification truth.
//!
//! Key design principles:
//! - Compression is always optional and configurable
//! - Transform order is explicitly tracked in manifests
//! - Verification boundaries are preserved across transforms
//! - Lossy compression requires explicit policy approval
//! - Compression metadata enables proof reconstruction

use crate::atp::manifest::{
    CompressionAlgorithm, CompressionMetadata, CompressionPolicy, ObjectKind, TransformOrder,
    TransformType,
};
use std::io::{Read, Write};

pub mod algorithms;
pub mod policy;
pub mod validation;

pub use algorithms::*;
pub use policy::*;
pub use validation::*;

/// Compression result with metadata for verification.
#[derive(Debug, Clone, PartialEq)]
pub struct CompressionResult {
    /// Compressed data.
    pub compressed_data: Vec<u8>,
    /// Compression metadata for manifest.
    pub metadata: CompressionMetadata,
    /// Original data hash (for verification boundary).
    pub plaintext_hash: [u8; 32],
    /// Compressed data hash.
    pub compressed_hash: [u8; 32],
}

/// Compression error types.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompressionError {
    /// Policy violation.
    PolicyViolation(String),
    /// Unsupported algorithm.
    UnsupportedAlgorithm(CompressionAlgorithm),
    /// Compression failed.
    CompressionFailed(String),
    /// Decompression failed.
    DecompressionFailed(String),
    /// Size threshold violation.
    SizeThresholdViolation,
    /// Compression bomb detected.
    CompressionBomb,
    /// Transform order violation.
    TransformOrderViolation(String),
}

impl std::fmt::Display for CompressionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::PolicyViolation(msg) => write!(f, "compression policy violation: {msg}"),
            Self::UnsupportedAlgorithm(alg) => {
                write!(f, "unsupported compression algorithm: {alg:?}")
            }
            Self::CompressionFailed(msg) => write!(f, "compression failed: {msg}"),
            Self::DecompressionFailed(msg) => write!(f, "decompression failed: {msg}"),
            Self::SizeThresholdViolation => write!(f, "size below compression threshold"),
            Self::CompressionBomb => write!(f, "compression bomb detected"),
            Self::TransformOrderViolation(msg) => write!(f, "transform order violation: {msg}"),
        }
    }
}

impl std::error::Error for CompressionError {}

/// ATP compression engine with policy enforcement.
pub struct CompressionEngine;

impl CompressionEngine {
    /// Apply compression according to policy and transform order.
    pub fn compress(
        data: &[u8],
        object_kind: ObjectKind,
        policy: &CompressionPolicy,
        transform_order: Option<&TransformOrder>,
    ) -> Result<CompressionResult, CompressionError> {
        // Validate compression is allowed for this object kind
        if !policy.apply_to_kinds.contains(&object_kind) {
            return Err(CompressionError::PolicyViolation(format!(
                "compression not allowed for object kind {object_kind:?}"
            )));
        }

        // Check size threshold
        if data.len() < policy.min_size_threshold as usize {
            return Err(CompressionError::SizeThresholdViolation);
        }

        // Validate transform order if specified
        if let Some(order) = transform_order {
            Self::validate_transform_position(order)?;
        }

        // Compute plaintext hash before compression
        let plaintext_hash = Self::compute_hash(data);

        // Apply compression
        let compressed_data = match policy.algorithm {
            CompressionAlgorithm::None => data.to_vec(),
            CompressionAlgorithm::Lz4 => Self::compress_lz4(data, policy.level)?,
            CompressionAlgorithm::Gzip => Self::compress_gzip(data, policy.level)?,
            CompressionAlgorithm::Brotli => Self::compress_brotli(data, policy.level)?,
        };

        // Compute compressed data hash
        let compressed_hash = Self::compute_hash(&compressed_data);

        // Calculate compression ratio and validate bounds
        let compression_ratio = compressed_data.len() as f32 / data.len() as f32;
        if compression_ratio > 1.2 {
            // If compression made things worse, consider using uncompressed
            return Ok(CompressionResult {
                compressed_data: data.to_vec(),
                metadata: CompressionMetadata {
                    algorithm: CompressionAlgorithm::None,
                    level: 0,
                    original_size: data.len() as u64,
                    compressed_size: data.len() as u64,
                    compression_ratio: 1.0,
                },
                plaintext_hash,
                compressed_hash: plaintext_hash,
            });
        }

        let metadata = CompressionMetadata {
            algorithm: policy.algorithm,
            level: policy.level,
            original_size: data.len() as u64,
            compressed_size: compressed_data.len() as u64,
            compression_ratio,
        };

        Ok(CompressionResult {
            compressed_data,
            metadata,
            plaintext_hash,
            compressed_hash,
        })
    }

    /// Decompress data according to metadata.
    pub fn decompress(
        compressed_data: &[u8],
        metadata: &CompressionMetadata,
    ) -> Result<Vec<u8>, CompressionError> {
        // Check for compression bomb
        if metadata.compression_ratio < 0.001 {
            // Ratio too good to be true, likely a bomb
            return Err(CompressionError::CompressionBomb);
        }

        match metadata.algorithm {
            CompressionAlgorithm::None => Ok(compressed_data.to_vec()),
            CompressionAlgorithm::Lz4 => {
                Self::decompress_lz4(compressed_data, metadata.original_size)
            }
            CompressionAlgorithm::Gzip => {
                Self::decompress_gzip(compressed_data, metadata.original_size)
            }
            CompressionAlgorithm::Brotli => {
                Self::decompress_brotli(compressed_data, metadata.original_size)
            }
        }
    }

    /// Check if compression is enabled for object type in policy.
    pub fn is_compression_enabled(policy: &CompressionPolicy, object_kind: ObjectKind) -> bool {
        !matches!(policy.algorithm, CompressionAlgorithm::None)
            && policy.apply_to_kinds.contains(&object_kind)
    }

    /// Validate transform position in the transform order.
    fn validate_transform_position(
        transform_order: &TransformOrder,
    ) -> Result<(), CompressionError> {
        let compression_pos = transform_order
            .transforms
            .iter()
            .position(|&t| t == TransformType::Compression);

        if let Some(pos) = compression_pos {
            // Compression should come after chunking but before encryption
            if let Some(chunk_pos) = transform_order
                .transforms
                .iter()
                .position(|&t| t == TransformType::Chunking)
            {
                if pos <= chunk_pos {
                    return Err(CompressionError::TransformOrderViolation(
                        "compression must come after chunking".to_string(),
                    ));
                }
            }

            if let Some(enc_pos) = transform_order
                .transforms
                .iter()
                .position(|&t| t == TransformType::Encryption)
            {
                if pos >= enc_pos {
                    return Err(CompressionError::TransformOrderViolation(
                        "compression must come before encryption".to_string(),
                    ));
                }
            }
        }

        Ok(())
    }

    /// Compute SHA-256 hash.
    fn compute_hash(data: &[u8]) -> [u8; 32] {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(data);
        hasher.finalize().into()
    }

    /// Compress using LZ4.
    fn compress_lz4(data: &[u8], _level: u8) -> Result<Vec<u8>, CompressionError> {
        lz4_flex::compress_prepend_size(data)
            .map_err(|e| CompressionError::CompressionFailed(e.to_string()))
    }

    /// Decompress LZ4.
    fn decompress_lz4(compressed: &[u8], expected_size: u64) -> Result<Vec<u8>, CompressionError> {
        let decompressed = lz4_flex::decompress_size_prepended(compressed)
            .map_err(|e| CompressionError::DecompressionFailed(e.to_string()))?;

        if decompressed.len() != expected_size as usize {
            return Err(CompressionError::DecompressionFailed(
                "decompressed size mismatch".to_string(),
            ));
        }

        Ok(decompressed)
    }

    /// Compress using Gzip.
    fn compress_gzip(data: &[u8], level: u8) -> Result<Vec<u8>, CompressionError> {
        use flate2::{Compression, write::GzEncoder};

        let mut encoder = GzEncoder::new(Vec::new(), Compression::new(level.into()));
        encoder
            .write_all(data)
            .map_err(|e| CompressionError::CompressionFailed(e.to_string()))?;

        encoder
            .finish()
            .map_err(|e| CompressionError::CompressionFailed(e.to_string()))
    }

    /// Decompress Gzip.
    fn decompress_gzip(compressed: &[u8], expected_size: u64) -> Result<Vec<u8>, CompressionError> {
        use flate2::read::GzDecoder;

        let mut decoder = GzDecoder::new(compressed);
        let mut decompressed = Vec::with_capacity(expected_size as usize);

        decoder
            .read_to_end(&mut decompressed)
            .map_err(|e| CompressionError::DecompressionFailed(e.to_string()))?;

        if decompressed.len() != expected_size as usize {
            return Err(CompressionError::DecompressionFailed(
                "decompressed size mismatch".to_string(),
            ));
        }

        Ok(decompressed)
    }

    /// Compress using Brotli.
    #[cfg(feature = "compression")]
    fn compress_brotli(data: &[u8], level: u8) -> Result<Vec<u8>, CompressionError> {
        let quality = u32::from(level.min(11));
        let mut encoder = brotli::CompressorWriter::new(Vec::new(), 4096, quality, 22);
        encoder
            .write_all(data)
            .map_err(|e| CompressionError::CompressionFailed(e.to_string()))?;
        encoder
            .flush()
            .map_err(|e| CompressionError::CompressionFailed(e.to_string()))?;
        Ok(encoder.into_inner())
    }

    /// Compress using Brotli.
    #[cfg(not(feature = "compression"))]
    fn compress_brotli(_data: &[u8], _level: u8) -> Result<Vec<u8>, CompressionError> {
        Err(CompressionError::UnsupportedAlgorithm(
            CompressionAlgorithm::Brotli,
        ))
    }

    /// Decompress Brotli.
    #[cfg(feature = "compression")]
    fn decompress_brotli(
        compressed: &[u8],
        expected_size: u64,
    ) -> Result<Vec<u8>, CompressionError> {
        let expected_size = usize::try_from(expected_size).map_err(|_| {
            CompressionError::DecompressionFailed("expected size does not fit usize".to_string())
        })?;
        let mut decoder = brotli::Decompressor::new(compressed, 4096);
        let mut decompressed = Vec::with_capacity(expected_size);

        decoder
            .read_to_end(&mut decompressed)
            .map_err(|e| CompressionError::DecompressionFailed(e.to_string()))?;

        if decompressed.len() != expected_size {
            return Err(CompressionError::DecompressionFailed(
                "decompressed size mismatch".to_string(),
            ));
        }

        Ok(decompressed)
    }

    /// Decompress Brotli.
    #[cfg(not(feature = "compression"))]
    fn decompress_brotli(
        _compressed: &[u8],
        _expected_size: u64,
    ) -> Result<Vec<u8>, CompressionError> {
        Err(CompressionError::UnsupportedAlgorithm(
            CompressionAlgorithm::Brotli,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lz4_compression_roundtrip() {
        let test_data =
            b"Hello, world! This is a test string for compression. compression compression";
        let policy = CompressionPolicy {
            algorithm: CompressionAlgorithm::Lz4,
            level: 1,
            min_size_threshold: 10,
            apply_to_kinds: vec![ObjectKind::FileObject],
        };

        let result =
            CompressionEngine::compress(test_data, ObjectKind::FileObject, &policy, None).unwrap();

        assert_eq!(result.metadata.algorithm, CompressionAlgorithm::Lz4);
        assert_eq!(result.metadata.original_size, test_data.len() as u64);
        assert!(result.metadata.compression_ratio <= 1.0);

        let decompressed =
            CompressionEngine::decompress(&result.compressed_data, &result.metadata).unwrap();

        assert_eq!(decompressed, test_data);
    }

    #[test]
    #[cfg(feature = "compression")]
    fn test_brotli_compression_roundtrip() {
        let test_data = b"ATP metadata compresses well when repeated: manifest manifest manifest chunk chunk chunk object object object";
        let policy = CompressionPolicy {
            algorithm: CompressionAlgorithm::Brotli,
            level: 6,
            min_size_threshold: 10,
            apply_to_kinds: vec![ObjectKind::FileObject],
        };

        let result =
            CompressionEngine::compress(test_data, ObjectKind::FileObject, &policy, None).unwrap();

        assert_eq!(result.metadata.algorithm, CompressionAlgorithm::Brotli);
        assert_eq!(result.metadata.original_size, test_data.len() as u64);
        assert_eq!(
            result.metadata.compressed_size,
            result.compressed_data.len() as u64
        );

        let decompressed =
            CompressionEngine::decompress(&result.compressed_data, &result.metadata).unwrap();

        assert_eq!(decompressed, test_data);
    }

    #[test]
    #[cfg(not(feature = "compression"))]
    fn test_brotli_reports_unsupported_without_feature() {
        let test_data = b"ATP metadata compresses well when repeated: manifest manifest manifest";
        let policy = CompressionPolicy {
            algorithm: CompressionAlgorithm::Brotli,
            level: 6,
            min_size_threshold: 10,
            apply_to_kinds: vec![ObjectKind::FileObject],
        };

        let result = CompressionEngine::compress(test_data, ObjectKind::FileObject, &policy, None);

        assert!(matches!(
            result,
            Err(CompressionError::UnsupportedAlgorithm(
                CompressionAlgorithm::Brotli
            ))
        ));
    }

    #[test]
    fn test_compression_disabled_for_wrong_object_kind() {
        let test_data = b"Hello, world!";
        let policy = CompressionPolicy {
            algorithm: CompressionAlgorithm::Lz4,
            level: 1,
            min_size_threshold: 10,
            apply_to_kinds: vec![ObjectKind::FileObject],
        };

        let result = CompressionEngine::compress(
            test_data,
            ObjectKind::Directory, // Not in apply_to_kinds
            &policy,
            None,
        );

        assert!(matches!(result, Err(CompressionError::PolicyViolation(_))));
    }

    #[test]
    fn test_size_threshold_enforcement() {
        let test_data = b"Hi"; // Too small
        let policy = CompressionPolicy {
            algorithm: CompressionAlgorithm::Lz4,
            level: 1,
            min_size_threshold: 100,
            apply_to_kinds: vec![ObjectKind::FileObject],
        };

        let result = CompressionEngine::compress(test_data, ObjectKind::FileObject, &policy, None);

        assert!(matches!(
            result,
            Err(CompressionError::SizeThresholdViolation)
        ));
    }

    #[test]
    fn test_transform_order_validation() {
        use crate::atp::manifest::{
            HashPoint, PrivacyLevel, TransformOrder, TransformType, VerificationBoundary,
            VerificationLevel,
        };

        let test_data = b"Hello, world! This is a test string for compression.";
        let policy = CompressionPolicy {
            algorithm: CompressionAlgorithm::Lz4,
            level: 1,
            min_size_threshold: 10,
            apply_to_kinds: vec![ObjectKind::FileObject],
        };

        // Valid order: Chunking -> Compression -> Encryption
        let valid_order = TransformOrder {
            transforms: vec![
                TransformType::Chunking,
                TransformType::Compression,
                TransformType::Encryption,
            ],
            hash_point: HashPoint::PostCompression,
            verification_boundary: VerificationBoundary {
                relay_verifiable: VerificationLevel::TransferIntegrity,
                mailbox_verifiable: VerificationLevel::ContentHash,
                e2e_verification_required: true,
                privacy_level: PrivacyLevel::MetadataVisible,
            },
        };

        let result = CompressionEngine::compress(
            test_data,
            ObjectKind::FileObject,
            &policy,
            Some(&valid_order),
        );
        assert!(result.is_ok());

        // Invalid order: Compression before Chunking
        let invalid_order = TransformOrder {
            transforms: vec![
                TransformType::Compression,
                TransformType::Chunking,
                TransformType::Encryption,
            ],
            hash_point: HashPoint::PostCompression,
            verification_boundary: VerificationBoundary {
                relay_verifiable: VerificationLevel::TransferIntegrity,
                mailbox_verifiable: VerificationLevel::ContentHash,
                e2e_verification_required: true,
                privacy_level: PrivacyLevel::MetadataVisible,
            },
        };

        let result = CompressionEngine::compress(
            test_data,
            ObjectKind::FileObject,
            &policy,
            Some(&invalid_order),
        );
        assert!(matches!(
            result,
            Err(CompressionError::TransformOrderViolation(_))
        ));
    }
}