clock_hash/
utils.rs

1//! Utility functions for ClockHash-256
2//!
3//! Provides rotation operations and helper functions.
4
5/// Rotate a 64-bit word left by n bits.
6///
7/// # Arguments
8///
9/// * `x` - The 64-bit word to rotate
10/// * `n` - Number of bits to rotate left (0-63)
11///
12/// # Returns
13///
14/// The rotated 64-bit word
15#[inline(always)]
16pub fn rotl64(x: u64, n: u32) -> u64 {
17    let n = n & 63; // Ensure n is in range 0..64
18    x.rotate_left(n)
19}
20
21/// Rotate a 64-bit word right by n bits.
22///
23/// # Arguments
24///
25/// * `x` - The 64-bit word to rotate
26/// * `n` - Number of bits to rotate right (0-63)
27///
28/// # Returns
29///
30/// The rotated 64-bit word
31#[inline(always)]
32pub fn rotr64(x: u64, n: u32) -> u64 {
33    let n = n & 63; // Ensure n is in range 0..64
34    x.rotate_right(n)
35}
36
37/// Rotate an 8-bit byte left by n bits.
38///
39/// # Arguments
40///
41/// * `x` - The 8-bit byte to rotate
42/// * `n` - Number of bits to rotate left (0-7)
43///
44/// # Returns
45///
46/// The rotated 8-bit byte
47#[inline(always)]
48pub fn rotl8(x: u8, n: u8) -> u8 {
49    let n = n & 7; // Ensure n is in range 0..8
50    x.rotate_left(n as u32)
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_rotl64() {
59        assert_eq!(rotl64(0x0000000000000001, 1), 0x0000000000000002);
60        assert_eq!(rotl64(0x8000000000000000, 1), 0x0000000000000001);
61        assert_eq!(rotl64(0x1234567890ABCDEF, 0), 0x1234567890ABCDEF);
62    }
63
64    #[test]
65    fn test_rotr64() {
66        assert_eq!(rotr64(0x0000000000000002, 1), 0x0000000000000001);
67        assert_eq!(rotr64(0x0000000000000001, 1), 0x8000000000000000);
68        assert_eq!(rotr64(0x1234567890ABCDEF, 0), 0x1234567890ABCDEF);
69    }
70
71    #[test]
72    fn test_rotl8() {
73        assert_eq!(rotl8(0x01, 1), 0x02);
74        assert_eq!(rotl8(0x80, 1), 0x01);
75        assert_eq!(rotl8(0xFF, 4), 0xFF);
76    }
77}