Skip to content

Commit d64f24f

Browse files
drskalmandavxyandresilvaLederstrumpf
authored andcommitted
BLS Core Crypto attempt #2 (paritytech#13618)
* Cherry pick all crypto related changes from pull-request paritytech#13311 applied to master's head * Import some stuff just if 'full_crypto' is on * Remove copyright year * Cleanup * First generic BLS draft * Finalize generic implementation * Restore tests * Fix rust docs * Fix after master merge * Fix after master merge * Use double bls with G1 as signature group and verify individual signatures using DLEQ proof. * Fix inclusions and types used within substrate * Remove unused cruft * Restore usage of upstream crates * Fix test * Reduce the diff by aligning Cargo.lock to master * Application-crypto provides bls381 * Implement bls381 for local keystore * Use new generic keystore features * import DoublePublickey[Scheme] from the bls-like root to be less confusing. * fix compilation * Apply suggestions from code review Co-authored-by: Robert Hambrock <[email protected]> * Clean leftovers * - update bls test vector after applying spec change recommendation. - send message as ref. * Different hard junction ids for different bls12 types * update to new bls-like * bls-like → w3f-bls * Make clippy happy * update test vector after replacing hash and crop with hash to field. * cargo fmt * account for paritytech#13972 * hide BLS behind "bls_non_production" feature flag * Remove Cargo.lock entries duplicated in merge * add bls377 to primitives/keystore and client/keystore add bls377 to primitives/application-crypto/ add bls_non_production to primitives/keystore and client/keystore bump up w3f-bls version * rename feature `bls_non_production` to `bls-experimental` --------- Co-authored-by: Davide Galassi <[email protected]> Co-authored-by: André Silva <[email protected]> Co-authored-by: Robert Hambrock <[email protected]>
1 parent e217864 commit d64f24f

File tree

14 files changed

+1730
-614
lines changed

14 files changed

+1730
-614
lines changed

Cargo.lock

Lines changed: 772 additions & 608 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/keystore/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ targets = ["x86_64-unknown-linux-gnu"]
1515

1616
[dependencies]
1717
array-bytes = "4.1"
18-
async-trait = "0.1.57"
1918
parking_lot = "0.12.1"
2019
serde_json = "1.0.85"
2120
thiserror = "1.0"
@@ -25,3 +24,11 @@ sp-keystore = { version = "0.13.0", path = "../../primitives/keystore" }
2524

2625
[dev-dependencies]
2726
tempfile = "3.1.0"
27+
28+
[features]
29+
# This feature adds BLS crypto primitives. It should not be used in production since
30+
# the BLS implementation and interface may still be subject to significant change.
31+
bls-experimental = [
32+
"sp-core/bls-experimental",
33+
"sp-keystore/bls-experimental",
34+
]

client/keystore/src/local.rs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
2020
use parking_lot::RwLock;
2121
use sp_application_crypto::{AppCrypto, AppPair, IsWrappedBy};
22+
#[cfg(feature = "bls-experimental")]
23+
use sp_core::{bls377, bls381};
2224
use sp_core::{
2325
crypto::{ByteArray, ExposeSecret, KeyTypeId, Pair as CorePair, SecretString, VrfSecret},
2426
ecdsa, ed25519, sr25519,
@@ -134,7 +136,7 @@ impl Keystore for LocalKeystore {
134136

135137
/// Generate a new pair compatible with the 'ed25519' signature scheme.
136138
///
137-
/// If the `[seed]` is `Some` then the key will be ephemeral and stored in memory.
139+
/// If `[seed]` is `Some` then the key will be ephemeral and stored in memory.
138140
fn sr25519_generate_new(
139141
&self,
140142
key_type: KeyTypeId,
@@ -176,7 +178,7 @@ impl Keystore for LocalKeystore {
176178

177179
/// Generate a new pair compatible with the 'sr25519' signature scheme.
178180
///
179-
/// If the `[seed]` is `Some` then the key will be ephemeral and stored in memory.
181+
/// If `[seed]` is `Some` then the key will be ephemeral and stored in memory.
180182
fn ed25519_generate_new(
181183
&self,
182184
key_type: KeyTypeId,
@@ -200,7 +202,7 @@ impl Keystore for LocalKeystore {
200202

201203
/// Generate a new pair compatible with the 'ecdsa' signature scheme.
202204
///
203-
/// If the `[seed]` is `Some` then the key will be ephemeral and stored in memory.
205+
/// If `[seed]` is `Some` then the key will be ephemeral and stored in memory.
204206
fn ecdsa_generate_new(
205207
&self,
206208
key_type: KeyTypeId,
@@ -232,6 +234,60 @@ impl Keystore for LocalKeystore {
232234
Ok(sig)
233235
}
234236

237+
#[cfg(feature = "bls-experimental")]
238+
fn bls381_public_keys(&self, key_type: KeyTypeId) -> Vec<bls381::Public> {
239+
self.public_keys::<bls381::Pair>(key_type)
240+
}
241+
242+
#[cfg(feature = "bls-experimental")]
243+
/// Generate a new pair compatible with the 'bls381' signature scheme.
244+
///
245+
/// If `[seed]` is `Some` then the key will be ephemeral and stored in memory.
246+
fn bls381_generate_new(
247+
&self,
248+
key_type: KeyTypeId,
249+
seed: Option<&str>,
250+
) -> std::result::Result<bls381::Public, TraitError> {
251+
self.generate_new::<bls381::Pair>(key_type, seed)
252+
}
253+
254+
#[cfg(feature = "bls-experimental")]
255+
fn bls381_sign(
256+
&self,
257+
key_type: KeyTypeId,
258+
public: &bls381::Public,
259+
msg: &[u8],
260+
) -> std::result::Result<Option<bls381::Signature>, TraitError> {
261+
self.sign::<bls381::Pair>(key_type, public, msg)
262+
}
263+
264+
#[cfg(feature = "bls-experimental")]
265+
fn bls377_public_keys(&self, key_type: KeyTypeId) -> Vec<bls377::Public> {
266+
self.public_keys::<bls377::Pair>(key_type)
267+
}
268+
269+
#[cfg(feature = "bls-experimental")]
270+
/// Generate a new pair compatible with the 'bls377' signature scheme.
271+
///
272+
/// If `[seed]` is `Some` then the key will be ephemeral and stored in memory.
273+
fn bls377_generate_new(
274+
&self,
275+
key_type: KeyTypeId,
276+
seed: Option<&str>,
277+
) -> std::result::Result<bls377::Public, TraitError> {
278+
self.generate_new::<bls377::Pair>(key_type, seed)
279+
}
280+
281+
#[cfg(feature = "bls-experimental")]
282+
fn bls377_sign(
283+
&self,
284+
key_type: KeyTypeId,
285+
public: &bls377::Public,
286+
msg: &[u8],
287+
) -> std::result::Result<Option<bls377::Signature>, TraitError> {
288+
self.sign::<bls377::Pair>(key_type, public, msg)
289+
}
290+
235291
fn insert(
236292
&self,
237293
key_type: KeyTypeId,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! BLS12-377 crypto applications.
19+
20+
pub use sp_core::bls::bls377::*;
21+
22+
mod app {
23+
crate::app_crypto!(super, sp_core::testing::BLS377);
24+
}
25+
26+
#[cfg(feature = "full_crypto")]
27+
pub use app::Pair as AppPair;
28+
pub use app::{Public as AppPublic, Signature as AppSignature};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! BLS12-381 crypto applications.
19+
20+
pub use sp_core::bls::bls381::*;
21+
22+
mod app {
23+
crate::app_crypto!(super, sp_core::testing::BLS381);
24+
}
25+
26+
#[cfg(feature = "full_crypto")]
27+
pub use app::Pair as AppPair;
28+
pub use app::{Public as AppPublic, Signature as AppSignature};

primitives/application-crypto/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ pub use serde;
4141
#[doc(hidden)]
4242
pub use sp_std::{ops::Deref, vec::Vec};
4343

44+
#[cfg(feature = "bls-experimental")]
45+
pub mod bls377;
46+
#[cfg(feature = "bls-experimental")]
47+
pub mod bls381;
4448
pub mod ecdsa;
4549
pub mod ed25519;
4650
pub mod sr25519;

primitives/core/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ secp256k1 = { version = "0.24.0", default-features = false, features = ["recover
5252
ss58-registry = { version = "1.34.0", default-features = false }
5353
sp-core-hashing = { version = "5.0.0", path = "./hashing", default-features = false, optional = true }
5454
sp-runtime-interface = { version = "7.0.0", default-features = false, path = "../runtime-interface" }
55+
# bls crypto
56+
w3f-bls = { version = "0.1.3", default-features = false, optional = true}
5557

5658
[dev-dependencies]
5759
sp-serializer = { version = "4.0.0-dev", path = "../serializer" }
5860
rand = "0.8.5"
5961
criterion = "0.4.0"
6062
serde_json = "1.0"
6163
sp-core-hashing-proc-macro = { version = "5.0.0", path = "./hashing/proc-macro" }
64+
hex-literal = "0.3.4"
6265

6366
[[bench]]
6467
name = "bench"
@@ -125,3 +128,7 @@ full_crypto = [
125128
"sp-core-hashing",
126129
"sp-runtime-interface/disable_target_static_assertions",
127130
]
131+
132+
# This feature adds BLS crypto primitives. It should not be used in production since
133+
# the BLS implementation and interface may still be subject to significant change.
134+
bls-experimental = ["w3f-bls"]

0 commit comments

Comments
 (0)