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
use alloc::string::String;
use core::{
fmt::{Debug, Display, Formatter},
ops::Deref,
};
use anyhow::Result;
use crate::fp::Fp;
use crate::fp4::Fp4;
pub const DIGEST_WORDS: usize = 8;
#[repr(transparent)]
#[derive(Eq, PartialEq, Copy)]
pub struct Digest([u32; DIGEST_WORDS]);
impl Digest {
pub fn new(data: [u32; DIGEST_WORDS]) -> Digest {
Digest(data)
}
pub fn try_from_slice(words: &[u32]) -> Result<Self> {
Ok(Digest(words.try_into()?))
}
pub fn from_slice(words: &[u32]) -> Self {
Self::try_from_slice(words).unwrap()
}
pub fn as_slice(&self) -> &[u32] {
&self.0
}
pub fn as_mut_slice(&mut self) -> &mut [u32] {
&mut self.0
}
pub fn get(&self) -> &[u32; DIGEST_WORDS] {
&self.0
}
pub fn get_mut(&mut self) -> &mut [u32; DIGEST_WORDS] {
&mut self.0
}
pub fn to_hex(&self) -> String {
fn hex(digit: u8) -> char {
char::from_digit(digit as u32, 16).unwrap()
}
self.0
.iter()
.flat_map(|word| word.to_be_bytes())
.flat_map(|byte| [hex(byte >> 4), hex(byte & 0xF)])
.collect()
}
}
impl Default for Digest {
fn default() -> Digest {
Digest([0; DIGEST_WORDS])
}
}
impl Display for Digest {
fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
for word in self.0 {
core::write!(f, "{:08x?}", word)?;
}
Ok(())
}
}
impl Debug for Digest {
fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
for word in self.0 {
core::write!(f, "{:08x?}", word)?;
}
Ok(())
}
}
impl Clone for Digest {
fn clone(&self) -> Digest {
Digest(self.0)
}
}
pub trait Sha: Clone + Debug {
type DigestPtr: Deref<Target = Digest> + Debug;
fn hash_bytes(&self, bytes: &[u8]) -> Self::DigestPtr;
fn hash_words(&self, words: &[u32]) -> Self::DigestPtr {
self.hash_bytes(bytemuck::cast_slice(words) as &[u8])
}
fn hash_pair(&self, a: &Digest, b: &Digest) -> Self::DigestPtr;
fn hash_fps(&self, fps: &[Fp]) -> Self::DigestPtr;
fn hash_fp4s(&self, fp4s: &[Fp4]) -> Self::DigestPtr;
fn mix(&self, pool: &mut Self::DigestPtr, val: &Digest);
}
pub use crate::sha_cpu::Impl as DefaultImplementation;
pub fn default_implementation() -> &'static DefaultImplementation {
static DEFAULT_IMPLEMENTATION: DefaultImplementation = DefaultImplementation {};
&DEFAULT_IMPLEMENTATION
}
#[cfg(test)]
pub mod tests {
use super::{Digest, Sha};
pub fn test_sha_impl<S: Sha>(sha: &S) {
assert_eq!(
*sha.hash_bytes("abc".as_bytes()),
Digest::new([
0xba7816bf, 0x8f01cfea, 0x414140de, 0x5dae2223, 0xb00361a3, 0x96177a9c, 0xb410ff61,
0xf20015ad
])
);
assert_eq!(
*sha.hash_bytes("".as_bytes()),
Digest::new([
0xe3b0c442, 0x98fc1c14, 0x9afbf4c8, 0x996fb924, 0x27ae41e4, 0x649b934c, 0xa495991b,
0x7852b855
])
);
assert_eq!(
*sha.hash_bytes("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".as_bytes()),
Digest::new([
0x248d6a61, 0xd20638b8, 0xe5c02693, 0x0c3e6039, 0xa33ce459, 0x64ff2167, 0xf6ecedd4,
0x19db06c1
])
);
assert_eq!(*sha.hash_bytes(
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" .as_bytes()),
Digest::new([0xcf5b16a7,
0x78af8380,
0x036ce59e,
0x7b049237,
0x0b249b11,
0xe8f07a51,
0xafac4503,
0x7afee9d1]));
assert_eq!(
sha.hash_bytes(&"Byzantium".as_bytes()).to_hex(),
"f75c763b4a52709ac294fc7bd7cf14dd45718c3d50b36f4732b05b8c6017492a"
);
crate::sha_rng::tests::test_sha_rng_impl(sha);
}
}