use alloc::{
string::String,
vec::Vec,
};
use core::{
convert::TryFrom,
fmt::{self, Display, Formatter},
hash::{Hash, Hasher},
};
#[cfg(test)]
use {
alloc::string::ToString,
core::mem,
};
use crate::Error;
pub type Z128 = [u8; Z128_LEN];
pub type Z224 = [u8; Z224_LEN];
pub type Z256 = [u8; Z256_LEN];
pub type Z384 = [u8; Z384_LEN];
pub type Z512 = [u8; Z512_LEN];
const Z128_LEN: usize = 128 / 8;
const Z224_LEN: usize = 224 / 8;
const Z256_LEN: usize = 256 / 8;
const Z384_LEN: usize = 384 / 8;
const Z512_LEN: usize = 512 / 8;
#[test]
fn test_type_lengths() {
assert_eq!(Z128_LEN, mem::size_of::<Z128>());
assert_eq!(Z224_LEN, mem::size_of::<Z224>());
assert_eq!(Z256_LEN, mem::size_of::<Z256>());
assert_eq!(Z384_LEN, mem::size_of::<Z384>());
assert_eq!(Z512_LEN, mem::size_of::<Z512>());
}
pub const HEX_STRS: &[&str] = &[
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d",
"2e", "2f", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f", "40", "41", "42", "43", "44",
"45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b",
"5c", "5d", "5e", "5f", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f", "70", "71", "72",
"73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"8a", "8b", "8c", "8d", "8e", "8f", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f", "a0",
"a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af", "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7",
"b8", "b9", "ba", "bb", "bc", "bd", "be", "bf", "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce",
"cf", "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df", "e0", "e1", "e2", "e3", "e4", "e5",
"e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef", "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc",
"fd", "fe", "ff",
];
#[test]
fn test_hex_strs() {
assert!(HEX_STRS.len() > usize::from(u8::max_value()));
for i in 0..HEX_STRS.len() {
assert_eq!(format!("{:02x}", i), HEX_STRS[i]);
}
}
pub fn bytes_to_hex<B>(bytes: B) -> String where B: AsRef<[u8]> {
let bytes = bytes.as_ref();
let mut result = String::with_capacity(bytes.len().saturating_mul(2));
for b in bytes {
result.push_str(HEX_STRS[usize::from(*b)]);
}
result
}
const DEC_STRS: &[&str] = &[
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70",
"71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93",
"94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114",
"115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134",
"135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154",
"155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174",
"175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194",
"195", "196", "197", "198", "199", "200", "201", "202", "203", "204", "205", "206", "207", "208", "209", "210", "211", "212", "213", "214",
"215", "216", "217", "218", "219", "220", "221", "222", "223", "224", "225", "226", "227", "228", "229", "230", "231", "232", "233", "234",
"235", "236", "237", "238", "239", "240", "241", "242", "243", "244", "245", "246", "247", "248", "249", "250", "251", "252", "253", "254",
"255",
];
#[test]
fn test_dec_strs() {
assert!(DEC_STRS.len() > usize::from(u8::max_value()));
for i in 0..DEC_STRS.len() {
assert_eq!(i.to_string(), DEC_STRS[i]);
}
}
#[derive(Clone)]
pub enum Zeros {
Z128(Z128),
Z224(Z224),
Z256(Z256),
Z384(Z384),
Z512(Z512),
}
impl Zeros {
pub const fn new_128() -> Self {
Zeros::Z128([0; Z128_LEN])
}
pub const fn new_224() -> Self {
Zeros::Z224([0; Z224_LEN])
}
pub const fn new_256() -> Self {
Zeros::Z256([0; Z256_LEN])
}
pub const fn new_384() -> Self {
Zeros::Z384([0; Z384_LEN])
}
pub const fn new_512() -> Self {
Zeros::Z512([0; Z512_LEN])
}
pub fn len(&self) -> usize {
match self {
Zeros::Z128(bytes) => bytes.len(),
Zeros::Z224(bytes) => bytes.len(),
Zeros::Z256(bytes) => bytes.len(),
Zeros::Z384(bytes) => bytes.len(),
Zeros::Z512(bytes) => bytes.len(),
}
}
pub fn same_class(&self, other: &Self) -> bool {
match (self, other) {
(Zeros::Z128(_), Zeros::Z128(_)) => true,
(Zeros::Z224(_), Zeros::Z224(_)) => true,
(Zeros::Z256(_), Zeros::Z256(_)) => true,
(Zeros::Z384(_), Zeros::Z384(_)) => true,
(Zeros::Z512(_), Zeros::Z512(_)) => true,
_ => false,
}
}
pub fn to_vec(&self) -> Vec<u8> {
match self {
Zeros::Z128(bytes) => bytes.to_vec(),
Zeros::Z224(bytes) => bytes.to_vec(),
Zeros::Z256(bytes) => bytes.to_vec(),
Zeros::Z384(bytes) => bytes.to_vec(),
Zeros::Z512(bytes) => bytes.to_vec(),
}
}
}
fn cmp_slices(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
for i in 0..a.len() {
if a[i] != b[i] {
return false;
}
}
true
}
impl Eq for Zeros {}
impl PartialEq for Zeros {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Zeros::Z128(a), Zeros::Z128(b)) => cmp_slices(a, b),
(Zeros::Z224(a), Zeros::Z224(b)) => cmp_slices(a, b),
(Zeros::Z256(a), Zeros::Z256(b)) => cmp_slices(a, b),
(Zeros::Z384(a), Zeros::Z384(b)) => cmp_slices(a, b),
(Zeros::Z512(a), Zeros::Z512(b)) => cmp_slices(a, b),
_ => false,
}
}
}
impl PartialEq<[u8]> for Zeros {
fn eq(&self, other: &[u8]) -> bool {
match self {
Zeros::Z128(bytes) => bytes.eq(other),
Zeros::Z224(bytes) => bytes.eq(other),
Zeros::Z256(bytes) => bytes.eq(other),
Zeros::Z384(bytes) => bytes.eq(other),
Zeros::Z512(bytes) => bytes.eq(other),
}
}
}
impl PartialEq<Zeros> for [u8] {
fn eq(&self, other: &Zeros) -> bool {
match other {
Zeros::Z128(bytes) => bytes.eq(self),
Zeros::Z224(bytes) => bytes.eq(self),
Zeros::Z256(bytes) => bytes.eq(self),
Zeros::Z384(bytes) => bytes.eq(self),
Zeros::Z512(bytes) => bytes.eq(self),
}
}
}
impl Hash for Zeros {
fn hash<H>(&self, h: &mut H) where H: Hasher {
match self {
Zeros::Z128(bytes) => bytes.hash(h),
Zeros::Z224(bytes) => bytes.hash(h),
Zeros::Z256(bytes) => bytes.hash(h),
Zeros::Z384(bytes) => bytes.hash(h),
Zeros::Z512(bytes) => bytes.hash(h),
};
}
}
macro_rules! impl_from_slices_for_zeros { ($($ty: ty, $code: tt,)+) => {
$(
impl From<$ty> for Zeros {
fn from(slice: $ty) -> Self {
$code(slice)
}
}
)+
}}
impl_from_slices_for_zeros! {
Z128, (Zeros::Z128),
Z224, (Zeros::Z224),
Z256, (Zeros::Z256),
Z384, (Zeros::Z384),
Z512, (Zeros::Z512),
}
impl AsRef<[u8]> for Zeros {
fn as_ref(&self) -> &[u8] {
match self {
Zeros::Z128(bytes) => bytes,
Zeros::Z224(bytes) => bytes,
Zeros::Z256(bytes) => bytes,
Zeros::Z384(bytes) => bytes,
Zeros::Z512(bytes) => bytes,
}
}
}
impl AsMut<[u8]> for Zeros {
fn as_mut(&mut self) -> &mut [u8] {
match self {
Zeros::Z128(bytes) => bytes,
Zeros::Z224(bytes) => bytes,
Zeros::Z256(bytes) => bytes,
Zeros::Z384(bytes) => bytes,
Zeros::Z512(bytes) => bytes,
}
}
}
impl Display for Zeros {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
f.write_str(&match self {
Zeros::Z128(bytes) => bytes_to_hex(&bytes[..]),
Zeros::Z224(bytes) => bytes_to_hex(&bytes[..]),
Zeros::Z256(bytes) => bytes_to_hex(&bytes[..]),
Zeros::Z384(bytes) => bytes_to_hex(&bytes[..]),
Zeros::Z512(bytes) => bytes_to_hex(&bytes[..]),
})
}
}
impl fmt::Debug for Zeros {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
let bytes = match self {
Zeros::Z128(bytes) => {
f.write_str("Zeros::Z128([")?;
&bytes[..]
}
Zeros::Z224(bytes) => {
f.write_str("Zeros::Z224([")?;
&bytes[..]
}
Zeros::Z256(bytes) => {
f.write_str("Zeros::Z256([")?;
&bytes[..]
}
Zeros::Z384(bytes) => {
f.write_str("Zeros::Z384([")?;
&bytes[..]
}
Zeros::Z512(bytes) => {
f.write_str("Zeros::Z512([")?;
&bytes[..]
}
};
let alternate = f.alternate();
for (i, b) in bytes.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
match alternate {
true => write!(f, "0x{}", HEX_STRS[usize::from(*b)])?,
false => f.write_str(DEC_STRS[usize::from(*b)])?,
};
}
f.write_str("])")
}
}
macro_rules! impl_try_from_zeros_for_slices { ($($code: tt, $ty: ty,)+) => {
$(
impl TryFrom<Zeros> for $ty {
type Error = Error;
fn try_from(zeros: Zeros) -> Result<Self, Self::Error> {
match zeros {
Zeros::$code(slice) => Ok(slice),
_ => Err(e!("Not an instance of {:?}", stringify!($code))),
}
}
}
)+
}}
impl_try_from_zeros_for_slices! {
Z128, Z128,
Z224, Z224,
Z256, Z256,
Z384, Z384,
Z512, Z512,
}
macro_rules! copy_slice_to_zeros { ($vec: ident, $size: ident) => {{
let mut array = [0_u8; $size];
for (i, b) in $vec.into_iter().enumerate() {
array[i] = *b;
}
Ok(Zeros::from(array))
}}}
impl TryFrom<&[u8]> for Zeros {
type Error = Error;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
match bytes.len() {
Z128_LEN => copy_slice_to_zeros!(bytes, Z128_LEN),
Z224_LEN => copy_slice_to_zeros!(bytes, Z224_LEN),
Z256_LEN => copy_slice_to_zeros!(bytes, Z256_LEN),
Z384_LEN => copy_slice_to_zeros!(bytes, Z384_LEN),
Z512_LEN => copy_slice_to_zeros!(bytes, Z512_LEN),
other => Err(e!("Slice size is not supported: {}", other)),
}
}
}
impl TryFrom<Vec<u8>> for Zeros {
type Error = Error;
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
Self::try_from(bytes.as_slice())
}
}