Skip to content

Commit ae185c6

Browse files
chore: moved message verification to feature (#58)
* chore: small import fixes and version bump * chore: moved message verification to feature
1 parent 2bfa9ab commit ae185c6

File tree

6 files changed

+68
-57
lines changed

6 files changed

+68
-57
lines changed

dash/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dashcore"
3-
version = "0.36.1"
3+
version = "0.36.2"
44
authors = [
55
"Samuel Westrich <sam@dash.org>",
66
"Anton Suprunchuk <anton@dash.org>",
@@ -19,7 +19,7 @@ edition = "2021"
1919

2020
# Please don't forget to add relevant features to docs.rs below
2121
[features]
22-
default = [ "std", "secp-recovery", "bincode", "quorum_validation" ]
22+
default = [ "std", "secp-recovery", "bincode" ]
2323
base64 = [ "base64-compat" ]
2424
rand-std = ["secp256k1/rand"]
2525
rand = ["secp256k1/rand"]
@@ -31,6 +31,7 @@ core-block-hash-use-x11 = ["dashcore_hashes/x11"]
3131
bls = ["blsful"]
3232
eddsa = ["ed25519-dalek"]
3333
quorum_validation = ["bls"]
34+
message_verification = ["bls"]
3435
bincode = [ "dep:bincode", "dashcore_hashes/bincode" ]
3536

3637
# At least one of std, no-std must be enabled.
@@ -80,7 +81,7 @@ secp256k1 = { features = [ "recovery", "rand", "hashes" ], version="0.30.0" }
8081
bip39 = "2.0.0"
8182
bincode_test = {package = "bincode", version= "1.3.3" }
8283
assert_matches = "1.5.0"
83-
dashcore = { path = ".", features = ["core-block-hash-use-x11"] }
84+
dashcore = { path = ".", features = ["core-block-hash-use-x11", "message_verification", "quorum_validation"] }
8485

8586
[[example]]
8687
name = "bip32"

dash/src/sml/masternode_list_engine/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod helpers;
2+
#[cfg(feature = "message_verification")]
23
mod message_request_verification;
34
mod non_rotated_quorum_construction;
45
mod rotated_quorum_construction;

dash/src/sml/quorum_entry/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ pub mod quorum_modifier_type;
44

55
#[cfg(feature = "quorum_validation")]
66
mod validation;
7+
8+
#[cfg(feature = "message_verification")]
9+
mod verify_message;

dash/src/sml/quorum_entry/validation.rs

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use bls_signatures::{BasicSchemeMPL, G1Element, G2Element, Scheme};
22
use blsful::Bls12381G2Impl;
3-
use hashes::{Hash, sha256d};
3+
use hashes::Hash;
44

5-
use crate::bls_sig_utils::BLSSignature;
65
use crate::sml::masternode_list_entry::MasternodeListEntry;
7-
use crate::sml::message_verification_error::MessageVerificationError;
86
use crate::sml::quorum_entry::qualified_quorum_entry::QualifiedQuorumEntry;
97
use crate::sml::quorum_validation_error::QuorumValidationError;
108

@@ -112,56 +110,6 @@ impl QualifiedQuorumEntry {
112110
.map_err(|e| QuorumValidationError::ThresholdSignatureNotValid(e.to_string()))
113111
}
114112

115-
/// Verifies a message digest using a BLS threshold signature.
116-
///
117-
/// This function checks whether the provided BLS signature is valid for the given
118-
/// `message_digest` using the quorum's public key. It converts the stored quorum public key
119-
/// and the provided BLS signature into the appropriate types before performing the verification.
120-
///
121-
/// # Arguments
122-
///
123-
/// * `message_digest` - A SHA-256 double-hashed (`sha256d::Hash`) digest of the message to be verified.
124-
/// * `signature` - The BLS signature (`BLSSignature`) that should authenticate the message.
125-
///
126-
/// # Returns
127-
///
128-
/// * `Ok(())` if the signature is valid for the given digest and quorum public key.
129-
/// * `Err(MessageVerificationError::ThresholdSignatureNotValid)` if the signature verification fails.
130-
///
131-
/// # Errors
132-
///
133-
/// Returns `MessageVerificationError::ThresholdSignatureNotValid` if:
134-
/// - The quorum's public key cannot be converted to the required `blsful::PublicKey<Bls12381G2Impl>`.
135-
/// - The provided signature cannot be converted to `blsful::Signature<Bls12381G2Impl>`.
136-
/// - The BLS verification process determines that the signature is invalid.
137-
///
138-
/// # Implementation Details
139-
///
140-
/// - The function retrieves the quorum's public key and attempts to convert it into the expected `blsful::PublicKey` type.
141-
/// - It converts the provided `BLSSignature` into a `blsful::Signature`.
142-
/// - It then calls the `verify` method, which checks if the signature is valid for the given message digest.
143-
/// - If verification fails, it returns a `MessageVerificationError::ThresholdSignatureNotValid` with relevant details.
144-
///
145-
pub fn verify_message_digest(
146-
&self,
147-
message_digest: [u8; 32],
148-
signature: BLSSignature,
149-
) -> Result<(), MessageVerificationError> {
150-
let public_key: blsful::PublicKey<Bls12381G2Impl> =
151-
self.quorum_entry.quorum_public_key.try_into()?;
152-
let bls_signature: blsful::Signature<Bls12381G2Impl> = signature.try_into()?;
153-
bls_signature.verify(&public_key, message_digest).map_err(|e| {
154-
MessageVerificationError::ThresholdSignatureNotValid(
155-
signature,
156-
sha256d::Hash::from_byte_array(message_digest),
157-
self.quorum_entry.quorum_public_key,
158-
self.quorum_entry.quorum_hash,
159-
self.quorum_entry.llmq_type,
160-
e.to_string(),
161-
)
162-
})
163-
}
164-
165113
/// Performs full quorum validation by verifying all necessary signatures.
166114
///
167115
/// This function validates the quorum by checking:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use blsful::Bls12381G2Impl;
2+
use hashes::{Hash, sha256d};
3+
4+
use crate::bls_sig_utils::BLSSignature;
5+
use crate::sml::message_verification_error::MessageVerificationError;
6+
use crate::sml::quorum_entry::qualified_quorum_entry::QualifiedQuorumEntry;
7+
8+
impl QualifiedQuorumEntry {
9+
/// Verifies a message digest using a BLS threshold signature.
10+
///
11+
/// This function checks whether the provided BLS signature is valid for the given
12+
/// `message_digest` using the quorum's public key. It converts the stored quorum public key
13+
/// and the provided BLS signature into the appropriate types before performing the verification.
14+
///
15+
/// # Arguments
16+
///
17+
/// * `message_digest` - A SHA-256 double-hashed (`sha256d::Hash`) digest of the message to be verified.
18+
/// * `signature` - The BLS signature (`BLSSignature`) that should authenticate the message.
19+
///
20+
/// # Returns
21+
///
22+
/// * `Ok(())` if the signature is valid for the given digest and quorum public key.
23+
/// * `Err(MessageVerificationError::ThresholdSignatureNotValid)` if the signature verification fails.
24+
///
25+
/// # Errors
26+
///
27+
/// Returns `MessageVerificationError::ThresholdSignatureNotValid` if:
28+
/// - The quorum's public key cannot be converted to the required `blsful::PublicKey<Bls12381G2Impl>`.
29+
/// - The provided signature cannot be converted to `blsful::Signature<Bls12381G2Impl>`.
30+
/// - The BLS verification process determines that the signature is invalid.
31+
///
32+
/// # Implementation Details
33+
///
34+
/// - The function retrieves the quorum's public key and attempts to convert it into the expected `blsful::PublicKey` type.
35+
/// - It converts the provided `BLSSignature` into a `blsful::Signature`.
36+
/// - It then calls the `verify` method, which checks if the signature is valid for the given message digest.
37+
/// - If verification fails, it returns a `MessageVerificationError::ThresholdSignatureNotValid` with relevant details.
38+
///
39+
pub fn verify_message_digest(
40+
&self,
41+
message_digest: [u8; 32],
42+
signature: BLSSignature,
43+
) -> Result<(), MessageVerificationError> {
44+
let public_key: blsful::PublicKey<Bls12381G2Impl> =
45+
self.quorum_entry.quorum_public_key.try_into()?;
46+
let bls_signature: blsful::Signature<Bls12381G2Impl> = signature.try_into()?;
47+
bls_signature.verify(&public_key, message_digest).map_err(|e| {
48+
MessageVerificationError::ThresholdSignatureNotValid(
49+
signature,
50+
sha256d::Hash::from_byte_array(message_digest),
51+
self.quorum_entry.quorum_public_key,
52+
self.quorum_entry.quorum_hash,
53+
self.quorum_entry.llmq_type,
54+
e.to_string(),
55+
)
56+
})
57+
}
58+
}

hashes/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dashcore_hashes"
3-
version = "0.15.1"
3+
version = "0.15.2"
44
authors = ["Samuel Westrich <sam@dash.org>"]
55
license = "CC0-1.0"
66
repository = "https://github.com/rust-dashcore/dash_hashes/"

0 commit comments

Comments
 (0)