create_signed.rs

Overview

This file provides functionality for creating cryptographically signed envelopes containing serialized data. It defines a trait and implements it for a generic envelope type specialized with the GoshBLS signature scheme. The primary purpose is to enable signing of arbitrary serializable data by nodes identified in a block keeper set, using BLS (Boneh–Lynn–Shacham) signatures. The file integrates cryptographic keys, node identification, and signature aggregation metadata to produce sealed (signed) envelopes.

Trait: CreateSealed<T>

The CreateSealed trait is a generic abstraction for creating sealed envelopes around data of type T. It requires the data to be serializable, cloneable, and safe for concurrent access.

Trait Definition

pub trait CreateSealed<T>
where
    T: Serialize + for<'b> Deserialize<'b> + Clone + Send + Sync + 'static,
{
    fn sealed(
        node_identifier: &NodeIdentifier,
        bk_set: &BlockKeeperSet,
        bls_keys_map: &HashMap<PubKey, (Secret, RndSeed)>,
        data: T,
    ) -> anyhow::Result<Envelope<GoshBLS, T>>;
}

Parameters

Return Value

Returns an anyhow::Result wrapping an Envelope<GoshBLS, T> on success, or an error otherwise.

Usage

The trait is intended to be used where a signed envelope needs to be created from arbitrary data that must be authenticated cryptographically.

Implementation of CreateSealed<TData> for Envelope<GoshBLS, TData>

This implementation provides the actual logic to create a signed envelope using the GoshBLS signature scheme.

Method: sealed

fn sealed(
    node_identifier: &NodeIdentifier,
    bk_set: &BlockKeeperSet,
    bls_keys_map: &HashMap<PubKey, (Secret, RndSeed)>,
    data: TData,
) -> anyhow::Result<Self>

Implementation Details

  1. Node Lookup
    Retrieves the block keeper data (bk_data) associated with the given node_identifier from the bk_set.
    If the node is not found, it returns a descriptive error including the list of valid node IDs.

  2. Secret Key Retrieval
    Obtains the secret key and random seed tuple corresponding to the node's public key from the bls_keys_map.
    If the secret is missing, it triggers a shutdown process (start_shutdown()) and returns an error. This ensures that missing critical cryptographic material is handled as a fatal condition.

  3. Data Signing
    Signs the input data using the BLS signature scheme implementation (GoshBLS) and the retrieved secret key.

  4. Signature Occurrences Map
    Constructs a HashMap to record how many times the signature occurs, keyed by the signer's index. Initially, this map contains a single entry with the current signer's index and a count of 1.

  5. Envelope Creation
    Calls Envelope::create() with the signature, signature occurrences map, and original data to generate the sealed envelope.

Return Value

Returns a Result containing the newly created Envelope with the signature and data, or an error if any step fails.

Example Usage

let envelope = Envelope::<GoshBLS, MyDataType>::sealed(
    &my_node_identifier,
    &my_block_keeper_set,
    &my_bls_keys_map,
    my_data,
)?;

Important Types and Their Roles

Interaction with Other System Components

Diagram: Structure and Workflow of create_signed.rs

flowchart TD
A[CreateSealed Trait] -->|implemented for| B[Envelope<GoshBLS, TData>]
B --> C["sealed() Method"]
C --> D[Lookup NodeIdentifier in BlockKeeperSet]
D --> E{Found?}
E -- Yes --> F[Get PubKey and SignerIndex]
F --> G[Retrieve Secret from bls_keys_map]
G --> H{Secret Found?}
H -- Yes --> I[Sign data with Secret using GoshBLS]
I --> J[Create signature_occurrences map]
J --> K["Call Envelope::create(signature, occurrences, data)"]
K --> L[Return sealed Envelope]
E -- No --> M[Return Error: Node not in BlockKeeperSet]
H -- No --> N["Call start_shutdown()"]
N --> O[Return Error: Secret missing]

This flowchart depicts the main steps performed in the sealed method: node identification, key retrieval, signing, envelope creation, and error handling.