atlas-common 0.1.1

Common functionality for Atlas ML provenance system
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use crate::error::{Error, Result};
use crate::hash::calculate_hash_with_algorithm;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;

/// Supported cryptographic hash algorithms
///
/// All algorithms use the SHA-2 family for security and performance.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HashAlgorithm {
    /// SHA-256 (256-bit/32-byte output)
    #[serde(rename = "sha256")]
    Sha256,
    /// SHA-384 (384-bit/48-byte output) - Default
    #[serde(rename = "sha384")]
    Sha384,
    /// SHA-512 (512-bit/64-byte output)
    #[serde(rename = "sha512")]
    Sha512,
}

impl HashAlgorithm {
    /// Get algorithm name as string
    ///
    /// # Example
    ///
    /// ```rust
    /// use atlas_common::hash::HashAlgorithm;
    ///
    /// assert_eq!(HashAlgorithm::Sha256.as_str(), "sha256");
    /// ```
    pub fn as_str(&self) -> &'static str {
        match self {
            HashAlgorithm::Sha256 => "sha256",
            HashAlgorithm::Sha384 => "sha384",
            HashAlgorithm::Sha512 => "sha512",
        }
    }

    /// Get the output size in bytes
    ///
    /// # Example
    ///
    /// ```rust
    /// use atlas_common::hash::HashAlgorithm;
    ///
    /// assert_eq!(HashAlgorithm::Sha256.output_size(), 32);
    /// assert_eq!(HashAlgorithm::Sha512.output_size(), 64);
    /// ```
    pub fn output_size(&self) -> usize {
        match self {
            HashAlgorithm::Sha256 => 32,
            HashAlgorithm::Sha384 => 48,
            HashAlgorithm::Sha512 => 64,
        }
    }

    /// Get the output size in hex characters
    ///
    /// # Example
    ///
    /// ```rust
    /// use atlas_common::hash::HashAlgorithm;
    ///
    /// assert_eq!(HashAlgorithm::Sha256.hex_length(), 64);
    /// ```
    pub fn hex_length(&self) -> usize {
        self.output_size() * 2
    }

    /// Check if a hash string matches this algorithm's expected format
    ///
    /// # Example
    ///
    /// ```rust
    /// use atlas_common::hash::HashAlgorithm;
    ///
    /// let hash = "a".repeat(64);
    /// assert!(HashAlgorithm::Sha256.validate_hash(&hash));
    /// assert!(!HashAlgorithm::Sha384.validate_hash(&hash));
    /// ```
    pub fn validate_hash(&self, hash: &str) -> bool {
        hash.len() == self.hex_length() && hash.chars().all(|c| c.is_ascii_hexdigit())
    }
}

impl Default for HashAlgorithm {
    fn default() -> Self {
        HashAlgorithm::Sha384
    }
}

impl fmt::Display for HashAlgorithm {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl FromStr for HashAlgorithm {
    type Err = Error;
    /// Parse algorithm from string
    ///
    /// Accepts: "sha256", "sha-256", "sha384", "sha-384", "sha512", "sha-512"
    /// (case-insensitive)
    fn from_str(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "sha256" | "sha-256" => Ok(HashAlgorithm::Sha256),
            "sha384" | "sha-384" => Ok(HashAlgorithm::Sha384),
            "sha512" | "sha-512" => Ok(HashAlgorithm::Sha512),
            _ => Err(Error::InvalidFormat(format!(
                "Unknown hash algorithm: {}. Supported: sha256, sha384, sha512",
                s
            ))),
        }
    }
}

/// Hardware capabilities detected at runtime
#[derive(Debug, Clone, Copy)]
pub struct HardwareCapabilities {
    /// Intel SHA-NI extensions available
    pub sha_extensions: bool,
    /// AVX-512 available (Intel Xeon optimization)
    pub avx512: bool,
    /// ARM crypto extensions (Apple Silicon/ARM64)
    pub arm_crypto: bool,
    /// Number of CPU cores
    pub cpu_cores: usize,
}

impl HardwareCapabilities {
    /// Detect available hardware capabilities
    pub fn detect() -> Self {
        let cpu_cores = num_cpus::get();

        #[cfg(target_arch = "x86_64")]
        {
            Self {
                sha_extensions: is_x86_feature_detected!("sha"),
                avx512: is_x86_feature_detected!("avx512f"),
                arm_crypto: false,
                cpu_cores,
            }
        }

        #[cfg(target_arch = "aarch64")]
        {
            Self {
                sha_extensions: false,
                avx512: false,
                arm_crypto: Self::detect_arm_crypto(),
                cpu_cores,
            }
        }

        #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
        {
            Self {
                sha_extensions: false,
                avx512: false,
                arm_crypto: false,
                cpu_cores,
            }
        }
    }

    #[cfg(target_arch = "aarch64")]
    fn detect_arm_crypto() -> bool {
        std::arch::is_aarch64_feature_detected!("aes")
            && std::arch::is_aarch64_feature_detected!("sha2")
    }

    /// Get optimal chunk size for parallel processing
    pub fn optimal_chunk_size(&self) -> usize {
        match self.cpu_cores {
            1..=4 => 16 * 1024 * 1024,  // 16MB
            5..=8 => 32 * 1024 * 1024,  // 32MB
            9..=16 => 64 * 1024 * 1024, // 64MB
            _ => 128 * 1024 * 1024,     // 128MB for high-core Xeons
        }
    }
}

/// Strategy for hash optimization
#[derive(Debug, Clone, Copy)]
enum HashOptimization {
    /// Intel SHA-NI (best for single-threaded)
    IntelShaExtensions,
    /// Intel Xeon parallel with AVX-512
    XeonParallel,
    /// Apple Silicon ARM crypto
    AppleSiliconCrypto,
    /// Generic multi-core parallel
    MultiCore,
    /// Standard software
    Software,
}

impl HashOptimization {
    fn select(capabilities: &HardwareCapabilities, data_size: usize) -> Self {
        let parallel_threshold = capabilities.optimal_chunk_size();

        #[cfg(target_arch = "x86_64")]
        {
            if capabilities.sha_extensions && data_size < parallel_threshold {
                return Self::IntelShaExtensions;
            }
            if capabilities.avx512 && data_size >= parallel_threshold && capabilities.cpu_cores >= 4
            {
                return Self::XeonParallel;
            }
            if capabilities.sha_extensions {
                return Self::IntelShaExtensions;
            }
        }

        #[cfg(target_arch = "aarch64")]
        {
            if capabilities.arm_crypto {
                return Self::AppleSiliconCrypto;
            }
        }

        if data_size >= parallel_threshold && capabilities.cpu_cores >= 3 {
            return Self::MultiCore;
        }

        Self::Software
    }
}

/// Builder for incremental hashing
///
/// Useful when hashing data that arrives in chunks or from multiple sources.
///
/// # Example
///
/// ```rust
/// use atlas_common::hash::{HashBuilder, HashAlgorithm};
///
/// let mut builder = HashBuilder::new(HashAlgorithm::Sha256);
/// builder.update(b"part1");
/// builder.update(b"part2");
/// let hash = builder.finalize();
/// ```
pub struct HashBuilder {
    algorithm: HashAlgorithm,
    hasher: HashBuilderInner,
}

enum HashBuilderInner {
    Sha256(sha2::Sha256),
    Sha384(sha2::Sha384),
    Sha512(sha2::Sha512),
}

impl HashBuilder {
    /// Create a new hash builder with the specified algorithm
    pub fn new(algorithm: HashAlgorithm) -> Self {
        use sha2::Digest;

        let hasher = match algorithm {
            HashAlgorithm::Sha256 => HashBuilderInner::Sha256(sha2::Sha256::new()),
            HashAlgorithm::Sha384 => HashBuilderInner::Sha384(sha2::Sha384::new()),
            HashAlgorithm::Sha512 => HashBuilderInner::Sha512(sha2::Sha512::new()),
        };

        Self { algorithm, hasher }
    }

    /// Update the hash with more data
    ///
    /// Can be called multiple times to add data incrementally.
    pub fn update(&mut self, data: &[u8]) {
        use sha2::Digest;

        match &mut self.hasher {
            HashBuilderInner::Sha256(h) => h.update(data),
            HashBuilderInner::Sha384(h) => h.update(data),
            HashBuilderInner::Sha512(h) => h.update(data),
        }
    }

    /// Finalize and get the hash as a hex string
    ///
    /// Consumes the builder.
    pub fn finalize(self) -> String {
        use sha2::Digest;

        match self.hasher {
            HashBuilderInner::Sha256(h) => hex::encode(h.finalize()),
            HashBuilderInner::Sha384(h) => hex::encode(h.finalize()),
            HashBuilderInner::Sha512(h) => hex::encode(h.finalize()),
        }
    }

    /// Get the algorithm being used
    pub fn algorithm(&self) -> HashAlgorithm {
        self.algorithm
    }
}

/// Trait for types that can be hashed
///
/// Implemented for `[u8]`, `str`, and `String`.
///
/// # Example
///
/// ```rust
/// use atlas_common::hash::{Hasher, HashAlgorithm};
///
/// let text = "Hello, World!";
/// let hash = text.hash(HashAlgorithm::Sha256);
///
/// let bytes = b"raw bytes";
/// let hash2 = bytes.hash(HashAlgorithm::Sha512);
///
/// let large_data = vec![0u8; 100_000_000]; // 100MB
/// let optimized_hash = large_data.hash_optimized(HashAlgorithm::Sha384);
/// ```
pub trait Hasher {
    fn hash(&self, algorithm: HashAlgorithm) -> String;
    fn hash_default(&self) -> String {
        self.hash(HashAlgorithm::default())
    }

    /// Hash with hardware optimization
    ///
    /// Uses Intel Xeon parallel processing, Apple Silicon crypto, or multi-core
    /// optimization based on available hardware and data size.
    fn hash_optimized(&self, algorithm: HashAlgorithm) -> String;
}

impl Hasher for [u8] {
    fn hash(&self, algorithm: HashAlgorithm) -> String {
        calculate_hash_with_algorithm(self, &algorithm)
    }

    fn hash_optimized(&self, algorithm: HashAlgorithm) -> String {
        calculate_hash_optimized(self, algorithm)
    }
}

impl Hasher for str {
    fn hash(&self, algorithm: HashAlgorithm) -> String {
        self.as_bytes().hash(algorithm)
    }

    fn hash_optimized(&self, algorithm: HashAlgorithm) -> String {
        self.as_bytes().hash_optimized(algorithm)
    }
}

impl Hasher for String {
    fn hash(&self, algorithm: HashAlgorithm) -> String {
        self.as_bytes().hash(algorithm)
    }

    fn hash_optimized(&self, algorithm: HashAlgorithm) -> String {
        self.as_bytes().hash_optimized(algorithm)
    }
}

/// Calculate hash with hardware optimization
///
/// Automatically selects the best optimization strategy:
/// - Intel Xeon: Uses SHA-NI extensions + AVX-512 parallel processing
/// - Apple Silicon: Uses ARM crypto extensions  
/// - Other: Uses multi-core parallel processing when beneficial (Risc?)
pub fn calculate_hash_optimized(data: &[u8], algorithm: HashAlgorithm) -> String {
    let capabilities = HardwareCapabilities::detect();
    let strategy = HashOptimization::select(&capabilities, data.len());

    match strategy {
        HashOptimization::IntelShaExtensions => calculate_intel_sha_ni(data, algorithm),
        HashOptimization::XeonParallel => calculate_xeon_parallel(data, algorithm, &capabilities),
        HashOptimization::AppleSiliconCrypto => calculate_apple_silicon(data, algorithm),
        HashOptimization::MultiCore => calculate_multicore_parallel(data, algorithm, &capabilities),
        HashOptimization::Software => calculate_hash_with_algorithm(data, &algorithm),
    }
}

#[cfg(target_arch = "x86_64")]
fn calculate_intel_sha_ni(data: &[u8], algorithm: HashAlgorithm) -> String {
    calculate_hash_with_algorithm(data, &algorithm)
}

#[cfg(target_arch = "x86_64")]
fn calculate_xeon_parallel(
    data: &[u8],
    algorithm: HashAlgorithm,
    capabilities: &HardwareCapabilities,
) -> String {
    let chunk_size = capabilities.optimal_chunk_size();

    if data.len() <= chunk_size {
        return calculate_hash_with_algorithm(data, &algorithm);
    }

    use rayon::prelude::*;

    let chunk_hashes: Vec<String> = data
        .par_chunks(chunk_size)
        .map(|chunk| calculate_hash_with_algorithm(chunk, &algorithm))
        .collect();

    let combined = chunk_hashes.join("");
    calculate_hash_with_algorithm(combined.as_bytes(), &algorithm)
}

#[cfg(target_arch = "aarch64")]
fn calculate_apple_silicon(data: &[u8], algorithm: HashAlgorithm) -> String {
    calculate_hash_with_algorithm(data, &algorithm)
}

fn calculate_multicore_parallel(
    data: &[u8],
    algorithm: HashAlgorithm,
    capabilities: &HardwareCapabilities,
) -> String {
    let chunk_size = capabilities.optimal_chunk_size();

    if data.len() <= chunk_size || capabilities.cpu_cores < 3 {
        return calculate_hash_with_algorithm(data, &algorithm);
    }

    use rayon::prelude::*;

    let chunk_hashes: Vec<String> = data
        .par_chunks(chunk_size)
        .map(|chunk| calculate_hash_with_algorithm(chunk, &algorithm))
        .collect();

    let combined = chunk_hashes.join("");
    calculate_hash_with_algorithm(combined.as_bytes(), &algorithm)
}

#[cfg(not(target_arch = "x86_64"))]
fn calculate_intel_sha_ni(data: &[u8], algorithm: HashAlgorithm) -> String {
    calculate_hash_with_algorithm(data, &algorithm)
}

#[cfg(not(target_arch = "x86_64"))]
fn calculate_xeon_parallel(
    data: &[u8],
    algorithm: HashAlgorithm,
    capabilities: &HardwareCapabilities,
) -> String {
    calculate_multicore_parallel(data, algorithm, capabilities)
}

#[cfg(not(target_arch = "aarch64"))]
fn calculate_apple_silicon(data: &[u8], algorithm: HashAlgorithm) -> String {
    calculate_hash_with_algorithm(data, &algorithm)
}

/// Batch hasher for processing multiple inputs efficiently
pub struct BatchHasher {
    capabilities: HardwareCapabilities,
}

impl BatchHasher {
    pub fn new() -> Self {
        Self {
            capabilities: HardwareCapabilities::detect(),
        }
    }

    /// Hash multiple inputs in parallel
    pub fn hash_batch(&self, inputs: &[&[u8]], algorithm: HashAlgorithm) -> Vec<String> {
        if inputs.len() < 4 || self.capabilities.cpu_cores < 3 {
            return inputs
                .iter()
                .map(|data| calculate_hash_optimized(data, algorithm))
                .collect();
        }

        use rayon::prelude::*;
        inputs
            .par_iter()
            .map(|data| calculate_hash_optimized(data, algorithm))
            .collect()
    }
}

impl Default for BatchHasher {
    fn default() -> Self {
        Self::new()
    }
}

/// Get hardware capabilities information
pub fn get_hardware_capabilities() -> HardwareCapabilities {
    HardwareCapabilities::detect()
}

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

    #[test]
    fn test_algorithm_properties() {
        assert_eq!(HashAlgorithm::Sha256.output_size(), 32);
        assert_eq!(HashAlgorithm::Sha256.hex_length(), 64);

        assert_eq!(HashAlgorithm::Sha384.output_size(), 48);
        assert_eq!(HashAlgorithm::Sha384.hex_length(), 96);

        assert_eq!(HashAlgorithm::Sha512.output_size(), 64);
        assert_eq!(HashAlgorithm::Sha512.hex_length(), 128);
    }

    #[test]
    fn test_algorithm_parsing() {
        assert_eq!(
            HashAlgorithm::from_str("sha256").unwrap(),
            HashAlgorithm::Sha256
        );
        assert_eq!(
            HashAlgorithm::from_str("SHA384").unwrap(),
            HashAlgorithm::Sha384
        );
        assert_eq!(
            HashAlgorithm::from_str("sha-512").unwrap(),
            HashAlgorithm::Sha512
        );

        assert!(HashAlgorithm::from_str("md5").is_err());
    }

    #[test]
    fn test_hash_builder() {
        let mut builder = HashBuilder::new(HashAlgorithm::Sha256);
        builder.update(b"hello ");
        builder.update(b"world");
        let hash = builder.finalize();

        assert_eq!(hash.len(), 64);

        let direct_hash = "hello world".hash(HashAlgorithm::Sha256);
        assert_eq!(hash, direct_hash);
    }

    #[test]
    fn test_hasher_trait() {
        let data = "test data";
        let hash1 = data.hash(HashAlgorithm::Sha256);
        let hash2 = data.as_bytes().hash(HashAlgorithm::Sha256);
        let hash3 = data.to_string().hash(HashAlgorithm::Sha256);

        assert_eq!(hash1, hash2);
        assert_eq!(hash2, hash3);
    }

    #[test]
    fn test_optimized_hashing() {
        let data = "test data for optimization";
        let normal_hash = data.hash(HashAlgorithm::Sha384);
        let optimized_hash = data.hash_optimized(HashAlgorithm::Sha384);

        assert_eq!(normal_hash, optimized_hash);
    }

    #[test]
    fn test_hardware_capabilities() {
        let caps = get_hardware_capabilities();
        assert!(caps.cpu_cores > 0);
        assert!(caps.optimal_chunk_size() >= 16 * 1024 * 1024);
    }

    #[test]
    fn test_batch_hasher() {
        let batch_hasher = BatchHasher::new();
        let inputs = vec![
            b"input 1".as_slice(),
            b"input 2".as_slice(),
            b"input 3".as_slice(),
        ];

        let batch_results = batch_hasher.hash_batch(&inputs, HashAlgorithm::Sha256);

        for (input, result) in inputs.iter().zip(batch_results.iter()) {
            let expected = input.hash(HashAlgorithm::Sha256);
            assert_eq!(*result, expected);
        }
    }

    #[test]
    fn test_large_data_optimization() {
        let small_data = vec![0u8; 1024];
        let large_data = vec![0u8; 50 * 1024 * 1024]; // 50MB

        let small_hash = calculate_hash_optimized(&small_data, HashAlgorithm::Sha384);
        let large_hash = calculate_hash_optimized(&large_data, HashAlgorithm::Sha384);

        assert_eq!(small_hash.len(), 96);
        assert_eq!(large_hash.len(), 96);

        let small_standard = calculate_hash_with_algorithm(&small_data, &HashAlgorithm::Sha384);
        assert_eq!(small_hash, small_standard);
    }
}