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

refactor(platform): replace bls library #2257

Open
wants to merge 16 commits into
base: v1.7-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
394 changes: 380 additions & 14 deletions Cargo.lock

Large diffs are not rendered by default.

45 changes: 24 additions & 21 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -254,35 +254,39 @@ WORKDIR /platform

# Download and install cargo-binstall
ENV BINSTALL_VERSION=1.10.11
RUN set -ex; \
if [ "$TARGETARCH" = "amd64" ]; then \
CARGO_BINSTALL_ARCH="x86_64-unknown-linux-musl"; \
elif [ "$TARGETARCH" = "arm64" ]; then \
CARGO_BINSTALL_ARCH="aarch64-unknown-linux-musl"; \
else \
echo "Unsupported architecture: $TARGETARCH"; exit 1; \
fi; \
# Construct download URL
DOWNLOAD_URL="https://github.com/cargo-bins/cargo-binstall/releases/download/v${BINSTALL_VERSION}/cargo-binstall-${CARGO_BINSTALL_ARCH}.tgz"; \
# Download and extract the cargo-binstall binary
curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0" -L --proto '=https' --tlsv1.2 -sSf "$DOWNLOAD_URL" | tar -xvzf -; \
./cargo-binstall -y --force cargo-binstall; \
rm ./cargo-binstall; \
source $HOME/.cargo/env; \
cargo binstall -V

RUN source $HOME/.cargo/env; \
cargo binstall [email protected] [email protected] \
RUN <<EOS
set -ex;
if [ "$TARGETARCH" = "amd64" ]; then
CARGO_BINSTALL_ARCH="x86_64-unknown-linux-musl";
elif [ "$TARGETARCH" = "arm64" ]; then
CARGO_BINSTALL_ARCH="aarch64-unknown-linux-musl";
else
echo "Unsupported architecture: $TARGETARCH"; exit 1;
fi;
# Construct download URL
DOWNLOAD_URL="https://github.com/cargo-bins/cargo-binstall/releases/download/v${BINSTALL_VERSION}/cargo-binstall-${CARGO_BINSTALL_ARCH}.tgz";
# Download and extract the cargo-binstall binary
curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0" -L --proto '=https' --tlsv1.2 -sSf "$DOWNLOAD_URL" | tar -xvzf -;
./cargo-binstall -y --force cargo-binstall;
rm ./cargo-binstall;
source $HOME/.cargo/env;
cargo binstall -V

source $HOME/.cargo/env;

cargo binstall [email protected] [email protected] \
--locked \
--no-discover-github-token \
--disable-telemetry \
--no-track \
--no-confirm

EOS

#
# Rust build planner to speed up builds
#
FROM deps AS build-planner
FROM lklimek/dash-platform-build-base AS build-planner

WORKDIR /platform
COPY . .
Expand Down Expand Up @@ -489,7 +493,6 @@ FROM build-js AS build-test-suite
# node_modules directory to reuse built binaries
RUN yarn workspaces focus --production @dashevo/platform-test-suite

#
# STAGE: FINAL TEST SUITE IMAGE
#
FROM node:20-alpine${ALPINE_VERSION} AS test-suite
Expand Down
4 changes: 1 addition & 3 deletions packages/rs-dpp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ dashcore = { git = "https://github.com/dashpay/rust-dashcore", features = [
"rand",
"signer",
"serde",
"bls",
"eddsa"
], default-features = false, tag = "0.32.0" }
], default-features = false, tag = "0.33.1" }
env_logger = { version = "0.11" }
getrandom = { version = "0.2", features = ["js"] }
hex = { version = "0.4" }
Expand Down
46 changes: 32 additions & 14 deletions packages/rs-dpp/src/bls/native_bls.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::bls_signatures::{
Bls12381G2Impl, Pairing, PublicKey, SecretKey, Signature, SignatureSchemes,
};
use crate::{BlsModule, ProtocolError, PublicKeyValidationError};
use anyhow::anyhow;
use dashcore::bls_signatures::{self, PrivateKey, PublicKey};

#[derive(Default)]
pub struct NativeBlsModule;
impl BlsModule for NativeBlsModule {
fn validate_public_key(&self, pk: &[u8]) -> Result<(), PublicKeyValidationError> {
match PublicKey::from_bytes(pk) {
match PublicKey::<Bls12381G2Impl>::try_from(pk) {
Ok(_) => Ok(()),
Err(e) => Err(PublicKeyValidationError::new(e.to_string())),
}
Expand All @@ -18,31 +20,47 @@ impl BlsModule for NativeBlsModule {
data: &[u8],
public_key: &[u8],
) -> Result<bool, ProtocolError> {
let public_key = PublicKey::from_bytes(public_key).map_err(anyhow::Error::msg)?;
let signature =
bls_signatures::Signature::from_bytes(signature).map_err(anyhow::Error::msg)?;
match public_key.verify(&signature, data) {
true => Ok(true),
// TODO change to specific error type
false => Err(anyhow!("Verification failed").into()),
let public_key =
PublicKey::<Bls12381G2Impl>::try_from(public_key).map_err(anyhow::Error::msg)?;
let signature_96_bytes = signature
.try_into()
.map_err(|_| anyhow!("signature wrong size"))?;
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
let g2_element =
<Bls12381G2Impl as Pairing>::Signature::from_compressed(&signature_96_bytes)
.into_option()
.ok_or(anyhow!("signature derivation failed"))?;

let signature = Signature::Basic(g2_element);

match signature.verify(&public_key, data) {
Ok(_) => Ok(true),
Err(_) => Err(anyhow!("Verification failed").into()),
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
}
}

fn private_key_to_public_key(&self, private_key: &[u8]) -> Result<Vec<u8>, ProtocolError> {
let fixed_len_key: [u8; 32] = private_key
.try_into()
.map_err(|_| anyhow!("the BLS private key must be 32 bytes long"))?;
let pk = PrivateKey::from_bytes(&fixed_len_key, false).map_err(anyhow::Error::msg)?;
let public_key = pk.g1_element().map_err(anyhow::Error::msg)?;
let public_key_bytes = public_key.to_bytes().to_vec();
let pk = SecretKey::<Bls12381G2Impl>::from_be_bytes(&fixed_len_key)
.into_option()
.ok_or(anyhow!("Incorrect Priv Key"))?;
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
let public_key = pk.public_key();
let public_key_bytes = public_key.0.to_compressed().to_vec();
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
Ok(public_key_bytes)
}

fn sign(&self, data: &[u8], private_key: &[u8]) -> Result<Vec<u8>, ProtocolError> {
let fixed_len_key: [u8; 32] = private_key
.try_into()
.map_err(|_| anyhow!("the BLS private key must be 32 bytes long"))?;
let pk = PrivateKey::from_bytes(&fixed_len_key, false).map_err(anyhow::Error::msg)?;
Ok(pk.sign(data).to_bytes().to_vec())
let pk = SecretKey::<Bls12381G2Impl>::from_be_bytes(&fixed_len_key)
.into_option()
.ok_or(anyhow!("Incorrect Priv Key"))?;
Ok(pk
.sign(SignatureSchemes::Basic, data)?
.as_raw_value()
.to_compressed()
.to_vec())
}
}
6 changes: 3 additions & 3 deletions packages/rs-dpp/src/core_types/validator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bls_signatures::PublicKey as BlsPublicKey;
use crate::bls_signatures::{Bls12381G2Impl, PublicKey as BlsPublicKey};
use crate::core_types::validator::v0::{ValidatorV0, ValidatorV0Getters, ValidatorV0Setters};
use dashcore::{ProTxHash, PubkeyHash};
#[cfg(feature = "core-types-serde-conversion")]
Expand All @@ -25,7 +25,7 @@ impl ValidatorV0Getters for Validator {
}
}

fn public_key(&self) -> &Option<BlsPublicKey> {
fn public_key(&self) -> &Option<BlsPublicKey<Bls12381G2Impl>> {
match self {
Validator::V0(v0) => v0.public_key(),
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl ValidatorV0Setters for Validator {
}
}

fn set_public_key(&mut self, public_key: Option<BlsPublicKey>) {
fn set_public_key(&mut self, public_key: Option<BlsPublicKey<Bls12381G2Impl>>) {
match self {
Validator::V0(v0) => v0.set_public_key(public_key),
}
Expand Down
29 changes: 18 additions & 11 deletions packages/rs-dpp/src/core_types/validator/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dashcore::{ProTxHash, PubkeyHash};
use std::fmt::{Debug, Formatter};

use crate::bls_signatures::PublicKey as BlsPublicKey;
use crate::bls_signatures::{Bls12381G2Impl, PublicKey as BlsPublicKey};
#[cfg(feature = "core-types-serde-conversion")]
use serde::{Deserialize, Serialize};

Expand All @@ -26,7 +26,7 @@ pub struct ValidatorV0 {
/// The proTxHash
pub pro_tx_hash: ProTxHash,
/// The public key share of this validator for this quorum
pub public_key: Option<BlsPublicKey>,
pub public_key: Option<BlsPublicKey<Bls12381G2Impl>>,
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
/// The node address
pub node_ip: String,
/// The node id
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Encode for ValidatorV0 {
match &self.public_key {
Some(public_key) => {
true.encode(encoder)?; // Indicate that public_key is present
public_key.to_bytes().encode(encoder)?;
public_key.0.to_compressed().encode(encoder)?;
}
None => {
false.encode(encoder)?; // Indicate that public_key is not present
Expand Down Expand Up @@ -94,9 +94,12 @@ impl Decode for ValidatorV0 {
let has_public_key = bool::decode(decoder)?;
let public_key = if has_public_key {
let public_key_bytes = <[u8; 48]>::decode(decoder)?;
Some(BlsPublicKey::from_bytes(&public_key_bytes).map_err(|_| {
DecodeError::OtherString("Failed to decode BlsPublicKey".to_string())
})?)

Some(
BlsPublicKey::try_from(public_key_bytes.as_slice()).map_err(|_| {
DecodeError::OtherString("Failed to decode BlsPublicKey".to_string())
})?,
)
} else {
None
};
Expand Down Expand Up @@ -150,7 +153,7 @@ pub trait ValidatorV0Getters {
/// Returns the proTxHash of the validator.
fn pro_tx_hash(&self) -> &ProTxHash;
/// Returns the public key share of this validator for this quorum.
fn public_key(&self) -> &Option<BlsPublicKey>;
fn public_key(&self) -> &Option<BlsPublicKey<Bls12381G2Impl>>;
/// Returns the node address of the validator.
fn node_ip(&self) -> &String;
/// Returns the node id of the validator.
Expand All @@ -170,7 +173,7 @@ pub trait ValidatorV0Setters {
/// Sets the proTxHash of the validator.
fn set_pro_tx_hash(&mut self, pro_tx_hash: ProTxHash);
/// Sets the public key share of this validator for this quorum.
fn set_public_key(&mut self, public_key: Option<BlsPublicKey>);
fn set_public_key(&mut self, public_key: Option<BlsPublicKey<Bls12381G2Impl>>);
/// Sets the node address of the validator.
fn set_node_ip(&mut self, node_ip: String);
/// Sets the node id of the validator.
Expand All @@ -190,7 +193,7 @@ impl ValidatorV0Getters for ValidatorV0 {
&self.pro_tx_hash
}

fn public_key(&self) -> &Option<BlsPublicKey> {
fn public_key(&self) -> &Option<BlsPublicKey<Bls12381G2Impl>> {
&self.public_key
}

Expand Down Expand Up @@ -224,7 +227,7 @@ impl ValidatorV0Setters for ValidatorV0 {
self.pro_tx_hash = pro_tx_hash;
}

fn set_public_key(&mut self, public_key: Option<BlsPublicKey>) {
fn set_public_key(&mut self, public_key: Option<BlsPublicKey<Bls12381G2Impl>>) {
self.public_key = public_key;
}

Expand Down Expand Up @@ -257,12 +260,16 @@ impl ValidatorV0Setters for ValidatorV0 {
mod tests {
use super::*;
use bincode::config;
use dashcore::blsful::SecretKey;
use rand::prelude::StdRng;
use rand::SeedableRng;

#[test]
fn test_serialize_deserialize_validator_v0() {
// Sample data for testing
let pro_tx_hash = ProTxHash::from_slice(&[1; 32]).unwrap();
let public_key = Some(BlsPublicKey::generate());
let mut rng = StdRng::seed_from_u64(0);
let public_key = Some(SecretKey::<Bls12381G2Impl>::random(&mut rng).public_key());
let node_ip = "127.0.0.1".to_string();
let node_id = PubkeyHash::from_slice(&[3; 20]).unwrap();
let core_port = 9999;
Expand Down
6 changes: 3 additions & 3 deletions packages/rs-dpp/src/core_types/validator_set/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bls_signatures::PublicKey as BlsPublicKey;
use crate::bls_signatures::{Bls12381G2Impl, PublicKey as BlsPublicKey};
use crate::core_types::validator::v0::ValidatorV0;
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
use crate::core_types::validator_set::v0::{
ValidatorSetV0, ValidatorSetV0Getters, ValidatorSetV0Setters,
Expand Down Expand Up @@ -80,7 +80,7 @@ impl ValidatorSetV0Getters for ValidatorSet {
}
}

fn threshold_public_key(&self) -> &BlsPublicKey {
fn threshold_public_key(&self) -> &BlsPublicKey<Bls12381G2Impl> {
match self {
ValidatorSet::V0(v0) => v0.threshold_public_key(),
}
Expand Down Expand Up @@ -112,7 +112,7 @@ impl ValidatorSetV0Setters for ValidatorSet {
}
}

fn set_threshold_public_key(&mut self, threshold_public_key: BlsPublicKey) {
fn set_threshold_public_key(&mut self, threshold_public_key: BlsPublicKey<Bls12381G2Impl>) {
match self {
ValidatorSet::V0(v0) => v0.set_threshold_public_key(threshold_public_key),
}
Expand Down
Loading
Loading