1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
pub mod ed25519;
pub mod keymanip;
pub mod rsa;
pub mod curve25519 {
pub use x25519_dalek::{EphemeralSecret, PublicKey, SharedSecret, StaticSecret};
}
pub trait ValidatableSignature {
fn is_valid(&self) -> bool;
fn as_ed25519(&self) -> Option<&ed25519::ValidatableEd25519Signature> {
None
}
}
pub fn validate_all_sigs(v: &[Box<dyn ValidatableSignature>]) -> bool {
let mut ed_sigs = Vec::new();
let mut non_ed_sigs = Vec::new();
for sig in v.iter() {
match sig.as_ed25519() {
Some(ed_sig) => ed_sigs.push(ed_sig),
None => non_ed_sigs.push(sig),
}
}
let ed_batch_is_valid = crate::pk::ed25519::validate_batch(&ed_sigs[..]);
ed_batch_is_valid && non_ed_sigs.iter().all(|b| b.is_valid())
}
#[cfg(test)]
mod test {
#[test]
fn validatable_ed_sig() {
use super::ed25519::{PublicKey, Signature, ValidatableEd25519Signature};
use super::ValidatableSignature;
use hex_literal::hex;
let pk = PublicKey::from_bytes(&hex!(
"fc51cd8e6218a1a38da47ed00230f058
0816ed13ba3303ac5deb911548908025"
))
.unwrap();
let sig: Signature = hex!(
"6291d657deec24024827e69c3abe01a3
0ce548a284743a445e3680d7db5ac3ac
18ff9b538d16f290ae67f760984dc659
4a7c15e9716ed28dc027beceea1ec40a"
)
.into();
let valid = ValidatableEd25519Signature::new(pk.clone(), sig.clone(), &hex!("af82"));
let invalid = ValidatableEd25519Signature::new(pk, sig, &hex!("af83"));
assert!(valid.is_valid());
assert!(!invalid.is_valid());
}
}