penis 0.1.1

A Rust implementation of the Penis Protocol
Documentation
/// Performs a right rotation on a 32-bit value
///
/// This is a constant-time operation used in SHA-256 hash computation.
///
/// # Arguments
///
/// * `x` - The value to rotate
/// * `n` - The number of positions to rotate right
///
/// # Returns
///
/// The rotated value
#[inline(always)]
pub(super) const fn rotr(x: u32, n: usize) -> u32 {
    (x >> n) | (x << (32 - n))
}

/// Computes the Σ₀ (sigma0) function used in SHA-256
///
/// Σ₀(x) = ROTR²(x) ⊕ ROTR¹³(x) ⊕ ROTR²²(x)
///
/// # Arguments
///
/// * `x` - The input value
///
/// # Returns
///
/// The result of the Σ₀ function
#[inline(always)]
pub(super) const fn sigma0(x: u32) -> u32 {
    rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22)
}

/// Computes the Σ₁ (sigma1) function used in SHA-256
///
/// Σ₁(x) = ROTR⁶(x) ⊕ ROTR¹¹(x) ⊕ ROTR²⁵(x)
///
/// # Arguments
///
/// * `x` - The input value
///
/// # Returns
///
/// The result of the Σ₁ function
#[inline(always)]
pub(super) const fn sigma1(x: u32) -> u32 {
    rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25)
}

/// Computes the σ₀ (lowercase sigma0) function used in SHA-256
///
/// σ₀(x) = ROTR⁷(x) ⊕ ROTR¹⁸(x) ⊕ SHR³(x)
///
/// # Arguments
///
/// * `x` - The input value
///
/// # Returns
///
/// The result of the σ₀ function
#[inline(always)]
pub(super) const fn lsigma0(x: u32) -> u32 {
    rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3)
}

/// Computes the σ₁ (lowercase sigma1) function used in SHA-256
///
/// σ₁(x) = ROTR¹⁷(x) ⊕ ROTR¹⁹(x) ⊕ SHR¹⁰(x)
///
/// # Arguments
///
/// * `x` - The input value
///
/// # Returns
///
/// The result of the σ₁ function
#[inline(always)]
pub(super) const fn lsigma1(x: u32) -> u32 {
    rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10)
}

/// Computes the Ch (choose) function used in SHA-256
///
/// Ch(x,y,z) = (x ∧ y) ⊕ (¬x ∧ z)
///
/// This function chooses bits from y or z based on x.
///
/// # Arguments
///
/// * `x` - The selector value
/// * `y` - First input value
/// * `z` - Second input value
///
/// # Returns
///
/// The result of the Ch function
#[inline(always)]
pub(super) const fn ch(x: u32, y: u32, z: u32) -> u32 {
    (x & y) ^ (!x & z)
}

/// Computes the Maj (majority) function used in SHA-256
///
/// Maj(x,y,z) = (x ∧ y) ⊕ (x ∧ z) ⊕ (y ∧ z)
///
/// This function returns the majority value of each bit position.
///
/// # Arguments
///
/// * `x` - First input value
/// * `y` - Second input value
/// * `z` - Third input value
///
/// # Returns
///
/// The result of the Maj function
#[inline(always)]
pub(super) const fn maj(x: u32, y: u32, z: u32) -> u32 {
    (x & y) ^ (x & z) ^ (y & z)
}