Skip to content

Commit

Permalink
implement aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanKung committed May 29, 2024
1 parent e532628 commit 394d76e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions crates/core/src/ecc/signers/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ pub fn verify(msgs: &[&[u8]], sig: &Signature, pks: &[PublicKey]) -> Result<bool
verify_hash(hashes.as_slice(), sig, pks)
}

// Aggregate signatures by multiplying them together. Calculated by signature = \sum_{i = 0}^n signature_i.
pub fn aggregate(signatures: &[Signature]) -> Result<Signature> {
signatures
.iter()
.map(|sig| sig.clone().try_into())
.collect::<Result<Vec<G2Projective>>>()?
.iter()
.sum::<G2Projective>()
.try_into()
}

/// Converts a BLS private key to a BLS public key.
/// Get the public key for this private key. Calculated by pk = g1 * sk.
pub fn public_key(key: &SecretKey) -> Result<PublicKey> {
Expand Down Expand Up @@ -209,4 +220,31 @@ mod test {
let h = hash_to_curve(msg.as_bytes()).unwrap();
assert_eq!(h, hashed_data);
}

#[test]
fn test_aggregate() {
let key1 = random_sk().unwrap();
let key2 = random_sk().unwrap();

let msg1 = "hello alice";
let msg2 = "hello bob";

let pk1 = public_key(&key1).unwrap();
let pk2 = public_key(&key2).unwrap();

let h1 = hash_to_curve(msg1.as_bytes()).unwrap();
let h2 = hash_to_curve(msg2.as_bytes()).unwrap();

let sig1 = sign_hash(key1, &h1).unwrap();
let sig2 = sign_hash(key2, &h2).unwrap();

let sig_agg = aggregate(&[sig1, sig2]).unwrap();

assert!(super::verify_hash(
vec![h1, h2].as_slice(),
&sig_agg,
vec![pk1.clone(), pk2.clone()].as_slice()
)
.unwrap());
}
}

0 comments on commit 394d76e

Please sign in to comment.