Skip to content

Commit 5190ad6

Browse files
authoredJan 31, 2023
Impl VerifyingKey::is_weak (dalek-cryptography#277)
* Implemented VerifyingKey::is_weak * Added unit test for VerifyingKey::is_weak
1 parent 1b86ff1 commit 5190ad6

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed
 

‎src/verifying.rs

+9
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ impl VerifyingKey {
163163
Context::new(self, context_value)
164164
}
165165

166+
/// Returns whether this is a _weak_ public key, i.e., if this public key has low order.
167+
///
168+
/// A weak public key can be used to generate a siganture that's valid for almost every
169+
/// message. [`Self::verify_strict`] denies weak keys, but if you want to check for this
170+
/// property before verification, then use this method.
171+
pub fn is_weak(&self) -> bool {
172+
self.1.is_small_order()
173+
}
174+
166175
/// Internal utility function for clamping a scalar representation and multiplying by the
167176
/// basepont to produce a public key.
168177
fn clamp_and_mul_base(bits: [u8; 32]) -> VerifyingKey {

‎tests/ed25519.rs

+6
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ mod vectors {
228228
assert!(vk.verify(message1, &sig).is_ok());
229229
assert!(vk.verify(message2, &sig).is_ok());
230230

231+
// Check that this public key appears as weak
232+
assert!(vk.is_weak());
233+
231234
// Now check that the sigs fail under verify_strict. This is because verify_strict rejects
232235
// small order pubkeys.
233236
assert!(vk.verify_strict(message1, &sig).is_err());
@@ -306,6 +309,9 @@ mod integrations {
306309
good_sig = signing_key.sign(&good);
307310
bad_sig = signing_key.sign(&bad);
308311

312+
// Check that an honestly generated public key is not weak
313+
assert!(!verifying_key.is_weak());
314+
309315
assert!(
310316
signing_key.verify(&good, &good_sig).is_ok(),
311317
"Verification of a valid signature failed!"

0 commit comments

Comments
 (0)