-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathcompression.rs
More file actions
74 lines (65 loc) · 2.4 KB
/
Copy pathcompression.rs
File metadata and controls
74 lines (65 loc) · 2.4 KB
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
use crate::hasher::CryptographicHasher;
use crate::permutation::CryptographicPermutation;
/// An `N`-to-1 compression function collision-resistant in a hash tree setting.
///
/// Unlike `CompressionFunction`, it may not be collision-resistant in general.
/// Instead it is only collision-resistant in hash-tree like settings where
/// the preimage of a non-leaf node must consist of compression outputs.
pub trait PseudoCompressionFunction<T, const N: usize>: Clone {
fn compress(&self, input: [T; N]) -> T;
}
/// An `N`-to-1 compression function.
pub trait CompressionFunction<T, const N: usize>: PseudoCompressionFunction<T, N> {}
#[derive(Clone, Debug)]
pub struct TruncatedPermutation<InnerP, const N: usize, const CHUNK: usize, const WIDTH: usize> {
inner_permutation: InnerP,
}
impl<InnerP, const N: usize, const CHUNK: usize, const WIDTH: usize>
TruncatedPermutation<InnerP, N, CHUNK, WIDTH>
{
pub const fn new(inner_permutation: InnerP) -> Self {
Self { inner_permutation }
}
}
impl<T, InnerP, const N: usize, const CHUNK: usize, const WIDTH: usize>
PseudoCompressionFunction<[T; CHUNK], N> for TruncatedPermutation<InnerP, N, CHUNK, WIDTH>
where
T: Copy + Default,
InnerP: CryptographicPermutation<[T; WIDTH]>,
{
fn compress(&self, input: [[T; CHUNK]; N]) -> [T; CHUNK] {
debug_assert!(CHUNK * N <= WIDTH);
let mut pre = [T::default(); WIDTH];
for i in 0..N {
pre[i * CHUNK..(i + 1) * CHUNK].copy_from_slice(&input[i]);
}
let post = self.inner_permutation.permute(pre);
post[..CHUNK].try_into().unwrap()
}
}
#[derive(Clone, Debug)]
pub struct CompressionFunctionFromHasher<H, const N: usize, const CHUNK: usize> {
hasher: H,
}
impl<H, const N: usize, const CHUNK: usize> CompressionFunctionFromHasher<H, N, CHUNK> {
pub const fn new(hasher: H) -> Self {
Self { hasher }
}
}
impl<T, H, const N: usize, const CHUNK: usize> PseudoCompressionFunction<[T; CHUNK], N>
for CompressionFunctionFromHasher<H, N, CHUNK>
where
T: Clone,
H: CryptographicHasher<T, [T; CHUNK]>,
{
fn compress(&self, input: [[T; CHUNK]; N]) -> [T; CHUNK] {
self.hasher.hash_iter(input.into_iter().flatten())
}
}
impl<T, H, const N: usize, const CHUNK: usize> CompressionFunction<[T; CHUNK], N>
for CompressionFunctionFromHasher<H, N, CHUNK>
where
T: Clone,
H: CryptographicHasher<T, [T; CHUNK]>,
{
}