Skip to content

Commit 76a6d47

Browse files
drskalmandavxy
andauthored
Migrate BEEFY BLS crypto to bls12-381 curve (#4931)
We are definitely going to use BLS12-381 for BEEFY and it is hard coded in JAM's spec. This PR implements missing tests for bls12-381 crypto, migrate BEEFY BLS crypto to bls12-381 and adapt the BEEFY primitive tests accordingly. --------- Co-authored-by: Davide Galassi <davxy@datawok.net>
1 parent 7f332cd commit 76a6d47

File tree

16 files changed

+460
-368
lines changed

16 files changed

+460
-368
lines changed

substrate/client/consensus/beefy/src/keystore.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use log::warn;
2020

2121
use sp_application_crypto::{key_types::BEEFY as BEEFY_KEY_TYPE, AppCrypto, RuntimeAppPublic};
2222
#[cfg(feature = "bls-experimental")]
23-
use sp_core::ecdsa_bls377;
23+
use sp_core::ecdsa_bls381;
2424
use sp_core::{ecdsa, keccak_256};
2525

2626
use sp_keystore::KeystorePtr;
@@ -100,13 +100,13 @@ impl<AuthorityId: AuthorityIdBound> BeefyKeystore<AuthorityId> {
100100
},
101101

102102
#[cfg(feature = "bls-experimental")]
103-
ecdsa_bls377::CRYPTO_ID => {
104-
let public: ecdsa_bls377::Public =
105-
ecdsa_bls377::Public::try_from(public.as_slice()).unwrap();
103+
ecdsa_bls381::CRYPTO_ID => {
104+
let public: ecdsa_bls381::Public =
105+
ecdsa_bls381::Public::try_from(public.as_slice()).unwrap();
106106
let sig = store
107-
.ecdsa_bls377_sign_with_keccak256(BEEFY_KEY_TYPE, &public, &message)
107+
.ecdsa_bls381_sign_with_keccak256(BEEFY_KEY_TYPE, &public, &message)
108108
.map_err(|e| error::Error::Keystore(e.to_string()))?
109-
.ok_or_else(|| error::Error::Signature("bls377_sign() failed".to_string()))?;
109+
.ok_or_else(|| error::Error::Signature("bls381_sign() failed".to_string()))?;
110110
let sig_ref: &[u8] = sig.as_ref();
111111
sig_ref.to_vec()
112112
},
@@ -146,8 +146,8 @@ impl<AuthorityId: AuthorityIdBound> BeefyKeystore<AuthorityId> {
146146
}),
147147

148148
#[cfg(feature = "bls-experimental")]
149-
ecdsa_bls377::CRYPTO_ID => store
150-
.ecdsa_bls377_public_keys(BEEFY_KEY_TYPE)
149+
ecdsa_bls381::CRYPTO_ID => store
150+
.ecdsa_bls381_public_keys(BEEFY_KEY_TYPE)
151151
.drain(..)
152152
.map(|pk| AuthorityId::try_from(pk.as_ref()))
153153
.collect::<Result<Vec<_>, _>>()
@@ -254,9 +254,9 @@ pub mod tests {
254254
AuthorityId::decode(&mut pk.as_ref()).unwrap()
255255
},
256256
#[cfg(feature = "bls-experimental")]
257-
ecdsa_bls377::CRYPTO_ID => {
257+
ecdsa_bls381::CRYPTO_ID => {
258258
let pk = store
259-
.ecdsa_bls377_generate_new(key_type, optional_seed.as_deref())
259+
.ecdsa_bls381_generate_new(key_type, optional_seed.as_deref())
260260
.ok()
261261
.unwrap();
262262
AuthorityId::decode(&mut pk.as_ref()).unwrap()
@@ -452,7 +452,7 @@ pub mod tests {
452452
#[cfg(feature = "bls-experimental")]
453453
#[test]
454454
fn sign_error_for_ecdsa_n_bls() {
455-
sign_error::<ecdsa_bls_crypto::AuthorityId>("bls377_sign() failed");
455+
sign_error::<ecdsa_bls_crypto::AuthorityId>("bls381_sign() failed");
456456
}
457457

458458
#[test]

substrate/client/keystore/src/local.rs

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use sp_core::bandersnatch;
3737
}
3838

3939
sp_keystore::bls_experimental_enabled! {
40-
use sp_core::{bls377, bls381, ecdsa_bls377, KeccakHasher};
40+
use sp_core::{bls381, ecdsa_bls381, KeccakHasher};
4141
}
4242

4343
use crate::{Error, Result};
@@ -357,68 +357,42 @@ impl Keystore for LocalKeystore {
357357
self.sign::<bls381::Pair>(key_type, public, msg)
358358
}
359359

360-
fn bls377_public_keys(&self, key_type: KeyTypeId) -> Vec<bls377::Public> {
361-
self.public_keys::<bls377::Pair>(key_type)
360+
fn ecdsa_bls381_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa_bls381::Public> {
361+
self.public_keys::<ecdsa_bls381::Pair>(key_type)
362362
}
363363

364-
/// Generate a new pair compatible with the 'bls377' signature scheme.
364+
/// Generate a new pair of paired-keys compatible with the '(ecdsa,bls381)' signature scheme.
365365
///
366366
/// If `[seed]` is `Some` then the key will be ephemeral and stored in memory.
367-
fn bls377_generate_new(
367+
fn ecdsa_bls381_generate_new(
368368
&self,
369369
key_type: KeyTypeId,
370370
seed: Option<&str>,
371-
) -> std::result::Result<bls377::Public, TraitError> {
372-
self.generate_new::<bls377::Pair>(key_type, seed)
371+
) -> std::result::Result<ecdsa_bls381::Public, TraitError> {
372+
self.generate_new::<ecdsa_bls381::Pair>(key_type, seed)
373373
}
374374

375-
fn bls377_sign(
375+
fn ecdsa_bls381_sign(
376376
&self,
377377
key_type: KeyTypeId,
378-
public: &bls377::Public,
378+
public: &ecdsa_bls381::Public,
379379
msg: &[u8],
380-
) -> std::result::Result<Option<bls377::Signature>, TraitError> {
381-
self.sign::<bls377::Pair>(key_type, public, msg)
380+
) -> std::result::Result<Option<ecdsa_bls381::Signature>, TraitError> {
381+
self.sign::<ecdsa_bls381::Pair>(key_type, public, msg)
382382
}
383383

384-
fn ecdsa_bls377_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa_bls377::Public> {
385-
self.public_keys::<ecdsa_bls377::Pair>(key_type)
386-
}
387-
388-
/// Generate a new pair of paired-keys compatible with the '(ecdsa,bls377)' signature scheme.
389-
///
390-
/// If `[seed]` is `Some` then the key will be ephemeral and stored in memory.
391-
fn ecdsa_bls377_generate_new(
392-
&self,
393-
key_type: KeyTypeId,
394-
seed: Option<&str>,
395-
) -> std::result::Result<ecdsa_bls377::Public, TraitError> {
396-
self.generate_new::<ecdsa_bls377::Pair>(key_type, seed)
397-
}
398-
399-
fn ecdsa_bls377_sign(
400-
&self,
401-
key_type: KeyTypeId,
402-
public: &ecdsa_bls377::Public,
403-
msg: &[u8],
404-
) -> std::result::Result<Option<ecdsa_bls377::Signature>, TraitError> {
405-
self.sign::<ecdsa_bls377::Pair>(key_type, public, msg)
406-
}
407-
408-
fn ecdsa_bls377_sign_with_keccak256(
384+
fn ecdsa_bls381_sign_with_keccak256(
409385
&self,
410386
key_type: KeyTypeId,
411-
public: &ecdsa_bls377::Public,
387+
public: &ecdsa_bls381::Public,
412388
msg: &[u8],
413-
) -> std::result::Result<Option<ecdsa_bls377::Signature>, TraitError> {
389+
) -> std::result::Result<Option<ecdsa_bls381::Signature>, TraitError> {
414390
let sig = self.0
415391
.read()
416-
.key_pair_by_type::<ecdsa_bls377::Pair>(public, key_type)?
392+
.key_pair_by_type::<ecdsa_bls381::Pair>(public, key_type)?
417393
.map(|pair| pair.sign_with_hasher::<KeccakHasher>(msg));
418394
Ok(sig)
419395
}
420-
421-
422396
}
423397
}
424398

substrate/primitives/application-crypto/src/bls377.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

substrate/primitives/application-crypto/src/bls381.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
// limitations under the License.
1717

1818
//! BLS12-381 crypto applications.
19+
use crate::{KeyTypeId, RuntimePublic};
20+
21+
use alloc::vec::Vec;
1922

2023
pub use sp_core::bls::bls381::*;
2124

@@ -26,3 +29,30 @@ mod app {
2629
#[cfg(feature = "full_crypto")]
2730
pub use app::Pair as AppPair;
2831
pub use app::{Public as AppPublic, Signature as AppSignature};
32+
33+
impl RuntimePublic for Public {
34+
type Signature = Signature;
35+
36+
/// Dummy implementation. Returns an empty vector.
37+
fn all(_key_type: KeyTypeId) -> Vec<Self> {
38+
Vec::new()
39+
}
40+
41+
fn generate_pair(key_type: KeyTypeId, seed: Option<Vec<u8>>) -> Self {
42+
sp_io::crypto::bls381_generate(key_type, seed)
43+
}
44+
45+
/// Dummy implementation. Returns `None`.
46+
fn sign<M: AsRef<[u8]>>(&self, _key_type: KeyTypeId, _msg: &M) -> Option<Self::Signature> {
47+
None
48+
}
49+
50+
/// Dummy implementation. Returns `false`.
51+
fn verify<M: AsRef<[u8]>>(&self, _msg: &M, _signature: &Self::Signature) -> bool {
52+
false
53+
}
54+
55+
fn to_raw_vec(&self) -> Vec<u8> {
56+
sp_core::crypto::ByteArray::to_raw_vec(self)
57+
}
58+
}

substrate/primitives/application-crypto/src/ecdsa_bls377.rs renamed to substrate/primitives/application-crypto/src/ecdsa_bls381.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
//! ECDSA and BLS12-377 paired crypto applications.
18+
//! ECDSA and BLS12-381 paired crypto applications.
1919
2020
use crate::{KeyTypeId, RuntimePublic};
2121
use alloc::vec::Vec;
2222

23-
pub use sp_core::paired_crypto::ecdsa_bls377::*;
23+
pub use sp_core::paired_crypto::ecdsa_bls381::*;
2424

2525
mod app {
26-
crate::app_crypto!(super, sp_core::testing::ECDSA_BLS377);
26+
crate::app_crypto!(super, sp_core::testing::ECDSA_BLS381);
2727
}
2828

2929
#[cfg(feature = "full_crypto")]
@@ -39,7 +39,7 @@ impl RuntimePublic for Public {
3939
}
4040

4141
fn generate_pair(key_type: KeyTypeId, seed: Option<Vec<u8>>) -> Self {
42-
sp_io::crypto::ecdsa_bls377_generate(key_type, seed)
42+
sp_io::crypto::ecdsa_bls381_generate(key_type, seed)
4343
}
4444

4545
/// Dummy implementation. Returns `None`.

substrate/primitives/application-crypto/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ pub use serde;
4747
#[cfg(feature = "bandersnatch-experimental")]
4848
pub mod bandersnatch;
4949
#[cfg(feature = "bls-experimental")]
50-
pub mod bls377;
51-
#[cfg(feature = "bls-experimental")]
5250
pub mod bls381;
5351
pub mod ecdsa;
5452
#[cfg(feature = "bls-experimental")]
55-
pub mod ecdsa_bls377;
53+
pub mod ecdsa_bls381;
5654
pub mod ed25519;
5755
pub mod sr25519;
5856
mod traits;

substrate/primitives/consensus/beefy/src/commitment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ mod tests {
482482
assert_eq!(
483483
encoded,
484484
array_bytes::hex2bytes_unchecked(
485-
"046d68343048656c6c6f20576f726c642105000000000000000000000000000000000000000000000004300400000008558455ad81279df0795cc985580e4fb75d72d948d1107b2ac80a09abed4da8480c746cc321f2319a5e99a830e314d10dd3cd68ce3dc0c33c86e99bcb7816f9ba015dd1c9b2237e54baa93d232cdf83a430b58a5efbc2f86ca1bab173a315ff6f15bef161425750c028055e9a23947b73002889a8b22168628438875a8ef25d76db998a80187b50719471286f054f3b3809b77a0cd87d7fe9c1a9d5d562683e25a70610f0804e92340549a43a7159b77b0c2d6e1f8105c337a86cdd9aaacdc496577f3db8c55ef9e6fd48f2c5c05a2274707491635d8ba3df64f324575b7b2a34487bca2324b6a0046395a71681be3d0c2a001074884b6998c82331bd57ffa0a02cbfd02483c765b9216eab6a1fc119206236bf7971be68acaebff7400edee943240006a6096c9cfa65e9eb4e67f025c27112d14b4574fb208c439500f45cf3a8060f6cf009044f3141cce0364a7c2710a19b1bdf4abf27f86e5e3db08bddd35a7d12"
485+
"046d68343048656c6c6f20576f726c642105000000000000000000000000000000000000000000000004300400000008558455ad81279df0795cc985580e4fb75d72d948d1107b2ac80a09abed4da8480c746cc321f2319a5e99a830e314d10dd3cd68ce3dc0c33c86e99bcb7816f9ba0182022df4689ef25499205f7154a1a62eb2d6d5c4a3657efed321e2c277998130d1b01a264c928afb79534cb0fa9dcf79f67ed4e6bf2de576bb936146f2fa60fa56b8651677cc764ea4fe317c62294c2a0c5966e439653eed0572fded5e2461c888518e0769718dcce9f3ff612fb89d262d6e1f8105c337a86cdd9aaacdc496577f3db8c55ef9e6fd48f2c5c05a2274707491635d8ba3df64f324575b7b2a34487bca2324b6a0046395a71681be3d0c2a00a90973bea76fac3a4e2d76a25ec3926d6a5a20aacee15ec0756cd268088ed5612b67b4a49349cee70bc1185078d17c7f7df9d944e8be30022d9680d0437c4ba4600d74050692e8ee9b96e37df2a39d1cb4b4af4b6a058342dd9e8c7481a3a0b8975ad8614c953e950253aa327698d842"
486486
)
487487
);
488488
}

substrate/primitives/consensus/beefy/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ pub mod ecdsa_crypto {
142142
#[cfg(feature = "bls-experimental")]
143143
pub mod bls_crypto {
144144
use super::{AuthorityIdBound, BeefyAuthorityId, Hash, RuntimeAppPublic, KEY_TYPE};
145-
use sp_application_crypto::{app_crypto, bls377};
146-
use sp_core::{bls377::Pair as BlsPair, crypto::Wraps, Pair as _};
145+
use sp_application_crypto::{app_crypto, bls381};
146+
use sp_core::{bls381::Pair as BlsPair, crypto::Wraps, Pair as _};
147147

148-
app_crypto!(bls377, KEY_TYPE);
148+
app_crypto!(bls381, KEY_TYPE);
149149

150150
/// Identity of a BEEFY authority using BLS as its crypto.
151151
pub type AuthorityId = Public;
@@ -184,10 +184,10 @@ pub mod bls_crypto {
184184
#[cfg(feature = "bls-experimental")]
185185
pub mod ecdsa_bls_crypto {
186186
use super::{AuthorityIdBound, BeefyAuthorityId, Hash, RuntimeAppPublic, KEY_TYPE};
187-
use sp_application_crypto::{app_crypto, ecdsa_bls377};
188-
use sp_core::{crypto::Wraps, ecdsa_bls377::Pair as EcdsaBlsPair};
187+
use sp_application_crypto::{app_crypto, ecdsa_bls381};
188+
use sp_core::{crypto::Wraps, ecdsa_bls381::Pair as EcdsaBlsPair};
189189

190-
app_crypto!(ecdsa_bls377, KEY_TYPE);
190+
app_crypto!(ecdsa_bls381, KEY_TYPE);
191191

192192
/// Identity of a BEEFY authority using (ECDSA,BLS) as its crypto.
193193
pub type AuthorityId = Public;

substrate/primitives/consensus/beefy/src/witness.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ mod tests {
9090
#[cfg(feature = "bls-experimental")]
9191
use w3f_bls::{
9292
single_pop_aggregator::SignatureAggregatorAssumingPoP, Message, SerializableToBytes,
93-
Signed, TinyBLS377,
93+
Signed, TinyBLS381,
9494
};
9595

9696
type TestCommitment = Commitment<u128>;
@@ -198,15 +198,15 @@ mod tests {
198198
// from signed take a function as the aggregator
199199
TestBlsSignedCommitmentWitness::from_signed::<_, _>(signed, |sigs| {
200200
// we are going to aggregate the signatures here
201-
let mut aggregatedsigs: SignatureAggregatorAssumingPoP<TinyBLS377> =
201+
let mut aggregatedsigs: SignatureAggregatorAssumingPoP<TinyBLS381> =
202202
SignatureAggregatorAssumingPoP::new(Message::new(b"", b"mock payload"));
203203

204204
for sig in sigs {
205205
match sig {
206206
Some(sig) => {
207207
let serialized_sig : Vec<u8> = (*sig.1).to_vec();
208208
aggregatedsigs.add_signature(
209-
&w3f_bls::Signature::<TinyBLS377>::from_bytes(
209+
&w3f_bls::Signature::<TinyBLS381>::from_bytes(
210210
serialized_sig.as_slice()
211211
).unwrap()
212212
);
@@ -219,7 +219,7 @@ mod tests {
219219

220220
// We can't use BlsSignature::try_from because it expected 112Bytes (CP (64) + BLS 48)
221221
// single signature while we are having a BLS aggregated signature corresponding to no CP.
222-
w3f_bls::Signature::<TinyBLS377>::from_bytes(witness.signature_accumulator.as_slice())
222+
w3f_bls::Signature::<TinyBLS381>::from_bytes(witness.signature_accumulator.as_slice())
223223
.unwrap();
224224
}
225225

0 commit comments

Comments
 (0)