Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
Cargo.lock
.idea/
.DS_Store
89 changes: 89 additions & 0 deletions examples/verify_secure_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//! Example demonstrating VerifySecure functionality
//!
//! VerifySecure is designed to verify aggregated signatures that were
//! created using secure aggregation (with deterministic coefficients).

use blsful::*;

fn main() {
println!("=== BLS VerifySecure Example ===\n");

// Create three signers
let signer1 = SecretKey::<Bls12381G1Impl>::from_hash(b"alice_key");
let signer2 = SecretKey::<Bls12381G1Impl>::from_hash(b"bob_key");
let signer3 = SecretKey::<Bls12381G1Impl>::from_hash(b"charlie_key");

// Get their public keys
let pk1 = PublicKey::from(&signer1);
let pk2 = PublicKey::from(&signer2);
let pk3 = PublicKey::from(&signer3);

let message = b"Important message requiring multiple signatures";

println!("Demonstrating rogue key attack vulnerability...\n");

// === Demonstrate the Rogue Key Attack Problem ===

// Attacker computes: pk_rogue = -pk1 - pk2 + pk_attacker
// Note: In a real attack, honest signers would create signatures, but for this
// demonstration we focus on showing how VerifySecure prevents the attack vector
let attacker = SecretKey::<Bls12381G1Impl>::from_hash(b"attacker_key");
let attacker_pk_raw = PublicKey::from(&attacker).0;
let mut rogue_pk_raw = attacker_pk_raw;
rogue_pk_raw -= pk1.0;
rogue_pk_raw -= pk2.0;
let rogue_pk = PublicKey::<Bls12381G1Impl>(rogue_pk_raw);

println!("--- Naive Aggregation (VULNERABLE) ---");
println!("With naive aggregation:");
println!(" pk_agg = pk1 + pk2 + pk_rogue");
println!(" = pk1 + pk2 + (pk_attacker - pk1 - pk2)");
println!(" = pk_attacker");
println!("\nThis allows the attacker to create a valid signature!");

// Create attacker's signature
let attacker_sig = attacker.sign(SignatureSchemes::Basic, message).unwrap();

// === Show How VerifySecure Prevents This ===

println!("\n--- Secure Aggregation with VerifySecure ---");
println!("With secure aggregation:");
println!(" pk_agg = t1*pk1 + t2*pk2 + t3*pk_rogue");
println!("where t1, t2, t3 are deterministic coefficients");
println!("\nThe attacker cannot predict or control these coefficients!");

print!("\nRogue key attack test: ");
// Try to verify attacker's signature against the three keys
// This would succeed with naive verification but fails with verify_secure
match attacker_sig.verify_secure(&[pk1, pk2, rogue_pk], message) {
Ok(()) => println!("✗ FAIL: Rogue key attack succeeded!"),
Err(_) => println!("✓ PASS: Attack prevented by VerifySecure"),
}

println!("\n--- Additional Security Properties ---");

// Test that key order doesn't matter
print!("Key order independence: ");
let test_sig = signer3.sign(SignatureSchemes::Basic, message).unwrap();
let result1 = test_sig.verify_secure(&[pk1, pk2, pk3], message).is_err();
let result2 = test_sig.verify_secure(&[pk3, pk1, pk2], message).is_err();
if result1 == result2 {
println!("✓ PASS: Same result regardless of key order");
} else {
println!("✗ FAIL: Different results for different key orders");
}

println!("\n=== Summary ===");
println!("VerifySecure prevents rogue public key attacks by:");
println!("1. Sorting public keys deterministically");
println!("2. Computing a hash of all sorted keys");
println!("3. Generating unique coefficients for each key");
println!("4. Using these coefficients during aggregation");
println!("\nThis ensures attackers cannot create keys that cancel");
println!("out honest participants' contributions.");

println!("\n=== Implementation Note ===");
println!("VerifySecure is designed to work with aggregate_secure.");
println!("For individual signature verification, use the standard");
println!("verify() method instead.");
}
54 changes: 54 additions & 0 deletions src/aggregate_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,60 @@ impl<C: BlsSignatureImpl> AggregateSignature<C> {
Self::try_from(signatures.as_ref())
}

/// Accumulate signatures using secure aggregation (prevents rogue key attacks)
///
/// This method should be used when aggregating signatures from multiple signers
/// for the same message. It applies deterministic coefficients to each signature
/// to prevent rogue public key attacks.
///
/// # Arguments
/// * `signatures` - The signatures to aggregate (must all be for the same message)
/// * `public_keys` - The public keys corresponding to each signature
///
/// # Returns
/// An aggregated signature that can be verified with `verify_secure`
///
/// # Errors
/// * `InvalidInputs` if array lengths don't match or are empty
/// * `InvalidSignatureScheme` if signatures use different schemes
pub fn from_signatures_secure<B: AsRef<[Signature<C>]>>(
signatures: B,
public_keys: &[PublicKey<C>],
) -> BlsResult<Self> {
let sigs = signatures.as_ref();

if sigs.len() != public_keys.len() {
return Err(BlsError::InvalidInputs(
"Mismatched array lengths".to_string(),
));
}

if sigs.is_empty() {
return Err(BlsError::InvalidInputs(
"Empty signatures array".to_string(),
));
}

// Check all signatures use the same scheme
if !sigs.iter().skip(1).all(|s| s.same_scheme(&sigs[0])) {
return Err(BlsError::InvalidSignatureScheme);
}

// Extract raw signatures
let raw_sigs: Vec<<C as Pairing>::Signature> =
sigs.iter().map(|s| *s.as_raw_value()).collect();

// Use secure aggregation
let agg_sig = secure_aggregation::aggregate_secure::<C>(public_keys, &raw_sigs)?;

// Wrap in appropriate scheme
match sigs[0] {
Signature::Basic(_) => Ok(Self::Basic(agg_sig)),
Signature::MessageAugmentation(_) => Ok(Self::MessageAugmentation(agg_sig)),
Signature::ProofOfPossession(_) => Ok(Self::ProofOfPossession(agg_sig)),
}
}

/// Verify the aggregated signature using the public keys
pub fn verify<B: AsRef<[u8]>>(&self, data: &[(PublicKey<C>, B)]) -> BlsResult<()> {
let ii = data.iter().map(|(pk, m)| (pk.0, m));
Expand Down
4 changes: 2 additions & 2 deletions src/elgamal_ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<C: BlsSignatureImpl> subtle::ConditionallySelectable for ElGamalCiphertext<
}
}

impl<'a, 'b, C: BlsSignatureImpl> Add<&'b ElGamalCiphertext<C>> for &'a ElGamalCiphertext<C> {
impl<'b, C: BlsSignatureImpl> Add<&'b ElGamalCiphertext<C>> for &ElGamalCiphertext<C> {
type Output = ElGamalCiphertext<C>;

fn add(self, rhs: &'b ElGamalCiphertext<C>) -> Self::Output {
Expand All @@ -63,7 +63,7 @@ impl<'a, C: BlsSignatureImpl> Add<&'a ElGamalCiphertext<C>> for ElGamalCiphertex
}
}

impl<'a, C: BlsSignatureImpl> Add<ElGamalCiphertext<C>> for &'a ElGamalCiphertext<C> {
impl<C: BlsSignatureImpl> Add<ElGamalCiphertext<C>> for &ElGamalCiphertext<C> {
type Output = ElGamalCiphertext<C>;

fn add(self, rhs: ElGamalCiphertext<C>) -> Self::Output {
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub enum BlsError {
/// An error occurred during serialization
#[error("serialization error: {0}")]
DeserializationError(String),
/// Invalid coefficient in secure aggregation (extremely rare)
#[error("invalid coefficient: zero coefficient generated")]
InvalidCoefficient,
}

/// The result type generated by this library
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod public_key;
mod public_key_share;
mod secret_key;
mod secret_key_share;
mod secure_aggregation;
mod sig_types;
mod sign_crypt_ciphertext;
mod sign_decryption_share;
Expand Down
Loading