Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

support crypto primitives for no_std introducing full_crypto feature #3778

Merged
merged 27 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9b8a283
introduced "with_crypto" feature and applied switches like in substra…
Sep 30, 2019
9edc07e
introduced "with_crypto" feature and applied switches like in substra…
Sep 30, 2019
7097676
distinguishing core::hash vs std::hash
Sep 30, 2019
48e6e34
Merge branch 'master' into brenzi-no-std-crypto
brenzi Oct 10, 2019
5ab8bb2
@bkchr's review requests fulfilled
Oct 30, 2019
a4e2619
Merge branch 'master' into brenzi-no-std-crypto
brenzi Oct 30, 2019
ae0c870
fixes
Oct 30, 2019
cc60d56
revert dependency upgrade ed25519-dalek
Oct 30, 2019
233b026
added full_crypto features to all crates using app_crypto! macro
Oct 30, 2019
53ec53a
fixing CI complaints.
Oct 30, 2019
f6c5040
fix again
Oct 30, 2019
aa96e0f
adding CI test for with_crypto feature
Oct 30, 2019
f83bfe3
added full_crypto for ecdsa. now builds wit h--no-deafault-features -…
Oct 30, 2019
c6ee791
remove --release from CI test
Oct 30, 2019
536d6c3
@bkchr requested changes. moved full_crypto CI test to build stage
Oct 30, 2019
291baeb
fixing no_std issue
Oct 30, 2019
bcc7e32
CI fresh copy from srml-staking
Oct 30, 2019
a13cf9a
gitlab CI with +nightly
Oct 30, 2019
7f25925
solved no-feature-in-macro dilemma
Oct 31, 2019
104e5ca
cosmetics
Oct 31, 2019
b579288
Update core/application-crypto/src/sr25519.rs
brenzi Oct 31, 2019
f38ec5f
Update core/application-crypto/src/ed25519.rs
brenzi Oct 31, 2019
272a8af
even more simple
Oct 31, 2019
42d0270
undo line delete
Oct 31, 2019
1c721f2
refactoring app_crypto macro. splitting functionalities based on full…
Nov 3, 2019
99ef109
Merge branch 'master' into brenzi-no-std-crypto
brenzi Nov 3, 2019
d36abf5
whitespace cosmetics
Nov 4, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ test-srml-staking: &test-srml-staking
- sccache -s






test-linux-stable-int:
<<: *test-linux
except:
Expand Down Expand Up @@ -275,6 +271,25 @@ build-linux-subkey:
- cp -r scripts/docker/subkey.Dockerfile ./artifacts/subkey/
- sccache -s

build-full-crypto-feature:
brenzi marked this conversation as resolved.
Show resolved Hide resolved
stage: build
<<: *collect-artifacts
<<: *docker-env
<<: *build-only
variables:
# Enable debug assertions since we are running optimized builds for testing
# but still want to have debug assertions.
RUST_BACKTRACE: 1
except:
variables:
- $DEPLOY_TAG
script:
- cd core/primitives/
- time cargo build --verbose --no-default-features --features full_crypto
- cd ../application-crypto
- time cargo build --verbose --no-default-features --features full_crypto
- sccache -s

build-rust-doc-release:
stage: build
<<: *docker-env
Expand Down
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion core/application-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ sr-primitives = { path = "../sr-primitives" }

[features]
default = [ "std" ]
std = [ "primitives/std", "codec/std", "serde", "rstd/std", "runtime-io/std" ]
std = [ "full_crypto", "primitives/std", "codec/std", "serde", "rstd/std", "runtime-io/std" ]

# This feature enables all crypto primitives for `no_std` builds like microcontrollers
# or Intel SGX.
# For the regular wasm runtime builds this should not be used.
full_crypto = [
"primitives/full_crypto"
]
3 changes: 2 additions & 1 deletion core/application-crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{RuntimePublic, KeyTypeId};
pub use primitives::ed25519::*;

mod app {
use rstd::vec::Vec;
brenzi marked this conversation as resolved.
Show resolved Hide resolved
use primitives::testing::ED25519;
crate::app_crypto!(super, ED25519);

Expand All @@ -31,7 +32,7 @@ mod app {

pub use app::Public as AppPublic;
pub use app::Signature as AppSignature;
#[cfg(feature="std")]
#[cfg(feature = "full_crypto")]
pub use app::Pair as AppPair;

impl RuntimePublic for Public {
Expand Down
24 changes: 14 additions & 10 deletions core/application-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#[doc(hidden)]
pub use primitives::{self, crypto::{CryptoType, Public, Derive, IsWrappedBy, Wraps}, RuntimeDebug};
#[doc(hidden)]
#[cfg(feature = "std")]
#[cfg(feature = "full_crypto")]
pub use primitives::crypto::{SecretStringError, DeriveJunction, Ss58Codec, Pair};
pub use primitives::{crypto::{KeyTypeId, key_types}};

Expand Down Expand Up @@ -53,9 +53,9 @@ pub use traits::*;
#[macro_export]
macro_rules! app_crypto {
brenzi marked this conversation as resolved.
Show resolved Hide resolved
($module:ident, $key_type:expr) => {
#[cfg(feature="std")]
#[cfg(feature = "full_crypto")]
$crate::app_crypto!($module::Pair, $module::Public, $module::Signature, $key_type);
#[cfg(not(feature="std"))]
#[cfg(not(feature = "full_crypto"))]
$crate::app_crypto!($module::Public, $module::Signature, $key_type);
};
($pair:ty, $public:ty, $sig:ty, $key_type:expr) => {
Expand All @@ -71,16 +71,19 @@ macro_rules! app_crypto {
type Pair = Pair;
}

#[cfg(feature = "std")]
#[cfg(feature = "full_crypto")]
impl $crate::Pair for Pair {
type Public = Public;
type Seed = <$pair as $crate::Pair>::Seed;
type Signature = Signature;
type DeriveError = <$pair as $crate::Pair>::DeriveError;

#[cfg(feature = "std")]
fn generate_with_phrase(password: Option<&str>) -> (Self, String, Self::Seed) {
let r = <$pair>::generate_with_phrase(password);
(Self(r.0), r.1, r.2)
}
#[cfg(feature = "std")]
fn from_phrase(phrase: &str, password: Option<&str>)
-> Result<(Self, Self::Seed), $crate::SecretStringError>
{
Expand Down Expand Up @@ -135,7 +138,7 @@ macro_rules! app_crypto {
$crate::codec::Decode,
$crate::RuntimeDebug,
)]
#[cfg_attr(feature = "std", derive(Hash))]
#[cfg_attr(feature = "full_crypto", derive(Hash))]
pub struct Public($public);
}

Expand Down Expand Up @@ -184,7 +187,7 @@ macro_rules! app_crypto {
}

impl $crate::CryptoType for Public {
#[cfg(feature="std")]
#[cfg(feature = "full_crypto")]
type Pair = Pair;
}

Expand All @@ -195,7 +198,7 @@ macro_rules! app_crypto {
impl $crate::AppKey for Public {
type UntypedGeneric = $public;
type Public = Public;
#[cfg(feature="std")]
#[cfg(feature = "full_crypto")]
type Pair = Pair;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
Expand Down Expand Up @@ -237,7 +240,8 @@ macro_rules! app_crypto {
$crate::codec::Decode,
$crate::RuntimeDebug,
)]
#[cfg_attr(feature = "std", derive(Hash))]
#[cfg_attr(feature = "full_crypto", derive(Hash))]

brenzi marked this conversation as resolved.
Show resolved Hide resolved
pub struct Signature($sig);
}

Expand All @@ -252,14 +256,14 @@ macro_rules! app_crypto {
}

impl $crate::CryptoType for Signature {
#[cfg(feature="std")]
#[cfg(feature = "full_crypto")]
type Pair = Pair;
}

impl $crate::AppKey for Signature {
type UntypedGeneric = $sig;
type Public = Public;
#[cfg(feature="std")]
#[cfg(feature = "full_crypto")]
type Pair = Pair;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
Expand Down
3 changes: 2 additions & 1 deletion core/application-crypto/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{RuntimePublic, KeyTypeId};
pub use primitives::sr25519::*;

mod app {
use rstd::vec::Vec;
brenzi marked this conversation as resolved.
Show resolved Hide resolved
use primitives::testing::SR25519;
crate::app_crypto!(super, SR25519);

Expand All @@ -31,7 +32,7 @@ mod app {

pub use app::Public as AppPublic;
pub use app::Signature as AppSignature;
#[cfg(feature="std")]
#[cfg(feature = "full_crypto")]
pub use app::Pair as AppPair;

impl RuntimePublic for Public {
Expand Down
20 changes: 13 additions & 7 deletions core/application-crypto/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

#[cfg(feature = "std")]
#[cfg(feature = "full_crypto")]
use primitives::crypto::Pair;

use codec::Codec;
Expand All @@ -30,7 +30,7 @@ pub trait AppKey: 'static + Send + Sync + Sized + CryptoType + Clone {
type Public: AppPublic;

/// The corresponding key pair type in this application scheme.
#[cfg(feature="std")]
#[cfg(feature = "full_crypto")]
type Pair: AppPair;

/// The corresponding signature type in this application scheme.
Expand All @@ -42,16 +42,22 @@ pub trait AppKey: 'static + Send + Sync + Sized + CryptoType + Clone {

/// Type which implements Hash in std, not when no-std (std variant).
#[cfg(feature = "std")]
pub trait MaybeHash: std::hash::Hash {}
pub trait MaybeHash: rstd::hash::Hash {}
#[cfg(feature = "std")]
impl<T: std::hash::Hash> MaybeHash for T {}
impl<T: rstd::hash::Hash> MaybeHash for T {}

/// Type which implements Hash in std, not when no-std (no-std variant).
#[cfg(not(feature = "std"))]
#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
pub trait MaybeHash {}
#[cfg(not(feature = "std"))]
#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
impl<T> MaybeHash for T {}

/// Type which implements Debug and Hash in std, not when no-std (no-std variant with crypto).
#[cfg(all(not(feature = "std"), feature = "full_crypto"))]
pub trait MaybeDebugHash: rstd::hash::Hash {}
#[cfg(all(not(feature = "std"), feature = "full_crypto"))]
impl<T: rstd::hash::Hash> MaybeDebugHash for T {}

/// A application's public key.
pub trait AppPublic:
AppKey + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + codec::Codec
Expand All @@ -62,7 +68,7 @@ pub trait AppPublic:
}

/// A application's key pair.
#[cfg(feature = "std")]
#[cfg(feature = "full_crypto")]
pub trait AppPair: AppKey + Pair<Public=<Self as AppKey>::Public> {
/// The wrapped type which is just a plain instance of `Pair`.
type Generic: IsWrappedBy<Self> + Pair<Public=<<Self as AppKey>::Public as AppPublic>::Generic>;
Expand Down
8 changes: 8 additions & 0 deletions core/consensus/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ test-client = { package = "substrate-test-runtime-client", path = "../../test-ru
tokio = "0.1.22"
env_logger = "0.7.0"
tempfile = "3.1.0"

[features]
default = ["full_crypto"]

# This feature enables all crypto primitives for `no_std` builds like microcontrollers
# or Intel SGX.
# For the regular wasm runtime builds this should not be used.
full_crypto = []
brenzi marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions core/consensus/aura/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ sr-primitives = { path = "../../../sr-primitives", default-features = false }
[features]
default = ["std"]
std = [
"full_crypto",
"rstd/std",
"codec/std",
"sr-primitives/std",
"substrate-client/std",
"app-crypto/std",
]

# This feature enables all crypto primitives for `no_std` builds like microcontrollers
# or Intel SGX.
# For the regular wasm runtime builds this should not be used.
full_crypto = []
6 changes: 6 additions & 0 deletions core/consensus/babe/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ codec = { package = "parity-scale-codec", version = "1.0.0", default-features =
[features]
default = ["std"]
std = [
"full_crypto",
"rstd/std",
"sr-primitives/std",
"substrate-client/std",
Expand All @@ -25,3 +26,8 @@ std = [
"slots",
"app-crypto/std",
]

# This feature enables all crypto primitives for `no_std` builds like microcontrollers
# or Intel SGX.
# For the regular wasm runtime builds this should not be used.
full_crypto = []
6 changes: 6 additions & 0 deletions core/finality-grandpa/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ serde = { version = "1.0.101", optional = true, features = ["derive"] }
[features]
default = ["std"]
std = [
"full_crypto",
"client/std",
"codec/std",
"sr-primitives/std",
"rstd/std",
"serde",
"app-crypto/std",
]

# This feature enables all crypto primitives for `no_std` builds like microcontrollers
# or Intel SGX.
# For the regular wasm runtime builds this should not be used.
full_crypto = []
Loading