ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
use crate::backend::aes128::Aes128;
#[cfg(all(
  any(target_arch = "x86", target_arch = "x86_64"),
  target_endian = "little",
  any(feature = "std", all(target_feature = "aes", target_feature = "sse2"))
))]
use crate::backend::tree::finalize_merge4_lk;
#[cfg(any(
  feature = "cpu",
  all(target_arch = "aarch64", target_endian = "little"),
  not(all(
    any(target_arch = "x86", target_arch = "x86_64"),
    target_endian = "little",
    target_feature = "aes",
    target_feature = "sse2"
  ))
))]
use crate::backend::tree::finalize_merge4_pre;

/// Encapsulates Pre-XOR (ARM64 AESE, scalar T-table) vs Post-XOR (x86 AES-NI/VAES) AES fusion.
pub(crate) trait AesMode<L: Aes128> {
  unsafe fn r1_zero(ctx: L, a: L::V, b: L::V) -> L::V;
  unsafe fn f_zero(ctx: L, s: L::V) -> L::V;
  unsafe fn chain_x01<const N: usize>(ctx: L, x0: L::V, x1: L::V, rk_li: [L::V; N]) -> L::V;
  unsafe fn acc_init_1(ctx: L, key: L::V, data: L::V) -> L::V;
  unsafe fn acc_absorb_1(ctx: L, acc: L::V, data: L::V) -> L::V;
  unsafe fn ctr_prep_1(ctx: L, ctr: L::V, rk0: L::V) -> L::V;
  unsafe fn ctr_mid_1(ctx: L, s: L::V, rk: L::V) -> L::V;
  unsafe fn ctr_fin_1(ctx: L, s: L::V, rk3: L::V, rk0: L::V) -> L::V;
  unsafe fn compress_17_32_core(
    ctx: L,
    b0: L::V,
    k0: L::V,
    b1: L::V,
    k1: L::V,
    lk: L::V,
  ) -> (L::V, L::V);
  unsafe fn compress_33_64_core<const HI: bool>(
    ctx: L,
    b_li: [L::V; 4],
    key_li: [L::V; 4],
    lk: L::V,
  ) -> (L::V, L::V);
  unsafe fn merge4_128<const HI: bool>(ctx: L, acc_li: [L::V; 4], lk: L::V) -> (L::V, L::V);
  unsafe fn merge8_128<const HI: bool>(ctx: L, acc_li: [L::V; 8], lk: L::V) -> (L::V, L::V);
  unsafe fn merge16_128<const HI: bool>(
    ctx: L,
    lo_li: [L::V; 8],
    hi_li: [L::V; 8],
    lk: L::V,
  ) -> (L::V, L::V);
}

#[cfg(any(
  feature = "cpu",
  all(target_arch = "aarch64", target_endian = "little"),
  not(all(
    any(target_arch = "x86", target_arch = "x86_64"),
    target_endian = "little",
    target_feature = "aes",
    target_feature = "sse2"
  ))
))]
mod pre_xor {
  use super::*;

  /// Hardware/software AES backends that fuse pre-round XOR natively (`AESE`, T-table).
  pub(crate) trait PreXorAes: Aes128 {
    /// Computes `MC(SR(SB(a ^ b)))`.
    unsafe fn pre_r1_zero(self, a: Self::V, b: Self::V) -> Self::V;
    /// Computes final AES round `SR(SB(a ^ b))` (omitting `MixColumns`).
    unsafe fn pre_fl_zero(self, a: Self::V, b: Self::V) -> Self::V;

    /// Computes `MC(SR(SB(state))) ^ rk`.
    #[inline(always)]
    unsafe fn pre_enc(self, state: Self::V, rk: Self::V) -> Self::V {
      // SAFETY: forwarded from caller contract.
      unsafe { self.xor(self.pre_r1_zero(state, self.zero()), rk) }
    }
  }

  /// Zero-sized [`AesMode`] strategy for Pre-XOR AES backends.
  pub(crate) struct PreXor;

  impl<L: PreXorAes> AesMode<L> for PreXor {
    #[inline(always)]
    unsafe fn r1_zero(ctx: L, a: L::V, b: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.pre_r1_zero(a, b) }
    }

    #[inline(always)]
    unsafe fn f_zero(ctx: L, s: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.pre_r1_zero(s, ctx.zero()) }
    }

    #[inline(always)]
    unsafe fn chain_x01<const N: usize>(ctx: L, x0: L::V, x1: L::V, rk_li: [L::V; N]) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let mut s = ctx.pre_r1_zero(x0, x1);
        for k in rk_li {
          s = ctx.pre_r1_zero(s, k);
        }
        s
      }
    }

    #[inline(always)]
    unsafe fn acc_init_1(ctx: L, key: L::V, data: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.pre_r1_zero(key, data) }
    }

    #[inline(always)]
    unsafe fn acc_absorb_1(ctx: L, acc: L::V, data: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.pre_r1_zero(acc, data) }
    }

    #[inline(always)]
    unsafe fn ctr_prep_1(ctx: L, ctr: L::V, rk0: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.pre_r1_zero(ctr, rk0) }
    }

    #[inline(always)]
    unsafe fn ctr_mid_1(ctx: L, s: L::V, rk: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.pre_r1_zero(s, rk) }
    }

    #[inline(always)]
    unsafe fn ctr_fin_1(ctx: L, s: L::V, rk3: L::V, rk0: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.xor(ctx.pre_fl_zero(s, rk3), rk0) }
    }

    #[inline(always)]
    unsafe fn compress_17_32_core(
      ctx: L,
      b0: L::V,
      k0: L::V,
      b1: L::V,
      k1: L::V,
      lk: L::V,
    ) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let a = ctx.pre_r1_zero(b0, k0);
        let c = ctx.pre_r1_zero(b1, k1);
        let a2 = ctx.pre_r1_zero(a, lk);
        (ctx.chain_x01_ff(a2, c, []), c)
      }
    }

    #[inline(always)]
    unsafe fn compress_33_64_core<const HI: bool>(
      ctx: L,
      b_li: [L::V; 4],
      key_li: [L::V; 4],
      lk: L::V,
    ) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let h_li = unroll4!(I => ctx.pre_r1_zero(b_li[I], key_li[I]));
        finalize_merge4_pre::<L, HI>(ctx, h_li, lk)
      }
    }

    #[inline(always)]
    unsafe fn merge4_128<const HI: bool>(ctx: L, acc_li: [L::V; 4], lk: L::V) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe { finalize_merge4_pre::<L, HI>(ctx, acc_li, lk) }
    }

    #[inline(always)]
    unsafe fn merge8_128<const HI: bool>(ctx: L, acc_li: [L::V; 8], lk: L::V) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let p_li = unroll4!(I => ctx.pre_enc(acc_li[I], acc_li[I + 4]));
        finalize_merge4_pre::<L, HI>(ctx, p_li, lk)
      }
    }

    #[inline(always)]
    unsafe fn merge16_128<const HI: bool>(
      ctx: L,
      lo_li: [L::V; 8],
      hi_li: [L::V; 8],
      lk: L::V,
    ) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let m_li = unroll8!(I => ctx.pre_enc(lo_li[I], hi_li[I]));
        let p_li = unroll4!(I => ctx.pre_enc(m_li[I], m_li[I + 4]));
        finalize_merge4_pre::<L, HI>(ctx, p_li, lk)
      }
    }
  }
}

#[cfg(any(
  feature = "cpu",
  all(target_arch = "aarch64", target_endian = "little"),
  not(all(
    any(target_arch = "x86", target_arch = "x86_64"),
    target_endian = "little",
    target_feature = "aes",
    target_feature = "sse2"
  ))
))]
pub(crate) use pre_xor::{PreXor, PreXorAes};

#[cfg(all(
  any(target_arch = "x86", target_arch = "x86_64"),
  target_endian = "little",
  any(feature = "std", all(target_feature = "aes", target_feature = "sse2"))
))]
mod post_xor {
  use super::*;

  /// Hardware AES backends that fuse post-round XOR natively (x86 `aesenc`/`aesenclast`).
  pub(crate) trait PostXorAes: Aes128 {
    /// Computes `MC(SR(SB(state))) ^ rk`.
    unsafe fn post_enc(self, state: Self::V, rk: Self::V) -> Self::V;
    /// Computes final AES round `SR(SB(state)) ^ rk` (omitting `MixColumns`).
    unsafe fn post_enclast(self, state: Self::V, rk: Self::V) -> Self::V;
  }

  /// Zero-sized [`AesMode`] strategy for Post-XOR AES backends.
  pub(crate) struct PostXor;

  impl<L: PostXorAes> AesMode<L> for PostXor {
    #[inline(always)]
    unsafe fn r1_zero(ctx: L, a: L::V, b: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.post_enc(ctx.xor(a, b), ctx.zero()) }
    }

    #[inline(always)]
    unsafe fn f_zero(ctx: L, s: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.post_enc(s, ctx.zero()) }
    }

    #[inline(always)]
    unsafe fn chain_x01<const N: usize>(ctx: L, x0: L::V, x1: L::V, rk_li: [L::V; N]) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let mut s = ctx.xor(x0, x1);
        for k in rk_li {
          s = ctx.post_enc(s, k);
        }
        ctx.post_enc(s, ctx.zero())
      }
    }

    #[inline(always)]
    unsafe fn acc_init_1(ctx: L, key: L::V, data: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.xor(key, data) }
    }

    #[inline(always)]
    unsafe fn acc_absorb_1(ctx: L, acc: L::V, data: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.post_enc(acc, data) }
    }

    #[inline(always)]
    unsafe fn ctr_prep_1(ctx: L, ctr: L::V, rk0: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.xor(ctr, rk0) }
    }

    #[inline(always)]
    unsafe fn ctr_mid_1(ctx: L, s: L::V, rk: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.post_enc(s, rk) }
    }

    #[inline(always)]
    unsafe fn ctr_fin_1(ctx: L, s: L::V, rk3: L::V, rk0: L::V) -> L::V {
      // SAFETY: forwarded from caller contract.
      unsafe { ctx.post_enclast(ctx.post_enc(s, rk3), rk0) }
    }

    #[inline(always)]
    unsafe fn compress_17_32_core(
      ctx: L,
      b0: L::V,
      k0: L::V,
      b1: L::V,
      k1: L::V,
      lk: L::V,
    ) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let z = ctx.zero();
        let a_lk = ctx.post_enc(ctx.xor(b0, k0), lk);
        let c = ctx.post_enc(ctx.xor(b1, k1), z);
        let s = ctx.post_enc(a_lk, c);
        let u = ctx.post_enc(s, z);
        let fin = ctx.post_enc(u, z);
        (ctx.add_u64(fin, u), c)
      }
    }

    #[inline(always)]
    unsafe fn compress_33_64_core<const HI: bool>(
      ctx: L,
      b_li: [L::V; 4],
      key_li: [L::V; 4],
      lk: L::V,
    ) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let z = ctx.zero();
        let l_li =
          unroll4!(I => ctx.post_enc(ctx.xor(b_li[I], key_li[I]), if I == 0 { lk } else { z }));
        finalize_merge4_lk::<L, HI>(ctx, l_li, lk)
      }
    }

    #[inline(always)]
    unsafe fn merge4_128<const HI: bool>(ctx: L, acc_li: [L::V; 4], lk: L::V) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let z = ctx.zero();
        let l_li = unroll4!(I => ctx.post_enc(acc_li[I], if I == 0 { lk } else { z }));
        finalize_merge4_lk::<L, HI>(ctx, l_li, lk)
      }
    }

    #[inline(always)]
    unsafe fn merge8_128<const HI: bool>(ctx: L, acc_li: [L::V; 8], lk: L::V) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let z = ctx.zero();
        let p_li = unroll4!(I => {
          let hi_end = ctx.post_enc(acc_li[I + 4], if I == 0 { lk } else { z });
          ctx.post_enc(ctx.post_enc(acc_li[I], z), hi_end)
        });
        finalize_merge4_lk::<L, HI>(ctx, p_li, lk)
      }
    }

    #[inline(always)]
    unsafe fn merge16_128<const HI: bool>(
      ctx: L,
      lo_li: [L::V; 8],
      hi_li: [L::V; 8],
      lk: L::V,
    ) -> (L::V, L::V) {
      // SAFETY: forwarded from caller contract.
      unsafe {
        let z = ctx.zero();
        let m_li = unroll8!(I => {
          let hi_end = ctx.post_enc(hi_li[I], if I == 4 { lk } else { z });
          ctx.post_enc(ctx.post_enc(lo_li[I], z), hi_end)
        });
        let p_li = unroll4!(I => ctx.post_enc(m_li[I], m_li[I + 4]));
        finalize_merge4_lk::<L, HI>(ctx, p_li, lk)
      }
    }
  }
}

#[cfg(all(
  any(target_arch = "x86", target_arch = "x86_64"),
  target_endian = "little",
  any(feature = "std", all(target_feature = "aes", target_feature = "sse2"))
))]
pub(crate) use post_xor::{PostXor, PostXorAes};