crypto/merkle: Port to incrementalmerkle 0.3 needed API

This commit is contained in:
parazyd
2022-04-16 19:09:15 +02:00
parent 624e48775a
commit ffa1a42bd6

View File

@@ -12,7 +12,7 @@ use serde::{
ser::Serializer,
Deserialize, Serialize,
};
use subtle::CtOption;
use subtle::{Choice, ConditionallySelectable, CtOption};
use crate::{
crypto::{
@@ -40,7 +40,7 @@ lazy_static! {
};
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct MerkleNode(pub pallas::Base);
impl MerkleNode {
@@ -76,9 +76,9 @@ impl<'de> Deserialize<'de> for MerkleNode {
}
}
impl std::hash::Hash for MerkleNode {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
<Option<pallas::Base>>::from(self.0).map(|b| b.to_repr()).hash(state)
impl ConditionallySelectable for MerkleNode {
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
MerkleNode(pallas::Base::conditional_select(&a.0, &b.0, choice))
}
}
@@ -129,3 +129,16 @@ impl Decodable for MerkleNode {
Ok(Self(Decodable::decode(&mut d)?))
}
}
impl Encodable for incrementalmerkletree::Position {
fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
u64::from(*self).encode(&mut s)
}
}
impl Decodable for incrementalmerkletree::Position {
fn decode<D: io::Read>(mut d: D) -> Result<Self> {
let dec: u64 = Decodable::decode(&mut d)?;
Ok(Self::try_from(dec).unwrap())
}
}