From 7777798a223fb192e3299e3fb770731c49dac404 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Tue, 4 Oct 2022 16:28:55 +0200 Subject: [PATCH] wip --- Cargo.toml | 1 + examples/pset_blind_coinjoin.rs | 43 ++------------------------------- examples/raw_blind.rs | 43 ++------------------------------- 3 files changed, 5 insertions(+), 82 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 53255065..e02ee167 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ elementsd = {version = "0.6.0", features=["0_21_0","bitcoind_22_0"], optional = [dev-dependencies] rand = "0.8" +rand_chacha = "0.3" serde_test = "1.0" serde_json = "1.0" serde_cbor = "0.8" # older than latest version to support 1.41.1 diff --git a/examples/pset_blind_coinjoin.rs b/examples/pset_blind_coinjoin.rs index 947bfc15..b734c6d0 100644 --- a/examples/pset_blind_coinjoin.rs +++ b/examples/pset_blind_coinjoin.rs @@ -27,6 +27,7 @@ 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 @@ -137,7 +138,7 @@ fn main() { let tests = test_data(); // Initially secp context and rng global state let secp = secp256k1_zkp::Secp256k1::new(); - let mut rng = CrappyRng::new(core::num::NonZeroU64::new(1).unwrap()); + let mut rng = rand_chacha::ChaCha20Rng::seed_from_u64(0); // NOTE, use from_seed() in production code, u64 has too few bits. let txouts = txout_data(); let (btc_txout, btc_txout_secrets, btc_inp) = txouts[0].clone(); @@ -285,43 +286,3 @@ 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 {} diff --git a/examples/raw_blind.rs b/examples/raw_blind.rs index 2ed66c51..c98d2b1a 100644 --- a/examples/raw_blind.rs +++ b/examples/raw_blind.rs @@ -15,6 +15,7 @@ use elements::{pset, secp256k1_zkp}; use elements::encode::{deserialize, serialize_hex}; use elements::hashes::hex::FromHex; use elements::{confidential, AssetId, TxOut}; +use rand::SeedableRng; /// Pset example workflow: /// Simple transaction spending a confidential asset @@ -138,7 +139,7 @@ fn main() { let tests = test_data(); // Initially secp context and rng global state let secp = secp256k1_zkp::Secp256k1::new(); - let mut rng = CrappyRng::new(core::num::NonZeroU64::new(1).unwrap()); + let mut rng = rand_chacha::ChaCha20Rng::seed_from_u64(0); // NOTE, use from_seed() in production code, u64 has too few bits. let txouts = txout_data(); let (btc_txout, btc_txout_secrets, btc_inp) = txouts[0].clone(); @@ -318,43 +319,3 @@ fn main() { let tx = pset.extract_tx().unwrap(); assert_eq!(serialize_hex(&tx), tests["extracted_tx"]); } - - -/// 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 {}