#![cfg(feature = "dhat-profiling")]
#[cfg(feature = "transaction")]
#[path = "support/beef_chain.rs"]
mod beef_chain;
use bsv_rs::primitives::bsv::shamir::split_private_key;
use bsv_rs::primitives::ec::PrivateKey;
use bsv_rs::primitives::hash;
use bsv_rs::primitives::symmetric::SymmetricKey;
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
static PROFILER: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn serial() -> std::sync::MutexGuard<'static, ()> {
PROFILER.lock().unwrap_or_else(|e| e.into_inner())
}
#[allow(dead_code)]
fn profile_operation<F>(name: &str, iterations: usize, mut op: F)
where
F: FnMut(),
{
let _profiler = dhat::Profiler::new_heap();
for _ in 0..iterations {
op();
}
let stats = dhat::HeapStats::get();
println!("\n=== {} ({} iterations) ===", name, iterations);
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(
" Avg bytes per iteration: {}",
stats.total_bytes / iterations as u64
);
}
#[test]
fn test_encryption_allocations() {
let _serial = serial();
let key = SymmetricKey::random();
let sizes: &[usize] = &[64, 256, 1024, 4096, 16384];
for &size in sizes {
let plaintext = vec![0u8; size];
let _profiler = dhat::Profiler::new_heap();
for _ in 0..100 {
let ciphertext = key.encrypt(&plaintext).unwrap();
let _ = key.decrypt(&ciphertext).unwrap();
}
let stats = dhat::HeapStats::get();
println!(
"\n=== AES-GCM Encrypt/Decrypt {} bytes (100 cycles) ===",
size
);
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(" Avg bytes per cycle: {}", stats.total_bytes / 100);
}
}
#[test]
fn test_key_derivation_allocations() {
let _serial = serial();
let alice = PrivateKey::random();
let bob = PrivateKey::random();
let bob_pub = bob.public_key();
{
let _profiler = dhat::Profiler::new_heap();
for i in 0..100 {
let invoice_id = format!("invoice-{}", i);
let _ = alice.derive_child(&bob_pub, &invoice_id);
}
let stats = dhat::HeapStats::get();
println!("\n=== BRC-42 Key Derivation (100 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(" Avg bytes per derivation: {}", stats.total_bytes / 100);
}
{
let _profiler = dhat::Profiler::new_heap();
for _ in 0..100 {
let _ = alice.derive_shared_secret(&bob_pub);
}
let stats = dhat::HeapStats::get();
println!("\n=== ECDH Shared Secret (100 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(" Avg bytes per derivation: {}", stats.total_bytes / 100);
}
}
#[test]
fn test_shamir_allocations() {
let _serial = serial();
let key = PrivateKey::random();
{
let _profiler = dhat::Profiler::new_heap();
for _ in 0..50 {
let _ = split_private_key(&key, 3, 5);
}
let stats = dhat::HeapStats::get();
println!("\n=== Shamir Split 3-of-5 (50 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(" Avg bytes per split: {}", stats.total_bytes / 50);
}
{
let _profiler = dhat::Profiler::new_heap();
for _ in 0..50 {
let _ = split_private_key(&key, 5, 10);
}
let stats = dhat::HeapStats::get();
println!("\n=== Shamir Split 5-of-10 (50 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(" Avg bytes per split: {}", stats.total_bytes / 50);
}
{
let shares = split_private_key(&key, 3, 5).unwrap();
let _profiler = dhat::Profiler::new_heap();
for _ in 0..50 {
let subset = bsv_rs::primitives::bsv::shamir::KeyShares::new(
shares.points[0..3].to_vec(),
3,
shares.integrity.clone(),
);
let _ = subset.recover_private_key();
}
let stats = dhat::HeapStats::get();
println!("\n=== Shamir Recover 3-of-5 (50 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(" Avg bytes per recovery: {}", stats.total_bytes / 50);
}
}
#[test]
fn test_signing_allocations() {
let _serial = serial();
let key = PrivateKey::random();
let pubkey = key.public_key();
let msg_hash = hash::sha256(b"benchmark message for signing");
{
let _profiler = dhat::Profiler::new_heap();
for _ in 0..100 {
let _ = key.sign(&msg_hash);
}
let stats = dhat::HeapStats::get();
println!("\n=== ECDSA Sign (100 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(" Avg bytes per sign: {}", stats.total_bytes / 100);
}
{
let sig = key.sign(&msg_hash).unwrap();
let _profiler = dhat::Profiler::new_heap();
for _ in 0..100 {
let _ = pubkey.verify(&msg_hash, &sig);
}
let stats = dhat::HeapStats::get();
println!("\n=== ECDSA Verify (100 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(" Avg bytes per verify: {}", stats.total_bytes / 100);
}
{
let _profiler = dhat::Profiler::new_heap();
for _ in 0..100 {
let sig = key.sign(&msg_hash).unwrap();
let _ = pubkey.verify(&msg_hash, &sig);
}
let stats = dhat::HeapStats::get();
println!("\n=== ECDSA Sign+Verify Cycle (100 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
println!(" Avg bytes per cycle: {}", stats.total_bytes / 100);
}
}
#[test]
fn test_hashing_allocations() {
let _serial = serial();
let data_1kb = vec![0u8; 1024];
let data_16kb = vec![0u8; 16384];
{
let _profiler = dhat::Profiler::new_heap();
for _ in 0..1000 {
let _ = hash::sha256(&data_1kb);
}
let stats = dhat::HeapStats::get();
println!("\n=== SHA-256 1KB (1000 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
}
{
let _profiler = dhat::Profiler::new_heap();
for _ in 0..100 {
let _ = hash::sha256(&data_16kb);
}
let stats = dhat::HeapStats::get();
println!("\n=== SHA-256 16KB (100 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
}
{
let _profiler = dhat::Profiler::new_heap();
for _ in 0..1000 {
let _ = hash::hash160(&data_1kb);
}
let stats = dhat::HeapStats::get();
println!("\n=== Hash160 1KB (1000 iterations) ===");
println!(" Total allocations: {}", stats.total_blocks);
println!(" Total bytes allocated: {}", stats.total_bytes);
println!(" Peak heap bytes: {}", stats.max_bytes);
}
}
#[cfg(feature = "transaction")]
#[test]
fn test_beef_stream_memory_is_flat_per_element() {
use beef_chain::{funding_root, subject, ChainSource, HEIGHT};
use bsv_rs::transaction::{verify_stream, verify_stream_structure, BeefStream};
use std::collections::HashMap;
let _serial = serial();
fn peak(f: impl FnOnce()) -> u64 {
let _profiler = dhat::Profiler::builder().testing().build();
f();
dhat::HeapStats::get().max_bytes as u64
}
println!("\n=== BEEF stream, the P0-5 deep chain ===");
let mut rows = Vec::new();
for n in [1_000usize, 10_000, 100_000] {
let tip = subject(n);
let stream_only = peak(|| {
let mut elements = 0usize;
for element in BeefStream::new(ChainSource::new(n)) {
element.expect("the chain reads");
elements += 1;
}
assert_eq!(elements, n + 1);
});
let structure = peak(|| {
let headers = HashMap::from([(HEIGHT, funding_root())]);
let verdict = verify_stream_structure(ChainSource::new(n), &headers, Some(tip));
assert!(verdict.expect("the source does not fail").is_valid());
});
let full = peak(|| {
let headers = HashMap::from([(HEIGHT, funding_root())]);
let verdict = verify_stream(ChainSource::new(n), &headers, Some(tip));
assert!(verdict.expect("the source does not fail").is_valid());
});
let per = |total: u64| (total - stream_only.min(total)) as f64 / n as f64;
println!(
" N={n}: stream alone {stream_only} B; structure {structure} B ({:.1} B per element); \
scripts {full} B ({:.1} B per element)",
per(structure),
per(full)
);
rows.push((n, stream_only, per(structure), per(full)));
}
let (_, first, ..) = rows[0];
for (n, stream_only, ..) in &rows {
assert_eq!(
*stream_only, first,
"the stream's peak moved with the depth at N={n}"
);
assert!(
*stream_only < 32 * 1024,
"the stream holds more than a chunk"
);
}
for (n, _, structure, full) in &rows {
assert!(*structure < 400.0, "N={n}: {structure} B per element");
assert!(*full < 400.0, "N={n}: {full} B per element");
assert!(
(*full - *structure).abs() * (*n as f64) < 16.0 * 1024.0,
"N={n}: the scripts kept {:.0} B more than the structure",
(*full - *structure) * *n as f64
);
}
let (_, _, at_1_000, _) = rows[0];
let (_, _, at_100_000, _) = rows[2];
assert!(
at_100_000 <= at_1_000 * 1.25,
"the bytes per element grew with the depth: {at_1_000} then {at_100_000}"
);
}
#[cfg(feature = "transaction")]
#[test]
fn test_beef_stream_memory_on_proven_transactions_with_many_inputs() {
use bsv_rs::primitives::Writer;
use bsv_rs::script::{LockingScript, UnlockingScript};
use bsv_rs::transaction::beef_stream::Hash32;
use bsv_rs::transaction::{
verify_stream, verify_stream_structure, MerklePath, Transaction, TransactionInput,
TransactionOutput, ATOMIC_BEEF, BEEF_V1,
};
use std::collections::HashMap;
let _serial = serial();
fn peak(f: impl FnOnce()) -> u64 {
let _profiler = dhat::Profiler::builder().testing().build();
f();
dhat::HeapStats::get().max_bytes as u64
}
fn internal(txid: &str) -> Hash32 {
let mut h: Hash32 = hex::decode(txid).unwrap().try_into().unwrap();
h.reverse();
h
}
const TXS: usize = 100;
const INPUTS: usize = 1_000;
let mut bumps = Vec::new();
let mut raws = Vec::new();
let mut headers: HashMap<u64, Hash32> = HashMap::new();
for t in 0..TXS {
let mut tx = Transaction::new();
for i in 0..INPUTS {
let mut input = TransactionInput::new(format!("{:064x}", t * INPUTS + i + 1), 0);
input.unlocking_script = Some(UnlockingScript::from_binary(&[]).unwrap());
tx.inputs.push(input);
}
tx.outputs.push(TransactionOutput::new(
1,
LockingScript::from_binary(&[0x51]).unwrap(),
));
let height = 980_000 + t as u32;
let bump = MerklePath::from_coinbase_txid(&tx.id(), height);
headers.insert(
u64::from(height),
internal(&bump.compute_root(None).unwrap()),
);
bumps.push(bump);
raws.push(tx.to_binary());
}
let mut w = Writer::new();
w.write_u32_le(BEEF_V1);
w.write_var_int(TXS as u64);
for bump in &bumps {
w.write_bytes(&bump.to_binary());
}
w.write_var_int(TXS as u64);
for (i, raw) in raws.iter().enumerate() {
w.write_bytes(raw);
w.write_u8(1);
w.write_var_int(i as u64);
}
let plain = w.into_bytes();
let last = Transaction::from_binary(raws.last().unwrap()).unwrap().id();
let mut atomic = Vec::with_capacity(36 + plain.len());
atomic.extend_from_slice(&ATOMIC_BEEF.to_le_bytes());
atomic.extend_from_slice(&internal(&last));
atomic.extend_from_slice(&plain);
let structure = peak(|| {
let v = verify_stream_structure(plain.as_slice(), &headers, None).unwrap();
assert!(v.is_valid(), "{v:?}");
});
let full = peak(|| {
let v = verify_stream(plain.as_slice(), &headers, None).unwrap();
assert!(v.is_valid(), "{v:?}");
});
let under_subject = peak(|| {
let v = verify_stream_structure(atomic.as_slice(), &headers, None).unwrap();
assert!(!v.is_valid(), "{v:?}");
});
let inputs = (TXS * INPUTS) as f64;
let per_input = (under_subject.saturating_sub(structure)) as f64 / inputs;
println!("\n=== BEEF stream, {TXS} proven transactions of {INPUTS} inputs ===");
println!(
" BEEF {} B; no subject: structure {structure} B, scripts {full} B; \
under a subject: {under_subject} B ({per_input:.1} B per input, its edge)",
plain.len()
);
let element = (raws[0].len() * 4) as u64;
for (name, bytes) in [("structure", structure), ("scripts", full)] {
assert!(
bytes < element + TXS as u64 * 2_048,
"{name}: {bytes} B with no subject"
);
}
assert!(
(28.0..160.0).contains(&per_input),
"{per_input:.1} B per input, its edge"
);
}