Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 5 additions & 41 deletions examples/pset_blind_coinjoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -137,7 +138,10 @@ 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());

// NOTE: Zero is not a reasonable seed for production code.
// It is used here so that we can match test vectors.
let mut rng = rand_chacha::ChaCha20Rng::from_seed([0u8; 32]);

let txouts = txout_data();
let (btc_txout, btc_txout_secrets, btc_inp) = txouts[0].clone();
Expand Down Expand Up @@ -285,43 +289,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 {}
46 changes: 5 additions & 41 deletions examples/raw_blind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -138,7 +139,10 @@ 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());

// NOTE: Zero is not a reasonable seed for production code.
// It is used here so that we can match test vectors.
let mut rng = rand_chacha::ChaCha20Rng::from_seed([0u8; 32]);

let txouts = txout_data();
let (btc_txout, btc_txout_secrets, btc_inp) = txouts[0].clone();
Expand Down Expand Up @@ -318,43 +322,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 {}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/blinded_one_inp_signed.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/blinded_signed.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/blinded_unsigned.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/extracted_tx.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/finalized.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/two_inp_two_out.hex

Large diffs are not rendered by default.