envelope_hash.rs

Overview

This file provides functionality for computing a cryptographic hash of an Envelope containing an AckiNackiBlock using the SHA-256 hash algorithm. It defines a strongly typed wrapper for the hash output and implements serialization, deserialization, and debug formatting for it. The hash function ensures message integrity and allows unique identification of envelopes within the system.

Types and Structures

AckiNackiEnvelopeHash

pub struct AckiNackiEnvelopeHash(pub [u8; 32]);

Debug Implementation Details

The Debug trait implementation customizes how instances of AckiNackiEnvelopeHash appear when printed with formatting macros like {:?}:

impl Debug for AckiNackiEnvelopeHash {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "AckiNackiEnvelopeHash<{}>", hex::encode(self.0))
    }
}

It converts the internal 32-byte hash into a lowercase hexadecimal string for clarity.

Functions

envelope_hash

pub fn envelope_hash(envelope: &Envelope<GoshBLS, AckiNackiBlock>) -> AckiNackiEnvelopeHash

Functionality

  1. Serializes the given Envelope using bincode serialization.

  2. Creates a new SHA-256 hasher instance.

  3. Feeds the serialized data into the hasher.

  4. Finalizes the hash computation and converts the result into a fixed-size byte array.

  5. Wraps the byte array into an AckiNackiEnvelopeHash struct and returns it.

Usage Example

let envelope: Envelope<GoshBLS, AckiNackiBlock> = /* obtain or create envelope */;
let hash = envelope_hash(&envelope);
println!("{:?}", hash); // Prints: AckiNackiEnvelopeHash<...hex string...>

Implementation Details

Interactions with Other Parts of the System

Diagram

classDiagram
class AckiNackiEnvelopeHash {
+[u8; 32]
+Debug::fmt()
}
class Envelope {
<<generic>>
}
class GoshBLS
class AckiNackiBlock
AckiNackiEnvelopeHash --> Envelope : hashes
Envelope <|-- Envelope<GoshBLS, AckiNackiBlock>
envelope_hash() --> AckiNackiEnvelopeHash
envelope_hash() --> Envelope

The diagram illustrates the relationship between the main types and the hashing function in this file. envelope_hash takes an Envelope parameterized with GoshBLS and AckiNackiBlock and produces an AckiNackiEnvelopeHash instance. The AckiNackiEnvelopeHash struct wraps a byte array and implements the Debug trait.