Skip to content

Commit 1f04321

Browse files
committed
Add ability to randomize context without the rand feature.
There is little reason to pull in the `rand` dep just for the `Rng` trait for users who want to randomize contexts. We should expose a randomize function that just takes 32 bytes.
1 parent a9049f8 commit 1f04321

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,13 @@ impl<C: Context> Secp256k1<C> {
646646
pub fn randomize<R: Rng + ?Sized>(&mut self, rng: &mut R) {
647647
let mut seed = [0; 32];
648648
rng.fill_bytes(&mut seed);
649+
self.seeded_randomize(&seed);
650+
}
651+
652+
/// (Re)randomizes the Secp256k1 context for cheap sidechannel resistance given 32 bytes of
653+
/// cryptographically-secure random data;
654+
/// see comment in libsecp256k1 commit d2275795f by Gregory Maxwell.
655+
pub fn seeded_randomize(&mut self, seed: &[u8; 32]) {
649656
unsafe {
650657
let err = ffi::secp256k1_context_randomize(self.ctx, seed.as_c_ptr());
651658
// This function cannot fail; it has an error return for future-proofing.
@@ -659,7 +666,6 @@ impl<C: Context> Secp256k1<C> {
659666
assert_eq!(err, 1);
660667
}
661668
}
662-
663669
}
664670

665671
fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool {

0 commit comments

Comments
 (0)