Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add old bls host functions #899

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 98 additions & 60 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ members = [
"primitives/orderbook",
"primitives/polkadex",
"primitives/thea",
"primitives/bls",
"pallets/thea",
"pallets/liquidity",
"pallets/thea-executor",
Expand Down Expand Up @@ -65,6 +66,7 @@ default-members = [
"primitives/orderbook",
"primitives/polkadex",
"primitives/thea",
"primitives/bls",
"pallets/thea",
"pallets/liquidity",
"pallets/thea-executor",
Expand All @@ -86,7 +88,6 @@ static_assertions = "1.1.0"
parity-scale-codec = { version = "3.1.5", default-features = false }
rust_decimal = { git = "https://github.com/Polkadex-Substrate/rust-decimal.git", branch = "master", default-features = false }
scale-info = { version = "2.1.2", default-features = false }
bls-primitives = { path = "primitives/bls-primitives", default-features = false }
thea-primitives = { path = "primitives/thea", default-features = false }
frame-system = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0", default-features = false }
frame-support = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
sc-executor = { workspace = true }
bls-primitives = { path = "../primitives/bls" }
node-polkadex-runtime = { path = "../runtimes/mainnet" }
frame-benchmarking = { workspace = true }
sp-statement-store = { workspace = true }
3 changes: 3 additions & 0 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub struct ExecutorDispatch;
impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
type ExtendHostFunctions = (
frame_benchmarking::benchmarking::HostFunctions,
// NOTE: BLS host functions is a un-removable relic and should not be used or removed from
// here
bls_primitives::host_functions::bls_crypto_ext::HostFunctions,
sp_statement_store::runtime_api::HostFunctions,
);

Expand Down
54 changes: 54 additions & 0 deletions primitives/bls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
name = "bls-primitives"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = { workspace = true, optional = true }
substrate-bip39 = { version = "0.4.4", optional = true }
tiny-bip39 = { version = "1.0.0", optional = true }
sp-runtime-interface = { workspace = true, default-features = false }
sp-application-crypto = { workspace = true, default-features = false }
sp-keystore = { workspace = true, default-features = false }
sp-externalities = { workspace = true, default-features = false }
sp-core = { workspace = true, default-features = false }
sp-std = { workspace = true, default-features = false }
serde_json = { workspace = true, optional = true }
blst = { version = "0.3.10", default-features = false, optional = true }
parity-scale-codec = { workspace = true, default-features = false, features = ["max-encoded-len"] }
scale-info = { workspace = true, default-features = false, features = ["derive"] }
hex = { workspace = true, 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",
"parity-scale-codec/std",
"sp-application-crypto/std",
"sp-keystore/std",
"sp-externalities/std",
"scale-info/std",
"sp-runtime-interface/std",
"sp-core/full_crypto",
"sp-core/std",
"sp-std/std",
"substrate-bip39",
"tiny-bip39",
"blst",
]
79 changes: 79 additions & 0 deletions primitives/bls/src/application_crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// This file is part of Polkadex.
//
// Copyright (c) 2023 Polkadex oü.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// DISCLAIMER: This module is deprecated and exists solely for the host function required during
// block sync. It will not be maintained and must not be used in production.

#[cfg(feature = "std")]
pub use app::Pair as AppPair;
pub use app::{Public as AppPublic, Signature as AppSignature};
use sp_application_crypto::{KeyTypeId, RuntimePublic};
use sp_std::vec::Vec;

pub use crate::*;

pub mod app {
use sp_core::crypto::KeyTypeId;

pub const BLS: KeyTypeId = KeyTypeId(*b"blsk");

sp_application_crypto::app_crypto!(super, BLS);

// impl sp_application_crypto::BoundToRuntimeAppPublic for Public {
// type Public = Self;
// }
}

impl RuntimePublic for Public {
type Signature = Signature;

fn all(_: KeyTypeId) -> Vec<Self> {
unimplemented!(
"BLS12-381 Host functions are not yet available in Polkadot,\
so this will not work"
)
}

#[cfg(not(feature = "parachain"))]
fn generate_pair(key: KeyTypeId, seed: Option<Vec<u8>>) -> Self {
crate::host_functions::bls_crypto_ext::bls_generate_pair(key, seed)
}

#[cfg(feature = "parachain")]
fn generate_pair(_: KeyTypeId, _: Option<Vec<u8>>) -> Self {
unimplemented!(
"BLS12-381 Host functions are not yet available in Polkadot,\
so this will not work"
)
}

fn sign<M: AsRef<[u8]>>(&self, _: KeyTypeId, _: &M) -> Option<Self::Signature> {
unimplemented!(
"BLS12-381 Host functions are not yet available in Polkadot,\
so this will not work"
)
}

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

fn to_raw_vec(&self) -> Vec<u8> {
self.0.to_vec()
}
}
57 changes: 57 additions & 0 deletions primitives/bls/src/host_functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// This file is part of Polkadex.
//
// Copyright (c) 2023 Polkadex oü.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// DISCLAIMER: This module is deprecated and exists solely for the host function required during
// block sync. It will not be maintained and must not be used in production.

use sp_application_crypto::RuntimePublic;
use sp_core::crypto::KeyTypeId;
#[cfg(feature = "std")]
use sp_core::Pair;
#[cfg(feature = "std")]
use sp_keystore::{Keystore, KeystoreExt};
use sp_std::vec::Vec;

use crate::Public;
use sp_runtime_interface::runtime_interface;

#[cfg(feature = "std")]
use sp_externalities::ExternalitiesExt;

#[runtime_interface]
pub trait BLSCryptoExt {
fn bls_generate_pair(&mut self, id: KeyTypeId, seed: Option<Vec<u8>>) -> Public {
let (pair, seed) = match seed {
None => {
let (pair, seed_string, _) = crate::Pair::generate_with_phrase(None);
(pair, seed_string)
},
Some(seed) => {
let seed = String::from_utf8(seed).expect("expected seed to be Utf-8");
(crate::Pair::from_string(seed.as_str(), None).expect("Seed not valid!"), seed)
},
};
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
let public_key = pair.public().to_raw_vec();
<(dyn Keystore + 'static)>::insert(keystore, id, seed.as_str(), public_key.as_slice())
.unwrap();
pair.public()
}
}
Loading
Loading