Skip to content

Commit f6ecc2e

Browse files
committed
test signing is deterministic
1 parent 30f051b commit f6ecc2e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

crates/aptos-crypto/src/slh_dsa_sha2_128s/slh_dsa_keys.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl PrivateKey {
9494
/// methods of the SigningKey implementation. This should remain private.
9595
fn sign_arbitrary_message(&self, message: &[u8]) -> Signature {
9696
use slh_dsa::signature::Signer;
97+
// NOTE: To hedge against fault attacks, can use RandomizedSigner::<slh_dsa::Signature<Sha2_128s>>::sign_with_rng().
9798
let signature = Signer::<slh_dsa::Signature<Sha2_128s>>::sign(&self.0, message);
9899
Signature(signature)
99100
}
@@ -535,4 +536,31 @@ mod tests {
535536
"Cloned private key should be equal to the original"
536537
);
537538
}
539+
540+
#[test]
541+
fn test_signing_is_deterministic() {
542+
// Generate a random private key
543+
let mut rng = rand::thread_rng();
544+
let key = PrivateKey::generate(&mut rng);
545+
546+
// Create a test message
547+
let message = b"test message for deterministic signing";
548+
549+
// Sign the same message twice
550+
let signature1 = key.sign_arbitrary_message(message);
551+
let signature2 = key.sign_arbitrary_message(message);
552+
553+
// Assert that the two signatures are identical
554+
assert_eq!(
555+
signature1, signature2,
556+
"Signing the same message twice should produce identical signatures"
557+
);
558+
559+
// Also verify the signatures are equal when comparing bytes
560+
assert_eq!(
561+
signature1.to_bytes(),
562+
signature2.to_bytes(),
563+
"Signature bytes should be identical for the same message"
564+
);
565+
}
538566
}

crates/aptos-crypto/src/slh_dsa_sha2_128s/slh_dsa_sigs.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,15 @@ impl fmt::Debug for Signature {
121121
write!(f, "slh_dsa_sha2_128s::Signature({})", self)
122122
}
123123
}
124+
125+
#[cfg(test)]
126+
mod tests {
127+
use super::*;
128+
129+
#[test]
130+
fn test_dummy_signature_deserializes() {
131+
// Create a dummy signature by deserializing some dummy bytes.
132+
// This test simply ensures this doesn't panic.
133+
let _ = Signature::dummy_signature();
134+
}
135+
}

0 commit comments

Comments
 (0)