11use log:: debug;
22use thiserror:: Error ;
33
4- use mithril_common:: crypto_helper:: {
5- key_decode_hex, Bytes , ProtocolClerk , ProtocolKeyRegistration , ProtocolMultiSignature ,
6- ProtocolPartyId , ProtocolStake , ProtocolStakeDistribution ,
7- } ;
8-
9- use crate :: entities;
4+ use mithril_common:: crypto_helper:: { key_decode_hex, Bytes , ProtocolMultiSignature } ;
5+ use mithril_common:: entities:: ProtocolParameters ;
106
117#[ cfg( test) ]
128use mockall:: automock;
139
1410#[ derive( Error , Debug ) ]
1511pub enum ProtocolError {
16- #[ error( "multi signature verification failed" ) ]
17- VerifyMultiSignatureError ( String ) ,
12+ #[ error( "multi signature verification failed: '{0}'" ) ]
13+ VerifyMultiSignature ( String ) ,
14+
15+ #[ error( "codec error: '{0}'" ) ]
16+ Codec ( String ) ,
1817}
1918
2019/// Verifier is the cryptographic engine in charge of verifying multi signatures and certificates
@@ -25,8 +24,8 @@ pub trait Verifier {
2524 & self ,
2625 message : & Bytes ,
2726 multi_signature : & str ,
28- signers_with_stakes : & [ entities :: SignerWithStake ] ,
29- protocol_parameters : & entities :: ProtocolParameters ,
27+ aggregate_verification_key : & str ,
28+ protocol_parameters : & ProtocolParameters ,
3029 ) -> Result < ( ) , ProtocolError > ;
3130}
3231
@@ -39,36 +38,6 @@ impl VerifierImpl {
3938 debug ! ( "New VerifierImpl created" ) ;
4039 Self { }
4140 }
42-
43- /// Creates a clerk
44- pub fn create_clerk (
45- & self ,
46- signers_with_stakes : & [ entities:: SignerWithStake ] ,
47- protocol_parameters : & entities:: ProtocolParameters ,
48- ) -> Result < ProtocolClerk , ProtocolError > {
49- let stakes = signers_with_stakes
50- . iter ( )
51- . map ( |signer| {
52- (
53- signer. party_id as ProtocolPartyId ,
54- signer. stake as ProtocolStake ,
55- )
56- } )
57- . collect :: < ProtocolStakeDistribution > ( ) ;
58- let mut key_registration = ProtocolKeyRegistration :: init ( & stakes) ;
59- signers_with_stakes. iter ( ) . for_each ( |signer| {
60- if let Ok ( verification_key) = key_decode_hex ( & signer. verification_key ) {
61- key_registration
62- . register ( signer. party_id as ProtocolPartyId , verification_key)
63- . unwrap ( ) ;
64- }
65- } ) ;
66- let closed_registration = key_registration. close ( ) ;
67- Ok ( ProtocolClerk :: from_registration (
68- protocol_parameters. to_owned ( ) . into ( ) ,
69- closed_registration,
70- ) )
71- }
7241}
7342
7443impl Default for VerifierImpl {
@@ -83,30 +52,30 @@ impl Verifier for VerifierImpl {
8352 & self ,
8453 message : & Bytes ,
8554 multi_signature : & str ,
86- signers_with_stakes : & [ entities :: SignerWithStake ] ,
87- protocol_parameters : & entities :: ProtocolParameters ,
55+ aggregate_verification_key : & str ,
56+ protocol_parameters : & ProtocolParameters ,
8857 ) -> Result < ( ) , ProtocolError > {
8958 debug ! ( "Verify multi signature for {:?}" , message) ;
9059 let multi_signature: ProtocolMultiSignature =
91- key_decode_hex ( multi_signature) . map_err ( ProtocolError :: VerifyMultiSignatureError ) ?;
60+ key_decode_hex ( multi_signature) . map_err ( ProtocolError :: Codec ) ?;
61+ let aggregate_verification_key =
62+ key_decode_hex ( aggregate_verification_key) . map_err ( ProtocolError :: Codec ) ?;
9263 multi_signature
9364 . verify (
9465 message,
95- & self
96- . create_clerk ( signers_with_stakes, protocol_parameters) ?
97- . compute_avk ( ) ,
66+ & aggregate_verification_key,
9867 & protocol_parameters. to_owned ( ) . into ( ) ,
9968 )
100- . map_err ( |e| ProtocolError :: VerifyMultiSignatureError ( e. to_string ( ) ) )
69+ . map_err ( |e| ProtocolError :: VerifyMultiSignature ( e. to_string ( ) ) )
10170 }
10271}
10372
10473#[ cfg( test) ]
10574mod tests {
10675 use super :: * ;
10776
108- use mithril_common:: crypto_helper:: key_encode_hex;
10977 use mithril_common:: crypto_helper:: tests_setup:: * ;
78+ use mithril_common:: crypto_helper:: { key_encode_hex, ProtocolClerk } ;
11079
11180 #[ test]
11281 fn test_multi_signer_multi_signature_ok ( ) {
@@ -125,27 +94,18 @@ mod tests {
12594
12695 let first_signer = & signers. first ( ) . unwrap ( ) . 3 ;
12796 let clerk = ProtocolClerk :: from_signer ( & first_signer) ;
97+ let aggregate_verification_key = clerk. compute_avk ( ) ;
12898 let multi_signature = clerk. aggregate ( & single_signatures, & message) . unwrap ( ) ;
12999
130100 let verifier = VerifierImpl :: new ( ) ;
131101 let protocol_parameters = protocol_parameters. into ( ) ;
132- let signers_with_stakes = signers
133- . iter ( )
134- . map ( |( party_id, stake, verification_key, _, _) | {
135- entities:: SignerWithStake :: new (
136- * party_id as u64 ,
137- key_encode_hex ( verification_key) . unwrap ( ) ,
138- * stake as u64 ,
139- )
140- } )
141- . collect :: < Vec < entities:: SignerWithStake > > ( ) ;
142102 let message_tampered = message[ 1 ..] . to_vec ( ) ;
143103 assert ! (
144104 verifier
145105 . verify_multi_signature(
146106 & message_tampered,
147107 & key_encode_hex( & multi_signature) . unwrap( ) ,
148- & signers_with_stakes ,
108+ & key_encode_hex ( & aggregate_verification_key ) . unwrap ( ) ,
149109 & protocol_parameters,
150110 )
151111 . is_err( ) ,
@@ -155,7 +115,7 @@ mod tests {
155115 . verify_multi_signature (
156116 & message,
157117 & key_encode_hex ( & multi_signature) . unwrap ( ) ,
158- & signers_with_stakes ,
118+ & key_encode_hex ( & aggregate_verification_key ) . unwrap ( ) ,
159119 & protocol_parameters,
160120 )
161121 . expect ( "multi signature verification should have succeeded" ) ;
0 commit comments