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
#![deny(unused_extern_crates)]
#[macro_use]
extern crate crunchy;
#[macro_use]
extern crate failure;
use std::fmt;
pub use self::curl::*;
pub use self::kerl::*;
mod curl;
mod iss;
mod keccak;
mod kerl;
type Result<T> = ::std::result::Result<T, failure::Error>;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum HashMode {
CURLP27,
CURLP81,
Kerl,
}
impl fmt::Display for HashMode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub trait Sponge
where
Self: Default + Clone + Send + 'static,
{
fn absorb(&mut self, trits: &[i8]) -> Result<()>;
fn squeeze(&mut self, out: &mut [i8]) -> Result<()>;
fn reset(&mut self);
}
pub fn hash_with_mode(mode: HashMode, trits: &[i8], out: &mut [i8]) -> Result<()> {
ensure!(
out.len() % 243 == 0,
"Output slice length isn't a multiple of 243: {}",
out.len()
);
match mode {
HashMode::CURLP27 | HashMode::CURLP81 => {
let mut curl = Curl::new(mode).unwrap();
curl.absorb(trits)?;
curl.squeeze(out)?;
}
HashMode::Kerl => {
let mut kerl = Kerl::default();
kerl.absorb(trits)?;
kerl.squeeze(out)?;
}
}
Ok(())
}