Struct massa_hash::hash::Hash

source ·
pub struct Hash(Hash);
Expand description

Hash wrapper, the underlying hash type is Blake3

The motivations for selecting Blake3 were- Speed: Blake3 is significantly faster than other popular hashing algorithms, such as SHA-256 and SHA-3. This is largely due to its ability to leverage modern CPU architectures and instruction sets, as well as its optimized implementation.

Security: Blake3 is designed to be highly secure and resistant to a wide range of attacks, including collision attacks, length-extension attacks, and timing attacks. It also offers better resistance to side-channel attacks than many other hashing algorithms.

Flexibility: Blake3 is highly flexible and can be used in a variety of applications, including as a general-purpose hash function, as a key derivation function, and as a message authentication code. It also supports a wide range of input sizes and can produce output of any desired length. Scalability: Blake3 can efficiently take advantage of multiple cores and SIMD (single instruction, multiple data) instructions, allowing it to scale well on modern CPUs.

Improved Compression Function: The compression function used in Blake3 is an improved version of the one used in its predecessor, Blake2. This improved compression function offers better diffusion and mixing properties, which contributes to its increased security.

Keyed Hashing: Blake3 supports keyed hashing, which allows users to use a secret key to generate a unique hash value. This feature can be useful in applications that require message authentication or integrity verification.

Tree Hashing: Blake3 supports tree hashing, which allows users to hash large files or data structures in a parallel and efficient manner. This feature can be useful in applications that involve large-scale data processing, such as cloud storage or distributed file systems.

Open Source: Blake3 is an open-source algorithm, which means that its code is publicly available for review and auditing by anyone. This can help improve its security and reliability, as well as increase transparency and trust among users.

Tuple Fields§

§0: Hash

Implementations§

source§

impl Hash

source

pub fn zero() -> Self

Creates a hash full of zeros bytes.

source

pub fn compute_from(data: &[u8]) -> Self

Compute a hash from data.

§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
source

pub fn compute_from_tuple(data: &[&[u8]]) -> Self

Compute a hash from tuple of byte arrays.

§Example
let hash = Hash::compute_from_tuple(&[&"hello".as_bytes(), &"world".as_bytes()]);
source

pub fn to_bs58_check(&self) -> String

Serialize a Hash using bs58 encoding with checksum.

§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized: String = hash.to_bs58_check();

Motivations for using base58 encoding:

base58_check is like base64 but-

  • fully standardized (no = vs /)
  • no weird characters (eg. +) only alphanumeric
  • ambiguous letters combined (eg. O vs 0, or l vs 1)
  • contains a checksum at the end to detect typing errors
source

pub fn to_bytes(&self) -> &[u8; 32]

Serialize a Hash as bytes.

§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized = hash.to_bytes();
source

pub fn into_bytes(self) -> [u8; 32]

Convert into bytes.

§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized = hash.into_bytes();
source

pub fn from_bs58_check(data: &str) -> Result<Hash, MassaHashError>

Deserialize using bs58 encoding with checksum.

§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized: String = hash.to_bs58_check();
let deserialized: Hash = Hash::from_bs58_check(&serialized).unwrap();
source

pub fn from_bytes(data: &[u8; 32]) -> Hash

Deserialize a Hash as bytes.

§Example
let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized = hash.into_bytes();
let deserialized: Hash = Hash::from_bytes(&serialized);

Trait Implementations§

source§

impl Clone for Hash

source§

fn clone(&self) -> Hash

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Hash

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Hash

source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Hash, D::Error>

::serde::Deserialize trait for Hash if the deserializer is human readable, deserialization is done using deserialize_bs58_check else, it uses deserialize_binary

§Example

Human readable deserialization :

let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized: String = serde_json::to_string(&hash).unwrap();
let deserialized: Hash = serde_json::from_str(&serialized).unwrap();
source§

impl Deserializer<Hash> for HashDeserializer

source§

fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( &self, buffer: &'a [u8], ) -> IResult<&'a [u8], Hash, E>

§Example
use massa_hash::{Hash, HashDeserializer};
use massa_serialization::{Serializer, Deserializer, DeserializeError};

let hash_deserializer = HashDeserializer::new();
let hash = Hash::compute_from(&"hello world".as_bytes());
let (rest, deserialized) = hash_deserializer.deserialize::<DeserializeError>(hash.to_bytes()).unwrap();
assert_eq!(deserialized, hash);
assert_eq!(rest.len(), 0);
source§

impl Display for Hash

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromStr for Hash

§

type Err = MassaHashError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Hash

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Hash

In massa, this function is generally useful for data structures that performs ordering and where hashes are used as keys. For e.g., it is used for the BTreeMap where the order of the addresses is to be maintained. This function helps to have a single coherent BTreeMap which is then used to perform the draw See Pos-Worker for more details.

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Hash

source§

fn eq(&self, other: &Hash) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Hash

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Hash

source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

::serde::Serialize trait for Hash if the serializer is human readable, serialization is done using serialize_bs58_check else, it uses serialize_binary

§Example

Human readable serialization :

let hash = Hash::compute_from(&"hello world".as_bytes());
let serialized: String = serde_json::to_string(&hash).unwrap();
source§

impl Serializer<Hash> for HashSerializer

source§

fn serialize( &self, value: &Hash, buffer: &mut Vec<u8>, ) -> Result<(), SerializeError>

Serialize a value T into a buffer of u8. Read more
source§

impl TryFrom<&[u8]> for Hash

source§

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Try parsing from byte slice.

§

type Error = MassaHashError

The type returned in the event of a conversion error.
source§

impl Copy for Hash

source§

impl Eq for Hash

source§

impl StructuralPartialEq for Hash

Auto Trait Implementations§

§

impl Freeze for Hash

§

impl RefUnwindSafe for Hash

§

impl Send for Hash

§

impl Sync for Hash

§

impl Unpin for Hash

§

impl UnwindSafe for Hash

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,