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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
pub type DefaultHasher = DefaultHasherNoDoc;
pub type DefaultSigner = DefaultSignerNoDoc;
pub type DefaultSecretKey = DefaultSecretKeyNoDoc;
include!("algorithms/macro_impl.rs");
macro_rules! algorithms {
($($tt:tt)+) => {
algorithms_impl!($($tt)+);
};
}
algorithms! {
HashAlgorithm {
Sha2 = (1, "SHA2_256"),
Sha3 = (3, "SHA3_256"),
}
SignatureAlgorithm {
P256 = (2, "ECDSA_P256"),
Secp256k1 = (3, "ECDSA_secp256k1"),
}
}
pub trait Signature {
type Serialized: AsRef<[u8]> + Clone;
fn serialize(&self) -> Self::Serialized;
}
pub trait FlowHasher {
type Algorithm: HashAlgorithm;
fn new() -> Self;
fn update<B: AsRef<[u8]> + ?Sized>(&mut self, bytes: &B);
fn finalize(self) -> [u8; 32];
}
pub trait FlowSigner {
type Algorithm: SignatureAlgorithm;
type SecretKey: Clone;
type PublicKey: Copy;
type Signature: Signature;
fn new() -> Self;
fn sign(&self, hasher: impl FlowHasher, secret_key: &Self::SecretKey) -> Self::Signature {
self.sign_populated(hasher.finalize(), secret_key)
}
fn sign_populated(&self, hashed: [u8; 32], secret_key: &Self::SecretKey) -> Self::Signature;
fn to_public_key(&self, secret_key: &Self::SecretKey) -> Self::PublicKey;
fn serialize_public_key(&self, public_key: &Self::PublicKey) -> [u8; 64];
}
pub trait SecretKey {
type Signer: FlowSigner<SecretKey = Self>;
}
#[cfg(feature = "secp256k1-sign")]
pub mod secp256k1 {
pub use secp256k1::*;
}
#[cfg(feature = "sha3-hash")]
pub mod sha3 {
pub use tiny_keccak::*;
}
#[cfg(feature = "secp256k1-rand")]
pub mod rand {
pub use rand::*;
}
#[cfg(feature = "sha3-hash")]
impl FlowHasher for tiny_keccak::Sha3 {
type Algorithm = Sha3;
fn new() -> Self {
tiny_keccak::Sha3::v256()
}
fn update<B: AsRef<[u8]> + ?Sized>(&mut self, bytes: &B) {
use tiny_keccak::Hasher;
Hasher::update(self, bytes.as_ref())
}
fn finalize(self) -> [u8; 32] {
use tiny_keccak::Hasher;
let mut output = [0; 32];
Hasher::finalize(self, &mut output);
output
}
}
#[cfg(feature = "secp256k1-sign")]
impl Signature for secp256k1::Signature {
type Serialized = [u8; 64];
fn serialize(&self) -> Self::Serialized {
self.serialize_compact()
}
}
#[cfg(feature = "secp256k1-sign")]
impl FlowSigner for secp256k1::Secp256k1<secp256k1::SignOnly> {
type Algorithm = Secp256k1;
type PublicKey = secp256k1::PublicKey;
type SecretKey = secp256k1::SecretKey;
type Signature = secp256k1::Signature;
fn new() -> Self {
Self::signing_only()
}
fn sign_populated(&self, hashed: [u8; 32], secret_key: &Self::SecretKey) -> Self::Signature {
struct Ttbh([u8; 32]);
impl secp256k1::ThirtyTwoByteHash for Ttbh {
fn into_32(self) -> [u8; 32] {
self.0
}
}
self.sign(&secp256k1::Message::from(Ttbh(hashed)), secret_key)
}
fn to_public_key(&self, secret_key: &Self::SecretKey) -> Self::PublicKey {
secp256k1::PublicKey::from_secret_key(self, secret_key)
}
fn serialize_public_key(&self, public_key: &Self::PublicKey) -> [u8; 64] {
let [_, rest @ ..] = public_key.serialize_uncompressed();
rest
}
}
#[cfg(feature = "secp256k1-sign")]
impl SecretKey for secp256k1::SecretKey {
type Signer = secp256k1::Secp256k1<secp256k1::SignOnly>;
}
#[cfg(feature = "sha3-hash")]
type DefaultHasherNoDoc = tiny_keccak::Sha3;
#[cfg(feature = "secp256k1-sign")]
type DefaultSignerNoDoc = secp256k1::Secp256k1<secp256k1::SignOnly>;
#[cfg(feature = "secp256k1-sign")]
type DefaultSecretKeyNoDoc = secp256k1::SecretKey;
#[cfg(not(any(feature = "sha3-hash")))]
type DefaultHasherNoDoc = NoDefaultHasherAvailable;
#[cfg(not(any(feature = "sha3-hash")))]
type DefaultSignerNoDoc = NoDefaultSignerAvailable;
#[cfg(not(any(feature = "sha3-hash")))]
#[doc(hidden)]
pub struct NoDefaultHasherAvailable;
#[cfg(not(any(feature = "secp256k1-sign")))]
#[doc(hidden)]
pub struct NoDefaultSignerAvailable;
#[cfg(not(any(feature = "secp256k1-sign")))]
#[doc(hidden)]
pub struct NoDefaultSecretKeyAvailable;