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
mod curlp;
mod kerl;
mod kind;
use super::HASH_LENGTH;
pub use curlp::{CurlP, CurlP27, CurlP81, CurlPRounds};
pub use kerl::Kerl;
pub use kind::SpongeKind;
use bee_ternary::{TritBuf, Trits};
use std::ops::DerefMut;
pub trait Sponge {
type Error;
fn reset(&mut self);
fn absorb(&mut self, input: &Trits) -> Result<(), Self::Error>;
fn squeeze_into(&mut self, buf: &mut Trits) -> Result<(), Self::Error>;
fn squeeze(&mut self) -> Result<TritBuf, Self::Error> {
let mut output = TritBuf::zeros(HASH_LENGTH);
self.squeeze_into(&mut output)?;
Ok(output)
}
fn digest_into(&mut self, input: &Trits, buf: &mut Trits) -> Result<(), Self::Error> {
self.absorb(input)?;
self.squeeze_into(buf)?;
self.reset();
Ok(())
}
fn digest(&mut self, input: &Trits) -> Result<TritBuf, Self::Error> {
self.absorb(input)?;
let output = self.squeeze()?;
self.reset();
Ok(output)
}
}
impl<T: Sponge, U: DerefMut<Target = T>> Sponge for U {
type Error = T::Error;
fn reset(&mut self) {
T::reset(self)
}
fn absorb(&mut self, input: &Trits) -> Result<(), Self::Error> {
T::absorb(self, input)
}
fn squeeze_into(&mut self, buf: &mut Trits) -> Result<(), Self::Error> {
T::squeeze_into(self, buf)
}
}