key_handling.rs

Overview

The key_handling.rs file provides functionality to load cryptographic key pairs from a JSON file into memory. It focuses on deserializing keys used by a BLS (Boneh–Lynn–Shacham) signature scheme, along with an associated random seed for each key pair. This file is essential for initializing cryptographic components that rely on BLS keys and ensuring that the keys and seeds are correctly parsed from external storage into usable Rust types.

Functionality

key_pairs_from_file<T>

Purpose

Reads a JSON file containing an array of key pairs and random seeds, parses each entry, and returns a map from public keys to tuples of secret keys and random seeds.

Signature

pub fn key_pairs_from_file<T>(keys_path: &str) -> HashMap<T::PubKey, (T::Secret, RndSeed)>
where
    T: BLSSignatureScheme,
    T::PubKey: FromStr + Sized + Hash + Eq,
    <T::PubKey as FromStr>::Err: Debug,
    T::Secret: FromStr + Sized,
    <T::Secret as FromStr>::Err: Debug,

Parameters

Returns

Detailed Behavior

Usage Example

use crate::bls::MyBLSScheme;

let key_map: HashMap<MyBLSScheme::PubKey, (MyBLSScheme::Secret, RndSeed)> =
    key_pairs_from_file::<MyBLSScheme>("path/to/keys.json");

This code reads a JSON file "path/to/keys.json", expecting it to contain an array of objects, each with "public", "secret", and "rnd" string fields representing keys and seeds encoded as strings. It returns a map usable in cryptographic operations.

Implementation Details

Interaction with Other Components

This file is typically used during system initialization, where key material needs to be loaded from persistent storage into runtime structures usable by cryptographic operations.


Mermaid Diagram

flowchart TD
A[key_pairs_from_file<T>] -->|Read keys_path| B[Read JSON file]
B --> C[Parse JSON into Vec<HashMap<String,String>>]
C --> D[Iterate over each HashMap]
D --> E[Extract "public", "secret", "rnd" strings]
E --> F[Parse public key: T::PubKey::from_str]
E --> G[Parse secret key: T::Secret::from_str]
E --> H[Parse rnd seed: RndSeed::from_str]
F --> I["Collect (public, (secret, rnd_seed)) pairs"]
G --> I
H --> I
I --> J["Return HashMap<T::PubKey, (T::Secret, RndSeed)>"]