Add blanket implementation of Hashable for incrementalmerkletree::Hashable + HashSer

This commit is contained in:
Kris Nuttycombe 2021-08-16 13:48:37 -06:00
parent 837ad19262
commit 7b953283ce
3 changed files with 43 additions and 43 deletions

View File

@ -1,7 +1,7 @@
//! Implementation of a Merkle tree of commitments used to prove the existence of notes.
use byteorder::{LittleEndian, ReadBytesExt};
use incrementalmerkletree::bridgetree;
use incrementalmerkletree::{self, bridgetree, Altitude};
use std::collections::VecDeque;
use std::io::{self, Read, Write};
@ -29,6 +29,45 @@ pub trait Hashable: Clone + Copy {
fn empty_root(_: usize) -> Self;
}
/// A hashable node within a Merkle tree.
pub trait HashSer {
/// Parses a node from the given byte source.
fn read<R: Read>(reader: R) -> io::Result<Self>
where
Self: Sized;
/// Serializes this node.
fn write<W: Write>(&self, writer: W) -> io::Result<()>;
}
impl<T> Hashable for T where T: incrementalmerkletree::Hashable + HashSer + Copy {
/// Parses a node from the given byte source.
fn read<R: Read>(reader: R) -> io::Result<Self> {
<Self as HashSer>::read(reader)
}
/// Serializes this node.
fn write<W: Write>(&self, writer: W) -> io::Result<()> {
<Self as HashSer>::write(self, writer)
}
/// Returns the parent node within the tree of the two given nodes.
fn combine(alt: usize, lhs: &Self, rhs: &Self) -> Self {
<Self as incrementalmerkletree::Hashable>::combine(Altitude::from(alt as u8), lhs, rhs)
}
/// Returns a blank leaf node.
fn blank() -> Self {
<Self as incrementalmerkletree::Hashable>::empty_leaf()
}
/// Returns the empty root for the given depth.
fn empty_root(alt: usize) -> Self {
<Self as incrementalmerkletree::Hashable>::empty_root(Altitude::from(alt as u8))
}
}
struct PathFiller<Node: Hashable> {
queue: VecDeque<Node>,
}

View File

@ -13,22 +13,11 @@ use incrementalmerkletree::{
};
use orchard::tree::MerkleCrhOrchardOutput;
use super::CommitmentTree;
use super::{CommitmentTree, HashSer};
use crate::serialize::{Optional, Vector};
pub const SER_V1: u8 = 1;
/// A hashable node within a Merkle tree.
pub trait HashSer {
/// Parses a node from the given byte source.
fn read<R: Read>(reader: R) -> io::Result<Self>
where
Self: Sized;
/// Serializes this node.
fn write<W: Write>(&self, writer: W) -> io::Result<()>;
}
pub fn read_frontier_v0<H: Hashable + super::Hashable, R: Read>(
mut reader: R,
) -> io::Result<Frontier<H, 32>> {

View File

@ -23,7 +23,7 @@ use subtle::{Choice, ConstantTimeEq};
use crate::{
constants::{self, SPENDING_KEY_GENERATOR},
merkle_tree::Hashable,
merkle_tree::{Hashable, HashSer},
transaction::components::amount::MAX_MONEY,
};
@ -83,34 +83,6 @@ impl Node {
}
}
impl Hashable for Node {
fn read<R: Read>(mut reader: R) -> io::Result<Self> {
let mut repr = [0u8; 32];
reader.read_exact(&mut repr)?;
Ok(Node::new(repr))
}
fn write<W: Write>(&self, mut writer: W) -> io::Result<()> {
writer.write_all(self.repr.as_ref())
}
fn combine(depth: usize, lhs: &Self, rhs: &Self) -> Self {
Node {
repr: merkle_hash(depth, &lhs.repr, &rhs.repr),
}
}
fn blank() -> Self {
Node {
repr: Note::uncommitted().to_repr(),
}
}
fn empty_root(depth: usize) -> Self {
EMPTY_ROOTS[depth]
}
}
impl incrementalmerkletree::Hashable for Node {
fn empty_leaf() -> Self {
Node {
@ -129,7 +101,7 @@ impl incrementalmerkletree::Hashable for Node {
}
}
impl crate::merkle_tree::incremental::HashSer for Node {
impl HashSer for Node {
fn read<R: Read>(mut reader: R) -> io::Result<Self> {
let mut repr = [0u8; 32];
reader.read_exact(&mut repr)?;