Skip to content

Commit

Permalink
fixing keystore issue and refactoring out bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
coax1d committed Jan 9, 2025
1 parent c94685b commit c913121
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 75 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,6 @@ members = [
"substrate/primitives/crypto/ec-utils",
"substrate/primitives/crypto/hashing",
"substrate/primitives/crypto/hashing/proc-macro",
"substrate/primitives/crypto/pubkeycrypto",
"substrate/primitives/crypto/pubkeycrypto/proc-macro",
"substrate/primitives/database",
"substrate/primitives/debug-derive",
"substrate/primitives/externalities",
Expand Down
17 changes: 15 additions & 2 deletions substrate/client/keystore/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ impl LocalKeystore {
.map(|pair| pair.vrf_pre_output(input));
Ok(pre_output)
}

fn generate_pop<T: CorePair + ProofOfPossessionGenerator>(
&self,
key_type: KeyTypeId,
public: &T::Public,
) -> std::result::Result<Option<T::Signature>, TraitError> {
let pop = self
.0
.read()
.key_pair_by_type::<T>(public, key_type)?
.map(|mut pair| pair.generate_proof_of_possession());
Ok(pop)
}
}

impl Keystore for LocalKeystore {
Expand Down Expand Up @@ -362,7 +375,7 @@ impl Keystore for LocalKeystore {
key_type: KeyTypeId,
public: &bls381::Public
) -> std::result::Result<Option<bls381::Signature>, TraitError> {
self.bls381_generate_pop(key_type, public)
self.generate_pop::<bls381::Pair>(key_type, public)
}

fn ecdsa_bls381_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa_bls381::Public> {
Expand All @@ -374,7 +387,7 @@ impl Keystore for LocalKeystore {
key_type: KeyTypeId,
public: &ecdsa_bls381::Public
) -> std::result::Result<Option<ecdsa_bls381::Signature>, TraitError> {
self.ecdsa_bls381_generate_pop(key_type, public)
self.generate_pop::<ecdsa_bls381::Pair>(key_type, public)
}

/// Generate a new pair of paired-keys compatible with the '(ecdsa,bls381)' signature scheme.
Expand Down
2 changes: 2 additions & 0 deletions substrate/primitives/application-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ codec = { features = ["derive"], workspace = true }
scale-info = { features = ["derive"], workspace = true }
serde = { optional = true, features = ["alloc", "derive"], workspace = true }
sp-io = { workspace = true }
log = { workspace = true }

[features]
default = ["std"]
std = [
"codec/std",
"full_crypto",
"log/std",
"scale-info/std",
"serde/std",
"sp-core/std",
Expand Down
25 changes: 16 additions & 9 deletions substrate/primitives/application-crypto/src/ecdsa_bls381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use sp_core::paired_crypto::ecdsa_bls381::*;
use sp_core::{
bls381,
crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG, CryptoType},
ecdsa, ecdsa_bls381,
ecdsa, ecdsa_bls381, testing::{ECDSA, BLS381},
};

mod app {
Expand All @@ -43,13 +43,10 @@ impl RuntimePublic for Public {
Vec::new()
}

fn generate_pair(key_type: KeyTypeId, seed: Option<Vec<u8>>) -> Self {
let tuple = sp_io::crypto::ecdsa_bls381_generate(key_type, seed);
let ecdsa_pub = tuple.0;
let bls381_pub = tuple.1;
let mut combined_pub_raw = [0u8; ecdsa_bls381::PUBLIC_KEY_LEN];
combined_pub_raw[..ecdsa::PUBLIC_KEY_SERIALIZED_SIZE].copy_from_slice(ecdsa_pub.as_ref());
combined_pub_raw[ecdsa::PUBLIC_KEY_SERIALIZED_SIZE..].copy_from_slice(bls381_pub.as_ref());
fn generate_pair(_key_type: KeyTypeId, seed: Option<Vec<u8>>) -> Self {
let ecdsa_pub = sp_io::crypto::ecdsa_generate(ECDSA, seed.clone());
let bls381_pub = sp_io::crypto::bls381_generate(BLS381, seed);
let combined_pub_raw = combine_pub(&ecdsa_pub, &bls381_pub);
Self::from_raw(combined_pub_raw)
}

Expand All @@ -68,7 +65,6 @@ impl RuntimePublic for Public {
return None
}

// Import logger and logg stuff here to see where it fails
let pub_key_as_bytes = self.to_raw_vec();

let (ecdsa_pub_as_bytes, bls381_pub_as_bytes) = split_pub_key_bytes(&pub_key_as_bytes)?;
Expand Down Expand Up @@ -132,6 +128,17 @@ fn combine_pop(
Some(combined_pop_raw)
}

/// Helper: Combine ECDSA and BLS381 pubs into single raw pub
fn combine_pub(
ecdsa_pub: &ecdsa::Public,
bls381_pub: &bls381::Public,
) -> [u8; ecdsa_bls381::PUBLIC_KEY_LEN] {
let mut combined_pub_raw = [0u8; ecdsa_bls381::PUBLIC_KEY_LEN];
combined_pub_raw[..ecdsa::PUBLIC_KEY_SERIALIZED_SIZE].copy_from_slice(ecdsa_pub.as_ref());
combined_pub_raw[ecdsa::PUBLIC_KEY_SERIALIZED_SIZE..].copy_from_slice(bls381_pub.as_ref());
combined_pub_raw
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions substrate/primitives/application-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ macro_rules! app_crypto_public_common {
self.0
}

/// Verify the proposed proof of possession is correct.
pub fn verify_proof_of_possession(
proof_of_possession: &<Self as $crate::AppCrypto>::Signature,
allegedly_possessed_pubkey: &Self
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/application-crypto/test/src/bls381.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_application_crypto::bls381::{AppPair, AppPublic};
use sp_core::{
crypto::{ByteArray, Pair},
crypto::ByteArray,
testing::BLS381,
};
use sp_keystore::{testing::MemoryKeystore, Keystore, KeystoreExt};
Expand All @@ -12,6 +11,7 @@ use substrate_test_runtime_client::{

#[test]
fn bls381_works_in_runtime() {
sp_tracing::try_init_simple();
let keystore = Arc::new(MemoryKeystore::new());
let test_client = TestClientBuilder::new().build();

Expand Down
1 change: 1 addition & 0 deletions substrate/primitives/application-crypto/test/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use substrate_test_runtime_client::{

#[test]
fn ecdsa_works_in_runtime() {
sp_tracing::try_init_simple();
let keystore = Arc::new(MemoryKeystore::new());
let test_client = TestClientBuilder::new().build();

Expand Down
15 changes: 2 additions & 13 deletions substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
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 sp_keystore::{testing::MemoryKeystore, KeystoreExt};
use std::sync::Arc;
use substrate_test_runtime_client::{
runtime::TestAPI, DefaultTestClientBuilderExt, TestClientBuilder, TestClientBuilderExt,
Expand All @@ -19,11 +14,5 @@ fn ecdsa_bls381_works_in_runtime() {
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");

// println!("Value returned is {:?}", public);


// let supported_keys = keystore.keys(ECDSA_BLS381).unwrap();
// assert!(supported_keys.contains(&public.expect("Things still no fail").to_raw_vec()));
runtime_api.test_ecdsa_bls381_crypto(test_client.chain_info().genesis_hash).expect("Things didnt fail");
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use substrate_test_runtime_client::{

#[test]
fn ed25519_works_in_runtime() {
sp_tracing::try_init_simple();
let keystore = Arc::new(MemoryKeystore::new());
let test_client = TestClientBuilder::new().build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use substrate_test_runtime_client::{

#[test]
fn sr25519_works_in_runtime() {
sp_tracing::try_init_simple();
let keystore = Arc::new(MemoryKeystore::new());
let test_client = TestClientBuilder::new().build();

Expand Down
29 changes: 1 addition & 28 deletions substrate/primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use sp_core::{
};

#[cfg(feature = "bls-experimental")]
use sp_core::{bls381, ecdsa_bls381};
use sp_core::bls381;

#[cfg(feature = "std")]
use sp_trie::{LayoutV0, LayoutV1, TrieConfiguration};
Expand Down Expand Up @@ -1236,33 +1236,6 @@ pub trait Crypto {
.flatten()
}

/// Generate an `(ecdsa,bls12-381)` key for the given key type using an optional `seed` and
/// store it in the keystore.
///
/// The `seed` needs to be a valid utf8.
///
/// Returns the public key.
#[cfg(feature = "bls-experimental")]
fn ecdsa_bls381_generate(
&mut self,
id: KeyTypeId,
seed: Option<Vec<u8>>,
) -> (ecdsa::Public, bls381::Public) {
let seed = seed.as_ref().map(|s| std::str::from_utf8(s).expect("Seed is valid utf8!"));

let ecdsa_pub = self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.ecdsa_generate_new(id, seed)
.expect("`ecdsa_bls381_generate` failed");

let bls_pub = self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.bls381_generate_new(id, seed)
.expect("`ecdsa_bls381_generate` failed");

(ecdsa_pub, bls_pub)
}

/// Generate a `bandersnatch` key pair for the given key type using an optional
/// `seed` and store it in the keystore.
///
Expand Down
5 changes: 3 additions & 2 deletions substrate/primitives/keystore/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use crate::{Error, Keystore, KeystorePtr};
#[cfg(feature = "bandersnatch-experimental")]
use sp_core::bandersnatch;
#[cfg(feature = "bls-experimental")]
use sp_core::{bls381, ecdsa_bls381, KeccakHasher};
use sp_core::{bls381, ecdsa_bls381, KeccakHasher, crypto::ProofOfPossessionGenerator};
use sp_core::{
crypto::{ByteArray, KeyTypeId, Pair, ProofOfPossessionGenerator, VrfSecret},
crypto::{ByteArray, KeyTypeId, Pair, VrfSecret},
ecdsa, ed25519, sr25519,
};

Expand Down Expand Up @@ -123,6 +123,7 @@ impl MemoryKeystore {
Ok(pre_output)
}

#[cfg(feature = "bls-experimental")]
fn generate_pop<T: Pair + ProofOfPossessionGenerator>(
&self,
key_type: KeyTypeId,
Expand Down
2 changes: 1 addition & 1 deletion substrate/primitives/runtime/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl sp_application_crypto::RuntimeAppPublic for UintAuthorityId {
None
}

fn verify_pop(&self, pop: &Self::Signature) -> bool {
fn verify_pop(&self, _pop: &Self::Signature) -> bool {
false
}

Expand Down
19 changes: 3 additions & 16 deletions substrate/test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub type Balance = u64;
pub mod bls_experimental {
use sp_application_crypto::{bls381, ecdsa_bls381};
pub type Bls381Public = bls381::AppPublic;
pub type EcdsaBls381Public = Option<ecdsa_bls381::AppPublic>;
pub type EcdsaBls381Public = ecdsa_bls381::AppPublic;
}

#[cfg(not(feature = "bls-experimental"))]
Expand Down Expand Up @@ -869,24 +869,11 @@ fn test_bls381_crypto() -> Bls381Public {
#[cfg(feature = "bls-experimental")]
fn test_ecdsa_bls381_crypto() -> EcdsaBls381Public {
let mut public0 = ecdsa_bls381::AppPublic::generate_pair(Some("have fabric vehicle glide wise exit drip movie parent knee grief squirrel".into()));
let mut public1 = ecdsa::AppPublic::generate_pair(Some("have fabric vehicle glide wise exit drip movie parent knee grief squirrel".into()));
let mut public2 = bls381::AppPublic::generate_pair(Some("have fabric vehicle glide wise exit drip movie parent knee grief squirrel".into()));

let ecdsa_bls381_bytes = public0.clone().to_raw_vec();
let ecdsa_bytes = public1.to_raw_vec();
let bls381_bytes = public2.to_raw_vec();

log::error!("ecdsa_bls381_bytes:: {:?} \nlen:: {}", ecdsa_bls381_bytes, ecdsa_bls381_bytes.len());
log::error!("ecdsa_bytes:: {:?} \nlen:: {}", ecdsa_bytes, ecdsa_bytes.len());
log::error!("bls381_bytes:: {:?} \nlen:: {}", bls381_bytes, bls381_bytes.len());


let pop = public0.generate_pop().expect("Can Generate Pop");


// assert!(public0.verify_pop(&pop));
// public0
None
assert!(public0.verify_pop(&pop));
public0
}

fn test_read_storage() {
Expand Down

0 comments on commit c913121

Please sign in to comment.