@@ -74,10 +74,11 @@ pub trait BeefyAuthorityId<MsgHash: Hash>: RuntimeAppPublic {
7474/// Your code should use the above types as concrete types for all crypto related
7575/// functionality.
7676pub mod ecdsa_crypto {
77- use super :: { BeefyAuthorityId , Hash , RuntimeAppPublic , KEY_TYPE as BEEFY_KEY_TYPE } ;
77+ use super :: { BeefyAuthorityId , Hash , RuntimeAppPublic , KEY_TYPE } ;
7878 use sp_application_crypto:: { app_crypto, ecdsa} ;
7979 use sp_core:: crypto:: Wraps ;
80- app_crypto ! ( ecdsa, BEEFY_KEY_TYPE ) ;
80+
81+ app_crypto ! ( ecdsa, KEY_TYPE ) ;
8182
8283 /// Identity of a BEEFY authority using ECDSA as its crypto.
8384 pub type AuthorityId = Public ;
@@ -115,10 +116,11 @@ pub mod ecdsa_crypto {
115116
116117#[ cfg( feature = "bls-experimental" ) ]
117118pub mod bls_crypto {
118- use super :: { BeefyAuthorityId , Hash , RuntimeAppPublic , KEY_TYPE as BEEFY_KEY_TYPE } ;
119+ use super :: { BeefyAuthorityId , Hash , RuntimeAppPublic , KEY_TYPE } ;
119120 use sp_application_crypto:: { app_crypto, bls377} ;
120121 use sp_core:: { bls377:: Pair as BlsPair , crypto:: Wraps , Pair as _} ;
121- app_crypto ! ( bls377, BEEFY_KEY_TYPE ) ;
122+
123+ app_crypto ! ( bls377, KEY_TYPE ) ;
122124
123125 /// Identity of a BEEFY authority using BLS as its crypto.
124126 pub type AuthorityId = Public ;
@@ -140,6 +142,46 @@ pub mod bls_crypto {
140142 }
141143 }
142144}
145+
146+ /// BEEFY cryptographic types for (ECDSA,BLS) crypto pair
147+ ///
148+ /// This module basically introduces four crypto types:
149+ /// - `ecdsa_bls_crypto::Pair`
150+ /// - `ecdsa_bls_crypto::Public`
151+ /// - `ecdsa_bls_crypto::Signature`
152+ /// - `ecdsa_bls_crypto::AuthorityId`
153+ ///
154+ /// Your code should use the above types as concrete types for all crypto related
155+ /// functionality.
156+ #[ cfg( feature = "bls-experimental" ) ]
157+ pub mod ecdsa_bls_crypto {
158+ use super :: { BeefyAuthorityId , Hash , RuntimeAppPublic , KEY_TYPE } ;
159+ use sp_application_crypto:: { app_crypto, ecdsa_bls377} ;
160+ use sp_core:: { crypto:: Wraps , ecdsa_bls377:: Pair as EcdsaBlsPair , Pair as _} ;
161+
162+ app_crypto ! ( ecdsa_bls377, KEY_TYPE ) ;
163+
164+ /// Identity of a BEEFY authority using (ECDSA,BLS) as its crypto.
165+ pub type AuthorityId = Public ;
166+
167+ /// Signature for a BEEFY authority using (ECDSA,BLS) as its crypto.
168+ pub type AuthoritySignature = Signature ;
169+
170+ impl < MsgHash : Hash > BeefyAuthorityId < MsgHash > for AuthorityId
171+ where
172+ <MsgHash as Hash >:: Output : Into < [ u8 ; 32 ] > ,
173+ {
174+ fn verify ( & self , signature : & <Self as RuntimeAppPublic >:: Signature , msg : & [ u8 ] ) -> bool {
175+ // `w3f-bls` library uses IETF hashing standard and as such does not exposes
176+ // a choice of hash to field function.
177+ // We are directly calling into the library to avoid introducing new host call.
178+ // and because BeefyAuthorityId::verify is being called in the runtime so we don't have
179+
180+ EcdsaBlsPair :: verify ( signature. as_inner_ref ( ) , msg, self . as_inner_ref ( ) )
181+ }
182+ }
183+ }
184+
143185/// The `ConsensusEngineId` of BEEFY.
144186pub const BEEFY_ENGINE_ID : sp_runtime:: ConsensusEngineId = * b"BEEF" ;
145187
@@ -458,4 +500,20 @@ mod tests {
458500 let ( other_pair, _) = bls_crypto:: Pair :: generate ( ) ;
459501 assert ! ( !BeefyAuthorityId :: <Keccak256 >:: verify( & other_pair. public( ) , & signature, msg, ) ) ;
460502 }
503+
504+ #[ test]
505+ #[ cfg( feature = "bls-experimental" ) ]
506+ fn ecdsa_bls_beefy_verify_works ( ) {
507+ let msg = & b"test-message" [ ..] ;
508+ let ( pair, _) = ecdsa_bls_crypto:: Pair :: generate ( ) ;
509+
510+ let signature: ecdsa_bls_crypto:: Signature = pair. as_inner_ref ( ) . sign ( & msg) . into ( ) ;
511+
512+ // Verification works if same hashing function is used when signing and verifying.
513+ assert ! ( BeefyAuthorityId :: <Keccak256 >:: verify( & pair. public( ) , & signature, msg) ) ;
514+
515+ // Other public key doesn't work
516+ let ( other_pair, _) = ecdsa_bls_crypto:: Pair :: generate ( ) ;
517+ assert ! ( !BeefyAuthorityId :: <Keccak256 >:: verify( & other_pair. public( ) , & signature, msg, ) ) ;
518+ }
461519}
0 commit comments