Compare commits

..

No commits in common. "8abd114a5b22991de60ccc9ab357289f2f5cd578" and "0162da4604d9931c16d12a0cec40dd85951368c5" have entirely different histories.

4 changed files with 49 additions and 36 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ghost-exodus" name = "ghost-exodus"
version = "0.0.19" version = "0.0.17"
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,7 +85,8 @@ 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 = ChaCha20Rng::seed_from_u64(694201337); let mut rng = Pallet::<T>::get_offchain_rng(authority_index);
let (_, public_package) = network_curve.dkg_part1( let (_, public_package) = network_curve.dkg_part1(
authority_index, authority_index,
max_signers, max_signers,
@ -352,8 +353,11 @@ mod benchmarks {
dummy_verifying_key, dummy_verifying_key,
).expect("Bounded verifying key should be correct"); ).expect("Bounded verifying key should be correct");
let metadata_hashes = [dummy_merkle_root.as_ref(), bounded_verifying_key.as_ref()]; let metadata_hashes = [
let verifying_hash = cumulative_hash::<SubstrateBlake2Hasher, _>(metadata_hashes); SubstrateBlake2Hasher::hash(dummy_merkle_root.as_ref()),
SubstrateBlake2Hasher::hash(bounded_verifying_key.as_ref()),
];
let verifying_hash = sequential_hash::<SubstrateBlake2Hasher, _>(metadata_hashes);
let verification_key = (network_curve, dkg_index, verifying_hash); let verification_key = (network_curve, dkg_index, verifying_hash);
let participants = Verifications::<T>::get(verification_key); let participants = Verifications::<T>::get(verification_key);

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::cumulative_hash, hash_chain::sequential_hash,
networks::{NetworkData, NetworkCurve, NetworkType}, networks::{NetworkData, NetworkCurve, NetworkType},
bounded_bitmap::{validate_bitmap_sizes, BoundedBitmap}, bounded_bitmap::{validate_bitmap_sizes, BoundedBitmap},
}; };
@ -40,6 +40,7 @@ use ghost_helpers::{
use ghost_traits::exodus::IdentifierConverter; use ghost_traits::exodus::IdentifierConverter;
use ghost_traits::{ use ghost_traits::{
hashing::GhostHasher,
bounded_bitmap::{ bounded_bitmap::{
BoundedBitmapGenerator, BoundedBitmapWriter, BoundedBitmapReader, BoundedBitmapGenerator, BoundedBitmapWriter, BoundedBitmapReader,
BoundedBitmapIterable, BoundedBitmapIterable,
@ -1253,9 +1254,11 @@ pub mod pallet {
return Err(Error::<T>::Round5PackageInvalidProof.into()); return Err(Error::<T>::Round5PackageInvalidProof.into());
}; };
let metadata_hashes = [round5.merkle_root.as_ref(), round5.verifying_key.as_ref()]; let metadata_hashes = [
let verifying_hash = cumulative_hash::<SubstrateBlake2Hasher, _>(metadata_hashes); SubstrateBlake2Hasher::hash(round5.merkle_root.as_ref()),
SubstrateBlake2Hasher::hash(round5.verifying_key.as_ref()),
];
let verifying_hash = sequential_hash::<SubstrateBlake2Hasher, _>(metadata_hashes);
let state = QualificationDkgState::<T>::get(&network_curve); let state = QualificationDkgState::<T>::get(&network_curve);
ensure!(state.is_fifth_phase(), Error::<T>::DkgWrongRound); ensure!(state.is_fifth_phase(), Error::<T>::DkgWrongRound);
@ -3556,10 +3559,12 @@ 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;
let exodus_storage_encoded = exodus_storage.encode(); #[cfg(not(any(test, feature = "runtime-benchmarks")))]
let rng_context = [exodus_storage_encoded.as_slice(), secret_share]; let mut rng = Self::get_offchain_rng();
#[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)?;
@ -3771,15 +3776,12 @@ impl<T: Config> Pallet<T> {
)); ));
} }
let dkg_storage_encoded = dkg_storage.encode(); #[cfg(not(any(test, feature = "runtime-benchmarks")))]
let qualification_state_encoded = qualification_state.encode(); let mut rng = Self::get_offchain_rng();
let rng_context = [ #[cfg(any(test, feature = "runtime-benchmarks"))]
dkg_storage_encoded.as_slice(), let mut rng = Self::get_offchain_rng(authority_index);
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)?;
@ -4734,17 +4736,11 @@ impl<T: Config> Pallet<T> {
dkg_index: DkgIndex, dkg_index: DkgIndex,
network_curve: NetworkCurve, network_curve: NetworkCurve,
) { ) {
let authority_index_bytes = authority_index.to_le_bytes(); #[cfg(not(any(test, feature = "runtime-benchmarks")))]
let dkg_index_bytes = dkg_index.to_le_bytes(); let mut rng = Self::get_offchain_rng();
let network_curve_bytes = [network_curve as u8];
let rng_context = [ #[cfg(any(test, feature = "runtime-benchmarks"))]
round1_secret_package, let mut rng = Self::get_offchain_rng(authority_index);
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; }
@ -4815,13 +4811,26 @@ impl<T: Config> Pallet<T> {
} }
} }
fn get_offchain_rng(context: &[&[u8]]) -> ChaCha20Rng { #[cfg(any(test, feature = "runtime-benchmarks"))]
let seed = sp_io::offchain::random_seed(); fn get_offchain_rng(authority_index: AuthIndex) -> ChaCha20Rng {
let seeded_context = sp_std::iter::once(&seed[..]) let mut seed = [0u8; 32];
.chain(context.iter().copied());
let hashed_seed = cumulative_hash::<SubstrateBlake2Hasher, _>(seeded_context); let unique_id = (authority_index as u64).saturating_add(1);
ChaCha20Rng::from_seed(hashed_seed.to_fixed_bytes()) 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();
ChaCha20Rng::from_seed(seed)
} }
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, Encode)] #[derive(Default)]
pub struct ExodusStorage<BlockNumber, NetworkCurve> pub struct ExodusStorage<BlockNumber, NetworkCurve>
where where
BlockNumber: Default, BlockNumber: Default,
@ -365,7 +365,7 @@ where
} }
} }
#[derive(Default, Encode, Clone, Copy)] #[derive(Default, Clone, Copy)]
pub struct DkgStorage<BlockNumber, NetworkCurve> pub struct DkgStorage<BlockNumber, NetworkCurve>
where where
BlockNumber: Copy + Clone, BlockNumber: Copy + Clone,