Struct rustreexo::accumulator::pollard::Pollard
source · pub struct Pollard {
pub leaves: u64,
/* private fields */
}
Expand description
The actual Pollard accumulator, it implements all methods required to update the forest and to prove/verify membership.
Fields§
§leaves: u64
The number of leaves in the forest. Actually, this is the number of leaves we ever added to the forest.
Implementations§
source§impl Pollard
impl Pollard
sourcepub fn serialize<W: Write>(&self, writer: W) -> Result<()>
pub fn serialize<W: Write>(&self, writer: W) -> Result<()>
Writes the Pollard to a writer. Used to send the accumulator over the wire or to disk.
Example
use rustreexo::accumulator::pollard::Pollard;
let mut pollard = Pollard::new();
let mut serialized = Vec::new();
pollard.serialize(&mut serialized).unwrap();
assert_eq!(
serialized,
vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
);
sourcepub fn deserialize<R: Read>(reader: R) -> Result<Pollard>
pub fn deserialize<R: Read>(reader: R) -> Result<Pollard>
Deserializes a pollard from a reader.
Example
use std::io::Cursor;
use rustreexo::accumulator::pollard::Pollard;
let mut serialized = Cursor::new(vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
let pollard = Pollard::deserialize(&mut serialized).unwrap();
assert_eq!(pollard.leaves, 0);
assert_eq!(pollard.get_roots().len(), 0);
sourcepub fn prove(&self, targets: &[NodeHash]) -> Result<Proof, String>
pub fn prove(&self, targets: &[NodeHash]) -> Result<Proof, String>
Proves that a given set of hashes is in the accumulator. It returns a proof and the hashes that we what to prove, but sorted by position in the tree.
Example
use rustreexo::accumulator::node_hash::NodeHash;
use rustreexo::accumulator::pollard::Pollard;
let mut pollard = Pollard::new();
let hashes = vec![0, 1, 2, 3, 4, 5, 6, 7]
.iter()
.map(|n| NodeHash::from([*n; 32]))
.collect::<Vec<_>>();
pollard.modify(&hashes, &[]).unwrap();
// We want to prove that the first two hashes are in the accumulator.
let proof = pollard.prove(&[hashes[1], hashes[0]]).unwrap();
//TODO: Verify the proof
sourcepub fn modify(
&mut self,
add: &[NodeHash],
del: &[NodeHash]
) -> Result<(), String>
pub fn modify( &mut self, add: &[NodeHash], del: &[NodeHash] ) -> Result<(), String>
Modify is the main API to a Pollard. Because order matters, you can only modify
a Pollard, and internally it’ll add and delete, in the correct order.
This method accepts two vectors as parameter, a vec of Hash and a vec of u64. The first one is a vec of leaf hashes for the newly created UTXOs. The second one is the position for the UTXOs being spent in this block as inputs.
Example
use bitcoin_hashes::sha256::Hash as Data;
use bitcoin_hashes::Hash;
use bitcoin_hashes::HashEngine;
use rustreexo::accumulator::node_hash::NodeHash;
use rustreexo::accumulator::pollard::Pollard;
let values = vec![0, 1, 2, 3, 4, 5, 6, 7];
let hashes = values
.into_iter()
.map(|val| {
let mut engine = Data::engine();
engine.input(&[val]);
NodeHash::from(Data::from_engine(engine).as_byte_array())
})
.collect::<Vec<_>>();
// Add 8 leaves to the pollard
let mut p = Pollard::new();
p.modify(&hashes, &[]).expect("Pollard should not fail");
assert_eq!(
p.get_roots()[0].get_data().to_string(),
String::from("b151a956139bb821d4effa34ea95c17560e0135d1e4661fc23cedc3af49dac42")
);