Skip to content

Commit

Permalink
feat: Support ed25519 batch signature verification
Browse files Browse the repository at this point in the history
  • Loading branch information
OtaK authored and augustocdias committed Aug 14, 2024
1 parent 5ae8c57 commit 3e68d88
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ branch = "otak/fix-1.0.3"
[features]
default = []
cryptobox-identity = []
unclamped-dh-exchange = []
hazmat = []

[dependencies]
Expand All @@ -42,7 +41,7 @@ sha2 = "0.10"
hkdf = "0.12"
hmac = "0.12"
chacha20 = "0.9"
ed25519-dalek = { version = "2.0.0-pre.0", features = ["zeroize"] }
ed25519-dalek = { version = "2.0.0-pre.0", features = ["zeroize", "batch"] }
x25519-dalek = { git = "https://github.com/dalek-cryptography/x25519-dalek.git", branch = "release/2.0" }
curve25519-dalek = { version = "4.0.0-rc.1", features = ["precomputed-tables"] }
subtle = "2.4"
Expand Down
5 changes: 1 addition & 4 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,10 @@ fn bench_verify_batched(c: &mut Criterion) {
&inputs,
|b, inputs| {
b.iter(|| {
for (sig, msg) in inputs {
let _r = x.public_key.verify(&sig, &msg);
}
let _r = x.public_key.verify_batch(inputs);
})
},
);

group.finish();
}

Expand Down
20 changes: 19 additions & 1 deletion src/internal/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,18 @@ impl PublicKey {
res.is_ok()
}

#[must_use]
pub fn verify_batch(
&self,
verifying_contents: &std::collections::HashMap<Signature, Vec<u8>>,
) -> bool {
let messages: Vec<&[u8]> = verifying_contents.values().map(|m| m.as_slice()).collect();
let signatures: Vec<_> = verifying_contents.keys().map(|s| s.0).collect();
let verifying_keys: Vec<_> = (0..signatures.len()).map(|_| self.0.clone()).collect();

ed25519_dalek::verify_batch(&messages, &signatures, &verifying_keys).is_ok()
}

pub fn as_slice(&self) -> &[u8] {
self.0.as_bytes()
}
Expand Down Expand Up @@ -665,10 +677,16 @@ pub fn rand_bytes(size: usize) -> Vec<u8> {
// Signature ////////////////////////////////////////////////////////////////

// SAFETY: ZeroizeOnDrop isn't needed as ed25519_dalek types already implement Zeroize + Drop
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct Signature(ed25519_dalek::Signature);

impl std::hash::Hash for Signature {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write(&self.0.to_bytes());
}
}

impl Signature {
pub fn encode<W: Write>(&self, e: &mut Encoder<W>) -> EncodeResult<()> {
e.object(1)?;
Expand Down

0 comments on commit 3e68d88

Please sign in to comment.