diff --git a/substrate/client/keystore/Cargo.toml b/substrate/client/keystore/Cargo.toml index 876cb40cb367..205d3092db16 100644 --- a/substrate/client/keystore/Cargo.toml +++ b/substrate/client/keystore/Cargo.toml @@ -25,6 +25,8 @@ sp-application-crypto = { path = "../../primitives/application-crypto" } sp-core = { path = "../../primitives/core" } sp-keystore = { path = "../../primitives/keystore" } etf-crypto-primitives = { git = "http://github.com/ideal-lab5/etf-sdk", branch = "w3fbls-migration" } +ark-serialize = "0.4.0" +w3f-bls = "0.1.3" [dev-dependencies] tempfile = "3.1.0" diff --git a/substrate/client/keystore/src/local.rs b/substrate/client/keystore/src/local.rs index daf842c82b2a..f0bfc68a5ba8 100644 --- a/substrate/client/keystore/src/local.rs +++ b/substrate/client/keystore/src/local.rs @@ -38,8 +38,6 @@ use sp_core::bandersnatch; sp_keystore::bls_experimental_enabled! { use sp_core::{bls377, bls381, ecdsa_bls377, KeccakHasher}; -use w3f_bls::{EngineBLS, TinyBLS377}; -use etf_crypto_primitives::proofs::hashed_el_gamal_sigma::BatchPoK; pub const ETF_KEY_TYPE: KeyTypeId = KeyTypeId(*b"etfn"); } use crate::{Error, Result}; @@ -430,20 +428,20 @@ impl Keystore for LocalKeystore { ) -> std::result::Result { if let Some(Some(etf_pair)) = self.0.read() .key_pair_by_type::(public, key_type)? - // .map(|pair| pair.acss_recover(pok_bytes, threshold)) { - .map(|pair| { - if let Ok(pok) = BatchPoK::<::PublicKeyGroup>::deserialize_compressed(&pok_bytes[..]) { - let sk = ETFKeypair(pair.0.into_vartime()); - if let Ok(recovered) = sk.recover(pok, threshold) { - let secret = w3f_bls::SecretKeyVT(recovered.0).into_split_dirty(); - let public = secret.into_public(); - return Pair(w3f_bls::Keypair { - secret, public, - }); - } - } - pair - }) { + .map(|pair| pair.acss_recover(pok_bytes, threshold)) { + // .map(|pair| { + // if let Ok(pok) = BatchPoK::<::PublicKeyGroup>::deserialize_compressed(&pok_bytes[..]) { + // let sk = ETFKeypair(pair.0.into_vartime()); + // if let Ok(recovered) = sk.recover(pok, threshold) { + // let secret = w3f_bls::SecretKeyVT(recovered.0).into_split_dirty(); + // let public = secret.into_public(); + // return Some(bls::Pair(w3f_bls::Keypair { + // secret, public, + // })); + // } + // } + // None + // }) { // "IBE.Extract" Q = s*H(message) + DLEQ Proof let extract = etf_pair.sign(&message); return Ok(extract); diff --git a/substrate/primitives/core/Cargo.toml b/substrate/primitives/core/Cargo.toml index f931faf8bd04..4a647b509038 100644 --- a/substrate/primitives/core/Cargo.toml +++ b/substrate/primitives/core/Cargo.toml @@ -60,6 +60,9 @@ k256 = { version = "0.13.3", features = ["alloc", "ecdsa"], default-features = f # secp256k1 crate, better performance, intended to be used on host side (std) secp256k1 = { version = "0.28.0", default-features = false, features = ["alloc", "recovery"], optional = true } +# etf primitives +etf-crypto-primitives = { git = "http://github.com/ideal-lab5/etf-sdk", branch = "w3fbls-migration", default-features = false} +ark-serialize = "0.4.0" # bls crypto w3f-bls = { version = "0.1.3", default-features = false, optional = true } # bandersnatch crypto @@ -91,6 +94,7 @@ std = [ "codec/std", "dyn-clonable", "ed25519-zebra/std", + "etf-crypto-primitives/std", "full_crypto", "futures", "futures/thread-pool", diff --git a/substrate/primitives/core/src/bls.rs b/substrate/primitives/core/src/bls.rs index bb04babb3f18..14b06b1292ec 100644 --- a/substrate/primitives/core/src/bls.rs +++ b/substrate/primitives/core/src/bls.rs @@ -35,6 +35,13 @@ use w3f_bls::{ SecretKey, SerializableToBytes, TinyBLS381, }; +use etf_crypto_primitives::{ + dpss::Keypair as ETFKeypair, + proofs::hashed_el_gamal_sigma::BatchPoK +}; + +use ark_serialize::CanonicalDeserialize; + /// BLS-377 specialized types pub mod bls377 { pub use super::{PUBLIC_KEY_SERIALIZED_SIZE, SIGNATURE_SERIALIZED_SIZE}; @@ -139,7 +146,24 @@ fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> (T::ID, secret_seed, cc).using_encoded(sp_crypto_hashing::blake2_256) } -impl Pair {} +impl Pair { + /// the ACSS Recover algorithm + /// attempt to recover a keypair from the proof of knowledge + pub fn acss_recover(&self, pok_bytes: &[u8], threshold: u8) -> Option { + let mutable_self = self.clone(); + if let Ok(pok) = BatchPoK::::deserialize_compressed(&pok_bytes[..]) { + let sk = ETFKeypair(mutable_self.0.into_vartime()); + if let Ok(recovered) = sk.recover(pok, threshold) { + let secret = w3f_bls::SecretKeyVT(recovered.0).into_split_dirty(); + let public = secret.into_public(); + return Some(Pair(w3f_bls::Keypair { + secret, public, + })); + } + } + None + } +} impl TraitPair for Pair { type Seed = Seed; diff --git a/substrate/primitives/keystore/src/testing.rs b/substrate/primitives/keystore/src/testing.rs index 3645f5a09dbb..e2b1208a6ed1 100644 --- a/substrate/primitives/keystore/src/testing.rs +++ b/substrate/primitives/keystore/src/testing.rs @@ -368,15 +368,13 @@ impl Keystore for MemoryKeystore { msg: &[u8], threshold: u8 ) -> Result { - // let sig = self.pair::(key_type, public) - // .map(|pair| pair.acss_recover(pok, threshold)) - // .ok_or(return Err(Error::Unavailable))? - // .unwrap(); - // let extract = sig.sign(&msg); - // // return Ok(extract); - // Ok(extract) - // TODO - return Err(Error::Unavailable); + let sig = self.pair::(key_type, public) + .map(|pair| pair.acss_recover(pok, threshold)) + .ok_or(return Err(Error::Unavailable))? + .unwrap(); + let extract = sig.sign(&msg); + // return Ok(extract); + Ok(extract) } fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()> {