Skip to content

Commit

Permalink
Use crappy rng for pset_blind_coinjoin, update test_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Sep 29, 2022
1 parent ed79cdd commit ecb2c30
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
44 changes: 41 additions & 3 deletions examples/pset_blind_coinjoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use elements::{pset, secp256k1_zkp};
use elements::encode::{deserialize, serialize_hex};
use elements::hashes::hex::FromHex;
use elements::{confidential, AssetId, TxOut};
use rand::SeedableRng;

// Assume txouts are simple pay to wpkh
// and keep the secrets correponding to
Expand Down Expand Up @@ -138,8 +137,7 @@ fn main() {
let tests = test_data();
// Initially secp context and rng global state
let secp = secp256k1_zkp::Secp256k1::new();
#[allow(deprecated)]
let mut rng = rand::ChaChaRng::seed_from_u64(0);
let mut rng = CrappyRng::new(core::num::NonZeroU64::new(1).unwrap());

let txouts = txout_data();
let (btc_txout, btc_txout_secrets, btc_inp) = txouts[0].clone();
Expand Down Expand Up @@ -287,3 +285,43 @@ fn main() {
tx.verify_tx_amt_proofs(&secp, &[btc_txout, asset_txout])
.unwrap();
}


/// Xorshift
pub struct CrappyRng(u64);

impl CrappyRng {
fn new(initial: core::num::NonZeroU64) -> Self {
Self(initial.get())
}
}

impl rand::RngCore for CrappyRng {

fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}

fn next_u64(&mut self) -> u64 {
let mut x = self.0;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
self.0 = x;
x
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
for chunk in dest.chunks_mut(8) {
let x = self.next_u64().to_be_bytes();
chunk.copy_from_slice(&x[..chunk.len()]);

}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
Ok(self.fill_bytes(dest))
}
}

impl rand::CryptoRng for CrappyRng {}
Loading

0 comments on commit ecb2c30

Please sign in to comment.