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 4 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
12 changes: 9 additions & 3 deletions Cargo.lock

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

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

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

with_crypto = [
"primitives/with_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,13 +21,14 @@ use crate::{RuntimePublic, KeyTypeId};
pub use primitives::ed25519::*;

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

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

impl RuntimePublic for Public {
Expand Down
26 changes: 16 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}};
#[doc(hidden)]
#[cfg(feature = "std")]
#[cfg(feature = "with_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 = "with_crypto")]
$crate::app_crypto!($module::Pair, $module::Public, $module::Signature, $key_type);
#[cfg(not(feature="std"))]
#[cfg(not(feature = "with_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 = "with_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 All @@ -95,6 +98,7 @@ macro_rules! app_crypto {
fn from_seed_slice(seed: &[u8]) -> Result<Self, $crate::SecretStringError> {
<$pair>::from_seed_slice(seed).map(Self)
}
#[cfg(feature = "std")]
fn from_standard_components<
I: Iterator<Item=$crate::DeriveJunction>
>(
Expand Down Expand Up @@ -142,7 +146,8 @@ macro_rules! app_crypto {
Clone, Default, Eq, PartialEq, Ord, PartialOrd, $crate::codec::Encode,
$crate::codec::Decode,
)]
#[cfg_attr(feature = "std", derive(Debug, Hash))]
#[cfg_attr(feature = "std", derive(Debug))]
#[cfg_attr(feature = "with_crypto", derive(Hash))]
pub struct Public($public);
}

Expand Down Expand Up @@ -191,7 +196,7 @@ macro_rules! app_crypto {
}

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

Expand All @@ -202,7 +207,7 @@ macro_rules! app_crypto {
impl $crate::AppKey for Public {
type UntypedGeneric = $public;
type Public = Public;
#[cfg(feature="std")]
#[cfg(feature = "with_crypto")]
type Pair = Pair;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
Expand Down Expand Up @@ -240,7 +245,8 @@ macro_rules! app_crypto {
$crate::wrap! {
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
#[derive(Clone, Default, Eq, PartialEq, $crate::codec::Encode, $crate::codec::Decode)]
#[cfg_attr(feature = "std", derive(Debug, Hash))]
#[cfg_attr(feature = "std", derive(Debug))]
#[cfg_attr(feature = "with_crypto", derive(Hash))]
pub struct Signature($sig);
}

Expand All @@ -255,14 +261,14 @@ macro_rules! app_crypto {
}

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

impl $crate::AppKey for Signature {
type UntypedGeneric = $sig;
type Public = Public;
#[cfg(feature="std")]
#[cfg(feature = "with_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,13 +21,14 @@ use crate::{RuntimePublic, KeyTypeId};
pub use primitives::sr25519::*;

mod app {
use crate::Vec;
use primitives::testing::SR25519;
crate::app_crypto!(super, SR25519);
}

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

impl RuntimePublic for Public {
Expand Down
18 changes: 12 additions & 6 deletions core/application-crypto/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use primitives::crypto::{KeyTypeId, CryptoType, IsWrappedBy, Public};
#[cfg(feature = "std")]
#[cfg(feature = "with_crypto")]
use primitives::crypto::Pair;
use codec::Codec;

Expand All @@ -28,7 +28,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 = "with_crypto")]
type Pair: AppPair;

/// The corresponding signature type in this application scheme.
Expand All @@ -44,12 +44,18 @@ pub trait MaybeDebugHash: std::fmt::Debug + std::hash::Hash {}
#[cfg(feature = "std")]
impl<T: std::fmt::Debug + std::hash::Hash> MaybeDebugHash for T {}

/// Type which implements Debug and Hash in std, not when no-std (no-std variant).
#[cfg(not(feature = "std"))]
/// Type which implements Debug and Hash in std, not when no-std (no-std variant without crypto).
#[cfg(all(not(feature = "std"), not(feature = "with_crypto")))]
pub trait MaybeDebugHash {}
#[cfg(not(feature = "std"))]
#[cfg(all(not(feature = "std"), not(feature = "with_crypto")))]
impl<T> MaybeDebugHash 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 = "with_crypto"))]
pub trait MaybeDebugHash: core::hash::Hash {}
#[cfg(all(not(feature = "std"), feature = "with_crypto"))]
impl<T: core::hash::Hash> MaybeDebugHash for T {}

/// A application's public key.
pub trait AppPublic: AppKey + Public + Ord + PartialOrd + Eq + PartialEq + MaybeDebugHash + codec::Codec {
/// The wrapped type which is just a plain instance of `Public`.
Expand All @@ -58,7 +64,7 @@ pub trait AppPublic: AppKey + Public + Ord + PartialOrd + Eq + PartialEq + Maybe
}

/// A application's key pair.
#[cfg(feature = "std")]
#[cfg(feature = "with_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
32 changes: 20 additions & 12 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ rstd = { package = "sr-std", path = "../sr-std", default-features = false }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
rustc-hex = { version = "2.0.1", default-features = false }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
twox-hash = { version = "1.5.0", optional = true }
twox-hash = { version = "1.5.0", default-features = false, optional = true }
byteorder = { version = "1.3.2", default-features = false }
primitive-types = { version = "0.5.1", default-features = false, features = ["codec"] }
impl-serde = { version = "0.2.1", optional = true }
log = { version = "0.4.8", optional = true }
wasmi = { version = "0.5.1", optional = true }
hash-db = { version = "0.15.2", default-features = false }
hash256-std-hasher = { version = "0.15.2", default-features = false }
ed25519-dalek = { version = "0.9.1", optional = true }
ed25519-dalek = { version = "1.0.0-pre.1", default-features = false, features = ["u64_backend"], optional = true }
base58 = { version = "0.1.0", optional = true }
blake2-rfc = { version = "0.2.18", optional = true }
schnorrkel = { version = "0.8.5", features = ["preaudit_deprecated"], optional = true }
blake2-rfc = { version = "0.2.18", default-features = false, optional = true }
schnorrkel = { version = "0.8.5", features = ["preaudit_deprecated"], default-features = false, optional = true }
rand = { version = "0.7.2", optional = true }
sha2 = { version = "0.8.0", optional = true }
substrate-bip39 = { version = "0.3.1", optional = true }
tiny-bip39 = { version = "0.6.2", optional = true }
hex = { version = "0.3.2", optional = true }
hex = { version = "0.4", default-features = false, optional = true }
regex = { version = "1.3.1", optional = true }
num-traits = { version = "0.2.8", default-features = false }
zeroize = "0.10.1"
lazy_static = { version = "1.4.0", optional = true }
zeroize = { version = "0.10.1", default-features = false }
lazy_static = { version = "1.4.0", default-features = false, optional = true }
parking_lot = { version = "0.9.0", optional = true }
externalities = { package = "substrate-externalities", path = "../externalities", optional = true }
primitives-storage = { package = "substrate-primitives-storage", path = "storage", default-features = false }
Expand All @@ -51,6 +51,7 @@ bench = false
[features]
default = ["std"]
std = [
"with_crypto",
"log",
"wasmi",
"lazy_static",
Expand All @@ -67,20 +68,27 @@ std = [
"rstd/std",
"serde",
"rustc-hex/std",
"twox-hash",
"blake2-rfc",
"ed25519-dalek",
"hex",
"twox-hash/std",
"blake2-rfc/std",
"ed25519-dalek/std",
"hex/std",
"base58",
"substrate-bip39",
"tiny-bip39",
"serde",
"byteorder/std",
"rand",
"sha2",
"schnorrkel",
"schnorrkel/std",
"regex",
"num-traits/std",
"externalities",
"primitives-storage/std",
]

with_crypto = [
brenzi marked this conversation as resolved.
Show resolved Hide resolved
"ed25519-dalek",
"blake2-rfc",
"schnorrkel",
"hex",
]
Loading