Skip to content

Commit

Permalink
Merge pull request #2064 from dusk-network/update-bls
Browse files Browse the repository at this point in the history
Change to use single-key signature scheme in contracts
  • Loading branch information
Eduardo Leegwater Simões authored Aug 2, 2024
2 parents b1cd3fd + 0556eeb commit 04405bc
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 154 deletions.
6 changes: 3 additions & 3 deletions consensus/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::user::cluster::Cluster;
use crate::user::committee::Committee;
use dusk_bytes::Serializable;
use execution_core::{BlsSigError, BlsSignature};
use execution_core::{BlsMultisigSignature, BlsSigError};
use node_data::bls::{PublicKey, PublicKeyBytes};
use node_data::ledger::{to_str, StepVotes};
use node_data::message::payload::Vote;
Expand Down Expand Up @@ -184,12 +184,12 @@ impl<V> fmt::Display for Aggregator<V> {

#[derive(Default)]
pub(super) struct AggrSignature {
data: Option<BlsSignature>,
data: Option<BlsMultisigSignature>,
}

impl AggrSignature {
pub fn add(&mut self, data: &[u8; 48]) -> Result<(), BlsSigError> {
let sig = BlsSignature::from_bytes(data)?;
let sig = BlsMultisigSignature::from_bytes(data)?;

let aggr_sig = match self.data {
Some(data) => data.aggregate(&[sig]),
Expand Down
2 changes: 0 additions & 2 deletions consensus/src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ pub enum StepSigError {
VoteSetTooSmall,
#[error("Verification error {0}")]
VerificationFailed(BlsSigError),
#[error("Empty Apk instance")]
EmptyApk,
#[error("Invalid Type")]
InvalidType,
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/proposal/block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<T: Operations> Generator<T> {
// Sign seed
let seed = ru
.secret_key
.sign(ru.pubkey_bls.inner(), &ru.seed().inner()[..])
.sign_multisig(ru.pubkey_bls.inner(), &ru.seed().inner()[..])
.to_bytes();

let start = Instant::now();
Expand Down
18 changes: 5 additions & 13 deletions consensus/src/quorum/verifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::user::sortition;

use crate::config::CONSENSUS_MAX_ITER;
use dusk_bytes::Serializable as BytesSerializable;
use execution_core::{BlsAggPublicKey, BlsSignature};
use execution_core::{BlsMultisigPublicKey, BlsMultisigSignature};
use tokio::sync::RwLock;

pub async fn verify_step_votes(
Expand Down Expand Up @@ -131,18 +131,10 @@ pub fn verify_votes(
}

impl Cluster<PublicKey> {
fn aggregate_pks(&self) -> Result<BlsAggPublicKey, StepSigError> {
fn aggregate_pks(&self) -> Result<BlsMultisigPublicKey, StepSigError> {
let pks: Vec<_> =
self.iter().map(|(pubkey, _)| *pubkey.inner()).collect();

match pks.split_first() {
Some((first, rest)) => {
let mut apk = BlsAggPublicKey::from(first);
apk.aggregate(rest)?;
Ok(apk)
}
None => Err(StepSigError::EmptyApk),
}
Ok(BlsMultisigPublicKey::aggregate(&pks)?)
}

pub fn to_voters(self) -> Vec<Voter> {
Expand All @@ -154,7 +146,7 @@ fn verify_step_signature(
header: &ConsensusHeader,
step: StepName,
vote: &Vote,
apk: BlsAggPublicKey,
apk: BlsMultisigPublicKey,
signature: &[u8; 48],
) -> Result<(), StepSigError> {
// Compile message to verify
Expand All @@ -164,7 +156,7 @@ fn verify_step_signature(
StepName::Proposal => Err(StepSigError::InvalidType)?,
};

let sig = BlsSignature::from_bytes(signature)?;
let sig = BlsMultisigSignature::from_bytes(signature)?;
let mut msg = header.signable();
msg.extend_from_slice(sign_seed);
vote.write(&mut msg).expect("Writing to vec should succeed");
Expand Down
6 changes: 2 additions & 4 deletions contracts/transfer/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,8 @@ pub fn create_moonlight_transaction(
nonce: u64,
exec: Option<impl Into<ContractExec>>,
) -> MoonlightTransaction {
let from = BlsPublicKey::from(from_sk);

let payload = MoonlightPayload {
from,
from: BlsPublicKey::from(from_sk),
to,
value,
deposit,
Expand All @@ -220,7 +218,7 @@ pub fn create_moonlight_transaction(
};

let digest = payload.to_hash_input_bytes();
let signature = from_sk.sign(&from, &digest);
let signature = from_sk.sign(&digest);

MoonlightTransaction::new(payload, signature)
}
Expand Down
2 changes: 1 addition & 1 deletion execution-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
dusk-bls12_381 = { version = "0.13", default-features = false, features = ["rkyv-impl"] }
dusk-jubjub = { version = "0.14", default-features = false, features = ["rkyv-impl"] }
dusk-poseidon = "0.39"
bls12_381-bls = { version = "0.3", default-features = false, features = ["rkyv-impl"] }
bls12_381-bls = { version = "0.4", default-features = false, features = ["rkyv-impl"] }
jubjub-schnorr = { version = "0.4", default-features = false, features = ["rkyv-impl"] }
phoenix-core = { version = "0.30.0-rc", default-features = false, features = ["rkyv-impl", "alloc"] }
dusk-bytes = "0.1"
Expand Down
5 changes: 3 additions & 2 deletions execution-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ pub use dusk_jubjub::{

// signature types
pub use bls12_381_bls::{
Error as BlsSigError, PublicKey as BlsPublicKey, SecretKey as BlsSecretKey,
Signature as BlsSignature, APK as BlsAggPublicKey,
Error as BlsSigError, MultisigPublicKey as BlsMultisigPublicKey,
MultisigSignature as BlsMultisigSignature, PublicKey as BlsPublicKey,
SecretKey as BlsSecretKey, Signature as BlsSignature,
};

pub use jubjub_schnorr::{
Expand Down
12 changes: 4 additions & 8 deletions execution-core/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,15 @@ impl Stake {
/// Create a new stake.
#[must_use]
pub fn new(sk: &BlsSecretKey, value: u64, nonce: u64) -> Self {
let account = BlsPublicKey::from(sk);

let mut stake = Stake {
account,
account: BlsPublicKey::from(sk),
value,
nonce,
signature: BlsSignature::default(),
};

let msg = stake.signature_message();
stake.signature = sk.sign(&account, &msg);
stake.signature = sk.sign(&msg);

stake
}
Expand Down Expand Up @@ -127,16 +125,14 @@ impl Withdraw {
/// Create a new withdraw call.
#[must_use]
pub fn new(sk: &BlsSecretKey, withdraw: TransferWithdraw) -> Self {
let account = BlsPublicKey::from(sk);

let mut stake_withdraw = Withdraw {
account,
account: BlsPublicKey::from(sk),
withdraw,
signature: BlsSignature::default(),
};

let msg = stake_withdraw.signature_message();
stake_withdraw.signature = sk.sign(&account, &msg);
stake_withdraw.signature = sk.sign(&msg);

stake_withdraw
}
Expand Down
3 changes: 1 addition & 2 deletions execution-core/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ impl Withdraw {
withdraw.signature = signature.into();
}
WithdrawSecretKey::Moonlight(sk) => {
let pk = BlsPublicKey::from(sk);
let signature = sk.sign(&pk, &msg);
let signature = sk.sign(&msg);
withdraw.signature = signature.into();
}
}
Expand Down
2 changes: 1 addition & 1 deletion execution-core/tests/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn new_moonlight_tx<R: RngCore + CryptoRng>(
};

let msg = payload.to_hash_input_bytes();
let signature = sk.sign(&pk, &msg);
let signature = sk.sign(&msg);

MoonlightTransaction::new(payload, signature).into()
}
Expand Down
7 changes: 4 additions & 3 deletions node-data/src/ledger/faults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use crate::{

use dusk_bytes::Serializable as DuskSerializeble;
use execution_core::{
stake::EPOCH, BlsAggPublicKey, BlsScalar, BlsSigError, BlsSignature,
stake::EPOCH, BlsMultisigPublicKey, BlsMultisigSignature, BlsScalar,
BlsSigError,
};
use thiserror::Error;
use tracing::error;
Expand Down Expand Up @@ -199,8 +200,8 @@ impl Fault {
msg: &[u8],
) -> Result<(), BlsSigError> {
let signature = sign_info.signature.inner();
let sig = BlsSignature::from_bytes(signature)?;
let pk = BlsAggPublicKey::from(sign_info.signer.inner());
let sig = BlsMultisigSignature::from_bytes(signature)?;
let pk = BlsMultisigPublicKey::aggregate(&[*sign_info.signer.inner()])?;
pk.verify(&sig, msg)
}
}
Expand Down
12 changes: 8 additions & 4 deletions node-data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use dusk_bytes::Serializable as DuskSerializable;
use execution_core::{
BlsAggPublicKey, BlsPublicKey, BlsSecretKey, BlsSigError, BlsSignature,
BlsMultisigPublicKey, BlsMultisigSignature, BlsPublicKey, BlsSecretKey,
BlsSigError,
};
use tracing::warn;

Expand Down Expand Up @@ -1158,16 +1159,19 @@ pub trait StepMessage {

fn verify_signature(&self) -> Result<(), BlsSigError> {
let signature = self.sign_info().signature.inner();
let sig = BlsSignature::from_bytes(signature)?;
let pk = BlsAggPublicKey::from(self.sign_info().signer.inner());
let sig = BlsMultisigSignature::from_bytes(signature)?;
let pk = BlsMultisigPublicKey::aggregate(&[*self
.sign_info()
.signer
.inner()])?;
let msg = self.signable();
pk.verify(&sig, &msg)
}

fn sign(&mut self, sk: &BlsSecretKey, pk: &BlsPublicKey) {
let msg = self.signable();
let sign_info = self.sign_info_mut();
let signature = sk.sign(pk, &msg).to_bytes();
let signature = sk.sign_multisig(pk, &msg).to_bytes();
sign_info.signature = signature.into();
sign_info.signer = PublicKey::new(*pk)
}
Expand Down
5 changes: 3 additions & 2 deletions node/benches/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use dusk_consensus::user::{
cluster::Cluster, committee::Committee, provisioners::Provisioners,
sortition::Config as SortitionConfig,
};
use execution_core::{BlsPublicKey, BlsSecretKey, BlsSignature};
use execution_core::{BlsMultisigSignature, BlsPublicKey, BlsSecretKey};
use node_data::ledger::{Attestation, StepVotes};
use node_data::message::payload::{
QuorumType, RatificationResult, ValidationResult, Vote,
Expand Down Expand Up @@ -84,7 +84,8 @@ fn create_step_votes(
}
_ => unreachable!(),
};
signatures.push(BlsSignature::from_bytes(sig.inner()).unwrap());
signatures
.push(BlsMultisigSignature::from_bytes(sig.inner()).unwrap());
cluster.add(pk, weight);
}
}
Expand Down
5 changes: 3 additions & 2 deletions node/src/chain/header_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
let pk = execution_core::BlsPublicKey::from_bytes(pk_bytes)
.map_err(|err| anyhow!("invalid pk bytes: {:?}", err))?;

let signature = execution_core::BlsSignature::from_bytes(seed)
let signature = execution_core::BlsMultisigSignature::from_bytes(seed)
.map_err(|err| anyhow!("invalid signature bytes: {}", err))?;

execution_core::BlsAggPublicKey::from(&pk)
execution_core::BlsMultisigPublicKey::aggregate(&[pk])
.map_err(|err| anyhow!("failed aggregating single key: {}", err))?
.verify(&signature, &self.prev_header.seed.inner()[..])
.map_err(|err| anyhow!("invalid seed: {:?}", err))?;

Expand Down
6 changes: 2 additions & 4 deletions rusk-abi/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use dusk_bytes::DeserializableSlice;
use dusk_plonk::prelude::{Proof, Verifier};
use dusk_poseidon::{Domain, Hash as PoseidonHash};
use execution_core::{
BlsAggPublicKey, BlsPublicKey, BlsScalar, BlsSignature, SchnorrPublicKey,
SchnorrSignature,
BlsPublicKey, BlsScalar, BlsSignature, SchnorrPublicKey, SchnorrSignature,
};
use piecrust::{Error as PiecrustError, Session, SessionData, VM};
use rkyv::ser::serializers::AllocSerializer;
Expand Down Expand Up @@ -165,6 +164,5 @@ pub fn verify_schnorr(

/// Verify a BLS signature is valid for the given public key and message
pub fn verify_bls(msg: Vec<u8>, pk: BlsPublicKey, sig: BlsSignature) -> bool {
let apk = BlsAggPublicKey::from(&pk);
apk.verify(&sig, &msg).is_ok()
pk.verify(&sig, &msg).is_ok()
}
2 changes: 1 addition & 1 deletion rusk-abi/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn stake_signature() {
let stake_sk = BlsSecretKey::random(&mut OsRng);
let stake_pk = BlsPublicKey::from(&stake_sk);

let stake_sig = stake_sk.sign(&stake_pk, &message);
let stake_sig = stake_sk.sign(&message);

let arg = (message, stake_pk, stake_sig);
let valid: bool = session
Expand Down
Loading

0 comments on commit 04405bc

Please sign in to comment.