clock-hash 1.0.0

ClockHash-256: Consensus hash function for ClockinChain
Documentation
//! Constants for ClockHash-256
//!
//! Defines the initialization vector, rotation schedule, multiplication primes,
//! and the S-box substitution table.

/// Initialization Vector (IV) - 8 u64 constants from irrational fractions
pub const IV: [u64; 8] = [
    0x243F6A8885A308D3, // IV0: π fraction
    0x13198A2E03707344, // IV1: e fraction
    0xA4093822299F31D0, // IV2: √2 fraction
    0x082EFA98EC4E6C89, // IV3: golden ratio φ fraction
    0x452821E638D01377, // IV4: φ² fraction
    0xBE5466CF34E90C6C, // IV5: π² fraction
    0xC0AC29B7C97C50DD, // IV6: e² fraction
    0x3F84D5B5B5470917, // IV7: √3 fraction
];

/// Rotation Schedule - 16 rotation values for ClockMix and ClockPermute
pub const ROTATION_SCHEDULE: [u32; 16] = [
    7, 19, 31, 43, 13, 29, 37, 53, 11, 23, 41, 59, 17, 33, 47, 61,
];

/// Multiplication Prime P0
pub const P0: u64 = 0x9E3779B185EBCA87;

/// Multiplication Prime P1
pub const P1: u64 = 0xC2B2AE3D27D4EB4F;

include!(concat!(env!("OUT_DIR"), "/sbox.rs"));

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

    #[test]
    fn test_iv_length() {
        assert_eq!(IV.len(), 8);
    }

    #[test]
    fn test_rotation_schedule_length() {
        assert_eq!(ROTATION_SCHEDULE.len(), 16);
    }

    #[test]
    fn test_sbox_length() {
        assert_eq!(SBOX.len(), 256);
    }

    #[test]
    fn test_sbox_uniqueness() {
        // Check that S-box values are reasonably distributed
        // Note: The current generation method may produce some duplicates,
        // which is acceptable as long as the distribution is good
        let mut seen = [0u8; 256];
        for &val in &SBOX {
            seen[val as usize] = seen[val as usize].saturating_add(1);
        }

        // Count unique values
        let unique_count = seen.iter().filter(|&&count| count > 0).count();

        // S-box should have at least 150 unique values (59% uniqueness)
        // This ensures reasonable distribution even if some duplicates exist
        // The current generation method produces ~169 unique values
        assert!(
            unique_count >= 150,
            "S-box should have at least 150 unique values, got {}",
            unique_count
        );
    }
}