From 906e203663e5983e020702c140e88492f2c55119 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 4 Dec 2023 20:24:39 +0000 Subject: [PATCH] zcash_primitives: Move Sapling `HashSer` impl into `merkle_tree` module The `HashSer` trait will remain in `zcash_primitives` while the Sapling type it is implemented for moves to `sapling-crypto`. Part of zcash/librustzcash#1044. --- zcash_primitives/src/merkle_tree.rs | 17 +++++++++++++++++ zcash_primitives/src/sapling/tree.rs | 19 ------------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/zcash_primitives/src/merkle_tree.rs b/zcash_primitives/src/merkle_tree.rs index 66c13807d..21a6a1f3f 100644 --- a/zcash_primitives/src/merkle_tree.rs +++ b/zcash_primitives/src/merkle_tree.rs @@ -23,6 +23,23 @@ pub trait HashSer { fn write(&self, writer: W) -> io::Result<()>; } +impl HashSer for sapling::Node { + fn read(mut reader: R) -> io::Result { + let mut repr = [0u8; 32]; + reader.read_exact(&mut repr)?; + Option::from(Self::from_bytes(repr)).ok_or_else(|| { + io::Error::new( + io::ErrorKind::InvalidData, + "Non-canonical encoding of Jubjub base field value.", + ) + }) + } + + fn write(&self, mut writer: W) -> io::Result<()> { + writer.write_all(&self.to_bytes()) + } +} + impl HashSer for MerkleHashOrchard { fn read(mut reader: R) -> io::Result where diff --git a/zcash_primitives/src/sapling/tree.rs b/zcash_primitives/src/sapling/tree.rs index 7e75791cc..b3b9f87ff 100644 --- a/zcash_primitives/src/sapling/tree.rs +++ b/zcash_primitives/src/sapling/tree.rs @@ -5,13 +5,11 @@ use lazy_static::lazy_static; use subtle::CtOption; use std::fmt; -use std::io::{self, Read, Write}; use super::{ note::ExtractedNoteCommitment, pedersen_hash::{pedersen_hash, Personalization}, }; -use crate::merkle_tree::HashSer; pub const NOTE_COMMITMENT_TREE_DEPTH: u8 = 32; pub type CommitmentTree = @@ -123,23 +121,6 @@ impl Hashable for Node { } } -impl HashSer for Node { - fn read(mut reader: R) -> io::Result { - let mut repr = [0u8; 32]; - reader.read_exact(&mut repr)?; - Option::from(Self::from_bytes(repr)).ok_or_else(|| { - io::Error::new( - io::ErrorKind::InvalidData, - "Non-canonical encoding of Jubjub base field value.", - ) - }) - } - - fn write(&self, mut writer: W) -> io::Result<()> { - writer.write_all(&self.to_bytes()) - } -} - impl From for bls12_381::Scalar { fn from(node: Node) -> Self { node.0