Skip to content

Commit a1e8020

Browse files
committed
Use crappy rng for raw_blind, update test_vector
1 parent ecb2c30 commit a1e8020

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

examples/raw_blind.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use elements::{pset, secp256k1_zkp};
1515
use elements::encode::{deserialize, serialize_hex};
1616
use elements::hashes::hex::FromHex;
1717
use elements::{confidential, AssetId, TxOut};
18-
use rand::SeedableRng;
18+
1919
/// Pset example workflow:
2020
/// Simple transaction spending a confidential asset
2121
/// with external signer and blinding done by rust-elements using raw APIs
@@ -138,8 +138,7 @@ fn main() {
138138
let tests = test_data();
139139
// Initially secp context and rng global state
140140
let secp = secp256k1_zkp::Secp256k1::new();
141-
#[allow(deprecated)]
142-
let mut rng = rand::ChaChaRng::seed_from_u64(0);
141+
let mut rng = CrappyRng::new(core::num::NonZeroU64::new(1).unwrap());
143142

144143
let txouts = txout_data();
145144
let (btc_txout, btc_txout_secrets, btc_inp) = txouts[0].clone();
@@ -271,7 +270,6 @@ fn main() {
271270
// Add both pset outputs to btc transaction
272271
pset.add_output(pset::Output::from_txout(btc_fees_txout));
273272
pset.add_output(pset::Output::from_txout(btc_change_txout));
274-
275273
assert_eq!(pset, deser_pset(&tests["blinded_unsigned"]));
276274

277275
// Verify the balance checks
@@ -320,3 +318,43 @@ fn main() {
320318
let tx = pset.extract_tx().unwrap();
321319
assert_eq!(serialize_hex(&tx), tests["extracted_tx"]);
322320
}
321+
322+
323+
/// Xorshift
324+
pub struct CrappyRng(u64);
325+
326+
impl CrappyRng {
327+
fn new(initial: core::num::NonZeroU64) -> Self {
328+
Self(initial.get())
329+
}
330+
}
331+
332+
impl rand::RngCore for CrappyRng {
333+
334+
fn next_u32(&mut self) -> u32 {
335+
self.next_u64() as u32
336+
}
337+
338+
fn next_u64(&mut self) -> u64 {
339+
let mut x = self.0;
340+
x ^= x << 13;
341+
x ^= x >> 7;
342+
x ^= x << 17;
343+
self.0 = x;
344+
x
345+
}
346+
347+
fn fill_bytes(&mut self, dest: &mut [u8]) {
348+
for chunk in dest.chunks_mut(8) {
349+
let x = self.next_u64().to_be_bytes();
350+
chunk.copy_from_slice(&x[..chunk.len()]);
351+
352+
}
353+
}
354+
355+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
356+
Ok(self.fill_bytes(dest))
357+
}
358+
}
359+
360+
impl rand::CryptoRng for CrappyRng {}

0 commit comments

Comments
 (0)