consensus_config/
crypto.rs1use fastcrypto::{
15 bls12381, ed25519,
16 error::FastCryptoError,
17 hash::{Blake2b256, HashFunction},
18 traits::{KeyPair as _, Signer as _, ToFromBytes as _, VerifyingKey as _},
19};
20use serde::{Deserialize, Serialize};
21use shared_crypto::intent::INTENT_PREFIX_LENGTH;
22
23#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
25pub struct NetworkPublicKey(ed25519::Ed25519PublicKey);
26pub struct NetworkPrivateKey(ed25519::Ed25519PrivateKey);
27pub struct NetworkKeyPair(ed25519::Ed25519KeyPair);
28
29impl NetworkPublicKey {
30 pub fn new(key: ed25519::Ed25519PublicKey) -> Self {
31 Self(key)
32 }
33
34 pub fn into_inner(self) -> ed25519::Ed25519PublicKey {
35 self.0
36 }
37
38 pub fn to_bytes(&self) -> [u8; 32] {
39 self.0.0.to_bytes()
40 }
41}
42
43impl NetworkPrivateKey {
44 pub fn into_inner(self) -> ed25519::Ed25519PrivateKey {
45 self.0
46 }
47}
48
49impl NetworkKeyPair {
50 pub fn new(keypair: ed25519::Ed25519KeyPair) -> Self {
51 Self(keypair)
52 }
53
54 pub fn generate<R: rand::Rng + fastcrypto::traits::AllowedRng>(rng: &mut R) -> Self {
55 Self(ed25519::Ed25519KeyPair::generate(rng))
56 }
57
58 pub fn public(&self) -> NetworkPublicKey {
59 NetworkPublicKey(self.0.public().clone())
60 }
61
62 pub fn private_key(self) -> NetworkPrivateKey {
63 NetworkPrivateKey(self.0.copy().private())
64 }
65
66 pub fn private_key_bytes(self) -> [u8; 32] {
67 self.0.private().0.to_bytes()
68 }
69}
70
71impl Clone for NetworkKeyPair {
72 fn clone(&self) -> Self {
73 Self(self.0.copy())
74 }
75}
76
77#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
79pub struct ProtocolPublicKey(ed25519::Ed25519PublicKey);
80pub struct ProtocolKeyPair(ed25519::Ed25519KeyPair);
81pub struct ProtocolKeySignature(ed25519::Ed25519Signature);
82
83impl ProtocolPublicKey {
84 pub fn new(key: ed25519::Ed25519PublicKey) -> Self {
85 Self(key)
86 }
87
88 pub fn verify(
89 &self,
90 message: &[u8],
91 signature: &ProtocolKeySignature,
92 ) -> Result<(), FastCryptoError> {
93 self.0.verify(message, &signature.0)
94 }
95
96 pub fn to_bytes(&self) -> &[u8] {
97 self.0.as_bytes()
98 }
99}
100
101impl ProtocolKeyPair {
102 pub fn new(keypair: ed25519::Ed25519KeyPair) -> Self {
103 Self(keypair)
104 }
105
106 pub fn generate<R: rand::Rng + fastcrypto::traits::AllowedRng>(rng: &mut R) -> Self {
107 Self(ed25519::Ed25519KeyPair::generate(rng))
108 }
109
110 pub fn public(&self) -> ProtocolPublicKey {
111 ProtocolPublicKey(self.0.public().clone())
112 }
113
114 pub fn sign(&self, message: &[u8]) -> ProtocolKeySignature {
115 ProtocolKeySignature(self.0.sign(message))
116 }
117}
118
119impl Clone for ProtocolKeyPair {
120 fn clone(&self) -> Self {
121 Self(self.0.copy())
122 }
123}
124
125impl ProtocolKeySignature {
126 pub fn from_bytes(bytes: &[u8]) -> Result<Self, FastCryptoError> {
127 Ok(Self(ed25519::Ed25519Signature::from_bytes(bytes)?))
128 }
129
130 pub fn to_bytes(&self) -> &[u8] {
131 self.0.as_bytes()
132 }
133}
134
135#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
138pub struct AuthorityPublicKey(bls12381::min_sig::BLS12381PublicKey);
139pub struct AuthorityKeyPair(bls12381::min_sig::BLS12381KeyPair);
140
141impl AuthorityPublicKey {
142 pub fn new(key: bls12381::min_sig::BLS12381PublicKey) -> Self {
143 Self(key)
144 }
145
146 pub fn inner(&self) -> &bls12381::min_sig::BLS12381PublicKey {
147 &self.0
148 }
149
150 pub fn to_bytes(&self) -> &[u8] {
151 self.0.as_bytes()
152 }
153}
154
155impl AuthorityKeyPair {
156 pub fn new(keypair: bls12381::min_sig::BLS12381KeyPair) -> Self {
157 Self(keypair)
158 }
159
160 pub fn generate<R: rand::Rng + fastcrypto::traits::AllowedRng>(rng: &mut R) -> Self {
161 Self(bls12381::min_sig::BLS12381KeyPair::generate(rng))
162 }
163
164 pub fn public(&self) -> AuthorityPublicKey {
165 AuthorityPublicKey(self.0.public().clone())
166 }
167}
168
169pub type DefaultHashFunction = Blake2b256;
171pub const DIGEST_LENGTH: usize = DefaultHashFunction::OUTPUT_SIZE;
172pub const INTENT_MESSAGE_LENGTH: usize = INTENT_PREFIX_LENGTH + DIGEST_LENGTH;