use crate::{endian::*, polyfill};
use core::ops::{BitXor, BitXorAssign};
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct Block([BigEndian<u64>; 2]);
pub const BLOCK_LEN: usize = 16;
impl Block {
#[inline]
pub fn zero() -> Self {
Self([Encoding::ZERO; 2])
}
#[inline]
pub fn overwrite_part_at(&mut self, index: usize, a: &[u8]) {
let mut tmp: [u8; BLOCK_LEN] = *self.as_ref();
tmp[index..][..a.len()].copy_from_slice(a);
*self = Self::from(&tmp)
}
#[inline]
pub fn zero_from(&mut self, index: usize) {
let mut tmp: [u8; BLOCK_LEN] = *self.as_ref();
polyfill::slice::fill(&mut tmp[index..], 0);
*self = Self::from(&tmp)
}
}
impl From<[u64; 2]> for Block {
#[inline]
fn from(other: [u64; 2]) -> Self {
Self([other[0].into(), other[1].into()])
}
}
impl Into<[u64; 2]> for Block {
#[inline]
fn into(self) -> [u64; 2] {
[self.0[0].into(), self.0[1].into()]
}
}
impl BitXorAssign for Block {
#[inline]
fn bitxor_assign(&mut self, a: Self) {
for (r, a) in self.0.iter_mut().zip(a.0.iter()) {
*r ^= *a;
}
}
}
impl BitXor for Block {
type Output = Self;
#[inline]
fn bitxor(self, a: Self) -> Self {
let mut r = self;
r.bitxor_assign(a);
r
}
}
impl From<&'_ [u8; BLOCK_LEN]> for Block {
#[inline]
fn from(bytes: &[u8; BLOCK_LEN]) -> Self {
Self(FromByteArray::from_byte_array(bytes))
}
}
impl AsRef<[u8; BLOCK_LEN]> for Block {
#[inline]
fn as_ref(&self) -> &[u8; BLOCK_LEN] {
self.0.as_byte_array()
}
}