Skip to content

Commit

Permalink
add back acss impl to bls impl
Browse files Browse the repository at this point in the history
  • Loading branch information
driemworks authored and juangirini committed May 22, 2024
1 parent d9dc9a0 commit d5043ab
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
2 changes: 2 additions & 0 deletions substrate/client/keystore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 14 additions & 16 deletions substrate/client/keystore/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -430,20 +428,20 @@ impl Keystore for LocalKeystore {
) -> std::result::Result<bls377::Signature, TraitError> {
if let Some(Some(etf_pair)) = self.0.read()
.key_pair_by_type::<bls377::Pair>(public, key_type)?
// .map(|pair| pair.acss_recover(pok_bytes, threshold)) {
.map(|pair| {
if let Ok(pok) = BatchPoK::<<TinyBLS377 as EngineBLS>::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::<<TinyBLS377 as EngineBLS>::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);
Expand Down
4 changes: 4 additions & 0 deletions substrate/primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +94,7 @@ std = [
"codec/std",
"dyn-clonable",
"ed25519-zebra/std",
"etf-crypto-primitives/std",
"full_crypto",
"futures",
"futures/thread-pool",
Expand Down
26 changes: 25 additions & 1 deletion substrate/primitives/core/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -139,7 +146,24 @@ fn derive_hard_junction<T: HardJunctionId>(secret_seed: &Seed, cc: &[u8; 32]) ->
(T::ID, secret_seed, cc).using_encoded(sp_crypto_hashing::blake2_256)
}

impl<T: EngineBLS> Pair<T> {}
impl<T: EngineBLS> Pair<T> {
/// 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<Self> {
let mutable_self = self.clone();
if let Ok(pok) = BatchPoK::<T::PublicKeyGroup>::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<T: BlsBound> TraitPair for Pair<T> {
type Seed = Seed;
Expand Down
16 changes: 7 additions & 9 deletions substrate/primitives/keystore/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,13 @@ impl Keystore for MemoryKeystore {
msg: &[u8],
threshold: u8
) -> Result<bls377::Signature, Error> {
// let sig = self.pair::<bls377::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::<bls377::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<(), ()> {
Expand Down

0 comments on commit d5043ab

Please sign in to comment.