Skip to content
Closed
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 @@ -12,6 +12,7 @@ rust-version = "1.74.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bitcoin = { version = "0.32.8", default-features = false, features = ["serde"] }
bitcoin_hashes = "0.18"
hex-conservative = "1.0.0"
serde = { version = "1.0", features = ["derive"], optional = true }
Expand Down
6 changes: 5 additions & 1 deletion benches/proof_benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::hint::black_box;

use bitcoin::hashes::Hash;
use bitcoin::BlockHash;
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::BenchmarkId;
Expand Down Expand Up @@ -55,7 +57,9 @@ fn proof_verification(c: &mut Criterion) {
let accumulator_size = 100;
let hashes = generate_test_hashes(accumulator_size, 42);
let stump = Stump::new();
let (stump, _) = stump.modify(&hashes, &[], &Proof::default()).unwrap();
let (stump, _) = stump
.modify(0, BlockHash::all_zeros(), &hashes, &[], &Proof::default())
.unwrap();

for target_count in [1, 10].iter() {
let del_hashes = hashes[..*target_count].to_vec();
Expand Down
15 changes: 12 additions & 3 deletions benches/stump_benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::hint::black_box;

use bitcoin::hashes::Hash;
use bitcoin::BlockHash;
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::BenchmarkId;
Expand Down Expand Up @@ -34,8 +36,13 @@ fn stump_modify_add_only(c: &mut Criterion) {

b.iter(|| {
let stump = Stump::new();
let result =
stump.modify(black_box(&hashes), black_box(&[]), black_box(&empty_proof));
let result = stump.modify(
0,
BlockHash::all_zeros(),
black_box(&hashes),
black_box(&[]),
black_box(&empty_proof),
);
black_box(result.unwrap())
});
});
Expand All @@ -50,7 +57,9 @@ fn stump_verify(c: &mut Criterion) {
let test_size = 1000;
let hashes = generate_test_hashes(test_size, 42);
let stump = Stump::new();
let (stump, _) = stump.modify(&hashes, &[], &Proof::default()).unwrap();
let (stump, _) = stump
.modify(0, BlockHash::all_zeros(), &hashes, &[], &Proof::default())
.unwrap();

for proof_size in [1, 10, 100].iter() {
let del_hashes = hashes[..*proof_size].to_vec();
Expand Down
4 changes: 3 additions & 1 deletion examples/full-accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use std::str::FromStr;

use bitcoin::hashes::Hash;
use bitcoin::BlockHash;
use rustreexo::accumulator::mem_forest::MemForest;
use rustreexo::accumulator::node_hash::BitcoinNodeHash;
use rustreexo::accumulator::proof::Proof;
Expand All @@ -28,7 +30,7 @@ fn main() {
let proof = p.prove(&[elements[0]]).unwrap();
// Verify the proof. Notice how we use the del_hashes returned by `prove` here.
let s = Stump::new()
.modify(&elements, &[], &Proof::default())
.modify(0, BlockHash::all_zeros(), &elements, &[], &Proof::default())
.unwrap()
.0;
assert_eq!(s.verify(&proof, &[elements[0]]), Ok(true));
Expand Down
10 changes: 8 additions & 2 deletions examples/proof-update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use std::str::FromStr;

use bitcoin::hashes::Hash;
use bitcoin::BlockHash;
use rustreexo::accumulator::node_hash::BitcoinNodeHash;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;
Expand All @@ -22,7 +24,9 @@ fn main() {
let utxos = get_utxo_hashes1();
// Add the UTXOs to the accumulator. update_data is the data we need to update the proof
// after the accumulator is updated.
let (s, update_data) = s.modify(&utxos, &[], &Proof::default()).unwrap();
let (s, update_data) = s
.modify(0, BlockHash::all_zeros(), &utxos, &[], &Proof::default())
.unwrap();
// Create an empty proof, we'll update it to hold our UTXOs
let p = Proof::default();
// Update the proof with the UTXOs we added to the accumulator. This proof was initially empty,
Expand All @@ -48,7 +52,9 @@ fn main() {
// We'll remove `0` as it got spent, and add 1..7 to our cache.
let new_utxos = get_utxo_hashes2();
// First, update the accumulator
let (stump, update_data) = s.modify(&new_utxos, &[utxos[0]], &p1).unwrap();
let (stump, update_data) = s
.modify(0, BlockHash::all_zeros(), &new_utxos, &[utxos[0]], &p1)
.unwrap();
// and the proof
let (p2, cached_hashes) = p
.update(
Expand Down
9 changes: 7 additions & 2 deletions examples/simple-stump-update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use std::str::FromStr;
use std::vec;

use bitcoin::hashes::Hash;
use bitcoin::BlockHash;
use rustreexo::accumulator::node_hash::BitcoinNodeHash;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;
Expand All @@ -28,7 +30,7 @@ fn main() {
// but only the Stump. To understand what is the second return value, see the documentation
// for `Stump::modify`, or the proof-update example.
let s = Stump::new()
.modify(&utxos, &[], &Proof::default())
.modify(0, BlockHash::all_zeros(), &utxos, &[], &Proof::default())
.unwrap()
.0;
// Create a proof that the first utxo is in the Stump.
Expand All @@ -42,7 +44,10 @@ fn main() {
"d3bd63d53c5a70050a28612a2f4b2019f40951a653ae70736d93745efb1124fa",
)
.unwrap();
let s = s.modify(&[new_utxo], &[utxos[0]], &proof).unwrap().0;
let s = s
.modify(0, BlockHash::all_zeros(), &[new_utxo], &[utxos[0]], &proof)
.unwrap()
.0;
// Now we can verify that the new utxo is in the Stump, and the old one is not.
let new_proof = Proof::new(vec![2], vec![new_utxo]);
assert_eq!(s.verify(&new_proof, &[new_utxo]), Ok(true));
Expand Down
17 changes: 12 additions & 5 deletions src/accumulator/pollard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ impl<Hash: AccumulatorHash> PollardNode<Hash> {
/// The deletion algorithm for utreexo works like this: let's say we have the following tree:
///
/// ```!
/// 06
/// |---------\
/// 04 05
/// 06
/// |---------\
/// 04 05
/// |-----\ |-----\
/// 00 01 02 03
/// 00 01 02 03
/// ```
///
/// to delete `03`, we simply move `02` up to `09`'s position, so now we have:
Expand Down Expand Up @@ -1266,6 +1266,8 @@ impl<Hash: AccumulatorHash> From<Stump<Hash>> for Pollard<Hash> {
mod tests {
use std::str::FromStr;

use bitcoin::hashes::Hash;
use bitcoin::BlockHash;
use serde::Deserialize;

use super::*;
Expand Down Expand Up @@ -1377,7 +1379,12 @@ mod tests {
];
let leaves = 15;

let stump = Stump { roots, leaves };
let stump = Stump {
block_height: 0,
block_hash: BlockHash::all_zeros(),
roots,
leaves,
};
let p: Pollard<BitcoinNodeHash> = stump.clone().into();

assert_eq!(stump.roots, p.roots());
Expand Down
18 changes: 14 additions & 4 deletions src/accumulator/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,8 @@ impl<Hash: AccumulatorHash> Proof<Hash> {
mod tests {
use std::str::FromStr;

use bitcoin::hashes::Hash;
use bitcoin::BlockHash;
use serde::Deserialize;

use super::Proof;
Expand Down Expand Up @@ -980,6 +982,8 @@ mod tests {
.collect();

let stump = Stump {
block_height: 0,
block_hash: BlockHash::all_zeros(),
leaves: case_values.initial_leaves,
roots,
};
Expand All @@ -1006,7 +1010,9 @@ mod tests {

let block_proof =
Proof::new(case_values.update.proof.targets.clone(), block_proof_hashes);
let (stump, updated) = stump.modify(&utxos, &del_hashes, &block_proof).unwrap();
let (stump, updated) = stump
.modify(0, BlockHash::all_zeros(), &utxos, &del_hashes, &block_proof)
.unwrap();
let (cached_proof, cached_hashes) = cached_proof
.update(
cached_hashes.clone(),
Expand Down Expand Up @@ -1193,7 +1199,7 @@ mod tests {
let preimages = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let hashes = preimages.into_iter().map(hash_from_u8).collect::<Vec<_>>();
let (stump, _) = Stump::new()
.modify(&hashes, &[], &Proof::default())
.modify(0, BlockHash::all_zeros(), &hashes, &[], &Proof::default())
.unwrap();

let proof_hashes = vec![
Expand Down Expand Up @@ -1222,6 +1228,8 @@ mod tests {

let (stump, modified) = stump
.modify(
0,
BlockHash::all_zeros(),
&[],
&[hash_from_u8(1), hash_from_u8(2), hash_from_u8(6)],
&proof,
Expand All @@ -1248,7 +1256,7 @@ mod tests {
let hashes = preimages.into_iter().map(hash_from_u8).collect::<Vec<_>>();
// Create a new stump with 8 leaves and 1 root
let s = Stump::new()
.modify(&hashes, &[], &Proof::default())
.modify(0, BlockHash::all_zeros(), &hashes, &[], &Proof::default())
.expect("This stump is valid")
.0;

Expand Down Expand Up @@ -1391,7 +1399,7 @@ mod tests {
let hashes = preimages.into_iter().map(hash_from_u8).collect::<Vec<_>>();
// Create a new stump with 8 leaves and 1 root
let s = Stump::new()
.modify(&hashes, &[], &Proof::default())
.modify(0, BlockHash::all_zeros(), &hashes, &[], &Proof::default())
.expect("This stump is valid")
.0;

Expand Down Expand Up @@ -1436,6 +1444,8 @@ mod tests {
.collect();

let s = Stump {
block_height: 0,
block_hash: BlockHash::all_zeros(),
leaves: case.numleaves as u64,
roots,
};
Expand Down
Loading
Loading