hyper-u256 0.1.0

Arithmétique 256-bit ultra-rapide en no_std avec optimisation SIMD et zéro allocation pour les corps finis et la cryptographie.
Documentation
use crate::U256;

/// Paire de `U256` traités ensemble pour amortir l'overhead d'appel de
/// fonction lors d'opérations par lots (batch processing).
///
/// ⚠️ Note d'implémentation : ce type ne contient **aucune** instruction
/// SIMD explicite (pas d'intrinsics `core::arch`, pas de `core::simd`).
/// Les deux `U256` sont traités par deux appels scalaires séquentiels.
/// Le compilateur peut, selon la cible et les flags (`-C target-cpu=native`),
/// choisir d'autovectoriser ce code — mais ce n'est ni garanti ni
/// contractuel. Ce type existe pour donner une API groupée pratique en
/// traitement par lots, pas pour promettre une exécution parallèle.
/// Les intrinsics SIMD explicites nécessiteraient du code `unsafe`, exclu
/// ici par `#![deny(unsafe_code)]` au niveau du crate.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C, align(64))]
pub struct U256x2 {
    pub low: U256,
    pub high: U256,
}

impl U256x2 {
    /// Construit une paire à partir de deux `U256`.
    #[inline(always)]
    pub const fn new(low: U256, high: U256) -> Self {
        Self { low, high }
    }

    /// Additionne deux paires `U256`, élément par élément
    /// (`self.low + rhs.low`, `self.high + rhs.high`), en `wrapping`.
    #[inline(always)]
    pub fn add_pair(&self, rhs: &Self) -> Self {
        Self {
            low: self.low.wrapping_add(&rhs.low),
            high: self.high.wrapping_add(&rhs.high),
        }
    }

    /// Multiplie deux paires `U256`, élément par élément, en `wrapping`.
    #[inline(always)]
    pub fn mul_pair(&self, rhs: &Self) -> Self {
        Self {
            low: self.low.wrapping_mul(&rhs.low),
            high: self.high.wrapping_mul(&rhs.high),
        }
    }
}

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

    #[test]
    fn test_new_and_fields() {
        let a = U256::from_limbs([1, 2, 3, 4]);
        let b = U256::from_limbs([5, 6, 7, 8]);
        let pair = U256x2::new(a, b);
        assert_eq!(pair.low, a);
        assert_eq!(pair.high, b);
    }

    #[test]
    fn test_add_pair_is_elementwise() {
        let pair_a = U256x2::new(U256::from_limbs([1, 0, 0, 0]), U256::from_limbs([10, 0, 0, 0]));
        let pair_b = U256x2::new(U256::from_limbs([2, 0, 0, 0]), U256::from_limbs([20, 0, 0, 0]));
        let res = pair_a.add_pair(&pair_b);
        assert_eq!(res.low, U256::from_limbs([3, 0, 0, 0]));
        assert_eq!(res.high, U256::from_limbs([30, 0, 0, 0]));
    }

    #[test]
    fn test_add_pair_carry_is_independent_per_lane() {
        // Le carry de `low` ne doit jamais contaminer `high`.
        let pair_a = U256x2::new(U256::from_limbs([u64::MAX, 0, 0, 0]), U256::from_limbs([1, 0, 0, 0]));
        let pair_b = U256x2::new(U256::from_limbs([1, 0, 0, 0]), U256::from_limbs([1, 0, 0, 0]));
        let res = pair_a.add_pair(&pair_b);
        assert_eq!(res.low, U256::from_limbs([0, 1, 0, 0]));
        assert_eq!(res.high, U256::from_limbs([2, 0, 0, 0]));
    }

    #[test]
    fn test_mul_pair_is_elementwise() {
        let pair_a = U256x2::new(U256::from_limbs([10, 0, 0, 0]), U256::from_limbs([3, 0, 0, 0]));
        let pair_b = U256x2::new(U256::from_limbs([5, 0, 0, 0]), U256::from_limbs([4, 0, 0, 0]));
        let res = pair_a.mul_pair(&pair_b);
        assert_eq!(res.low, U256::from_limbs([50, 0, 0, 0]));
        assert_eq!(res.high, U256::from_limbs([12, 0, 0, 0]));
    }

    #[test]
    fn test_equality_derive() {
        let a = U256x2::new(U256::ZERO, U256::ONE);
        let b = U256x2::new(U256::ZERO, U256::ONE);
        let c = U256x2::new(U256::ONE, U256::ZERO);
        assert_eq!(a, b);
        assert_ne!(a, c);
    }
}