consensus_config/
crypto.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Here we select the cryptographic types that are used by default in the code base.
5//! The whole code base should only:
6//! - refer to those aliases and not use the individual scheme implementations
7//! - not use the schemes in a way that break genericity (e.g. using their Struct impl functions)
8//! - swap one of those aliases to point to another type if necessary
9//!
10//! Beware: if you change those aliases to point to another scheme implementation, you will have
11//! to change all four aliases to point to concrete types that work with each other. Failure to do
12//! so will result in a ton of compilation errors, and worse: it will not make sense!
13
14use fastcrypto::{
15    ed25519,
16    encoding::{Base64, Encoding as _},
17    error::FastCryptoError,
18    hash::{Blake2b256, HashFunction},
19    traits::{KeyPair as _, Signer as _, ToFromBytes as _, VerifyingKey as _},
20};
21use serde::{Deserialize, Serialize};
22use shared_crypto::intent::INTENT_PREFIX_LENGTH;
23
24/// Network key is used for TLS and as the network identity of the authority.
25#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
26pub struct NetworkPublicKey(ed25519::Ed25519PublicKey);
27pub struct NetworkPrivateKey(ed25519::Ed25519PrivateKey);
28pub struct NetworkKeyPair(ed25519::Ed25519KeyPair);
29
30impl NetworkPublicKey {
31    pub fn new(key: ed25519::Ed25519PublicKey) -> Self {
32        Self(key)
33    }
34
35    pub fn into_inner(self) -> ed25519::Ed25519PublicKey {
36        self.0
37    }
38
39    pub fn to_bytes(&self) -> [u8; 32] {
40        self.0.0.to_bytes()
41    }
42}
43
44impl NetworkPrivateKey {
45    pub fn into_inner(self) -> ed25519::Ed25519PrivateKey {
46        self.0
47    }
48}
49
50impl NetworkKeyPair {
51    pub fn new(keypair: ed25519::Ed25519KeyPair) -> Self {
52        Self(keypair)
53    }
54
55    pub fn generate<R: rand::Rng + fastcrypto::traits::AllowedRng>(rng: &mut R) -> Self {
56        Self(ed25519::Ed25519KeyPair::generate(rng))
57    }
58
59    pub fn public(&self) -> NetworkPublicKey {
60        NetworkPublicKey(self.0.public().clone())
61    }
62
63    pub fn private_key(self) -> NetworkPrivateKey {
64        NetworkPrivateKey(self.0.copy().private())
65    }
66
67    pub fn private_key_bytes(self) -> [u8; 32] {
68        self.0.private().0.to_bytes()
69    }
70}
71
72impl Clone for NetworkKeyPair {
73    fn clone(&self) -> Self {
74        Self(self.0.copy())
75    }
76}
77
78/// Protocol key is used for signing blocks and verifying block signatures.
79#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
80pub struct ProtocolPublicKey(ed25519::Ed25519PublicKey);
81pub struct ProtocolKeyPair(ed25519::Ed25519KeyPair);
82pub struct ProtocolKeySignature(ed25519::Ed25519Signature);
83
84impl ProtocolPublicKey {
85    pub fn new(key: ed25519::Ed25519PublicKey) -> Self {
86        Self(key)
87    }
88
89    pub fn verify(
90        &self,
91        message: &[u8],
92        signature: &ProtocolKeySignature,
93    ) -> Result<(), FastCryptoError> {
94        self.0.verify(message, &signature.0)
95    }
96
97    pub fn to_bytes(&self) -> &[u8] {
98        self.0.as_bytes()
99    }
100}
101
102impl ProtocolKeyPair {
103    pub fn new(keypair: ed25519::Ed25519KeyPair) -> Self {
104        Self(keypair)
105    }
106
107    pub fn generate<R: rand::Rng + fastcrypto::traits::AllowedRng>(rng: &mut R) -> Self {
108        Self(ed25519::Ed25519KeyPair::generate(rng))
109    }
110
111    pub fn public(&self) -> ProtocolPublicKey {
112        ProtocolPublicKey(self.0.public().clone())
113    }
114
115    pub fn sign(&self, message: &[u8]) -> ProtocolKeySignature {
116        ProtocolKeySignature(self.0.sign(message))
117    }
118}
119
120impl Clone for ProtocolKeyPair {
121    fn clone(&self) -> Self {
122        Self(self.0.copy())
123    }
124}
125
126impl ProtocolKeySignature {
127    pub fn from_bytes(bytes: &[u8]) -> Result<Self, FastCryptoError> {
128        Ok(Self(ed25519::Ed25519Signature::from_bytes(bytes)?))
129    }
130
131    pub fn to_bytes(&self) -> &[u8] {
132        self.0.as_bytes()
133    }
134}
135
136/// Authority name is a raw bytes identity for an authority, matching `AuthorityName`
137/// on the Sui side. It is only used for identity sanity checks and not for cryptographic
138/// verification. The bytes originate from the authority's BLS12381 public key.
139pub const AUTHORITY_NAME_LENGTH: usize = 96;
140
141#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
142pub struct AuthorityName([u8; AUTHORITY_NAME_LENGTH]);
143
144impl AuthorityName {
145    pub fn new(bytes: [u8; AUTHORITY_NAME_LENGTH]) -> Self {
146        Self(bytes)
147    }
148
149    pub fn from_bytes(bytes: &[u8]) -> Self {
150        let mut arr = [0u8; AUTHORITY_NAME_LENGTH];
151        arr.copy_from_slice(bytes);
152        Self(arr)
153    }
154
155    pub fn to_bytes(&self) -> &[u8] {
156        &self.0
157    }
158}
159
160impl std::fmt::Debug for AuthorityName {
161    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
162        write!(f, "AuthorityName({})", Base64::encode(self.0))
163    }
164}
165
166impl Serialize for AuthorityName {
167    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
168        if serializer.is_human_readable() {
169            serializer.serialize_str(&Base64::encode(self.0))
170        } else {
171            serializer.serialize_bytes(&self.0)
172        }
173    }
174}
175
176impl<'de> Deserialize<'de> for AuthorityName {
177    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
178        if deserializer.is_human_readable() {
179            let s = String::deserialize(deserializer)?;
180            let bytes = Base64::decode(&s).map_err(serde::de::Error::custom)?;
181            let arr: [u8; AUTHORITY_NAME_LENGTH] = bytes
182                .try_into()
183                .map_err(|_| serde::de::Error::custom("invalid authority name length"))?;
184            Ok(Self(arr))
185        } else {
186            let bytes = <Vec<u8>>::deserialize(deserializer)?;
187            let arr: [u8; AUTHORITY_NAME_LENGTH] = bytes
188                .try_into()
189                .map_err(|_| serde::de::Error::custom("invalid authority name length"))?;
190            Ok(Self(arr))
191        }
192    }
193}
194
195/// Defines algorithm and format of block and commit digests.
196pub type DefaultHashFunction = Blake2b256;
197pub const DIGEST_LENGTH: usize = DefaultHashFunction::OUTPUT_SIZE;
198pub const INTENT_MESSAGE_LENGTH: usize = INTENT_PREFIX_LENGTH + DIGEST_LENGTH;