Skip to content

Commit

Permalink
Merge pull request #56 from Davidson-Souza/partial-pollard
Browse files Browse the repository at this point in the history
feat: Pollard and partial forest support
  • Loading branch information
Davidson-Souza authored Dec 3, 2024
2 parents 9890fa7 + ce1f4e6 commit 1ee240f
Show file tree
Hide file tree
Showing 8 changed files with 1,978 additions and 857 deletions.
16 changes: 9 additions & 7 deletions examples/custom-hash-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
//! for zero-knowledge proofs, and is used in projects like ZCash and StarkNet.
//! If you want to work with utreexo proofs in zero-knowledge you may want to use this instead
//! of our usual sha512-256 that we use by default, since that will give you smaller circuits.
//! This example shows how to use both the [Pollard](crate::accumulator::pollard::Pollard) and
//! This example shows how to use both the [MemForest](crate::accumulator::MemForest::MemForest) and
//! proofs with a custom hash type. The code here should be pretty much all you need to do to
//! use your custom hashes, just tweak the implementation of
//! [NodeHash](crate::accumulator::node_hash::NodeHash) for your hash type.
use rustreexo::accumulator::mem_forest::MemForest;
use rustreexo::accumulator::node_hash::AccumulatorHash;
use rustreexo::accumulator::pollard::Pollard;
use starknet_crypto::poseidon_hash_many;
use starknet_crypto::Felt;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// We need a stateful wrapper around the actual hash, this is because we use those different
/// values inside our accumulator. Here we use an enum to represent the different states, you
/// may want to use a struct with more data, depending on your needs.
Expand All @@ -34,6 +34,8 @@ enum PoseidonHash {
/// returns sane values (that is, if we call [NodeHash::placeholder] calling [NodeHash::is_placeholder]
/// on the result should return true).
Placeholder,

#[default]
/// This is an empty value, it represents a node that was deleted from the accumulator.
///
/// Same as the placeholder, you can implement this the way you want, just make sure that
Expand Down Expand Up @@ -123,17 +125,17 @@ impl AccumulatorHash for PoseidonHash {
}

fn main() {
// Create a vector with two utxos that will be added to the Pollard
// Create a vector with two utxos that will be added to the MemForest
let elements = vec![
PoseidonHash::Hash(Felt::from(1)),
PoseidonHash::Hash(Felt::from(2)),
];

// Create a new Pollard, and add the utxos to it
let mut p = Pollard::<PoseidonHash>::new_with_hash();
// Create a new MemForest, and add the utxos to it
let mut p = MemForest::<PoseidonHash>::new_with_hash();
p.modify(&elements, &[]).unwrap();

// Create a proof that the first utxo is in the Pollard
// Create a proof that the first utxo is in the MemForest
let proof = p.prove(&[elements[0]]).unwrap();

// check that the proof has exactly one target
Expand Down
12 changes: 6 additions & 6 deletions examples/full-accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use std::str::FromStr;

use rustreexo::accumulator::mem_forest::MemForest;
use rustreexo::accumulator::node_hash::BitcoinNodeHash;
use rustreexo::accumulator::pollard::Pollard;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;

Expand All @@ -20,19 +20,19 @@ fn main() {
)
.unwrap(),
];
// Create a new Pollard, and add the utxos to it
let mut p = Pollard::new();
// Create a new MemForest, and add the utxos to it
let mut p = MemForest::new();
p.modify(&elements, &[]).unwrap();

// Create a proof that the first utxo is in the Pollard
// Create a proof that the first utxo is in the MemForest
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())
.unwrap()
.0;
assert_eq!(s.verify(&proof, &[elements[0]]), Ok(true));
// Now we want to update the Pollard, by removing the first utxo, and adding a new one.
// Now we want to update the MemForest, by removing the first utxo, and adding a new one.
// This would be in case we received a new block with a transaction spending the first utxo,
// and creating a new one.
let new_utxo = BitcoinNodeHash::from_str(
Expand All @@ -41,6 +41,6 @@ fn main() {
.unwrap();
p.modify(&[new_utxo], &[elements[0]]).unwrap();

// Now we can prove that the new utxo is in the Pollard.
// Now we can prove that the new utxo is in the MemForest.
let _ = p.prove(&[new_utxo]).unwrap();
}
Loading

0 comments on commit 1ee240f

Please sign in to comment.