From adfb06848ab527c127b2a611692542e573eaaad0 Mon Sep 17 00:00:00 2001 From: Andrew Burger Date: Mon, 16 Dec 2024 15:45:30 +0100 Subject: [PATCH] fixing AppPair usage and fixed bls test paired still broken --- .../application-crypto/src/bandersnatch.rs | 4 +- .../application-crypto/src/bls381.rs | 4 +- .../application-crypto/src/ecdsa.rs | 4 +- .../application-crypto/src/ecdsa_bls381.rs | 4 +- .../application-crypto/src/ed25519.rs | 4 +- .../primitives/application-crypto/src/lib.rs | 10 +++ .../application-crypto/src/sr25519.rs | 4 +- .../application-crypto/test/Cargo.toml | 10 +-- .../application-crypto/test/src/bls381.rs | 7 +- .../test/src/ecdsa_bls381.rs | 25 +++++++ .../application-crypto/test/src/lib.rs | 3 + .../test-utils/runtime/client/Cargo.toml | 5 +- substrate/test-utils/runtime/src/lib.rs | 67 ++++++++++++------- 13 files changed, 101 insertions(+), 50 deletions(-) create mode 100644 substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs diff --git a/substrate/primitives/application-crypto/src/bandersnatch.rs b/substrate/primitives/application-crypto/src/bandersnatch.rs index 50961855ed34..3c297c4e3817 100644 --- a/substrate/primitives/application-crypto/src/bandersnatch.rs +++ b/substrate/primitives/application-crypto/src/bandersnatch.rs @@ -22,7 +22,7 @@ use alloc::vec::Vec; pub use sp_core::bandersnatch::*; use sp_core::{ - crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG}, + crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG, CryptoType}, Pair as TraitPair, }; @@ -65,7 +65,7 @@ impl RuntimePublic for Public { fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone()); - AppPair::verify_proof_of_possession(&pop, &pub_key) + ::Pair::verify_proof_of_possession(&pop, &pub_key) } fn to_raw_vec(&self) -> Vec { diff --git a/substrate/primitives/application-crypto/src/bls381.rs b/substrate/primitives/application-crypto/src/bls381.rs index 6fcf5c6b530f..4841455d0635 100644 --- a/substrate/primitives/application-crypto/src/bls381.rs +++ b/substrate/primitives/application-crypto/src/bls381.rs @@ -21,7 +21,7 @@ use crate::{KeyTypeId, RuntimePublic}; use alloc::vec::Vec; pub use sp_core::bls::bls381::*; -use sp_core::crypto::ProofOfPossessionVerifier; +use sp_core::crypto::{ProofOfPossessionVerifier, CryptoType}; mod app { crate::app_crypto!(super, sp_core::testing::BLS381); @@ -60,7 +60,7 @@ impl RuntimePublic for Public { fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone()); - AppPair::verify_proof_of_possession(&pop, &pub_key) + ::Pair::verify_proof_of_possession(&pop, &pub_key) } fn to_raw_vec(&self) -> Vec { diff --git a/substrate/primitives/application-crypto/src/ecdsa.rs b/substrate/primitives/application-crypto/src/ecdsa.rs index 756572a4701f..e122a5569c2c 100644 --- a/substrate/primitives/application-crypto/src/ecdsa.rs +++ b/substrate/primitives/application-crypto/src/ecdsa.rs @@ -21,7 +21,7 @@ use crate::{KeyTypeId, RuntimePublic}; use alloc::vec::Vec; -use sp_core::crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG}; +use sp_core::crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG, CryptoType}; pub use sp_core::ecdsa::*; mod app { @@ -58,7 +58,7 @@ impl RuntimePublic for Public { fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone()); - AppPair::verify_proof_of_possession(&pop, &pub_key) + ::Pair::verify_proof_of_possession(&pop, &pub_key) } fn to_raw_vec(&self) -> Vec { diff --git a/substrate/primitives/application-crypto/src/ecdsa_bls381.rs b/substrate/primitives/application-crypto/src/ecdsa_bls381.rs index c0f80b8857d7..c39624c87f5e 100644 --- a/substrate/primitives/application-crypto/src/ecdsa_bls381.rs +++ b/substrate/primitives/application-crypto/src/ecdsa_bls381.rs @@ -23,7 +23,7 @@ use alloc::vec::Vec; pub use sp_core::paired_crypto::ecdsa_bls381::*; use sp_core::{ bls381, - crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG}, + crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG, CryptoType}, ecdsa, ecdsa_bls381, }; @@ -78,7 +78,7 @@ impl RuntimePublic for Public { fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone()); - AppPair::verify_proof_of_possession(&pop, &pub_key) + ::Pair::verify_proof_of_possession(&pop, &pub_key) } fn to_raw_vec(&self) -> Vec { diff --git a/substrate/primitives/application-crypto/src/ed25519.rs b/substrate/primitives/application-crypto/src/ed25519.rs index 51bb24c6be88..1621b7c7c363 100644 --- a/substrate/primitives/application-crypto/src/ed25519.rs +++ b/substrate/primitives/application-crypto/src/ed25519.rs @@ -21,7 +21,7 @@ use crate::{KeyTypeId, RuntimePublic}; use alloc::vec::Vec; -use sp_core::crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG}; +use sp_core::crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG, CryptoType}; pub use sp_core::ed25519::*; mod app { @@ -58,7 +58,7 @@ impl RuntimePublic for Public { fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone()); - AppPair::verify_proof_of_possession(&pop, &pub_key) + ::Pair::verify_proof_of_possession(&pop, &pub_key) } fn to_raw_vec(&self) -> Vec { diff --git a/substrate/primitives/application-crypto/src/lib.rs b/substrate/primitives/application-crypto/src/lib.rs index 69af988b5358..b5a273cd3693 100644 --- a/substrate/primitives/application-crypto/src/lib.rs +++ b/substrate/primitives/application-crypto/src/lib.rs @@ -26,6 +26,7 @@ pub use sp_core::crypto::{key_types, CryptoTypeId, DeriveJunction, KeyTypeId, Ss #[doc(hidden)] pub use sp_core::crypto::{ DeriveError, Pair, ProofOfPossessionGenerator, ProofOfPossessionVerifier, SecretStringError, + POP_CONTEXT_TAG, }; #[doc(hidden)] pub use sp_core::{ @@ -366,6 +367,15 @@ macro_rules! app_crypto_public_common { pub fn into_inner(self) -> $public { self.0 } + + pub fn verify_proof_of_possession( + proof_of_possession: &::Signature, + allegedly_possessed_pubkey: &Self + ) -> bool { + let pub_key_as_bytes = allegedly_possessed_pubkey.0.to_vec(); + let pop_statement = [$crate::POP_CONTEXT_TAG, pub_key_as_bytes.as_slice()].concat(); + <::Pair as $crate::Pair>::verify(&proof_of_possession, pop_statement, allegedly_possessed_pubkey) + } } }; } diff --git a/substrate/primitives/application-crypto/src/sr25519.rs b/substrate/primitives/application-crypto/src/sr25519.rs index e44596f6c979..cbff20e04c59 100644 --- a/substrate/primitives/application-crypto/src/sr25519.rs +++ b/substrate/primitives/application-crypto/src/sr25519.rs @@ -21,7 +21,7 @@ use crate::{KeyTypeId, RuntimePublic}; use alloc::vec::Vec; -use sp_core::crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG}; +use sp_core::crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG, CryptoType}; pub use sp_core::sr25519::*; mod app { @@ -58,7 +58,7 @@ impl RuntimePublic for Public { fn verify_pop(&self, pop: &Self::Signature) -> bool { let pop = AppSignature::from(pop.clone()); let pub_key = AppPublic::from(self.clone()); - AppPair::verify_proof_of_possession(&pop, &pub_key) + ::Pair::verify_proof_of_possession(&pop, &pub_key) } fn to_raw_vec(&self) -> Vec { diff --git a/substrate/primitives/application-crypto/test/Cargo.toml b/substrate/primitives/application-crypto/test/Cargo.toml index e8d3f2beccf3..8bdbc3d107c4 100644 --- a/substrate/primitives/application-crypto/test/Cargo.toml +++ b/substrate/primitives/application-crypto/test/Cargo.toml @@ -20,13 +20,9 @@ sp-api = { workspace = true, default-features = true } sp-application-crypto = { workspace = true, default-features = true } sp-core = { workspace = true, features = ["full_crypto"] } sp-keystore = { workspace = true } -substrate-test-runtime-client = { workspace = true } +substrate-test-runtime-client = { workspace = true, features = ["bls-experimental"] } [features] -default = ["std"] -std = [ - "full_crypto", -] +std = [] -full_crypto = ["sp-core/full_crypto"] -bls-experimental = [] +bls-experimental = ["substrate-test-runtime-client/bls-experimental"] diff --git a/substrate/primitives/application-crypto/test/src/bls381.rs b/substrate/primitives/application-crypto/test/src/bls381.rs index a8a311537670..978e306b532b 100644 --- a/substrate/primitives/application-crypto/test/src/bls381.rs +++ b/substrate/primitives/application-crypto/test/src/bls381.rs @@ -1,5 +1,5 @@ use sp_api::{ApiExt, ProvideRuntimeApi}; -use sp_application_crypto::ecdsa::AppPair; +use sp_application_crypto::bls381::{AppPair, AppPublic}; use sp_core::{ crypto::{ByteArray, Pair}, testing::BLS381, @@ -18,11 +18,8 @@ fn bls381_works_in_runtime() { let mut runtime_api = test_client.runtime_api(); runtime_api.register_extension(KeystoreExt::new(keystore.clone())); - let (signature, public) = runtime_api - .test_bls381_crypto(test_client.chain_info().genesis_hash) - .expect("Tests `bls381` crypto."); + let public = runtime_api.test_bls381_crypto(test_client.chain_info().genesis_hash).expect("things didnt fail"); let supported_keys = keystore.keys(BLS381).unwrap(); assert!(supported_keys.contains(&public.to_raw_vec())); - assert!(AppPair::verify(&signature, "bls381", &public)); } \ No newline at end of file diff --git a/substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs b/substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs new file mode 100644 index 000000000000..e4c9ee8d2281 --- /dev/null +++ b/substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs @@ -0,0 +1,25 @@ +use sp_api::{ApiExt, ProvideRuntimeApi}; +use sp_application_crypto::ecdsa_bls381::{AppPair, AppPublic}; +use sp_core::{ + crypto::{ByteArray, Pair}, + testing::ECDSA_BLS381, +}; +use sp_keystore::{testing::MemoryKeystore, Keystore, KeystoreExt}; +use std::sync::Arc; +use substrate_test_runtime_client::{ + runtime::TestAPI, DefaultTestClientBuilderExt, TestClientBuilder, TestClientBuilderExt, +}; + +#[test] +fn ecdsa_bls381_works_in_runtime() { + let keystore = Arc::new(MemoryKeystore::new()); + let test_client = TestClientBuilder::new().build(); + + let mut runtime_api = test_client.runtime_api(); + runtime_api.register_extension(KeystoreExt::new(keystore.clone())); + + let public = runtime_api.test_ecdsa_bls381_crypto(test_client.chain_info().genesis_hash).expect("things didnt fail"); + + // let supported_keys = keystore.keys(ECDSA_BLS381).unwrap(); + // assert!(supported_keys.contains(&public.expect("Things still no fail").to_raw_vec())); +} diff --git a/substrate/primitives/application-crypto/test/src/lib.rs b/substrate/primitives/application-crypto/test/src/lib.rs index 42ecf98c93c1..5481935bf3d9 100644 --- a/substrate/primitives/application-crypto/test/src/lib.rs +++ b/substrate/primitives/application-crypto/test/src/lib.rs @@ -25,3 +25,6 @@ mod ed25519; mod sr25519; #[cfg(all(test, feature = "bls-experimental"))] mod bls381; + +#[cfg(all(test, feature = "bls-experimental"))] +mod ecdsa_bls381; diff --git a/substrate/test-utils/runtime/client/Cargo.toml b/substrate/test-utils/runtime/client/Cargo.toml index 5dd3c304f4a8..09c90e1da2ae 100644 --- a/substrate/test-utils/runtime/client/Cargo.toml +++ b/substrate/test-utils/runtime/client/Cargo.toml @@ -25,4 +25,7 @@ sp-consensus = { workspace = true, default-features = true } sp-core = { workspace = true, default-features = true } sp-runtime = { workspace = true, default-features = true } substrate-test-client = { workspace = true } -substrate-test-runtime = { workspace = true } +substrate-test-runtime = { workspace = true, features = ["bls-experimental"] } + +[features] +bls-experimental = ["substrate-test-runtime/bls-experimental"] diff --git a/substrate/test-utils/runtime/src/lib.rs b/substrate/test-utils/runtime/src/lib.rs index 2bf52948bcb6..1deeda2c317e 100644 --- a/substrate/test-utils/runtime/src/lib.rs +++ b/substrate/test-utils/runtime/src/lib.rs @@ -185,23 +185,25 @@ pub type Header = sp_runtime::generic::Header; /// Balance of an account. pub type Balance = u64; -#[cfg(not(feature = "bls-experimental"))] -type Bls381Signature = (); - +// Conditional types for `Bls381Signature` #[cfg(feature = "bls-experimental")] -type Bls381Signature = bls381::AppSignature; +pub mod bls_experimental { + use sp_application_crypto::{bls381, ecdsa_bls381}; + pub type Bls381Public = bls381::AppPublic; + pub type EcdsaBls381Public = Option; +} #[cfg(not(feature = "bls-experimental"))] -type Bls381Public = (); - -#[cfg(feature = "bls-experimental")] -type Bls381Public = bls381::AppPublic; +pub mod bls_disabled { + pub type Bls381Public = (); + pub type EcdsaBls381Public = (); +} #[cfg(feature = "bls-experimental")] -compile_error!("bls-experimental is active"); +pub use bls_experimental::*; #[cfg(not(feature = "bls-experimental"))] -compile_error!("bls-experimental is NOT active"); +pub use bls_disabled::*; decl_runtime_apis! { #[api_version(2)] @@ -240,10 +242,14 @@ decl_runtime_apis! { /// /// Returns the signature generated for the message `ecdsa`. fn test_ecdsa_crypto() -> (ecdsa::AppSignature, ecdsa::AppPublic); - /// Test that 'bls381' crypto works in the runtime + /// Test that `bls381` crypto works in the runtime + /// + /// Returns the public key. + fn test_bls381_crypto() -> Bls381Public; + /// Test that `ecdsa_bls381_crypto` works in the runtime /// - /// Returns the signature generated for the message `bls381`. - fn test_bls381_crypto() -> Bls381Public + /// Returns the public key + fn test_ecdsa_bls381_crypto() -> EcdsaBls381Public; /// Run various tests against storage. fn test_storage(); /// Check a witness. @@ -610,11 +616,21 @@ impl_runtime_apis! { test_bls381_crypto() } + #[cfg(feature = "bls-experimental")] + fn test_ecdsa_bls381_crypto() -> EcdsaBls381Public { + test_ecdsa_bls381_crypto() + } + #[cfg(not(feature = "bls-experimental"))] fn test_bls381_crypto() -> Bls381Public { () } + #[cfg(not(feature = "bls-experimental"))] + fn test_ecdsa_bls381_crypto() { + () + } + fn test_storage() { test_read_storage(); test_read_child_storage(); @@ -842,22 +858,23 @@ fn test_ecdsa_crypto() -> (ecdsa::AppSignature, ecdsa::AppPublic) { #[cfg(feature = "bls-experimental")] fn test_bls381_crypto() -> Bls381Public { - let public0 = bls381::AppPublic::generate_pair(None); - // let public1 = bls381::AppPublic::generate_pair(None); - // let public2 = bls381::AppPublic::generate_pair(None); + let mut public0 = bls381::AppPublic::generate_pair(None); + + let pop = public0.generate_pop().expect("Can generate Pop for bls381"); - // let all = bls381::AppPublic::all(); - // assert!(all.contains(&public0)); - // assert!(all.contains(&public1)); - // assert!(all.contains(&public2)); + assert!(public0.verify_pop(&pop)); + public0 +} - // let pop = bls381::AppPublic::generate_pop(); +#[cfg(feature = "bls-experimental")] +fn test_ecdsa_bls381_crypto() -> EcdsaBls381Public { + let mut public0 = ecdsa_bls381::AppPublic::generate_pair(None); - // let signature = public0.sign(&"bls381").expect("Generates a valid `bls381` signature."); + let pop = public0.generate_pop().expect("Can generate ecdsa_bls381"); - // assert!(public0.verify(&"bls381", &signature)); - // (signature, public0) - // (bls381::AppSignature::from_bytes(&"0"), bls381::AppPublic::from_bytes(&"9")) + // assert!(public0.verify_pop(&pop)); + // public0 + None } fn test_read_storage() {