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
use super::{Proof, VRFPrivateKey, VRFPublicKey};
use crate::serialization::from_digest;
use crate::{errors::VrfError, node_label::NodeLabel, storage::types::AkdLabel};
use async_trait::async_trait;
use std::convert::TryInto;
use winter_crypto::Hasher;
#[async_trait]
pub trait VRFKeyStorage: Clone + Sync + Send {
async fn retrieve(&self) -> Result<Vec<u8>, VrfError>;
async fn get_vrf_private_key(&self) -> Result<VRFPrivateKey, VrfError> {
match self.retrieve().await {
Ok(bytes) => {
let pk_ref: &[u8] = &bytes;
pk_ref.try_into()
}
Err(other) => Err(other),
}
}
async fn get_vrf_public_key(&self) -> Result<VRFPublicKey, VrfError> {
self.get_vrf_private_key().await.map(|key| (&key).into())
}
async fn get_node_label<H: Hasher>(
&self,
uname: &AkdLabel,
stale: bool,
version: u64,
) -> Result<NodeLabel, VrfError> {
let proof = self.get_label_proof::<H>(uname, stale, version).await?;
let output: super::ecvrf_impl::Output = (&proof).into();
Ok(NodeLabel::new(output.to_truncated_bytes(), 256u32))
}
async fn get_node_label_from_vrf_pf<H: Hasher>(
&self,
proof: Proof,
) -> Result<NodeLabel, VrfError> {
let output: super::ecvrf_impl::Output = (&proof).into();
Ok(NodeLabel::new(output.to_truncated_bytes(), 256u32))
}
async fn get_label_proof<H: Hasher>(
&self,
uname: &AkdLabel,
stale: bool,
version: u64,
) -> Result<Proof, VrfError> {
let key = self.get_vrf_private_key().await?;
let name_hash_bytes = H::hash(uname);
let stale_bytes = if stale { &[0u8] } else { &[1u8] };
let hashed_label = H::merge(&[
name_hash_bytes,
H::merge_with_int(H::hash(stale_bytes), version),
]);
let message_vec = from_digest::<H>(hashed_label);
let message: &[u8] = message_vec.as_slice();
let proof = key.prove(message);
Ok(proof)
}
}