return errors instead of unwraping, remove unneeded files

This commit is contained in:
Conrado Gouvea 2023-06-26 15:23:07 -03:00
parent 02910565bf
commit 0070088950
9 changed files with 63 additions and 457 deletions

37
Cargo.lock generated
View File

@ -285,7 +285,8 @@ name = "dkg"
version = "0.1.0"
dependencies = [
"exitcode",
"frost-ed25519 0.4.0 (git+https://github.com/ZcashFoundation/frost.git?rev=30462edbfc74ef6ac988e2a967722602dfb86c21)",
"eyre",
"frost-ed25519 0.5.0",
"hex",
"itertools",
"rand",
@ -326,6 +327,16 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193"
[[package]]
name = "eyre"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "fastrand"
version = "1.9.0"
@ -370,8 +381,9 @@ dependencies = [
[[package]]
name = "frost-core"
version = "0.4.0"
source = "git+https://github.com/ZcashFoundation/frost.git?rev=30462edbfc74ef6ac988e2a967722602dfb86c21#30462edbfc74ef6ac988e2a967722602dfb86c21"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0611ff62c410c38100fba5debc0f7caaf54fe6219e0c631d4fd698587757aa23"
dependencies = [
"byteorder",
"criterion",
@ -398,18 +410,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24f56348765eef8f99de247aba00c1599ba980ca372aa2e4c26c4e9d11e6e4b2"
dependencies = [
"curve25519-dalek",
"frost-core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"frost-core 0.4.0",
"rand_core",
"sha2",
]
[[package]]
name = "frost-ed25519"
version = "0.4.0"
source = "git+https://github.com/ZcashFoundation/frost.git?rev=30462edbfc74ef6ac988e2a967722602dfb86c21#30462edbfc74ef6ac988e2a967722602dfb86c21"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e6ab361daf95ca692647e63377ced9b97017e4f4d475485d008dbc15519eab"
dependencies = [
"curve25519-dalek",
"frost-core 0.4.0 (git+https://github.com/ZcashFoundation/frost.git?rev=30462edbfc74ef6ac988e2a967722602dfb86c21)",
"frost-core 0.5.0",
"rand_core",
"sha2",
]
@ -465,6 +478,12 @@ dependencies = [
"serde",
]
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "instant"
version = "0.1.12"
@ -612,7 +631,7 @@ dependencies = [
name = "participant"
version = "0.1.0"
dependencies = [
"frost-ed25519 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"frost-ed25519 0.4.0",
"hex",
]
@ -1007,7 +1026,7 @@ name = "trusted-dealer"
version = "0.1.0"
dependencies = [
"exitcode",
"frost-ed25519 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"frost-ed25519 0.4.0",
"hex",
"itertools",
"rand",

View File

@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
# frost-ed25519 = "0.4.0"
frost-ed25519 = { git = "https://github.com/ZcashFoundation/frost.git", rev = "30462edbfc74ef6ac988e2a967722602dfb86c21", features = ["serde"] }
eyre = "0.6.8"
frost-ed25519 = { version = "0.5.0", features = ["serde"] }
hex = { version = "0.4", features = ["serde"] }
thiserror = "1.0"
rand = "0.8"

View File

@ -27,8 +27,8 @@ pub fn cli(
writeln!(
logger,
"Round 1 Package to send to all other participants (your identifier: {}):\n\n{}\n",
serde_json::to_string(&config.identifier).unwrap(),
serde_json::to_string(&package).unwrap()
serde_json::to_string(&config.identifier)?,
serde_json::to_string(&package)?
)?;
writeln!(logger, "=== ROUND 1: RECEIVE PACKAGES ===\n")?;
@ -55,9 +55,9 @@ pub fn cli(
writeln!(
logger,
"Round 2 Package to send to participant {} (your identifier: {}):\n\n{}\n",
serde_json::to_string(package.receiver_identifier()).unwrap(),
serde_json::to_string(&config.identifier).unwrap(),
serde_json::to_string(&package).unwrap()
serde_json::to_string(package.receiver_identifier())?,
serde_json::to_string(&config.identifier)?,
serde_json::to_string(&package)?
)?;
}
@ -87,12 +87,12 @@ pub fn cli(
writeln!(
logger,
"Participant key package:\n\n{}\n",
serde_json::to_string(&key_package).unwrap(),
serde_json::to_string(&key_package)?,
)?;
writeln!(
logger,
"Partcipant public key package:\n\n{}\n",
serde_json::to_string(&public_key_package).unwrap(),
serde_json::to_string(&public_key_package)?,
)?;
Ok(())

View File

@ -2,7 +2,11 @@ use frost::{
keys::dkg::{round1, round2},
Error, Identifier,
};
use frost_ed25519 as frost;
use eyre::eyre;
use std::io::{BufRead, Write};
#[derive(Debug, PartialEq, Clone)]
@ -39,21 +43,21 @@ pub fn request_inputs(
writeln!(logger, "The minimum number of signers: (2 or more)")?;
let mut min = String::new();
input.read_line(&mut min).unwrap();
input.read_line(&mut min)?;
let min_signers = min
.trim()
.parse::<u16>()
.map_err(|_| Error::InvalidMinSigners)?;
.map_err(|_| eyre!("Invalid minimum number of signers"))?;
writeln!(logger, "The maximum number of signers: ")?;
let mut max = String::new();
input.read_line(&mut max).unwrap();
input.read_line(&mut max)?;
let max_signers = max
.trim()
.parse::<u16>()
.map_err(|_| Error::InvalidMaxSigners)?;
.map_err(|_| eyre!("Invalid maximum number of signers"))?;
writeln!(
logger,
@ -62,12 +66,12 @@ pub fn request_inputs(
let mut identifier_input = String::new();
input.read_line(&mut identifier_input).unwrap();
input.read_line(&mut identifier_input)?;
let u16_identifier = identifier_input
.trim()
.parse::<u16>()
.map_err(|_| Error::MalformedIdentifier)?;
.map_err(|_| eyre!("Invalid identifier"))?;
let identifier = u16_identifier.try_into()?;
let config = Config {
@ -81,6 +85,15 @@ pub fn request_inputs(
Ok(config)
}
pub fn read_identifier(input: &mut impl BufRead) -> Result<Identifier, Box<dyn std::error::Error>> {
let mut identifier_input = String::new();
input.read_line(&mut identifier_input)?;
let bytes = hex::decode(identifier_input.trim())?;
let serialization = bytes.try_into().map_err(|_| eyre!("Invalid Identifier"))?;
let identifier = Identifier::deserialize(&serialization)?;
Ok(identifier)
}
pub fn read_round1_package(
input: &mut impl BufRead,
logger: &mut dyn Write,
@ -88,20 +101,14 @@ pub fn read_round1_package(
writeln!(logger, "The sender's identifier (hex string):")?;
let mut identifier_input = String::new();
input.read_line(&mut identifier_input).unwrap();
let identifier = Identifier::deserialize(
&hex::decode(identifier_input.trim())
.unwrap()
.try_into()
.unwrap(),
)
.unwrap();
input.read_line(&mut identifier_input)?;
let identifier = read_identifier(input)?;
writeln!(logger, "Their JSON-encoded Round 1 Package:")?;
let mut package_input = String::new();
input.read_line(&mut package_input).unwrap();
let round1_package = serde_json::from_str(&package_input).unwrap();
input.read_line(&mut package_input)?;
let round1_package = serde_json::from_str(&package_input)?;
Ok((identifier, round1_package))
}
@ -112,21 +119,13 @@ pub fn read_round2_package(
) -> Result<(Identifier, round2::Package), Box<dyn std::error::Error>> {
writeln!(logger, "The sender's identifier (hex string):")?;
let mut identifier_input = String::new();
input.read_line(&mut identifier_input).unwrap();
let identifier = Identifier::deserialize(
&hex::decode(identifier_input.trim())
.unwrap()
.try_into()
.unwrap(),
)
.unwrap();
let identifier = read_identifier(input)?;
writeln!(logger, "Their JSON-encoded Round 1 Package:")?;
let mut package_input = String::new();
input.read_line(&mut package_input).unwrap();
let round2_package = serde_json::from_str(&package_input).unwrap();
input.read_line(&mut package_input)?;
let round2_package = serde_json::from_str(&package_input)?;
Ok((identifier, round2_package))
}

View File

@ -1,7 +1,5 @@
mod cli;
mod inputs;
mod output;
mod trusted_dealer_keygen;
#[cfg(test)]
mod tests;

View File

@ -1,47 +0,0 @@
// use frost::keys::{PublicKeyPackage, SecretShare, VerifiableSecretSharingCommitment};
// use frost::Identifier;
// use frost_ed25519 as frost;
// use itertools::Itertools;
// use std::collections::HashMap;
pub trait Logger {
fn log(&mut self, value: String);
}
// fn encode_commitment(vss_commitment: &VerifiableSecretSharingCommitment) -> String {
// let serialized = vss_commitment.serialize();
// let num = serialized.len();
// let mut out = hex::encode([num as u8]);
// for cc in serialized {
// out = out + &hex::encode(cc)
// }
// out
// }
// #[cfg(test)]
// mod tests {
// use crate::output::encode_commitment;
// use frost::keys::VerifiableSecretSharingCommitment;
// use frost_ed25519 as frost;
// use hex::FromHex;
// #[test]
// fn check_encode_commitment() {
// let coeff_comm_1 = "538d43e67bc9c22a3befdf24e68f29bfc9bcbd844736e5b82fdab1545bceddcf";
// let coeff_comm_2 = "6bc2053a2bedc6a071c74495965c960a6d2655720edba2a5aa68b8e160c9f55d";
// let coeff_comm_3 = "eb73cfae619afa59984754e5f3e93ba2357164ce113b09e542365d8313d6f091";
// let expected = "03".to_string() + coeff_comm_1 + coeff_comm_2 + coeff_comm_3;
// let decoded_1 = <[u8; 32]>::from_hex(coeff_comm_1).unwrap();
// let decoded_2 = <[u8; 32]>::from_hex(coeff_comm_2).unwrap();
// let decoded_3 = <[u8; 32]>::from_hex(coeff_comm_3).unwrap();
// let vss_commitment =
// VerifiableSecretSharingCommitment::deserialize(vec![decoded_1, decoded_2, decoded_3])
// .unwrap();
// let commitment = encode_commitment(&vss_commitment);
// assert!(commitment == expected)
// }
// }

View File

@ -1,3 +1,2 @@
mod inputs_tests;
mod integration_test;
mod output_tests;

View File

@ -1,316 +0,0 @@
// use frost::Identifier;
// use frost_ed25519 as frost;
// use rand::thread_rng;
// use crate::inputs::Config;
// use crate::output::{print_values, Logger};
// use crate::trusted_dealer_keygen::{dkg, split_secret};
// struct TestLogger(Vec<String>);
// impl Logger for TestLogger {
// fn log(&mut self, value: String) {
// self.0.push(value);
// }
// }
// fn encode_commitment_helper(commitment: Vec<[u8; 32]>) -> String {
// let len_test = commitment.len() as u8;
// let mut out = hex::encode([len_test]);
// for c in commitment {
// out = out + &hex::encode(c)
// }
// out
// }
// #[test]
// fn check_output_without_secret() {
// let mut test_logger = TestLogger(Vec::new());
// let mut rng = thread_rng();
// let config = Config {
// min_signers: 2,
// max_signers: 3,
// secret: Vec::new(),
// };
// let (shares, pubkeys) = dkg(&config, &mut rng).unwrap();
// print_values(&shares, &pubkeys, &mut test_logger);
// let signer_1 = Identifier::try_from(1).unwrap();
// let signer_2 = Identifier::try_from(2).unwrap();
// let signer_3 = Identifier::try_from(3).unwrap();
// assert_eq!(
// test_logger.0[0],
// format!(
// "Group public key: \"{}\"",
// hex::encode(pubkeys.group_public.to_bytes())
// )
// );
// assert_eq!(test_logger.0[1], format!("Participant {:?}", signer_1));
// assert_eq!(
// test_logger.0[2],
// format!(
// "Secret share: \"{}\"",
// hex::encode(shares[&signer_1].value.to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[3],
// format!(
// "Public key: \"{}\"",
// hex::encode(pubkeys.signer_pubkeys[&signer_1].to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[4],
// format!(
// "Commitment: {}",
// encode_commitment_helper(shares[&signer_1].commitment.serialize())
// )
// );
// assert_eq!(test_logger.0[5], format!("Participant {:?}", signer_2));
// assert_eq!(
// test_logger.0[6],
// format!(
// "Secret share: \"{}\"",
// hex::encode(shares[&signer_2].value.to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[7],
// format!(
// "Public key: \"{}\"",
// hex::encode(pubkeys.signer_pubkeys[&signer_2].to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[8],
// format!(
// "Commitment: {}",
// encode_commitment_helper(shares[&signer_2].commitment.serialize())
// )
// );
// assert_eq!(test_logger.0[9], format!("Participant {:?}", signer_3));
// assert_eq!(
// test_logger.0[10],
// format!(
// "Secret share: \"{}\"",
// hex::encode(shares[&signer_3].value.to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[11],
// format!(
// "Public key: \"{}\"",
// hex::encode(pubkeys.signer_pubkeys[&signer_3].to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[12],
// format!(
// "Commitment: {}",
// encode_commitment_helper(shares[&signer_3].commitment.serialize())
// )
// );
// }
// #[test]
// fn check_output_with_secret() {
// let mut test_logger = TestLogger(Vec::new());
// let mut rng = thread_rng();
// let secret: Vec<u8> = vec![
// 123, 28, 51, 211, 245, 41, 29, 133, 222, 102, 72, 51, 190, 177, 173, 70, 159, 127, 182, 2,
// 90, 14, 199, 139, 58, 121, 12, 110, 19, 169, 131, 4,
// ];
// let config = Config {
// min_signers: 2,
// max_signers: 3,
// secret,
// };
// let (shares, pubkeys) = split_secret(&config, &mut rng).unwrap();
// print_values(&shares, &pubkeys, &mut test_logger);
// let signer_1 = Identifier::try_from(1).unwrap();
// let signer_2 = Identifier::try_from(2).unwrap();
// let signer_3 = Identifier::try_from(3).unwrap();
// assert_eq!(
// test_logger.0[0],
// format!(
// "Group public key: \"{}\"",
// hex::encode(pubkeys.group_public.to_bytes())
// )
// );
// assert_eq!(test_logger.0[1], format!("Participant {:?}", signer_1));
// assert_eq!(
// test_logger.0[2],
// format!(
// "Secret share: \"{}\"",
// hex::encode(shares[&signer_1].value.to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[3],
// format!(
// "Public key: \"{}\"",
// hex::encode(pubkeys.signer_pubkeys[&signer_1].to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[4],
// format!(
// "Commitment: {}",
// encode_commitment_helper(shares[&signer_1].commitment.serialize())
// )
// );
// assert_eq!(test_logger.0[5], format!("Participant {:?}", signer_2));
// assert_eq!(
// test_logger.0[6],
// format!(
// "Secret share: \"{}\"",
// hex::encode(shares[&signer_2].value.to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[7],
// format!(
// "Public key: \"{}\"",
// hex::encode(pubkeys.signer_pubkeys[&signer_2].to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[8],
// format!(
// "Commitment: {}",
// encode_commitment_helper(shares[&signer_2].commitment.serialize())
// )
// );
// assert_eq!(test_logger.0[9], format!("Participant {:?}", signer_3));
// assert_eq!(
// test_logger.0[10],
// format!(
// "Secret share: \"{}\"",
// hex::encode(shares[&signer_3].value.to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[11],
// format!(
// "Public key: \"{}\"",
// hex::encode(pubkeys.signer_pubkeys[&signer_3].to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[12],
// format!(
// "Commitment: {}",
// encode_commitment_helper(shares[&signer_3].commitment.serialize())
// )
// );
// }
// #[test]
// fn check_output_with_large_num_of_signers() {
// let mut test_logger = TestLogger(Vec::new());
// let mut rng = thread_rng();
// let config = Config {
// min_signers: 10,
// max_signers: 20,
// secret: Vec::new(),
// };
// let (shares, pubkeys) = dkg(&config, &mut rng).unwrap();
// print_values(&shares, &pubkeys, &mut test_logger);
// let signer_10 = Identifier::try_from(10).unwrap();
// assert_eq!(
// test_logger.0[0],
// format!(
// "Group public key: \"{}\"",
// hex::encode(pubkeys.group_public.to_bytes())
// )
// );
// assert_eq!(test_logger.0[37], format!("Participant {:?}", signer_10));
// assert_eq!(
// test_logger.0[38],
// format!(
// "Secret share: \"{}\"",
// hex::encode(shares[&signer_10].value.to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[39],
// format!(
// "Public key: \"{}\"",
// hex::encode(pubkeys.signer_pubkeys[&signer_10].to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[40],
// format!(
// "Commitment: {}",
// encode_commitment_helper(shares[&signer_10].commitment.serialize())
// )
// );
// }
// #[test]
// fn check_output_with_secret_with_large_num_of_signers() {
// let mut test_logger = TestLogger(Vec::new());
// let mut rng = thread_rng();
// let secret: Vec<u8> = vec![
// 123, 28, 51, 211, 245, 41, 29, 133, 222, 102, 72, 51, 190, 177, 173, 70, 159, 127, 182, 2,
// 90, 14, 199, 139, 58, 121, 12, 110, 19, 169, 131, 4,
// ];
// let config = Config {
// min_signers: 10,
// max_signers: 20,
// secret,
// };
// let (shares, pubkeys) = split_secret(&config, &mut rng).unwrap();
// print_values(&shares, &pubkeys, &mut test_logger);
// let signer_10 = Identifier::try_from(10).unwrap();
// assert_eq!(
// test_logger.0[0],
// format!(
// "Group public key: \"{}\"",
// hex::encode(pubkeys.group_public.to_bytes())
// )
// );
// assert_eq!(test_logger.0[37], format!("Participant {:?}", signer_10));
// assert_eq!(
// test_logger.0[38],
// format!(
// "Secret share: \"{}\"",
// hex::encode(shares[&signer_10].value.to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[39],
// format!(
// "Public key: \"{}\"",
// hex::encode(pubkeys.signer_pubkeys[&signer_10].to_bytes())
// )
// );
// assert_eq!(
// test_logger.0[40],
// format!(
// "Commitment: {}",
// encode_commitment_helper(shares[&signer_10].commitment.serialize())
// )
// );
// }

View File

@ -1,46 +0,0 @@
// use frost::keys::dkg::round1::{Package, SecretPackage};
// use frost::Error;
// use frost_ed25519 as frost;
// use rand::rngs::ThreadRng;
// use crate::inputs::Config;
// #[cfg(test)]
// mod tests {
// use rand::thread_rng;
// use crate::{inputs::Config, trusted_dealer_keygen::split_secret};
// #[test]
// fn return_malformed_signing_key_error_if_secret_is_invalid() {
// let mut rng = thread_rng();
// let secret_config = Config {
// min_signers: 2,
// max_signers: 3,
// secret: b"helloIamaninvalidsecret111111111".to_vec(),
// };
// let out = split_secret(&secret_config, &mut rng);
// assert!(out.is_err());
// }
// #[test]
// fn return_malformed_signing_key_error_if_secret_is_invalid_type() {
// let mut rng = thread_rng();
// let secret: Vec<u8> = vec![
// 123, 28, 51, 211, 245, 41, 29, 133, 222, 102, 72, 51, 190, 177, 173, 70, 159, 127, 182,
// 2, 90, 14, 199, 139, 58, 121, 12, 110, 19, 169, 131,
// ];
// let secret_config = Config {
// min_signers: 2,
// max_signers: 3,
// secret,
// };
// let out = split_secret(&secret_config, &mut rng);
// assert!(out.is_err());
// }
// }