From 8a2405218fa1604f7bc424fbafd453a65804a4a6 Mon Sep 17 00:00:00 2001 From: sinui0 <65924192+sinui0@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:54:51 +0900 Subject: [PATCH] add Hash trait --- src/hasher.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/hasher.rs b/src/hasher.rs index 95a0ded..4282a72 100644 --- a/src/hasher.rs +++ b/src/hasher.rs @@ -1,6 +1,22 @@ use crate::prelude::*; -use core::convert::TryFrom; -use core::mem; +use core::{convert::TryFrom, mem}; + +/// This type is used as a hash type in the library. +/// +/// It is recommended to use fixed size u8 array as a hash type. For example, +/// for sha256 the type would be `[u8; 32]`, representing 32 bytes, +/// which is the size of the sha256 digest. Also, fixed sized arrays of `u8` +/// by default satisfy all trait bounds required by this type. +/// +/// # Trait bounds +/// `Copy` is required as the hash needs to be copied to be concatenated/propagated +/// when constructing nodes. +/// `PartialEq` is required to compare equality when verifying proof +/// `Into>` is required to be able to serialize proof +/// `TryFrom>` is required to parse hashes from a serialized proof +pub trait Hash: Copy + PartialEq + Into> + TryFrom> {} + +impl Hash for T where T: Copy + PartialEq + Into> + TryFrom> {} /// Hasher is a trait used to provide a hashing algorithm for the library. /// @@ -27,19 +43,7 @@ use core::mem; /// } /// ``` pub trait Hasher: Clone { - /// This type is used as a hash type in the library. - /// It is recommended to use fixed size u8 array as a hash type. For example, - /// for sha256 the type would be `[u8; 32]`, representing 32 bytes, - /// which is the size of the sha256 digest. Also, fixed sized arrays of `u8` - /// by default satisfy all trait bounds required by this type. - /// - /// # Trait bounds - /// `Copy` is required as the hash needs to be copied to be concatenated/propagated - /// when constructing nodes. - /// `PartialEq` is required to compare equality when verifying proof - /// `Into>` is required to be able to serialize proof - /// `TryFrom>` is required to parse hashes from a serialized proof - type Hash: Copy + PartialEq + Into> + TryFrom>; + type Hash: Hash; /// This associated function takes a slice of bytes and returns a hash of it. /// Used by `concat_and_hash` function to build a tree from concatenated hashes