Skip to content

Commit 16fc4ee

Browse files
committed
Fixed the counter Rng
1 parent 6e58c8b commit 16fc4ee

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/lib.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -999,18 +999,37 @@ mod tests {
999999

10001000
#[cfg(all(test, feature = "unstable"))]
10011001
mod benches {
1002-
use rand::{Rng, thread_rng};
1002+
use rand::{thread_rng, RngCore};
10031003
use test::{Bencher, black_box};
10041004

10051005
use super::{Secp256k1, Message};
10061006

10071007
#[bench]
10081008
pub fn generate(bh: &mut Bencher) {
1009-
struct CounterRng(u32);
1010-
impl Rng for CounterRng {
1011-
fn next_u32(&mut self) -> u32 { self.0 += 1; self.0 }
1009+
struct CounterRng(u64);
1010+
impl RngCore for CounterRng {
1011+
fn next_u32(&mut self) -> u32 {
1012+
self.next_u64() as u32
1013+
}
1014+
1015+
fn next_u64(&mut self) -> u64 {
1016+
self.0 += 1;
1017+
self.0
1018+
}
1019+
1020+
fn fill_bytes(&mut self, dest: &mut [u8]) {
1021+
for chunk in dest.chunks_mut(64/8) {
1022+
let rand: [u8; 64/8] = unsafe {std::mem::transmute(self.next_u64())};
1023+
chunk.copy_from_slice(&rand[..chunk.len()]);
1024+
}
1025+
}
1026+
1027+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
1028+
Ok(self.fill_bytes(dest))
1029+
}
10121030
}
10131031

1032+
10141033
let s = Secp256k1::new();
10151034
let mut r = CounterRng(0);
10161035
bh.iter( || {

0 commit comments

Comments
 (0)