Skip to content

Commit

Permalink
Replace the quadradic duplicate check logic with a linear version (#72)
Browse files Browse the repository at this point in the history
It's ~2% slower with 10 hashes and ~2% faster with 100 hashes (with very
noisy benchmarks). But the important part is that the runtime should
scale linearly with the number of inputs. E.g., this version is 2.5x
faster for 1000 signatures.

fixes #68
  • Loading branch information
Stebalien committed Oct 20, 2023
1 parent 5cb8107 commit 89b6656
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io;
use std::{collections::HashSet, io};

#[cfg(feature = "multicore")]
use rayon::prelude::*;
Expand Down Expand Up @@ -139,12 +139,15 @@ pub fn verify(signature: &Signature, hashes: &[G2Projective], public_keys: &[Pub
// Enforce that messages are distinct as a countermeasure against BLS's rogue-key attack.
// See Section 3.1. of the IRTF's BLS signatures spec:
// https://tools.ietf.org/html/draft-irtf-cfrg-bls-signature-02#section-3.1
for i in 0..(n_hashes - 1) {
for j in (i + 1)..n_hashes {
if hashes[i] == hashes[j] {
return false;
}
}
if hashes
.iter()
// This is the best way to get something we can actually hash.
.map(|h| G2Affine::from(h).to_compressed())
.collect::<HashSet<_>>()
.len()
!= hashes.len()
{
return false;
}

#[cfg(feature = "multicore")]
Expand Down

0 comments on commit 89b6656

Please sign in to comment.