Skip to content

Commit 98bfb48

Browse files
committed
musig: make zero-check in SessionSecretRand::assume_unique constant time
I haven't checked against the assembler code and this check is simple enough that I suspect that the compiler is going to undermine me, but the use of ptr::read_volatile *should* prevent that. Anyway make a best-effort attempt.
1 parent d318169 commit 98bfb48

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/musig.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ impl SessionSecretRand {
6565
/// a random number generator, or if that is not available, the output of a
6666
/// stable monotonic counter.
6767
pub fn assume_unique_per_nonce_gen(inner: [u8; 32]) -> Self {
68-
assert_ne!(inner, [0; 32], "session secrets may not be all zero");
68+
// See SecretKey::eq for this "constant-time" algorithm for comparison against zero.
69+
let inner_or = inner.iter().fold(0, |accum, x| accum | *x);
70+
assert!(
71+
unsafe { core::ptr::read_volatile(&inner_or) != 0 },
72+
"session secrets may not be all zero",
73+
);
74+
6975
SessionSecretRand(inner)
7076
}
7177

0 commit comments

Comments
 (0)