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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
// Copyright (c) 2021-2024 Espresso Systems (espressosys.com)
// This file is part of the HotShot repository.
// You should have received a copy of the MIT License
// along with the HotShot repository. If not, see <https://mit-license.org/>.
//! Minimal compatibility over public key signatures
// data is serialized as big-endian for signing purposes
#![forbid(clippy::little_endian_bytes)]
use std::{
fmt::{Debug, Display},
hash::Hash,
};
use ark_serialize::SerializationError;
use bitvec::prelude::*;
use committable::Committable;
use jf_signature::SignatureError;
use jf_vid::VidScheme;
use primitive_types::U256;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tagged_base64::{TaggedBase64, Tb64Error};
use super::EncodeBytes;
use crate::{
bundle::Bundle, traits::node_implementation::NodeType, utils::BuilderCommitment,
vid::VidSchemeType,
};
/// Type representing stake table entries in a `StakeTable`
pub trait StakeTableEntryType<K> {
/// Get the stake value
fn stake(&self) -> U256;
/// Get the public key
fn public_key(&self) -> K;
}
/// Trait for abstracting private signature key
pub trait PrivateSignatureKey:
Send + Sync + Sized + Clone + Debug + Eq + Hash + for<'a> TryFrom<&'a TaggedBase64>
{
/// Serialize the private key into bytes
fn to_bytes(&self) -> Vec<u8>;
/// Deserialize the private key from bytes
/// # Errors
/// If deserialization fails.
fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self>;
/// Serialize the private key into TaggedBase64 blob.
/// # Errors
/// If serialization fails.
fn to_tagged_base64(&self) -> Result<TaggedBase64, Tb64Error>;
}
/// Trait for abstracting public key signatures
/// Self is the public key type
pub trait SignatureKey:
Send
+ Sync
+ Clone
+ Sized
+ Debug
+ Hash
+ Serialize
+ for<'a> Deserialize<'a>
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ Display
+ for<'a> TryFrom<&'a TaggedBase64>
+ Into<TaggedBase64>
{
/// The private key type for this signature algorithm
type PrivateKey: PrivateSignatureKey;
/// The type of the entry that contain both public key and stake value
type StakeTableEntry: StakeTableEntryType<Self>
+ Send
+ Sync
+ Sized
+ Clone
+ Debug
+ Hash
+ Eq
+ Serialize
+ for<'a> Deserialize<'a>;
/// The type of the quorum certificate parameters used for assembled signature
type QcParams: Send + Sync + Sized + Clone + Debug + Hash;
/// The type of the assembled signature, without `BitVec`
type PureAssembledSignatureType: Send
+ Sync
+ Sized
+ Clone
+ Debug
+ Hash
+ PartialEq
+ Eq
+ Serialize
+ for<'a> Deserialize<'a>
+ Into<TaggedBase64>
+ for<'a> TryFrom<&'a TaggedBase64>;
/// The type of the assembled qc: assembled signature + `BitVec`
type QcType: Send
+ Sync
+ Sized
+ Clone
+ Debug
+ Hash
+ PartialEq
+ Eq
+ Serialize
+ for<'a> Deserialize<'a>;
/// Type of error that can occur when signing data
type SignError: std::error::Error + Send + Sync;
// Signature type represented as a vec/slice of bytes to let the implementer handle the nuances
// of serialization, to avoid Cryptographic pitfalls
/// Validate a signature
fn validate(&self, signature: &Self::PureAssembledSignatureType, data: &[u8]) -> bool;
/// Produce a signature
/// # Errors
/// If unable to sign the data with the key
fn sign(
private_key: &Self::PrivateKey,
data: &[u8],
) -> Result<Self::PureAssembledSignatureType, Self::SignError>;
/// Produce a public key from a private key
fn from_private(private_key: &Self::PrivateKey) -> Self;
/// Serialize a public key to bytes
fn to_bytes(&self) -> Vec<u8>;
/// Deserialize a public key from bytes
/// # Errors
///
/// Will return `Err` if deserialization fails
fn from_bytes(bytes: &[u8]) -> Result<Self, SerializationError>;
/// Generate a new key pair
fn generated_from_seed_indexed(seed: [u8; 32], index: u64) -> (Self, Self::PrivateKey);
/// get the stake table entry from the public key and stake value
fn stake_table_entry(&self, stake: u64) -> Self::StakeTableEntry;
/// only get the public key from the stake table entry
fn public_key(entry: &Self::StakeTableEntry) -> Self;
/// get the public parameter for the assembled signature checking
fn public_parameter(
stake_entries: Vec<Self::StakeTableEntry>,
threshold: U256,
) -> Self::QcParams;
/// check the quorum certificate for the assembled signature, returning `Ok(())` if it is valid.
///
/// # Errors
/// Returns an error if the signature key fails to validate
fn check(
real_qc_pp: &Self::QcParams,
data: &[u8],
qc: &Self::QcType,
) -> Result<(), SignatureError>;
/// get the assembled signature and the `BitVec` separately from the assembled signature
fn sig_proof(signature: &Self::QcType) -> (Self::PureAssembledSignatureType, BitVec);
/// assemble the signature from the partial signature and the indication of signers in `BitVec`
fn assemble(
real_qc_pp: &Self::QcParams,
signers: &BitSlice,
sigs: &[Self::PureAssembledSignatureType],
) -> Self::QcType;
/// generates the genesis public key. Meant to be dummy/filler
#[must_use]
fn genesis_proposer_pk() -> Self;
}
/// Builder Signature Key trait with minimal requirements
pub trait BuilderSignatureKey:
Send
+ Sync
+ Clone
+ Sized
+ Debug
+ Hash
+ Serialize
+ DeserializeOwned
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ Display
{
/// The type of the keys builder would use to sign its messages
type BuilderPrivateKey: PrivateSignatureKey;
/// The type of the signature builder would use to sign its messages
type BuilderSignature: Send
+ Sync
+ Sized
+ Clone
+ Debug
+ Eq
+ Serialize
+ for<'a> Deserialize<'a>
+ Hash;
/// Type of error that can occur when signing data
type SignError: std::error::Error + Send + Sync;
/// validate the message with the builder's public key
fn validate_builder_signature(&self, signature: &Self::BuilderSignature, data: &[u8]) -> bool;
/// validate signature over sequencing fee information
/// with the builder's public key
fn validate_fee_signature<Metadata: EncodeBytes>(
&self,
signature: &Self::BuilderSignature,
fee_amount: u64,
metadata: &Metadata,
vid_commitment: &<VidSchemeType as VidScheme>::Commit,
) -> bool {
self.validate_builder_signature(
signature,
&aggregate_fee_data(fee_amount, metadata, vid_commitment),
)
}
/// validate signature over sequencing fee information
/// with the builder's public key (marketplace version)
fn validate_sequencing_fee_signature_marketplace(
&self,
signature: &Self::BuilderSignature,
fee_amount: u64,
view_number: u64,
) -> bool {
self.validate_builder_signature(
signature,
&aggregate_fee_data_marketplace(fee_amount, view_number),
)
}
/// validate the bundle's signature using the builder's public key
fn validate_bundle_signature<TYPES: NodeType<BuilderSignatureKey = Self>>(
&self,
bundle: Bundle<TYPES>,
) -> bool where {
let commitments = bundle
.transactions
.iter()
.flat_map(|txn| <[u8; 32]>::from(txn.commit()))
.collect::<Vec<u8>>();
self.validate_builder_signature(&bundle.signature, &commitments)
}
/// validate signature over block information with the builder's public key
fn validate_block_info_signature(
&self,
signature: &Self::BuilderSignature,
block_size: u64,
fee_amount: u64,
payload_commitment: &BuilderCommitment,
) -> bool {
self.validate_builder_signature(
signature,
&aggregate_block_info_data(block_size, fee_amount, payload_commitment),
)
}
/// sign the message with the builder's private key
/// # Errors
/// If unable to sign the data with the key
fn sign_builder_message(
private_key: &Self::BuilderPrivateKey,
data: &[u8],
) -> Result<Self::BuilderSignature, Self::SignError>;
/// sign sequencing fee offer for proposed payload
/// # Errors
/// If unable to sign the data with the key
fn sign_fee<Metadata: EncodeBytes>(
private_key: &Self::BuilderPrivateKey,
fee_amount: u64,
metadata: &Metadata,
vid_commitment: &<VidSchemeType as VidScheme>::Commit,
) -> Result<Self::BuilderSignature, Self::SignError> {
Self::sign_builder_message(
private_key,
&aggregate_fee_data(fee_amount, metadata, vid_commitment),
)
}
/// sign fee offer for proposed payload (marketplace version)
/// # Errors
/// If unable to sign the data with the key
fn sign_sequencing_fee_marketplace(
private_key: &Self::BuilderPrivateKey,
fee_amount: u64,
view_number: u64,
) -> Result<Self::BuilderSignature, Self::SignError> {
Self::sign_builder_message(
private_key,
&aggregate_fee_data_marketplace(fee_amount, view_number),
)
}
/// sign transactions (marketplace version)
/// # Errors
/// If unable to sign the data with the key
fn sign_bundle<TYPES: NodeType>(
private_key: &Self::BuilderPrivateKey,
transactions: &[TYPES::Transaction],
) -> Result<Self::BuilderSignature, Self::SignError> {
let commitments = transactions
.iter()
.flat_map(|txn| <[u8; 32]>::from(txn.commit()))
.collect::<Vec<u8>>();
Self::sign_builder_message(private_key, &commitments)
}
/// sign information about offered block
/// # Errors
/// If unable to sign the data with the key
fn sign_block_info(
private_key: &Self::BuilderPrivateKey,
block_size: u64,
fee_amount: u64,
payload_commitment: &BuilderCommitment,
) -> Result<Self::BuilderSignature, Self::SignError> {
Self::sign_builder_message(
private_key,
&aggregate_block_info_data(block_size, fee_amount, payload_commitment),
)
}
/// Generate a new key pair
fn generated_from_seed_indexed(seed: [u8; 32], index: u64) -> (Self, Self::BuilderPrivateKey);
}
/// Aggregate all inputs used for signature over fee data
fn aggregate_fee_data<Metadata: EncodeBytes>(
fee_amount: u64,
metadata: &Metadata,
vid_commitment: &<VidSchemeType as VidScheme>::Commit,
) -> Vec<u8> {
let mut fee_info = Vec::new();
fee_info.extend_from_slice(fee_amount.to_be_bytes().as_ref());
fee_info.extend_from_slice(metadata.encode().as_ref());
fee_info.extend_from_slice(vid_commitment.as_ref());
fee_info
}
/// Aggregate all inputs used for signature over fee data
fn aggregate_fee_data_marketplace(fee_amount: u64, view_number: u64) -> Vec<u8> {
let mut fee_info = Vec::new();
fee_info.extend_from_slice(fee_amount.to_be_bytes().as_ref());
fee_info.extend_from_slice(view_number.to_be_bytes().as_ref());
fee_info
}
/// Aggregate all inputs used for signature over block info
fn aggregate_block_info_data(
block_size: u64,
fee_amount: u64,
payload_commitment: &BuilderCommitment,
) -> Vec<u8> {
let mut block_info = Vec::new();
block_info.extend_from_slice(block_size.to_be_bytes().as_ref());
block_info.extend_from_slice(fee_amount.to_be_bytes().as_ref());
block_info.extend_from_slice(payload_commitment.as_ref());
block_info
}