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
//! Defines a 512-bit value.
use crate::Block;
use std::hash::Hash;

/// A 512-bit value.
#[derive(
    Clone,
    Copy,
    Default,
    Debug,
    PartialOrd,
    PartialEq,
    Ord,
    Eq,
    Hash,
    bytemuck::Pod,
    bytemuck::Zeroable,
    bytemuck::TransparentWrapper,
)]
#[repr(transparent)]
pub struct Block512(pub(crate) [Block; 4]);

impl Block512 {
    /// Return the first `n` bytes, where `n` must be `<= 64`.
    #[inline]
    pub fn prefix(&self, n: usize) -> &[u8] {
        &self.as_ref()[0..n]
    }

    /// Return the first `n` bytes as mutable, where `n` must be `<= 64`.
    #[inline]
    pub fn prefix_mut(&mut self, n: usize) -> &mut [u8] {
        &mut self.as_mut()[0..n]
    }
}

impl AsMut<[u8]> for Block512 {
    fn as_mut(&mut self) -> &mut [u8] {
        bytemuck::bytes_of_mut(self)
    }
}

impl AsRef<[u8]> for Block512 {
    fn as_ref(&self) -> &[u8] {
        bytemuck::bytes_of(self)
    }
}

impl std::ops::BitXor for Block512 {
    type Output = Self;

    #[inline]
    fn bitxor(self, rhs: Self) -> Self {
        let b0 = self.0[0] ^ rhs.0[0];
        let b1 = self.0[1] ^ rhs.0[1];
        let b2 = self.0[2] ^ rhs.0[2];
        let b3 = self.0[3] ^ rhs.0[3];
        Self([b0, b1, b2, b3])
    }
}

impl std::ops::BitXorAssign for Block512 {
    #[inline]
    fn bitxor_assign(&mut self, rhs: Self) {
        for (a, b) in self.0.iter_mut().zip(rhs.0.iter()) {
            *a ^= *b;
        }
    }
}

impl std::fmt::Display for Block512 {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{:#?}", self.0)
    }
}

impl rand::distributions::Distribution<Block512> for rand::distributions::Standard {
    #[inline]
    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Block512 {
        let b1 = rng.gen::<Block>();
        let b2 = rng.gen::<Block>();
        let b3 = rng.gen::<Block>();
        let b4 = rng.gen::<Block>();
        Block512([b1, b2, b3, b4])
    }
}

impl From<Block512> for [u32; 16] {
    #[inline]
    fn from(m: Block512) -> [u32; 16] {
        bytemuck::cast(m)
    }
}

impl From<Block512> for [Block; 4] {
    #[inline]
    fn from(m: Block512) -> [Block; 4] {
        m.0
    }
}

impl<'a> From<&'a Block512> for &'a [Block; 4] {
    #[inline]
    fn from(m: &Block512) -> &[Block; 4] {
        &m.0
    }
}

impl<'a> From<&'a mut Block512> for &'a mut [Block; 4] {
    #[inline]
    fn from(m: &mut Block512) -> &mut [Block; 4] {
        &mut m.0
    }
}

impl<'a> From<&'a mut Block512> for &'a mut [u8; 64] {
    #[inline]
    fn from(m: &'a mut Block512) -> Self {
        bytemuck::cast_mut(m)
    }
}

impl From<[Block; 4]> for Block512 {
    #[inline]
    fn from(m: [Block; 4]) -> Block512 {
        Block512(m)
    }
}

impl From<[u8; 64]> for Block512 {
    #[inline]
    fn from(m: [u8; 64]) -> Block512 {
        bytemuck::cast(m)
    }
}

impl TryFrom<&[u8]> for Block512 {
    type Error = core::array::TryFromSliceError;
    #[inline]
    fn try_from(u: &[u8]) -> Result<Self, Self::Error> {
        let bytes = <[u8; 512 / 8]>::try_from(u)?;
        Ok(bytemuck::cast(bytes))
    }
}

#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
struct Helper {
    pub blocks: [Block; 4],
}

#[cfg(feature = "serde")]
impl Serialize for Block512 {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let helper = Helper { blocks: self.0 };
        helper.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Block512 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let helper = Helper::deserialize(deserializer)?;
        Ok(Block512::from(helper.blocks))
    }
}