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
use tiny_keccak::{Hasher, Sha3};
pub(crate) type Hash = [u8; 32];
pub(crate) type DbcContentHash = [u8; 32];
mod dbc;
mod dbc_content;
mod dbc_transaction;
mod error;
mod key_manager;
mod mint;
pub use crate::{
dbc::Dbc,
dbc_content::DbcContent,
dbc_transaction::DbcTransaction,
error::{Error, Result},
key_manager::{ChainNode, KeyCache, KeyManager, PublicKey, Signature},
mint::{Mint, MintRequest},
};
fn sha3_256(input: &[u8]) -> Hash {
let mut sha3 = Sha3::v256();
let mut output = [0; 32];
sha3.update(input);
sha3.finalize(&mut output);
output
}
#[cfg(test)]
mod tests {
use super::*;
use quickcheck::{Arbitrary, Gen};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct TinyInt(u8);
impl TinyInt {
pub fn coerce<T: From<u8>>(self) -> T {
self.0.into()
}
}
impl std::fmt::Debug for TinyInt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Arbitrary for TinyInt {
fn arbitrary(g: &mut Gen) -> Self {
Self(u8::arbitrary(g) % 5)
}
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
Box::new((0..(self.0)).into_iter().rev().map(Self))
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct TinyVec<T>(Vec<T>);
impl<T: Clone> TinyVec<T> {
pub fn vec(&self) -> Vec<T> {
self.0.clone()
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for TinyVec<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl<T: Arbitrary> Arbitrary for TinyVec<T> {
fn arbitrary(g: &mut Gen) -> Self {
let n = u8::arbitrary(g) % 7;
let mut vec = Vec::new();
for _ in 0..n {
vec.push(T::arbitrary(g));
}
Self(vec)
}
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
Box::new(self.0.shrink().map(|vec| Self(vec)))
}
}
#[test]
fn hash() {
let data = b"hello world";
let expected = b"\
\x64\x4b\xcc\x7e\x56\x43\x73\x04\x09\x99\xaa\xc8\x9e\x76\x22\xf3\
\xca\x71\xfb\xa1\xd9\x72\xfd\x94\xa3\x1c\x3b\xfb\xf2\x4e\x39\x38\
";
assert_eq!(sha3_256(data), *expected);
}
}