|
| 1 | +use std::cmp::Ordering; |
| 2 | +use std::hash::{Hash, Hasher}; |
| 3 | + |
| 4 | +use blake2::digest::{Digest, FixedOutput}; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +use crate::bls_multi_signature::Signature; |
| 8 | +use crate::eligibility_check::ev_lt_phi; |
| 9 | +use crate::{ |
| 10 | + Index, Stake, StmAggrVerificationKey, StmParameters, StmSignatureError, StmVerificationKey, |
| 11 | +}; |
| 12 | + |
| 13 | +/// Signature created by a single party who has won the lottery. |
| 14 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 15 | +pub struct StmSig { |
| 16 | + /// The signature from the underlying MSP scheme. |
| 17 | + pub sigma: Signature, |
| 18 | + /// The index(es) for which the signature is valid |
| 19 | + pub indexes: Vec<Index>, |
| 20 | + /// Merkle tree index of the signer. |
| 21 | + pub signer_index: Index, |
| 22 | +} |
| 23 | + |
| 24 | +impl StmSig { |
| 25 | + /// Verify an stm signature by checking that the lottery was won, the merkle path is correct, |
| 26 | + /// the indexes are in the desired range and the underlying multi signature validates. |
| 27 | + pub fn verify<D: Clone + Digest + FixedOutput>( |
| 28 | + &self, |
| 29 | + params: &StmParameters, |
| 30 | + pk: &StmVerificationKey, |
| 31 | + stake: &Stake, |
| 32 | + avk: &StmAggrVerificationKey<D>, |
| 33 | + msg: &[u8], |
| 34 | + ) -> Result<(), StmSignatureError> { |
| 35 | + let msgp = avk.get_mt_commitment().concat_with_msg(msg); |
| 36 | + self.verify_core(params, pk, stake, &msgp, &avk.get_total_stake())?; |
| 37 | + Ok(()) |
| 38 | + } |
| 39 | + |
| 40 | + /// Verify that all indices of a signature are valid. |
| 41 | + pub(crate) fn check_indices( |
| 42 | + &self, |
| 43 | + params: &StmParameters, |
| 44 | + stake: &Stake, |
| 45 | + msg: &[u8], |
| 46 | + total_stake: &Stake, |
| 47 | + ) -> Result<(), StmSignatureError> { |
| 48 | + for &index in &self.indexes { |
| 49 | + if index > params.m { |
| 50 | + return Err(StmSignatureError::IndexBoundFailed(index, params.m)); |
| 51 | + } |
| 52 | + |
| 53 | + let ev = self.sigma.eval(msg, index); |
| 54 | + |
| 55 | + if !ev_lt_phi(params.phi_f, ev, *stake, *total_stake) { |
| 56 | + return Err(StmSignatureError::LotteryLost); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | + |
| 63 | + /// Convert an `StmSig` into bytes |
| 64 | + /// |
| 65 | + /// # Layout |
| 66 | + /// * Stake |
| 67 | + /// * Number of valid indexes (as u64) |
| 68 | + /// * Indexes of the signature |
| 69 | + /// * Public Key |
| 70 | + /// * Signature |
| 71 | + /// * Merkle index of the signer. |
| 72 | + pub fn to_bytes(&self) -> Vec<u8> { |
| 73 | + let mut output = Vec::new(); |
| 74 | + output.extend_from_slice(&(self.indexes.len() as u64).to_be_bytes()); |
| 75 | + |
| 76 | + for index in &self.indexes { |
| 77 | + output.extend_from_slice(&index.to_be_bytes()); |
| 78 | + } |
| 79 | + |
| 80 | + output.extend_from_slice(&self.sigma.to_bytes()); |
| 81 | + |
| 82 | + output.extend_from_slice(&self.signer_index.to_be_bytes()); |
| 83 | + output |
| 84 | + } |
| 85 | + |
| 86 | + /// Extract a batch compatible `StmSig` from a byte slice. |
| 87 | + pub fn from_bytes<D: Clone + Digest + FixedOutput>( |
| 88 | + bytes: &[u8], |
| 89 | + ) -> Result<StmSig, StmSignatureError> { |
| 90 | + let mut u64_bytes = [0u8; 8]; |
| 91 | + |
| 92 | + u64_bytes.copy_from_slice(&bytes[0..8]); |
| 93 | + let nr_indexes = u64::from_be_bytes(u64_bytes) as usize; |
| 94 | + |
| 95 | + let mut indexes = Vec::new(); |
| 96 | + for i in 0..nr_indexes { |
| 97 | + u64_bytes.copy_from_slice(&bytes[8 + i * 8..16 + i * 8]); |
| 98 | + indexes.push(u64::from_be_bytes(u64_bytes)); |
| 99 | + } |
| 100 | + |
| 101 | + let offset = 8 + nr_indexes * 8; |
| 102 | + let sigma = Signature::from_bytes(&bytes[offset..offset + 48])?; |
| 103 | + |
| 104 | + u64_bytes.copy_from_slice(&bytes[offset + 48..offset + 56]); |
| 105 | + let signer_index = u64::from_be_bytes(u64_bytes); |
| 106 | + |
| 107 | + Ok(StmSig { |
| 108 | + sigma, |
| 109 | + indexes, |
| 110 | + signer_index, |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + /// Compare two `StmSig` by their signers' merkle tree indexes. |
| 115 | + pub fn cmp_stm_sig(&self, other: &Self) -> Ordering { |
| 116 | + self.signer_index.cmp(&other.signer_index) |
| 117 | + } |
| 118 | + |
| 119 | + /// Verify a core signature by checking that the lottery was won, |
| 120 | + /// the indexes are in the desired range and the underlying multi signature validates. |
| 121 | + pub fn verify_core( |
| 122 | + &self, |
| 123 | + params: &StmParameters, |
| 124 | + pk: &StmVerificationKey, |
| 125 | + stake: &Stake, |
| 126 | + msg: &[u8], |
| 127 | + total_stake: &Stake, |
| 128 | + ) -> Result<(), StmSignatureError> { |
| 129 | + self.sigma.verify(msg, pk)?; |
| 130 | + self.check_indices(params, stake, msg, total_stake)?; |
| 131 | + |
| 132 | + Ok(()) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl Hash for StmSig { |
| 137 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 138 | + Hash::hash_slice(&self.sigma.to_bytes(), state) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl PartialEq for StmSig { |
| 143 | + fn eq(&self, other: &Self) -> bool { |
| 144 | + self.sigma == other.sigma |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +impl Eq for StmSig {} |
| 149 | + |
| 150 | +impl PartialOrd for StmSig { |
| 151 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 152 | + Some(std::cmp::Ord::cmp(self, other)) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +impl Ord for StmSig { |
| 157 | + fn cmp(&self, other: &Self) -> Ordering { |
| 158 | + self.cmp_stm_sig(other) |
| 159 | + } |
| 160 | +} |
0 commit comments