Skip to content

Commit

Permalink
feat: make PresignatureId be generated and reproducible from triple i…
Browse files Browse the repository at this point in the history
…ds (#752)

* Make PresignatureId be generated from triple ids

* Use the first 8 bytes instead
  • Loading branch information
ChaoticTempest authored Jul 29, 2024
1 parent d368dd5 commit 7a0d45c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions chain-signatures/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions chain-signatures/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rand = "0.8"
reqwest = { version = "0.11.16", features = ["blocking", "json"] }
semver = "1.0.23"
sha2 = "0.10.8"
sha3 = "0.10.8"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "1"
Expand Down
2 changes: 1 addition & 1 deletion chain-signatures/node/src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mod cryptography;
mod signature;

pub mod consensus;
pub mod contract;
pub mod message;
pub mod monitor;
pub mod presignature;
pub mod signature;
pub mod state;
pub mod triple;

Expand Down
23 changes: 22 additions & 1 deletion chain-signatures/node/src/protocol/presignature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use chrono::Utc;
use crypto_shared::PublicKey;
use k256::Secp256k1;
use mpc_contract::config::ProtocolConfig;
use sha3::{Digest, Sha3_256};
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet, VecDeque};
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -213,7 +214,7 @@ impl PresignatureManager {
private_share: &SecretKeyShare,
timeout: u64,
) -> Result<(), InitializationError> {
let id = rand::random();
let id = hash_as_id(triple0.id, triple1.id);

// Check if the `id` is already in the system. Error out and have the next cycle try again.
if self.generators.contains_key(&id)
Expand Down Expand Up @@ -516,3 +517,23 @@ impl PresignatureManager {
messages
}
}

pub fn hash_as_id(triple0: TripleId, triple1: TripleId) -> PresignatureId {
let mut hasher = Sha3_256::new();
hasher.update(triple0.to_le_bytes());
hasher.update(triple1.to_le_bytes());
let id: [u8; 32] = hasher.finalize().into();
let id = u64::from_le_bytes(first_8_bytes(id));

PresignatureId::from(id)
}

const fn first_8_bytes(input: [u8; 32]) -> [u8; 8] {
let mut output = [0u8; 8];
let mut i = 0;
while i < 8 {
output[i] = input[i];
i += 1;
}
output
}

0 comments on commit 7a0d45c

Please sign in to comment.