clock-hash 1.0.0

ClockHash-256: Consensus hash function for ClockinChain
Documentation
//! Utility functions for ClockHash-256
//!
//! Provides rotation operations and helper functions.

/// Rotate a 64-bit word left by n bits.
///
/// # Arguments
///
/// * `x` - The 64-bit word to rotate
/// * `n` - Number of bits to rotate left (0-63)
///
/// # Returns
///
/// The rotated 64-bit word
#[inline(always)]
pub fn rotl64(x: u64, n: u32) -> u64 {
    let n = n & 63; // Ensure n is in range 0..64
    x.rotate_left(n)
}

/// Rotate a 64-bit word right by n bits.
///
/// # Arguments
///
/// * `x` - The 64-bit word to rotate
/// * `n` - Number of bits to rotate right (0-63)
///
/// # Returns
///
/// The rotated 64-bit word
#[inline(always)]
pub fn rotr64(x: u64, n: u32) -> u64 {
    let n = n & 63; // Ensure n is in range 0..64
    x.rotate_right(n)
}

/// Rotate an 8-bit byte left by n bits.
///
/// # Arguments
///
/// * `x` - The 8-bit byte to rotate
/// * `n` - Number of bits to rotate left (0-7)
///
/// # Returns
///
/// The rotated 8-bit byte
#[inline(always)]
pub fn rotl8(x: u8, n: u8) -> u8 {
    let n = n & 7; // Ensure n is in range 0..8
    x.rotate_left(n as u32)
}

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

    #[test]
    fn test_rotl64() {
        assert_eq!(rotl64(0x0000000000000001, 1), 0x0000000000000002);
        assert_eq!(rotl64(0x8000000000000000, 1), 0x0000000000000001);
        assert_eq!(rotl64(0x1234567890ABCDEF, 0), 0x1234567890ABCDEF);
    }

    #[test]
    fn test_rotr64() {
        assert_eq!(rotr64(0x0000000000000002, 1), 0x0000000000000001);
        assert_eq!(rotr64(0x0000000000000001, 1), 0x8000000000000000);
        assert_eq!(rotr64(0x1234567890ABCDEF, 0), 0x1234567890ABCDEF);
    }

    #[test]
    fn test_rotl8() {
        assert_eq!(rotl8(0x01, 1), 0x02);
        assert_eq!(rotl8(0x80, 1), 0x01);
        assert_eq!(rotl8(0xFF, 4), 0xFF);
    }
}