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
use curve25519_dalek::scalar::Scalar;
use keccak_hash::keccak_256;
use std::io;
use crate::consensus::encode::{self, Decodable, Encodable};
use crate::util::key::PrivateKey;
#[cfg(feature = "serde_support")]
use serde::{Deserialize, Serialize};
fixed_hash::construct_fixed_hash!(
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Hash(32);
);
impl Hash {
pub fn null_hash() -> Hash {
Hash([0u8; 32])
}
pub fn hash(input: &[u8]) -> Hash {
let mut out = [0u8; 32];
keccak_256(input, &mut out);
Hash(out)
}
pub fn to_bytes(&self) -> [u8; 32] {
self.0
}
pub fn as_scalar(&self) -> PrivateKey {
PrivateKey::from_scalar(Scalar::from_bytes_mod_order(self.0))
}
pub fn hash_to_scalar(input: &[u8]) -> PrivateKey {
Self::hash(input).as_scalar()
}
}
impl Decodable for Hash {
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Hash, encode::Error> {
Ok(Hash(Decodable::consensus_decode(d)?))
}
}
impl Encodable for Hash {
fn consensus_encode<S: io::Write>(&self, s: &mut S) -> Result<usize, io::Error> {
self.0.consensus_encode(s)
}
}
pub trait Hashable {
fn hash(&self) -> Hash;
fn hash_to_scalar(&self) -> PrivateKey {
self.hash().as_scalar()
}
}
fixed_hash::construct_fixed_hash!(
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Hash8(8);
);
impl Decodable for Hash8 {
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Hash8, encode::Error> {
Ok(Hash8(Decodable::consensus_decode(d)?))
}
}
impl Encodable for Hash8 {
fn consensus_encode<S: io::Write>(&self, s: &mut S) -> Result<usize, io::Error> {
self.0.consensus_encode(s)
}
}