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

Commit cefe4dc

Browse files
brenzibkchr
authored andcommitted
support crypto primitives for no_std introducing full_crypto feature (#3778)
* introduced "with_crypto" feature and applied switches like in substrate-api-client fork * introduced "with_crypto" feature and applied switches like in substraTEE-worker fork * distinguishing core::hash vs std::hash * @bkchr's review requests fulfilled * fixes * revert dependency upgrade ed25519-dalek * added full_crypto features to all crates using app_crypto! macro * fixing CI complaints. * fix again * adding CI test for with_crypto feature * added full_crypto for ecdsa. now builds wit h--no-deafault-features --features with_crypto * remove --release from CI test * @bkchr requested changes. moved full_crypto CI test to build stage * fixing no_std issue * CI fresh copy from srml-staking * gitlab CI with +nightly * solved no-feature-in-macro dilemma * cosmetics * Update core/application-crypto/src/sr25519.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update core/application-crypto/src/ed25519.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * even more simple * undo line delete * refactoring app_crypto macro. splitting functionalities based on full_crypto feature * whitespace cosmetics
1 parent 1d5cae9 commit cefe4dc

File tree

13 files changed

+349
-148
lines changed

13 files changed

+349
-148
lines changed

.gitlab-ci.yml

+20
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,26 @@ node-exits:
230230
script:
231231
- ./ci/check_for_exit.sh
232232

233+
234+
test-full-crypto-feature: &test-full-crypto-feature
235+
stage: test
236+
<<: *docker-env
237+
variables:
238+
# Enable debug assertions since we are running optimized builds for testing
239+
# but still want to have debug assertions.
240+
RUSTFLAGS: -Cdebug-assertions=y
241+
RUST_BACKTRACE: 1
242+
except:
243+
variables:
244+
- $DEPLOY_TAG
245+
script:
246+
- cd core/primitives/
247+
- time cargo +nightly build --verbose --no-default-features --features full_crypto
248+
- cd ../application-crypto
249+
- time cargo +nightly build --verbose --no-default-features --features full_crypto
250+
- sccache -s
251+
252+
233253
#### stage: build
234254

235255
build-linux-substrate:

Cargo.lock

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/application-crypto/Cargo.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ sr-primitives = { path = "../sr-primitives" }
1818

1919
[features]
2020
default = [ "std" ]
21-
std = [ "primitives/std", "codec/std", "serde", "rstd/std", "runtime-io/std" ]
21+
std = [ "full_crypto", "primitives/std", "codec/std", "serde", "rstd/std", "runtime-io/std" ]
22+
23+
# This feature enables all crypto primitives for `no_std` builds like microcontrollers
24+
# or Intel SGX.
25+
# For the regular wasm runtime builds this should not be used.
26+
full_crypto = [
27+
"primitives/full_crypto"
28+
]

core/application-crypto/src/ed25519.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod app {
3131

3232
pub use app::Public as AppPublic;
3333
pub use app::Signature as AppSignature;
34-
#[cfg(feature="std")]
34+
#[cfg(feature = "full_crypto")]
3535
pub use app::Pair as AppPair;
3636

3737
impl RuntimePublic for Public {

core/application-crypto/src/lib.rs

+152-36
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#[doc(hidden)]
2424
pub use primitives::{self, crypto::{CryptoType, Public, Derive, IsWrappedBy, Wraps}, RuntimeDebug};
2525
#[doc(hidden)]
26-
#[cfg(feature = "std")]
26+
#[cfg(feature = "full_crypto")]
2727
pub use primitives::crypto::{SecretStringError, DeriveJunction, Ss58Codec, Pair};
2828
pub use primitives::{crypto::{KeyTypeId, key_types}};
2929

@@ -50,17 +50,43 @@ pub use traits::*;
5050
/// // of value `b"fuba"`.
5151
/// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
5252
/// ```
53+
#[cfg(feature = "full_crypto")]
5354
#[macro_export]
5455
macro_rules! app_crypto {
5556
($module:ident, $key_type:expr) => {
56-
#[cfg(feature="std")]
57-
$crate::app_crypto!($module::Pair, $module::Public, $module::Signature, $key_type);
58-
#[cfg(not(feature="std"))]
59-
$crate::app_crypto!($module::Public, $module::Signature, $key_type);
57+
$crate::app_crypto_public_full_crypto!($module::Public, $key_type);
58+
$crate::app_crypto_public_common!($module::Public, $module::Signature, $key_type);
59+
$crate::app_crypto_signature_full_crypto!($module::Signature, $key_type);
60+
$crate::app_crypto_signature_common!($module::Signature, $key_type);
61+
$crate::app_crypto_pair!($module::Pair, $key_type);
6062
};
61-
($pair:ty, $public:ty, $sig:ty, $key_type:expr) => {
62-
$crate::app_crypto!($public, $sig, $key_type);
63+
}
6364

65+
/// Declares Public, Pair, Signature types which are functionally equivalent to `$pair`, but are new
66+
/// Application-specific types whose identifier is `$key_type`.
67+
///
68+
/// ```rust
69+
///# use substrate_application_crypto::{app_crypto, wrap, ed25519, KeyTypeId};
70+
/// // Declare a new set of crypto types using Ed25519 logic that identifies as `KeyTypeId`
71+
/// // of value `b"fuba"`.
72+
/// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
73+
/// ```
74+
#[cfg(not(feature = "full_crypto"))]
75+
#[macro_export]
76+
macro_rules! app_crypto {
77+
($module:ident, $key_type:expr) => {
78+
$crate::app_crypto_public_not_full_crypto!($module::Public, $key_type);
79+
$crate::app_crypto_public_common!($module::Public, $module::Signature, $key_type);
80+
$crate::app_crypto_signature_not_full_crypto!($module::Signature, $key_type);
81+
$crate::app_crypto_signature_common!($module::Signature, $key_type);
82+
};
83+
}
84+
85+
/// Declares Pair type which is functionally equivalent to `$pair`, but is new
86+
/// Application-specific type whose identifier is `$key_type`.
87+
#[macro_export]
88+
macro_rules! app_crypto_pair {
89+
($pair:ty, $key_type:expr) => {
6490
$crate::wrap!{
6591
/// A generic `AppPublic` wrapper type over $pair crypto; this has no specific App.
6692
#[derive(Clone)]
@@ -71,16 +97,18 @@ macro_rules! app_crypto {
7197
type Pair = Pair;
7298
}
7399

74-
#[cfg(feature = "std")]
75100
impl $crate::Pair for Pair {
76101
type Public = Public;
77102
type Seed = <$pair as $crate::Pair>::Seed;
78103
type Signature = Signature;
79104
type DeriveError = <$pair as $crate::Pair>::DeriveError;
105+
106+
#[cfg(feature = "std")]
80107
fn generate_with_phrase(password: Option<&str>) -> (Self, String, Self::Seed) {
81108
let r = <$pair>::generate_with_phrase(password);
82109
(Self(r.0), r.1, r.2)
83110
}
111+
#[cfg(feature = "std")]
84112
fn from_phrase(phrase: &str, password: Option<&str>)
85113
-> Result<(Self, Self::Seed), $crate::SecretStringError>
86114
{
@@ -115,18 +143,28 @@ macro_rules! app_crypto {
115143
fn public(&self) -> Self::Public { Public(self.0.public()) }
116144
fn to_raw_vec(&self) -> Vec<u8> { self.0.to_raw_vec() }
117145
}
146+
118147
impl $crate::AppKey for Pair {
119148
type UntypedGeneric = $pair;
120149
type Public = Public;
121150
type Pair = Pair;
122151
type Signature = Signature;
123152
const ID: $crate::KeyTypeId = $key_type;
124153
}
154+
125155
impl $crate::AppPair for Pair {
126156
type Generic = $pair;
127157
}
128158
};
129-
($public:ty, $sig:ty, $key_type:expr) => {
159+
}
160+
161+
/// Declares Public type which is functionally equivalent to `$public`, but is new
162+
/// Application-specific type whose identifier is `$key_type`.
163+
/// can only be used together with `full_crypto` feature
164+
/// For full functionality, app_crypto_public_common! must be called too.
165+
#[macro_export]
166+
macro_rules! app_crypto_public_full_crypto {
167+
($public:ty, $key_type:expr) => {
130168
$crate::wrap!{
131169
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
132170
#[derive(
@@ -135,10 +173,59 @@ macro_rules! app_crypto {
135173
$crate::codec::Decode,
136174
$crate::RuntimeDebug,
137175
)]
138-
#[cfg_attr(feature = "std", derive(Hash))]
176+
#[derive(Hash)]
139177
pub struct Public($public);
140178
}
141179

180+
impl $crate::CryptoType for Public {
181+
type Pair = Pair;
182+
}
183+
184+
impl $crate::AppKey for Public {
185+
type UntypedGeneric = $public;
186+
type Public = Public;
187+
type Pair = Pair;
188+
type Signature = Signature;
189+
const ID: $crate::KeyTypeId = $key_type;
190+
}
191+
}
192+
}
193+
194+
/// Declares Public type which is functionally equivalent to `$public`, but is new
195+
/// Application-specific type whose identifier is `$key_type`.
196+
/// can only be used without `full_crypto` feature
197+
/// For full functionality, app_crypto_public_common! must be called too.
198+
#[macro_export]
199+
macro_rules! app_crypto_public_not_full_crypto {
200+
($public:ty, $key_type:expr) => {
201+
$crate::wrap!{
202+
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
203+
#[derive(
204+
Clone, Default, Eq, PartialEq, Ord, PartialOrd,
205+
$crate::codec::Encode,
206+
$crate::codec::Decode,
207+
$crate::RuntimeDebug,
208+
)]
209+
pub struct Public($public);
210+
}
211+
212+
impl $crate::CryptoType for Public {}
213+
214+
impl $crate::AppKey for Public {
215+
type UntypedGeneric = $public;
216+
type Public = Public;
217+
type Signature = Signature;
218+
const ID: $crate::KeyTypeId = $key_type;
219+
}
220+
}
221+
}
222+
223+
/// Declares Public type which is functionally equivalent to `$public`, but is new
224+
/// Application-specific type whose identifier is `$key_type`.
225+
/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
226+
#[macro_export]
227+
macro_rules! app_crypto_public_common {
228+
($public:ty, $sig:ty, $key_type:expr) => {
142229
impl $crate::Derive for Public {
143230
#[cfg(feature = "std")]
144231
fn derive<Iter: Iterator<Item=$crate::DeriveJunction>>(&self,
@@ -183,24 +270,10 @@ macro_rules! app_crypto {
183270
fn as_mut(&mut self) -> &mut [u8] { self.0.as_mut() }
184271
}
185272

186-
impl $crate::CryptoType for Public {
187-
#[cfg(feature="std")]
188-
type Pair = Pair;
189-
}
190-
191273
impl $crate::Public for Public {
192274
fn from_slice(x: &[u8]) -> Self { Self(<$public>::from_slice(x)) }
193275
}
194276

195-
impl $crate::AppKey for Public {
196-
type UntypedGeneric = $public;
197-
type Public = Public;
198-
#[cfg(feature="std")]
199-
type Pair = Pair;
200-
type Signature = Signature;
201-
const ID: $crate::KeyTypeId = $key_type;
202-
}
203-
204277
impl $crate::AppPublic for Public {
205278
type Generic = $public;
206279
}
@@ -229,41 +302,84 @@ macro_rules! app_crypto {
229302
<$public as $crate::RuntimePublic>::verify(self.as_ref(), msg, &signature.as_ref())
230303
}
231304
}
305+
}
306+
}
232307

308+
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
309+
/// Application-specific type whose identifier is `$key_type`.
310+
/// can only be used together with `full_crypto` feature
311+
/// For full functionality, app_crypto_public_common! must be called too.
312+
#[macro_export]
313+
macro_rules! app_crypto_signature_full_crypto {
314+
($sig:ty, $key_type:expr) => {
233315
$crate::wrap! {
234316
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
235317
#[derive(Clone, Default, Eq, PartialEq,
236318
$crate::codec::Encode,
237319
$crate::codec::Decode,
238320
$crate::RuntimeDebug,
239321
)]
240-
#[cfg_attr(feature = "std", derive(Hash))]
322+
#[derive(Hash)]
241323
pub struct Signature($sig);
242324
}
243325

244-
impl $crate::Deref for Signature {
245-
type Target = [u8];
246-
247-
fn deref(&self) -> &Self::Target { self.0.as_ref() }
326+
impl $crate::CryptoType for Signature {
327+
type Pair = Pair;
248328
}
249329

250-
impl AsRef<[u8]> for Signature {
251-
fn as_ref(&self) -> &[u8] { self.0.as_ref() }
330+
impl $crate::AppKey for Signature {
331+
type UntypedGeneric = $sig;
332+
type Public = Public;
333+
type Pair = Pair;
334+
type Signature = Signature;
335+
const ID: $crate::KeyTypeId = $key_type;
252336
}
337+
}
338+
}
253339

254-
impl $crate::CryptoType for Signature {
255-
#[cfg(feature="std")]
256-
type Pair = Pair;
340+
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
341+
/// Application-specific type whose identifier is `$key_type`.
342+
/// can only be used without `full_crypto` feature
343+
/// For full functionality, app_crypto_public_common! must be called too.
344+
#[macro_export]
345+
macro_rules! app_crypto_signature_not_full_crypto {
346+
($sig:ty, $key_type:expr) => {
347+
$crate::wrap! {
348+
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
349+
#[derive(Clone, Default, Eq, PartialEq,
350+
$crate::codec::Encode,
351+
$crate::codec::Decode,
352+
$crate::RuntimeDebug,
353+
)]
354+
pub struct Signature($sig);
257355
}
356+
357+
impl $crate::CryptoType for Signature {}
258358

259359
impl $crate::AppKey for Signature {
260360
type UntypedGeneric = $sig;
261361
type Public = Public;
262-
#[cfg(feature="std")]
263-
type Pair = Pair;
264362
type Signature = Signature;
265363
const ID: $crate::KeyTypeId = $key_type;
266364
}
365+
}
366+
}
367+
368+
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
369+
/// Application-specific type whose identifier is `$key_type`.
370+
/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
371+
#[macro_export]
372+
macro_rules! app_crypto_signature_common {
373+
($sig:ty, $key_type:expr) => {
374+
impl $crate::Deref for Signature {
375+
type Target = [u8];
376+
377+
fn deref(&self) -> &Self::Target { self.0.as_ref() }
378+
}
379+
380+
impl AsRef<[u8]> for Signature {
381+
fn as_ref(&self) -> &[u8] { self.0.as_ref() }
382+
}
267383

268384
impl $crate::AppSignature for Signature {
269385
type Generic = $sig;

core/application-crypto/src/sr25519.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod app {
3131

3232
pub use app::Public as AppPublic;
3333
pub use app::Signature as AppSignature;
34-
#[cfg(feature="std")]
34+
#[cfg(feature = "full_crypto")]
3535
pub use app::Pair as AppPair;
3636

3737
impl RuntimePublic for Public {

0 commit comments

Comments
 (0)