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 26 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
20 changes: 20 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ node-exits:
script:
- ./ci/check_for_exit.sh


test-full-crypto-feature: &test-full-crypto-feature
stage: test
<<: *docker-env
variables:
# Enable debug assertions since we are running optimized builds for testing
# but still want to have debug assertions.
RUSTFLAGS: -Cdebug-assertions=y
RUST_BACKTRACE: 1
except:
variables:
- $DEPLOY_TAG
script:
- cd core/primitives/
- time cargo +nightly build --verbose --no-default-features --features full_crypto
- cd ../application-crypto
- time cargo +nightly build --verbose --no-default-features --features full_crypto
- sccache -s


#### stage: build

build-linux-substrate:
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"
]
2 changes: 1 addition & 1 deletion core/application-crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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
188 changes: 152 additions & 36 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 All @@ -50,17 +50,43 @@ pub use traits::*;
/// // of value `b"fuba"`.
/// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
/// ```
#[cfg(feature = "full_crypto")]
#[macro_export]
macro_rules! app_crypto {
brenzi marked this conversation as resolved.
Show resolved Hide resolved
($module:ident, $key_type:expr) => {
#[cfg(feature="std")]
$crate::app_crypto!($module::Pair, $module::Public, $module::Signature, $key_type);
#[cfg(not(feature="std"))]
$crate::app_crypto!($module::Public, $module::Signature, $key_type);
$crate::app_crypto_public_full_crypto!($module::Public, $key_type);
$crate::app_crypto_public_common!($module::Public, $module::Signature, $key_type);
$crate::app_crypto_signature_full_crypto!($module::Signature, $key_type);
$crate::app_crypto_signature_common!($module::Signature, $key_type);
$crate::app_crypto_pair!($module::Pair, $key_type);
};
($pair:ty, $public:ty, $sig:ty, $key_type:expr) => {
$crate::app_crypto!($public, $sig, $key_type);
}

/// Declares Public, Pair, Signature types which are functionally equivalent to `$pair`, but are new
/// Application-specific types whose identifier is `$key_type`.
///
/// ```rust
///# use substrate_application_crypto::{app_crypto, wrap, ed25519, KeyTypeId};
/// // Declare a new set of crypto types using Ed25519 logic that identifies as `KeyTypeId`
/// // of value `b"fuba"`.
/// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
/// ```
#[cfg(not(feature = "full_crypto"))]
#[macro_export]
macro_rules! app_crypto {
($module:ident, $key_type:expr) => {
$crate::app_crypto_public_not_full_crypto!($module::Public, $key_type);
$crate::app_crypto_public_common!($module::Public, $module::Signature, $key_type);
$crate::app_crypto_signature_not_full_crypto!($module::Signature, $key_type);
$crate::app_crypto_signature_common!($module::Signature, $key_type);
};
}

/// Declares Pair type which is functionally equivalent to `$pair`, but is new
/// Application-specific type whose identifier is `$key_type`.
#[macro_export]
macro_rules! app_crypto_pair {
($pair:ty, $key_type:expr) => {
$crate::wrap!{
/// A generic `AppPublic` wrapper type over $pair crypto; this has no specific App.
#[derive(Clone)]
Expand All @@ -71,16 +97,18 @@ macro_rules! app_crypto {
type Pair = Pair;
}

#[cfg(feature = "std")]
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 @@ -115,18 +143,28 @@ macro_rules! app_crypto {
fn public(&self) -> Self::Public { Public(self.0.public()) }
fn to_raw_vec(&self) -> Vec<u8> { self.0.to_raw_vec() }
}

impl $crate::AppKey for Pair {
type UntypedGeneric = $pair;
type Public = Public;
type Pair = Pair;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
}

impl $crate::AppPair for Pair {
type Generic = $pair;
}
};
($public:ty, $sig:ty, $key_type:expr) => {
}

/// Declares Public type which is functionally equivalent to `$public`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// can only be used together with `full_crypto` feature
/// For full functionality, app_crypto_public_common! must be called too.
#[macro_export]
macro_rules! app_crypto_public_full_crypto {
($public:ty, $key_type:expr) => {
$crate::wrap!{
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
#[derive(
Expand All @@ -135,10 +173,59 @@ macro_rules! app_crypto {
$crate::codec::Decode,
$crate::RuntimeDebug,
)]
#[cfg_attr(feature = "std", derive(Hash))]
#[derive(Hash)]
pub struct Public($public);
}

impl $crate::CryptoType for Public {
type Pair = Pair;
}

impl $crate::AppKey for Public {
type UntypedGeneric = $public;
type Public = Public;
type Pair = Pair;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
}
}
}

/// Declares Public type which is functionally equivalent to `$public`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// can only be used without `full_crypto` feature
/// For full functionality, app_crypto_public_common! must be called too.
#[macro_export]
macro_rules! app_crypto_public_not_full_crypto {
($public:ty, $key_type:expr) => {
$crate::wrap!{
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
#[derive(
Clone, Default, Eq, PartialEq, Ord, PartialOrd,
$crate::codec::Encode,
$crate::codec::Decode,
$crate::RuntimeDebug,
)]
pub struct Public($public);
}

impl $crate::CryptoType for Public {}

impl $crate::AppKey for Public {
type UntypedGeneric = $public;
type Public = Public;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
}
}
}

/// Declares Public type which is functionally equivalent to `$public`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
#[macro_export]
macro_rules! app_crypto_public_common {
($public:ty, $sig:ty, $key_type:expr) => {
impl $crate::Derive for Public {
#[cfg(feature = "std")]
fn derive<Iter: Iterator<Item=$crate::DeriveJunction>>(&self,
Expand Down Expand Up @@ -183,24 +270,10 @@ macro_rules! app_crypto {
fn as_mut(&mut self) -> &mut [u8] { self.0.as_mut() }
}

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

impl $crate::Public for Public {
fn from_slice(x: &[u8]) -> Self { Self(<$public>::from_slice(x)) }
}

impl $crate::AppKey for Public {
type UntypedGeneric = $public;
type Public = Public;
#[cfg(feature="std")]
type Pair = Pair;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
}

impl $crate::AppPublic for Public {
type Generic = $public;
}
Expand Down Expand Up @@ -229,41 +302,84 @@ macro_rules! app_crypto {
<$public as $crate::RuntimePublic>::verify(self.as_ref(), msg, &signature.as_ref())
}
}
}
}

/// Declares Signature type which is functionally equivalent to `$sig`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// can only be used together with `full_crypto` feature
/// For full functionality, app_crypto_public_common! must be called too.
#[macro_export]
macro_rules! app_crypto_signature_full_crypto {
($sig:ty, $key_type:expr) => {
$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,
$crate::RuntimeDebug,
)]
#[cfg_attr(feature = "std", derive(Hash))]
#[derive(Hash)]
pub struct Signature($sig);
}

impl $crate::Deref for Signature {
type Target = [u8];

fn deref(&self) -> &Self::Target { self.0.as_ref() }
impl $crate::CryptoType for Signature {
type Pair = Pair;
}

impl AsRef<[u8]> for Signature {
fn as_ref(&self) -> &[u8] { self.0.as_ref() }
impl $crate::AppKey for Signature {
type UntypedGeneric = $sig;
type Public = Public;
type Pair = Pair;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
}
}
}

impl $crate::CryptoType for Signature {
#[cfg(feature="std")]
type Pair = Pair;
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// can only be used without `full_crypto` feature
/// For full functionality, app_crypto_public_common! must be called too.
#[macro_export]
macro_rules! app_crypto_signature_not_full_crypto {
($sig:ty, $key_type:expr) => {
$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,
$crate::RuntimeDebug,
)]
pub struct Signature($sig);
}

impl $crate::CryptoType for Signature {}

impl $crate::AppKey for Signature {
type UntypedGeneric = $sig;
type Public = Public;
#[cfg(feature="std")]
type Pair = Pair;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
}
}
}

/// Declares Signature type which is functionally equivalent to `$sig`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
#[macro_export]
macro_rules! app_crypto_signature_common {
($sig:ty, $key_type:expr) => {
impl $crate::Deref for Signature {
type Target = [u8];

fn deref(&self) -> &Self::Target { self.0.as_ref() }
}

impl AsRef<[u8]> for Signature {
fn as_ref(&self) -> &[u8] { self.0.as_ref() }
}

impl $crate::AppSignature for Signature {
type Generic = $sig;
Expand Down
2 changes: 1 addition & 1 deletion core/application-crypto/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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
Loading