implement more secure entropy hedging for offchain RNG

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2026-10-01 21:20:17 +03:00
parent 0162da4604
commit 6b0033425c
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
4 changed files with 31 additions and 38 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ghost-exodus" name = "ghost-exodus"
version = "0.0.17" version = "0.0.18"
description = "Threshold signature generation with DKG included" description = "Threshold signature generation with DKG included"
license.workspace = true license.workspace = true
authors.workspace = true authors.workspace = true

View File

@ -85,8 +85,7 @@ mod benchmarks {
dkg_state.insert_index(authority_index); dkg_state.insert_index(authority_index);
QualificationDkgState::<T>::insert(&network_curve, dkg_state); QualificationDkgState::<T>::insert(&network_curve, dkg_state);
let mut rng = Pallet::<T>::get_offchain_rng(authority_index); let mut rng = ChaCha20Rng::seed_from_u64(694201337);
let (_, public_package) = network_curve.dkg_part1( let (_, public_package) = network_curve.dkg_part1(
authority_index, authority_index,
max_signers, max_signers,

View File

@ -31,7 +31,7 @@ use sp_io::hashing::blake2_256;
use ghost_helpers::{ use ghost_helpers::{
get_byzantium_quorum_threshold, SubstrateBlake2Hasher, get_byzantium_quorum_threshold, SubstrateBlake2Hasher,
hash_chain::sequential_hash, hash_chain::{cumulative_hash, sequential_hash},
networks::{NetworkData, NetworkCurve, NetworkType}, networks::{NetworkData, NetworkCurve, NetworkType},
bounded_bitmap::{validate_bitmap_sizes, BoundedBitmap}, bounded_bitmap::{validate_bitmap_sizes, BoundedBitmap},
}; };
@ -3559,12 +3559,10 @@ impl<T: Config> Pallet<T> {
let authority_index = exodus_storage.authority_index; let authority_index = exodus_storage.authority_index;
let network_curve = exodus_storage.network_curve; let network_curve = exodus_storage.network_curve;
#[cfg(not(any(test, feature = "runtime-benchmarks")))] let exodus_storage_encoded = exodus_storage.encode();
let mut rng = Self::get_offchain_rng(); let rng_context = [exodus_storage_encoded.as_slice(), secret_share];
#[cfg(any(test, feature = "runtime-benchmarks"))]
let mut rng = Self::get_offchain_rng(authority_index);
let mut rng = Self::get_offchain_rng(&rng_context);
let (nonce, hiding_commitment, binding_commitment) = network_curve let (nonce, hiding_commitment, binding_commitment) = network_curve
.generate_nonce_commitment(secret_share, &mut rng)?; .generate_nonce_commitment(secret_share, &mut rng)?;
@ -3776,12 +3774,15 @@ impl<T: Config> Pallet<T> {
)); ));
} }
#[cfg(not(any(test, feature = "runtime-benchmarks")))] let dkg_storage_encoded = dkg_storage.encode();
let mut rng = Self::get_offchain_rng(); let qualification_state_encoded = qualification_state.encode();
#[cfg(any(test, feature = "runtime-benchmarks"))] let rng_context = [
let mut rng = Self::get_offchain_rng(authority_index); dkg_storage_encoded.as_slice(),
qualification_state_encoded.as_slice(),
];
let mut rng = Self::get_offchain_rng(&rng_context);
let (secret_package, public_package) = network_curve let (secret_package, public_package) = network_curve
.dkg_part1(authority_index, max_signers, min_signers, &mut rng)?; .dkg_part1(authority_index, max_signers, min_signers, &mut rng)?;
@ -4736,11 +4737,17 @@ impl<T: Config> Pallet<T> {
dkg_index: DkgIndex, dkg_index: DkgIndex,
network_curve: NetworkCurve, network_curve: NetworkCurve,
) { ) {
#[cfg(not(any(test, feature = "runtime-benchmarks")))] let authority_index_bytes = authority_index.to_le_bytes();
let mut rng = Self::get_offchain_rng(); let dkg_index_bytes = dkg_index.to_le_bytes();
let network_curve_bytes = [network_curve as u8];
#[cfg(any(test, feature = "runtime-benchmarks"))] let rng_context = [
let mut rng = Self::get_offchain_rng(authority_index); round1_secret_package,
authority_index_bytes.as_slice(),
dkg_index_bytes.as_slice(),
network_curve_bytes.as_slice(),
];
let mut rng = Self::get_offchain_rng(&rng_context);
for (&receiver_index, round2_package) in round2_packages.iter() { for (&receiver_index, round2_package) in round2_packages.iter() {
if receiver_index == authority_index { continue; } if receiver_index == authority_index { continue; }
@ -4811,26 +4818,13 @@ impl<T: Config> Pallet<T> {
} }
} }
#[cfg(any(test, feature = "runtime-benchmarks"))] fn get_offchain_rng(context: &[&[u8]]) -> ChaCha20Rng {
fn get_offchain_rng(authority_index: AuthIndex) -> ChaCha20Rng {
let mut seed = [0u8; 32];
let unique_id = (authority_index as u64).saturating_add(1);
let index_bytes = unique_id.to_le_bytes();
for (i, &byte) in index_bytes.iter().enumerate() {
if i < seed.len() {
seed[i] ^= byte;
}
}
rand_chacha::ChaCha20Rng::from_seed(seed)
}
#[cfg(not(any(test, feature = "runtime-benchmarks")))]
fn get_offchain_rng() -> ChaCha20Rng {
let seed = sp_io::offchain::random_seed(); let seed = sp_io::offchain::random_seed();
ChaCha20Rng::from_seed(seed) let seeded_context = sp_std::iter::once(&seed[..])
.chain(context.iter().copied());
let hashed_seed = cumulative_hash::<SubstrateBlake2Hasher, _>(seeded_context);
ChaCha20Rng::from_seed(hashed_seed.to_fixed_bytes())
} }
fn create_storage_key(first: &[u8], second: &[u8]) -> Vec<u8> { fn create_storage_key(first: &[u8], second: &[u8]) -> Vec<u8> {

View File

@ -75,7 +75,7 @@ impl<ExodusInnerType> ExodusStorageWrapper<ExodusInnerType> {
} }
} }
#[derive(Default)] #[derive(Default, Encode)]
pub struct ExodusStorage<BlockNumber, NetworkCurve> pub struct ExodusStorage<BlockNumber, NetworkCurve>
where where
BlockNumber: Default, BlockNumber: Default,
@ -365,7 +365,7 @@ where
} }
} }
#[derive(Default, Clone, Copy)] #[derive(Default, Encode, Clone, Copy)]
pub struct DkgStorage<BlockNumber, NetworkCurve> pub struct DkgStorage<BlockNumber, NetworkCurve>
where where
BlockNumber: Copy + Clone, BlockNumber: Copy + Clone,