Skip to content

Commit

Permalink
Remove BLS Host function (#731)
Browse files Browse the repository at this point in the history
1. Removes the Host functions required for BLS 
2. Removes BLS Keystores
3. Cleans up bls-primitives
  • Loading branch information
Gauthamastro committed Apr 27, 2023
2 parents 896cc84 + e18a27f commit 60eb4fa
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 387 deletions.
124 changes: 123 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ edition = "2021"
sc-executor = { workspace = true }
node-polkadex-runtime = { path = "../runtime", version = "4.0.0" }
frame-benchmarking = { workspace = true }
bls-primitives = { path = "../primitives/bls-primitives" }
5 changes: 1 addition & 4 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
pub struct ExecutorDispatch;

impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
type ExtendHostFunctions = (
frame_benchmarking::benchmarking::HostFunctions,
bls_primitives::crypto::bls_ext::HostFunctions,
);
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
node_polkadex_runtime::api::dispatch(method, data)
Expand Down
1 change: 0 additions & 1 deletion clients/orderbook/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod rpc;
pub mod sync;

use crate::protocol_standard_name;
use bls_primitives::BLS_DEV_PHRASE;
use futures::{channel::mpsc::UnboundedSender, stream::FuturesUnordered, StreamExt};
use memory_db::{HashKey, MemoryDB};
use orderbook_primitives::{
Expand Down
4 changes: 2 additions & 2 deletions clients/thea/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ impl From<sc_keystore::Error> for Error {
}
}

impl From<blst::BLST_ERROR> for Error {
fn from(value: blst::BLST_ERROR) -> Self {
impl From<bls_primitives::Error> for Error {
fn from(value: bls_primitives::Error) -> Self {
Self::BLSError(format!("{value:?}"))
}
}
8 changes: 3 additions & 5 deletions clients/thea/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use sp_consensus::SyncOracle;
use sp_core::{Pair, H256};
use sp_keyring::AccountKeyring;
use sp_keystore::CryptoStore;
use sp_runtime::traits::AppVerify;

use crate::Client;
use polkadex_primitives::utils::return_set_bits;
Expand Down Expand Up @@ -87,12 +88,9 @@ impl TestApi {
signatories.push((*auths.get(index).unwrap()).clone().into());
}

let bls_signature: bls_primitives::Signature = signature.into();
// Check signature
assert!(bls_primitives::crypto::verify_aggregate_(
&signatories[..],
&message.encode(),
&signature.into(),
));
assert!(bls_signature.verify(&signatories, &message.encode()));

self.incoming_nonce.write().insert(message.network, message.nonce);
self.incoming_messages.write().insert((message.network, message.nonce), message);
Expand Down
6 changes: 1 addition & 5 deletions clients/thea/src/tests/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ impl ForeignConnector for DummyForeignConnector {
}

// Check signature
assert!(bls_primitives::crypto::verify_aggregate_(
&signatories[..],
&message.encode(),
&payload.aggregate_signature.into(),
));
assert!(payload.aggregate_signature.verify(&signatories, &message.encode()));

*self.incoming_nonce.write() = message.nonce;
self.incoming_messages.write().insert(message.nonce, message);
Expand Down
2 changes: 1 addition & 1 deletion pallets/ocex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ frame-benchmarking = { workspace = true, default-features = false, optional = tr
sp-core = { workspace = true, default-features = false }
liquidity = { path = "../liquidity", default-features = false }
orderbook-primitives = { path = "../../primitives/orderbook", default-features = false }
bls-primitives = { path = "../../primitives/bls-primitives", default-features = false }
sp-application-crypto = { workspace = true }

[dev-dependencies]
Expand All @@ -35,6 +34,7 @@ pallet-balances = { workspace = true, features = ["std"] }
sp-application-crypto = { workspace = true }
sp-keystore = { workspace = true }
sp-io = { workspace = true }
bls-primitives = { path = "../../primitives/bls-primitives", default-features = false }

[features]
default = ["std"]
Expand Down
6 changes: 1 addition & 5 deletions pallets/ocex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1468,11 +1468,7 @@ impl<T: Config + frame_system::offchain::SendTransactionTypes<Call<T>>> Pallet<T
match snapshot_summary.aggregate_signature {
None => return InvalidTransaction::Custom(12).into(),
Some(signature) => {
if !bls_primitives::crypto::bls_ext::verify(
&authority.into(),
&snapshot_summary.sign_data(),
&signature,
) {
if !signature.verify(&[authority.into()], &snapshot_summary.sign_data()) {
return InvalidTransaction::Custom(13).into()
}
},
Expand Down
8 changes: 3 additions & 5 deletions pallets/thea-message-handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,10 @@ impl<T: Config> Pallet<T> {
Some(auth) => signatories.push((*auth).clone().into()),
}
}

// Verify the aggregate signature.
if !bls_primitives::crypto::bls_ext::verify_aggregate(
&signatories[..],
&payload.encode(),
&(*signature).clone().into(),
) {
let bls_signature: bls_primitives::Signature = signature.clone().into();
if !bls_signature.verify(&signatories, payload.encode().as_ref()) {
return Err(InvalidTransaction::BadSigner.into())
}

Expand Down
7 changes: 2 additions & 5 deletions pallets/thea/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,8 @@ impl<T: Config> Pallet<T> {
}
}
// Verify the aggregate signature.
if !bls_primitives::crypto::bls_ext::verify_aggregate(
&signatories[..],
&payload.encode(),
&(*signature).clone().into(),
) {
let bls_signature: bls_primitives::Signature = signature.clone().into();
if !bls_signature.verify(&signatories, payload.encode().as_ref()) {
return Err(InvalidTransaction::BadSigner.into())
}

Expand Down
13 changes: 13 additions & 0 deletions primitives/bls-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@ parity-scale-codec = { workspace = true, default-features = false, features = ["
scale-info = { workspace = true, default-features = false, features = ["derive"] }
hex = { version = "0.4.3", optional = true }


# Ark works
ark-bls12-381 = {version="0.4.0", default-features = false, features = ["curve"]}
ark-ec = {version="0.4.2", default-features = false}
ark-ff = {version="0.4.2", default-features = false}
ark-serialize = {version="0.4.2", default-features = false}
sha2 = {version="0.10.6", default-features = false}

[features]
default = ["std"]
std = [
"sha2/std",
"ark-bls12-381/std",
"ark-ec/std",
"ark-ff/std",
"ark-serialize/std",
"log",
"hex",
"serde_json",
Expand Down
34 changes: 6 additions & 28 deletions primitives/bls-primitives/src/application_crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,22 @@ impl RuntimePublic for Public {
type Signature = Signature;

fn all(_: KeyTypeId) -> Vec<Self> {
crypto::bls_ext::all()
unimplemented!()
}

fn generate_pair(_: KeyTypeId, seed: Option<Vec<u8>>) -> Self {
crypto::bls_ext::generate_pair_and_store(seed)
fn generate_pair(_: KeyTypeId, _: Option<Vec<u8>>) -> Self {
unimplemented!()
}

fn sign<M: AsRef<[u8]>>(&self, _: KeyTypeId, msg: &M) -> Option<Self::Signature> {
crypto::bls_ext::sign(self, msg.as_ref())
fn sign<M: AsRef<[u8]>>(&self, _: KeyTypeId, _: &M) -> Option<Self::Signature> {
unimplemented!()
}

fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
crypto::bls_ext::verify(self, msg.as_ref(), signature)
signature.verify(&[*self], msg.as_ref())
}

fn to_raw_vec(&self) -> Vec<u8> {
self.0.to_vec()
}
}

#[cfg(test)]
mod tests {
use crate::crypto::sign;
use sp_core::blake2_256;

#[test]
pub fn test_generate_and_load_back() {
use super::*;
let key_type = KeyTypeId(*b"blsk");
let public = Public::generate_pair(key_type, Some(b"owner word vocal dose decline sunset battle example forget excite gentle waste//1//orderbook".to_vec()));
let loaded_keys = Public::all(key_type);
assert_eq!(loaded_keys.len(), 1);
assert_eq!(loaded_keys[0], public);
let message = blake2_256(&vec![0, 1]);

let signature = sign(&public, &message).unwrap();
println!("Pubkey: {:?}", public.0);
println!("Signature: {:?}", signature.0);
assert!(crate::crypto::bls_ext::verify(&public, message.as_ref(), &signature));
}
}
Loading

0 comments on commit 60eb4fa

Please sign in to comment.