Skip to content

Commit

Permalink
fixing AppPair usage and fixed bls test paired still broken
Browse files Browse the repository at this point in the history
  • Loading branch information
coax1d committed Dec 16, 2024
1 parent cc71f5a commit adfb068
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 50 deletions.
4 changes: 2 additions & 2 deletions substrate/primitives/application-crypto/src/bandersnatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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)
<AppPublic as CryptoType>::Pair::verify_proof_of_possession(&pop, &pub_key)
}

fn to_raw_vec(&self) -> Vec<u8> {
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/application-crypto/src/bls381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
<AppPublic as CryptoType>::Pair::verify_proof_of_possession(&pop, &pub_key)
}

fn to_raw_vec(&self) -> Vec<u8> {
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/application-crypto/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
<AppPublic as CryptoType>::Pair::verify_proof_of_possession(&pop, &pub_key)
}

fn to_raw_vec(&self) -> Vec<u8> {
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/application-crypto/src/ecdsa_bls381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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)
<AppPublic as CryptoType>::Pair::verify_proof_of_possession(&pop, &pub_key)
}

fn to_raw_vec(&self) -> Vec<u8> {
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/application-crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
<AppPublic as CryptoType>::Pair::verify_proof_of_possession(&pop, &pub_key)
}

fn to_raw_vec(&self) -> Vec<u8> {
Expand Down
10 changes: 10 additions & 0 deletions substrate/primitives/application-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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: &<Self as $crate::AppCrypto>::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();
<<Self as $crate::AppCrypto>::Pair as $crate::Pair>::verify(&proof_of_possession, pop_statement, allegedly_possessed_pubkey)
}
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/application-crypto/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
<AppPublic as CryptoType>::Pair::verify_proof_of_possession(&pop, &pub_key)
}

fn to_raw_vec(&self) -> Vec<u8> {
Expand Down
10 changes: 3 additions & 7 deletions substrate/primitives/application-crypto/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
7 changes: 2 additions & 5 deletions substrate/primitives/application-crypto/test/src/bls381.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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));
}
25 changes: 25 additions & 0 deletions substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs
Original file line number Diff line number Diff line change
@@ -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()));
}
3 changes: 3 additions & 0 deletions substrate/primitives/application-crypto/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 4 additions & 1 deletion substrate/test-utils/runtime/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
67 changes: 42 additions & 25 deletions substrate/test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,25 @@ pub type Header = sp_runtime::generic::Header<BlockNumber, Hashing>;
/// 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<ecdsa_bls381::AppPublic>;
}

#[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)]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit adfb068

Please sign in to comment.