|
| 1 | +use crate::bls_multi_signature::{SigningKey, VerificationKeyPoP}; |
| 2 | +use crate::key_reg::{ClosedKeyReg, RegParty}; |
| 3 | +use crate::participant::StmSigner; |
| 4 | +use crate::stm::{Stake, StmParameters}; |
| 5 | +use crate::RegisterError; |
| 6 | +use blake2::digest::Digest; |
| 7 | +use digest::FixedOutput; |
| 8 | +use rand_core::{CryptoRng, RngCore}; |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | + |
| 11 | +/// Wrapper of the MultiSignature Verification key with proof of possession |
| 12 | +pub type StmVerificationKeyPoP = VerificationKeyPoP; |
| 13 | + |
| 14 | +/// Initializer for `StmSigner`. |
| 15 | +/// This is the data that is used during the key registration procedure. |
| 16 | +/// Once the latter is finished, this instance is consumed into an `StmSigner`. |
| 17 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 18 | +pub struct StmInitializer { |
| 19 | + /// This participant's stake. |
| 20 | + pub stake: Stake, |
| 21 | + /// Current protocol instantiation parameters. |
| 22 | + pub params: StmParameters, |
| 23 | + /// Secret key. |
| 24 | + pub(crate) sk: SigningKey, |
| 25 | + /// Verification (public) key + proof of possession. |
| 26 | + pub(crate) pk: StmVerificationKeyPoP, |
| 27 | +} |
| 28 | + |
| 29 | +impl StmInitializer { |
| 30 | + /// Builds an `StmInitializer` that is ready to register with the key registration service. |
| 31 | + /// This function generates the signing and verification key with a PoP, and initialises the structure. |
| 32 | + pub fn setup<R: RngCore + CryptoRng>(params: StmParameters, stake: Stake, rng: &mut R) -> Self { |
| 33 | + let sk = SigningKey::gen(rng); |
| 34 | + let pk = StmVerificationKeyPoP::from(&sk); |
| 35 | + Self { |
| 36 | + stake, |
| 37 | + params, |
| 38 | + sk, |
| 39 | + pk, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Extract the verification key. |
| 44 | + pub fn verification_key(&self) -> StmVerificationKeyPoP { |
| 45 | + self.pk |
| 46 | + } |
| 47 | + |
| 48 | + /// Build the `avk` for the given list of parties. |
| 49 | + /// |
| 50 | + /// Note that if this StmInitializer was modified *between* the last call to `register`, |
| 51 | + /// then the resulting `StmSigner` may not be able to produce valid signatures. |
| 52 | + /// |
| 53 | + /// Returns an `StmSigner` specialized to |
| 54 | + /// * this `StmSigner`'s ID and current stake |
| 55 | + /// * this `StmSigner`'s parameter valuation |
| 56 | + /// * the `avk` as built from the current registered parties (according to the registration service) |
| 57 | + /// * the current total stake (according to the registration service) |
| 58 | + /// # Error |
| 59 | + /// This function fails if the initializer is not registered. |
| 60 | + pub fn new_signer<D: Digest + Clone + FixedOutput>( |
| 61 | + self, |
| 62 | + closed_reg: ClosedKeyReg<D>, |
| 63 | + ) -> Result<StmSigner<D>, RegisterError> { |
| 64 | + let mut my_index = None; |
| 65 | + for (i, rp) in closed_reg.reg_parties.iter().enumerate() { |
| 66 | + if rp.0 == self.pk.vk { |
| 67 | + my_index = Some(i as u64); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + if my_index.is_none() { |
| 72 | + return Err(RegisterError::UnregisteredInitializer); |
| 73 | + } |
| 74 | + |
| 75 | + Ok(StmSigner::set_stm_signer( |
| 76 | + my_index.unwrap(), |
| 77 | + self.stake, |
| 78 | + self.params, |
| 79 | + self.sk, |
| 80 | + self.pk.vk, |
| 81 | + closed_reg, |
| 82 | + )) |
| 83 | + } |
| 84 | + |
| 85 | + /// Creates a new core signer that does not include closed registration. |
| 86 | + /// Takes `eligible_parties` as a parameter and determines the signer's index in the parties. |
| 87 | + /// `eligible_parties` is verified and trusted which is only run by a full-node |
| 88 | + /// that has already verified the parties. |
| 89 | + pub fn new_core_signer<D: Digest + Clone + FixedOutput>( |
| 90 | + self, |
| 91 | + eligible_parties: &[RegParty], |
| 92 | + ) -> Option<StmSigner<D>> { |
| 93 | + let mut parties = eligible_parties.to_vec(); |
| 94 | + parties.sort_unstable(); |
| 95 | + let mut my_index = None; |
| 96 | + for (i, rp) in parties.iter().enumerate() { |
| 97 | + if rp.0 == self.pk.vk { |
| 98 | + my_index = Some(i as u64); |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + if let Some(index) = my_index { |
| 103 | + Some(StmSigner::set_core_signer( |
| 104 | + index, |
| 105 | + self.stake, |
| 106 | + self.params, |
| 107 | + self.sk, |
| 108 | + self.pk.vk, |
| 109 | + )) |
| 110 | + } else { |
| 111 | + None |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /// Convert to bytes |
| 116 | + /// # Layout |
| 117 | + /// * Stake (u64) |
| 118 | + /// * Params |
| 119 | + /// * Secret Key |
| 120 | + /// * Public key (including PoP) |
| 121 | + pub fn to_bytes(&self) -> [u8; 256] { |
| 122 | + let mut out = [0u8; 256]; |
| 123 | + out[..8].copy_from_slice(&self.stake.to_be_bytes()); |
| 124 | + out[8..32].copy_from_slice(&self.params.to_bytes()); |
| 125 | + out[32..64].copy_from_slice(&self.sk.to_bytes()); |
| 126 | + out[64..].copy_from_slice(&self.pk.to_bytes()); |
| 127 | + out |
| 128 | + } |
| 129 | + |
| 130 | + /// Convert a slice of bytes to an `StmInitializer` |
| 131 | + /// # Error |
| 132 | + /// The function fails if the given string of bytes is not of required size. |
| 133 | + pub fn from_bytes(bytes: &[u8]) -> Result<StmInitializer, RegisterError> { |
| 134 | + let mut u64_bytes = [0u8; 8]; |
| 135 | + u64_bytes.copy_from_slice(&bytes[..8]); |
| 136 | + let stake = u64::from_be_bytes(u64_bytes); |
| 137 | + let params = StmParameters::from_bytes(&bytes[8..32])?; |
| 138 | + let sk = SigningKey::from_bytes(&bytes[32..])?; |
| 139 | + let pk = StmVerificationKeyPoP::from_bytes(&bytes[64..])?; |
| 140 | + |
| 141 | + Ok(Self { |
| 142 | + stake, |
| 143 | + params, |
| 144 | + sk, |
| 145 | + pk, |
| 146 | + }) |
| 147 | + } |
| 148 | +} |
0 commit comments