Remove sender_i from DkgShares

It was a piece of duplicated data used to achieve context-less
de)serialization. This new Vec code is a bit tricker to first read, yet overall
clean and removes a potential fault.

Saves 2 bytes from DkgShares messages.
This commit is contained in:
Luke Parker
2023-09-01 00:03:53 -04:00
parent 5113ab9ec4
commit fa8ff62b09
5 changed files with 65 additions and 90 deletions

View File

@@ -160,18 +160,17 @@ async fn dkg_test() {
for (k, key) in keys.iter().enumerate() {
let attempt = 0;
let mut shares = HashMap::new();
let mut shares = vec![];
for i in 0 .. keys.len() {
if i != k {
let mut share = vec![0; 256];
OsRng.fill_bytes(&mut share);
shares.insert(Participant::new((i + 1).try_into().unwrap()).unwrap(), share);
shares.push(share);
}
}
let mut tx = Transaction::DkgShares {
attempt,
sender_i: Participant::new((k + 1).try_into().unwrap()).unwrap(),
shares,
confirmation_nonces: crate::tributary::dkg_confirmation_nonces(key, &spec),
signed: Transaction::empty_signed(),
@@ -219,10 +218,15 @@ async fn dkg_test() {
.enumerate()
.filter_map(|(l, tx)| {
if let Transaction::DkgShares { shares, .. } = tx {
shares
.get(&Participant::new((i + 1).try_into().unwrap()).unwrap())
.cloned()
.map(|share| (Participant::new((l + 1).try_into().unwrap()).unwrap(), share))
if i == l {
None
} else {
let relative_i = i - (if i > l { 1 } else { 0 });
Some((
Participant::new((l + 1).try_into().unwrap()).unwrap(),
shares[relative_i].clone(),
))
}
} else {
panic!("txs had non-shares");
}

View File

@@ -1,10 +1,7 @@
use core::fmt::Debug;
use std::collections::HashMap;
use rand_core::{RngCore, OsRng};
use frost::Participant;
use tributary::{ReadWrite, tests::random_signed};
use crate::tributary::{SignData, Transaction};
@@ -20,10 +17,6 @@ mod dkg;
mod handle_p2p;
mod sync;
fn random_u16<R: RngCore>(rng: &mut R) -> u16 {
u16::try_from(rng.next_u64() >> 48).unwrap()
}
fn random_u32<R: RngCore>(rng: &mut R) -> u32 {
u32::try_from(rng.next_u64() >> 32).unwrap()
}
@@ -70,18 +63,17 @@ fn serialize_transaction() {
// This supports a variable share length, yet share length is expected to be constant among
// shares
let share_len = usize::try_from(OsRng.next_u64() % 512).unwrap();
// Create a valid map of shares
let mut shares = HashMap::new();
// Create a valid vec of shares
let mut shares = vec![];
// Create up to 512 participants
for i in 0 .. (OsRng.next_u64() % 512) {
let mut share = vec![0; share_len];
OsRng.fill_bytes(&mut share);
shares.insert(Participant::new(u16::try_from(i + 1).unwrap()).unwrap(), share);
shares.push(share);
}
test_read_write(Transaction::DkgShares {
attempt: random_u32(&mut OsRng),
sender_i: Participant::new(random_u16(&mut OsRng).saturating_add(1)).unwrap(),
shares,
confirmation_nonces: {
let mut nonces = [0; 64];