106
106
//! ```
107
107
108
108
use crate :: bls_multi_signature:: { Signature , VerificationKey } ;
109
- use crate :: eligibility_check:: ev_lt_phi;
110
109
use crate :: error:: {
111
110
AggregationError , CoreVerifierError , RegisterError , StmAggregateSignatureError ,
112
111
StmSignatureError ,
113
112
} ;
114
113
use crate :: key_reg:: { ClosedKeyReg , RegParty } ;
115
114
use crate :: merkle_tree:: { BatchPath , MTLeaf , MerkleTreeCommitmentBatchCompat } ;
116
115
use crate :: participant:: { StmSigner , StmVerificationKey } ;
116
+ use crate :: single_signature:: StmSig ;
117
117
use blake2:: digest:: { Digest , FixedOutput } ;
118
118
use serde:: ser:: SerializeTuple ;
119
119
use serde:: { Deserialize , Serialize , Serializer } ;
120
- use std:: cmp:: Ordering ;
121
120
use std:: collections:: { BTreeMap , HashMap , HashSet } ;
122
121
use std:: convert:: { From , TryFrom , TryInto } ;
123
- use std:: hash:: { Hash , Hasher } ;
122
+ use std:: hash:: Hash ;
124
123
125
124
/// The quantity of stake held by a party, represented as a `u64`.
126
125
pub type Stake = u64 ;
@@ -192,6 +191,16 @@ pub struct StmAggrVerificationKey<D: Clone + Digest + FixedOutput> {
192
191
total_stake : Stake ,
193
192
}
194
193
194
+ impl < D : Digest + Clone + FixedOutput > StmAggrVerificationKey < D > {
195
+ pub fn get_mt_commitment ( & self ) -> MerkleTreeCommitmentBatchCompat < D > {
196
+ self . mt_commitment . clone ( )
197
+ }
198
+
199
+ pub fn get_total_stake ( & self ) -> Stake {
200
+ self . total_stake
201
+ }
202
+ }
203
+
195
204
impl < D : Digest + Clone + FixedOutput > PartialEq for StmAggrVerificationKey < D > {
196
205
fn eq ( & self , other : & Self ) -> bool {
197
206
self . mt_commitment == other. mt_commitment && self . total_stake == other. total_stake
@@ -209,155 +218,6 @@ impl<D: Clone + Digest + FixedOutput> From<&ClosedKeyReg<D>> for StmAggrVerifica
209
218
}
210
219
}
211
220
212
- /// Signature created by a single party who has won the lottery.
213
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
214
- pub struct StmSig {
215
- /// The signature from the underlying MSP scheme.
216
- pub sigma : Signature ,
217
- /// The index(es) for which the signature is valid
218
- pub indexes : Vec < Index > ,
219
- /// Merkle tree index of the signer.
220
- pub signer_index : Index ,
221
- }
222
-
223
- impl StmSig {
224
- /// Verify an stm signature by checking that the lottery was won, the merkle path is correct,
225
- /// the indexes are in the desired range and the underlying multi signature validates.
226
- pub fn verify < D : Clone + Digest + FixedOutput > (
227
- & self ,
228
- params : & StmParameters ,
229
- pk : & StmVerificationKey ,
230
- stake : & Stake ,
231
- avk : & StmAggrVerificationKey < D > ,
232
- msg : & [ u8 ] ,
233
- ) -> Result < ( ) , StmSignatureError > {
234
- let msgp = avk. mt_commitment . concat_with_msg ( msg) ;
235
- self . verify_core ( params, pk, stake, & msgp, & avk. total_stake ) ?;
236
- Ok ( ( ) )
237
- }
238
-
239
- /// Verify that all indices of a signature are valid.
240
- pub ( crate ) fn check_indices (
241
- & self ,
242
- params : & StmParameters ,
243
- stake : & Stake ,
244
- msg : & [ u8 ] ,
245
- total_stake : & Stake ,
246
- ) -> Result < ( ) , StmSignatureError > {
247
- for & index in & self . indexes {
248
- if index > params. m {
249
- return Err ( StmSignatureError :: IndexBoundFailed ( index, params. m ) ) ;
250
- }
251
-
252
- let ev = self . sigma . eval ( msg, index) ;
253
-
254
- if !ev_lt_phi ( params. phi_f , ev, * stake, * total_stake) {
255
- return Err ( StmSignatureError :: LotteryLost ) ;
256
- }
257
- }
258
-
259
- Ok ( ( ) )
260
- }
261
-
262
- /// Convert an `StmSig` into bytes
263
- ///
264
- /// # Layout
265
- /// * Stake
266
- /// * Number of valid indexes (as u64)
267
- /// * Indexes of the signature
268
- /// * Public Key
269
- /// * Signature
270
- /// * Merkle index of the signer.
271
- pub fn to_bytes ( & self ) -> Vec < u8 > {
272
- let mut output = Vec :: new ( ) ;
273
- output. extend_from_slice ( & ( self . indexes . len ( ) as u64 ) . to_be_bytes ( ) ) ;
274
-
275
- for index in & self . indexes {
276
- output. extend_from_slice ( & index. to_be_bytes ( ) ) ;
277
- }
278
-
279
- output. extend_from_slice ( & self . sigma . to_bytes ( ) ) ;
280
-
281
- output. extend_from_slice ( & self . signer_index . to_be_bytes ( ) ) ;
282
- output
283
- }
284
-
285
- /// Extract a batch compatible `StmSig` from a byte slice.
286
- pub fn from_bytes < D : Clone + Digest + FixedOutput > (
287
- bytes : & [ u8 ] ,
288
- ) -> Result < StmSig , StmSignatureError > {
289
- let mut u64_bytes = [ 0u8 ; 8 ] ;
290
-
291
- u64_bytes. copy_from_slice ( & bytes[ 0 ..8 ] ) ;
292
- let nr_indexes = u64:: from_be_bytes ( u64_bytes) as usize ;
293
-
294
- let mut indexes = Vec :: new ( ) ;
295
- for i in 0 ..nr_indexes {
296
- u64_bytes. copy_from_slice ( & bytes[ 8 + i * 8 ..16 + i * 8 ] ) ;
297
- indexes. push ( u64:: from_be_bytes ( u64_bytes) ) ;
298
- }
299
-
300
- let offset = 8 + nr_indexes * 8 ;
301
- let sigma = Signature :: from_bytes ( & bytes[ offset..offset + 48 ] ) ?;
302
-
303
- u64_bytes. copy_from_slice ( & bytes[ offset + 48 ..offset + 56 ] ) ;
304
- let signer_index = u64:: from_be_bytes ( u64_bytes) ;
305
-
306
- Ok ( StmSig {
307
- sigma,
308
- indexes,
309
- signer_index,
310
- } )
311
- }
312
-
313
- /// Compare two `StmSig` by their signers' merkle tree indexes.
314
- pub fn cmp_stm_sig ( & self , other : & Self ) -> Ordering {
315
- self . signer_index . cmp ( & other. signer_index )
316
- }
317
-
318
- /// Verify a core signature by checking that the lottery was won,
319
- /// the indexes are in the desired range and the underlying multi signature validates.
320
- pub fn verify_core (
321
- & self ,
322
- params : & StmParameters ,
323
- pk : & StmVerificationKey ,
324
- stake : & Stake ,
325
- msg : & [ u8 ] ,
326
- total_stake : & Stake ,
327
- ) -> Result < ( ) , StmSignatureError > {
328
- self . sigma . verify ( msg, pk) ?;
329
- self . check_indices ( params, stake, msg, total_stake) ?;
330
-
331
- Ok ( ( ) )
332
- }
333
- }
334
-
335
- impl Hash for StmSig {
336
- fn hash < H : Hasher > ( & self , state : & mut H ) {
337
- Hash :: hash_slice ( & self . sigma . to_bytes ( ) , state)
338
- }
339
- }
340
-
341
- impl PartialEq for StmSig {
342
- fn eq ( & self , other : & Self ) -> bool {
343
- self . sigma == other. sigma
344
- }
345
- }
346
-
347
- impl Eq for StmSig { }
348
-
349
- impl PartialOrd for StmSig {
350
- fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
351
- Some ( std:: cmp:: Ord :: cmp ( self , other) )
352
- }
353
- }
354
-
355
- impl Ord for StmSig {
356
- fn cmp ( & self , other : & Self ) -> Ordering {
357
- self . cmp_stm_sig ( other)
358
- }
359
- }
360
-
361
221
/// Signature with its registered party.
362
222
#[ derive( Debug , Clone , Hash , Deserialize , Eq , PartialEq , Ord , PartialOrd ) ]
363
223
pub struct StmSigRegParty {
0 commit comments