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

Overhaul crypto (Schnorr/Ristretto, HDKD, BIP39) #1795

Merged
merged 43 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6ec42d7
Merge remote-tracking branch 'origin/master' into gav-enable-ristretto
gavofyork Feb 14, 2019
f774028
Merge remote-tracking branch 'origin/master' into gav-enable-ristretto
gavofyork Feb 14, 2019
12f88af
Rijig to Ristretto
gavofyork Feb 14, 2019
8170cc9
Rebuild wasm
gavofyork Feb 14, 2019
ec6c7c9
adds compatibility test with the wasm module
kianenigma Feb 15, 2019
f8f5097
Merge branch 'gav-enable-ristretto' of github.com:paritytech/substrat…
kianenigma Feb 15, 2019
494e05b
Merge remote-tracking branch 'origin/master' into gav-enable-ristretto
gavofyork Mar 6, 2019
3491d5b
Merge remote-tracking branch 'origin/master' into gav-enable-ristretto
gavofyork Mar 7, 2019
af2061e
Add Ed25519-BIP39 support
gavofyork Mar 7, 2019
d80d906
Bump subkey version
gavofyork Mar 7, 2019
b7ad4f5
Update CLI output
gavofyork Mar 7, 2019
fd5503c
New keys.
gavofyork Mar 7, 2019
72fbadc
Standard phrase/password/path keys.
gavofyork Mar 8, 2019
fb1c40e
Subkey uses S-URI for secrets
gavofyork Mar 8, 2019
ddaffb0
Move everything to use new HDKD crypto.
gavofyork Mar 8, 2019
2142a38
Test fixes
gavofyork Mar 8, 2019
34259d6
Merge remote-tracking branch 'origin/master' into gav-enable-ristretto
gavofyork Mar 8, 2019
2ab6e42
Ignore old test vector.
gavofyork Mar 8, 2019
ca9c198
fix the ^^ old test vector.
kianenigma Mar 8, 2019
81b47bf
Fix tests
gavofyork Mar 10, 2019
fab9560
Test fixes
gavofyork Mar 10, 2019
fab1d97
Cleanups
gavofyork Mar 10, 2019
131a349
Fix broken key conversion logic in grandpa
gavofyork Mar 10, 2019
c284b91
Remove legacy Keyring usage
gavofyork Mar 10, 2019
d008f3b
Traitify `Pair`
gavofyork Mar 11, 2019
5dc4442
Replace Ed25519AuthorityId with ed25519::Public
gavofyork Mar 12, 2019
519a80e
Expunge Ed25519AuthorityId type!
gavofyork Mar 12, 2019
c019fa9
Replace Sr25519AuthorityId with sr25519::Public
gavofyork Mar 12, 2019
ab60208
Remove dodgy crypto type-punning conversions
gavofyork Mar 12, 2019
11e053e
Fix some tests
gavofyork Mar 12, 2019
5acdfb9
Avoid trait
gavofyork Mar 12, 2019
9837ddd
Deduplicate DeriveJunction string decode
gavofyork Mar 12, 2019
3e29972
Remove cruft code
gavofyork Mar 12, 2019
9fadc2b
Fix test
gavofyork Mar 12, 2019
c1fec25
Minor removals
gavofyork Mar 12, 2019
5c61823
Merge remote-tracking branch 'origin/master' into gav-enable-ristretto
gavofyork Mar 12, 2019
40dbc3f
Build fix
gavofyork Mar 12, 2019
d601233
Subkey supports sign and verify
gavofyork Mar 13, 2019
57de3e1
Inspect works for public key URIs
gavofyork Mar 13, 2019
2982704
Remove more crypto type-punning
gavofyork Mar 13, 2019
8a76fb1
Fix typo
gavofyork Mar 13, 2019
c11b5b5
Fix tests
gavofyork Mar 13, 2019
df6778a
Merge remote-tracking branch 'origin/master' into gav-enable-ristretto
gavofyork Mar 13, 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: 11 additions & 1 deletion core/primitives/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ where
mod test {
use super::*;
use hex_literal::{hex, hex_impl};

#[test]
fn sr_test_vector_should_work() {
let pair: Pair = Pair::from_seed(&hex!(
Expand Down Expand Up @@ -333,4 +333,14 @@ mod test {
let enc = hex!["090fa15cb5b1666222fff584b4cc2b1761fe1e238346b340491b37e25ea183ff"];
assert_eq!(Public::from_ss58check(k).unwrap(), Public::from_raw(enc));
}

#[test]
fn verify_from_wasm_works() {
// The values in this testcases are compared to the output of ./pkg/node-test.js in schnorrkel-js
// This is to make sure that the wasm library in compatible
let pk = Pair::from_seed(&hex!("0000000000000000000000000000000000000000000000000000000000000000"));
let public = pk.public();
let js_signature = Signature::from(&hex!("c072a48a17597166462a05b65ba9d122ea351021ccf6503a9570cdeabaa354019ae587bc007ce98e89d9507d02b6bf763452efdb7b9187cf6ee3b343d94d9105"));
assert!(verify_strong(&js_signature, b"SUBSTRATE", public));
}
}
19 changes: 19 additions & 0 deletions core/sr-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,25 @@ impl From<H512> for Sr25519Signature {
}
}

/// Ed25519 ---or--- Sr25519 signature verify.
#[derive(Eq, PartialEq, Clone, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub struct EdSr25519Signature(pub H512);

impl Verify for EdSr25519Signature {
type Signer = H256;
fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &Self::Signer) -> bool {
runtime_io::sr25519_verify((self.0).as_fixed_bytes(), msg.get(), &signer.as_bytes()) ||
runtime_io::ed25519_verify((self.0).as_fixed_bytes(), msg.get(), &signer.as_bytes())
}
}

impl From<H512> for EdSr25519Signature {
fn from(h: H512) -> EdSr25519Signature {
EdSr25519Signature(h)
}
}

/// Context for executing a call into the runtime.
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
Expand Down
48 changes: 28 additions & 20 deletions node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Substrate chain configurations.

use primitives::{Ed25519AuthorityId as AuthorityId, ed25519};
use primitives::{Ed25519AuthorityId as AuthorityId, ed25519, sr25519};
use node_primitives::AccountId;
use node_runtime::{ConsensusConfig, CouncilSeatsConfig, CouncilVotingConfig, DemocracyConfig,
SessionConfig, StakingConfig, TimestampConfig, BalancesConfig, TreasuryConfig,
Expand All @@ -40,25 +40,28 @@ pub fn dried_danta_config() -> Result<ChainSpec, String> {

fn staging_testnet_config_genesis() -> GenesisConfig {
// stash, controller, session-key
// generated with secret:
// for i in 1 2 3 4 ; do for j in stash controller; do subkey -p danta-$i-$j restore $secret; done; done
let initial_authorities: Vec<(AccountId, AccountId, AuthorityId)> = vec![(
hex!["fbecf7767fc63a6f9fa8094bbc5751d7269cd8e619cfdd9edfbe1fbc716b173e"].into(), // 5Hm2GcbuUct7sWX8d56zRktxr9D9Lw5hTFjSUhUoVHwFNmYW TODO: change once we switch to sr25519
hex!["6ed35e632190b9c795f019030e6c5cff1508655db28c83577e0a4366c9bd5773"].into(), // 5Ea1uyGz6H5WHZhWvPDxxLXWyiUkzWDwx54Hcn8LJ5dbFawH TODO: change once we switch to sr25519
hex!["d807f8bd6b4b02b3db716dd5372960b094ed0e62b5704a07bc990130a642992b"].into(), // 5GwxZv7LxSUQn89TLUaLi3oEWhFcabqW3nHcEg2J88gZNhrb
hex!["1a934af462454e512e22b5d9455c0c3c2df479b1c61406b3d990f6bc2eb25e09"].into(), // 5CfYrg5cW8UebBdfJpJbKFhZLyk7yHWXUgdxZnSGb2dWKgpt
hex!["82c39b31a2b79a90f8e66e7a77fdb85a4ed5517f2ae39f6a80565e8ecae85cf5"].into(),
),(
hex!["30b76ef977b84a575992ef52f561db315221123c68074269d3d51ce211c4a3dc"].into(), // 5DAaeTwVuyUmTyLBR5vKEDWeDJ75nhLutDuCJH58it7EHDM2 TODO: change once we switch to sr25519
hex!["a270edf24cb2a472b0e913fc43bfd4da0ef337cc715eaf94073d5198f7659f0c"].into(), // 5FjhAKgzpuzt1dYWE7H7Jb1sEHSuG5hcyZdPtfX829gmFVXh TODO: change once we switch to sr25519
hex!["12652f26e427c56268095bb0ec5824471e37722b073a9fa5de61c61c1de94656"].into(), // 5CUpn2JmpsWkHQjZgWjN3rqPEUnjjUQZYcMk14nbUgR2Gpab
hex!["5279e73e22971d729276ebad4eb6804d1b9c0c35bd32e8aba4513c674760a461"].into(), // 5Dvqzke7Mdp3fP6Ysut7UXPSepPr3Qguys6LNkZGPSwXwAkR
hex!["4de37a07567ebcbf8c64568428a835269a566723687058e017b6d69db00a77e7"].into(),
),(
hex!["7b9e79c1bfc71ad0c4389565c01e79269dc512cb9bd856489671662481355417"].into(), // 5ErnpkRUbmM3WdbQwnVwfZeYs3iKmggEQceyB9db9ft18dSn TODO: change once we switch to sr25519
hex!["9ffec660c4d328306cf5e38faf4b132fb5c9f38287af95d9b25629fc29de3945"].into(), // 5FgV9vxNpdCXMUmHCLQcsN4mUUUG6ZpFuvAMrm5X4BUnFhie TODO: change once we switch to sr25519
hex!["a81d738fdeeaed440cfce5635e0820d0d23e89207cf66a62b8c0d2a968e37d32"].into(), // 5Fs8ehAjDEnenDwULCPnEr3HVXgepAVfyk9ABW84NfxCYtWD
hex!["443a2c779a5f5dada8ee6921efec9673f67e5ce1bd6012899ff6c1adc437696c"].into(), // 5DcAPqR269myKXhZmwbU1x2xLbuTojr85jHNRuDhrFdZ3vwi
hex!["063d7787ebca768b7445dfebe7d62cbb1625ff4dba288ea34488da266dd6dca5"].into(),
),(
hex!["7e58b096b95c4b3b271f27fedd9f2c51edd48b9d37046240e601180c9dcc8c27"].into(), // 5EvNEhYYd4b9giczuCo2o8bfLZoKW9jnTeUukfL1NWsAAeEx TODO: change once we switch to sr25519
hex!["36dfc933bb0848d8addf16a961369b2e122633a5819a19e43c8142381a1280e3"].into(), // 5DJevPKpz4EEvmSpK7W6KemS3i5JYPq5FEuEewgRY2cZCxNg TODO: change once we switch to sr25519
hex!["e269e835e0bc07c497d55bc17c7bb29c85c5615f9e61582ffdeca7e5f5c66578"].into(), // 5HBa95U5HDFCV1N5Xyrjti65F71tHRQcPbZBmkxRJ39SpqzM
hex!["3e9829e6fd4fc7501b504fc16f12177c6c7f38aeb3b8344efb9b15ee85118b2c"].into(), // 5DUn2afs2QevZ6PrGu8snrt76157oacH6JXUD8JNM18VKMwK
hex!["8101764f45778d4980dadaceee6e8af2517d3ab91ac9bec9cd1714fa5994081c"].into(),
)];
// generated with secret: subkey -p danta-root restore $secret
let endowed_accounts: Vec<AccountId> = vec![
hex!["f295940fa750df68a686fcf4abd4111c8a9c5a5a5a83c4c8639c451a94a7adfd"].into(), // 5HYmsxGRAmZMjyZYmf7uGPL2YDQGHEt6NjGrfUuxNEgeGBRN TODO: change once we switch to sr25519
hex!["343df6f04ffae0840f214f6cb0da00b612c7e9347f980e7afafc520582f79136"].into(), // 5DFCkiP9vky31C1ZP3LpuQYinLAFwQqq6vda7NXa8ALCpq5D TODO: change once we switch to sr25519
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the TODO still valid?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no...

];
const MILLICENTS: u128 = 1_000_000_000;
const CENTS: u128 = 1_000 * MILLICENTS; // assume this is worth about a cent.
Expand Down Expand Up @@ -182,23 +185,22 @@ pub fn staging_testnet_config() -> ChainSpec {
)
}

/// Helper function to generate AuthorityID from seed
/// Helper function to generate AccountId from seed
pub fn get_account_id_from_seed(seed: &str) -> AccountId {
let padded_seed = pad_seed(seed);
// NOTE from ed25519 impl:
// prefer pkcs#8 unless security doesn't matter -- this is used primarily for tests.
ed25519::Pair::from_seed(&padded_seed).public().0.into()
sr25519::Pair::from_seed(&pad_seed(seed)).public().0.into()
}

/// Helper function to generate AuthorityId from seed
pub fn get_session_key_from_seed(seed: &str) -> AuthorityId {
ed25519::Pair::from_seed(&pad_seed(seed)).public().0.into()
}

/// Helper function to generate stash, controller and session key from seed
pub fn get_authority_keys_from_seed(seed: &str) -> (AccountId, AccountId, AuthorityId) {
let padded_seed = pad_seed(seed);
// NOTE from ed25519 impl:
// prefer pkcs#8 unless security doesn't matter -- this is used primarily for tests.
(
get_account_id_from_seed(&format!("{}-stash", seed)),
get_account_id_from_seed(seed),
ed25519::Pair::from_seed(&padded_seed).public().0.into()
get_session_key_from_seed(seed)
)
}

Expand All @@ -216,6 +218,12 @@ pub fn testnet_genesis(
get_account_id_from_seed("Dave"),
get_account_id_from_seed("Eve"),
get_account_id_from_seed("Ferdie"),
get_account_id_from_seed("Alice-stash"),
get_account_id_from_seed("Bob-stash"),
get_account_id_from_seed("Charlie-stash"),
get_account_id_from_seed("Dave-stash"),
get_account_id_from_seed("Eve-stash"),
get_account_id_from_seed("Ferdie-stash"),
]
});

Expand Down Expand Up @@ -275,7 +283,7 @@ pub fn testnet_genesis(
presentation_duration: 10,
approval_voting_period: 20,
term_duration: 1000000,
desired_seats: (endowed_accounts.len() - initial_authorities.len()) as u32,
desired_seats: (endowed_accounts.len() / 2 - initial_authorities.len()) as u32,
inactive_grace_period: 1,
}),
council_voting: Some(CouncilVotingConfig {
Expand Down
4 changes: 2 additions & 2 deletions node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ mod tests {
use super::Executor;
use substrate_executor::{WasmExecutor, NativeExecutionDispatch};
use parity_codec::{Encode, Decode, Joiner};
use keyring::ed25519::Keyring;
use keyring::sr25519::Keyring;
use runtime_support::{Hashable, StorageValue, StorageMap, traits::Currency};
use state_machine::{CodeExecutor, Externalities, TestExternalities};
use primitives::{
twox_128, Blake2Hasher, ChangesTrieConfiguration, ed25519::{Public, Pair}, NeverNativeValue,
twox_128, Blake2Hasher, ChangesTrieConfiguration, sr25519::{Public, Pair}, NeverNativeValue,
NativeOrEncoded
};
use node_primitives::{Hash, BlockNumber, AccountId};
Expand Down
4 changes: 2 additions & 2 deletions node/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
#![cfg_attr(not(feature = "std"), feature(alloc))]

use runtime_primitives::{
generic, traits::{Verify, BlakeTwo256}, Ed25519Signature, OpaqueExtrinsic
generic, traits::{Verify, BlakeTwo256}, EdSr25519Signature, OpaqueExtrinsic
};

/// An index to a block.
pub type BlockNumber = u64;

/// Alias to 512-bit hash when used in the context of a signature on the chain.
pub type Signature = Ed25519Signature;
pub type Signature = EdSr25519Signature;

/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
Expand Down
4 changes: 2 additions & 2 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("node"),
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 34,
impl_version: 34,
spec_version: 35,
impl_version: 35,
apis: RUNTIME_API_VERSIONS,
};

Expand Down
Binary file not shown.