Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors 326 #4

Merged
merged 5 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
run: cargo llvm-cov report --lcov --ignore-filename-regex 'tests.rs' --output-path lcov.info

- name: Upload coverage report to Codecov
uses: codecov/[email protected].2
uses: codecov/[email protected].3
7 changes: 7 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ thiserror = "1.0"
rand = "0.8"
hex = "0.4"
itertools = "0.10.5"
exitcode = "1.1.2"
35 changes: 21 additions & 14 deletions src/inputs.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
use std::io;
use frost_ed25519::Error;
use std::io::BufRead;

#[derive(Debug, PartialEq, Copy, Clone)]
pub struct Config {
pub min_signers: u16,
pub max_signers: u16,
}

pub fn validate_inputs(config: &Config) -> Result<Config, frost_ed25519::Error> {
pub fn validate_inputs(config: &Config) -> Result<(), Error> {
if config.min_signers < 2 {
return Err(frost_ed25519::Error::InvalidMinSigners);
return Err(Error::InvalidMinSigners);
}

if config.max_signers < 2 {
return Err(frost_ed25519::Error::InvalidMaxSigners);
return Err(Error::InvalidMaxSigners);
}

if config.min_signers > config.max_signers {
return Err(frost_ed25519::Error::InvalidMinSigners);
return Err(Error::InvalidMinSigners);
}

Ok(*config)
Ok(())
}

pub fn request_inputs() -> Config {
pub fn request_inputs(input: &mut impl BufRead) -> Result<Config, Error> {
println!("The minimum number of signers: (2 or more)");

let mut min = String::new();
io::stdin().read_line(&mut min).expect("invalid input");
input.read_line(&mut min).unwrap();

let min_signers = min.trim().parse::<u16>().expect("Invalid input");
let min_signers = min
.trim()
.parse::<u16>()
.map_err(|_| Error::InvalidMinSigners)?;

println!("The maximum number of signers: (must be greater than minimum number of signers)");
println!("The maximum number of signers: ");

let mut max = String::new();
io::stdin().read_line(&mut max).expect("invalid input");
let max_signers = max.trim().parse::<u16>().expect("invalid input");
input.read_line(&mut max).unwrap();
let max_signers = max
.trim()
.parse::<u16>()
.map_err(|_| Error::InvalidMaxSigners)?;

Config {
Ok(Config {
min_signers,
max_signers,
}
})
}
24 changes: 21 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,31 @@ use crate::inputs::{request_inputs, validate_inputs};
use crate::trusted_dealer_keygen::trusted_dealer_keygen;

fn main() -> io::Result<()> {
let config = request_inputs();
let mut reader = Box::new(io::stdin().lock());
let config = request_inputs(&mut reader);
if let Err(e) = config {
eprintln!("Error: {}", e);
std::process::exit(exitcode::DATAERR)
}

let config = config.unwrap();

let mut rng = thread_rng();

validate_inputs(&config).expect("An error occurred");
let valid = validate_inputs(&config);
if let Err(e) = valid {
eprintln!("Error: {}", e);
std::process::exit(exitcode::DATAERR)
}

// Print outputs
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng);
let out = trusted_dealer_keygen(config, &mut rng);
if let Err(e) = out {
eprintln!("Error: {}", e);
std::process::exit(1)
}

let (key_packages, pubkeys) = out.unwrap();

let mut console_logger = ConsoleLogger::default();

Expand Down
32 changes: 30 additions & 2 deletions src/tests/inputs_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::inputs::{validate_inputs, Config};
use crate::inputs::{request_inputs, validate_inputs, Config};

#[test]
fn check_valid_input_for_signers() {
Expand All @@ -9,7 +9,7 @@ fn check_valid_input_for_signers() {

let expected = validate_inputs(&config);

assert_eq!(expected, Ok(config));
assert_eq!(expected, Ok(()));
}

#[test]
Expand Down Expand Up @@ -47,3 +47,31 @@ fn return_error_if_max_participant_is_less_than_2() {

assert_eq!(expected, Err(frost_ed25519::Error::InvalidMaxSigners));
}

#[test]
fn return_config_if_valid_input() {
let mut valid_input = "3\n6\n".as_bytes();
let config = request_inputs(&mut valid_input).unwrap();
let expected = Config {
min_signers: 3,
max_signers: 6,
};

assert_eq!(expected, config)
}

#[test]
fn return_error_if_invalid_min_signers_input() {
let mut invalid_input = "hello\n6\n".as_bytes();
let expected = request_inputs(&mut invalid_input);

assert_eq!(expected, Err(frost_ed25519::Error::InvalidMinSigners))
}

#[test]
fn return_error_if_invalid_max_signers_input() {
let mut invalid_input = "4\nworld\n".as_bytes();
let expected = request_inputs(&mut invalid_input);

assert_eq!(expected, Err(frost_ed25519::Error::InvalidMaxSigners))
}
2 changes: 1 addition & 1 deletion src/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn check_keygen() {
min_signers: 2,
max_signers: 3,
};
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng);
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng).unwrap();
let (nonces, commitments) =
signature_gen::generate_nonces_and_commitments(config.min_signers, &key_packages, &mut rng);
let message = "message to sign".as_bytes();
Expand Down
2 changes: 1 addition & 1 deletion src/tests/output_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn check_output() {
min_signers: 2,
max_signers: 3,
};
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng);
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng).unwrap();

print_values(&key_packages, pubkeys, &mut test_logger);

Expand Down
11 changes: 5 additions & 6 deletions src/trusted_dealer_keygen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use frost::keys::{KeyPackage, PublicKeyPackage};
use frost::Identifier;
use frost::{Error, Identifier};
use frost_ed25519 as frost;
use rand::rngs::ThreadRng;
use std::collections::HashMap;
Expand All @@ -9,17 +9,16 @@ use crate::inputs::Config;
pub fn trusted_dealer_keygen(
config: Config,
rng: &mut ThreadRng,
) -> (HashMap<Identifier, KeyPackage>, PublicKeyPackage) {
) -> Result<(HashMap<Identifier, KeyPackage>, PublicKeyPackage), Error> {
let (shares, pubkeys) =
frost::keys::keygen_with_dealer(config.max_signers, config.min_signers, rng)
.expect("Error generating keys"); // TODO: handle error
frost::keys::keygen_with_dealer(config.max_signers, config.min_signers, rng)?;

let mut key_packages: HashMap<_, _> = HashMap::new();

for (k, v) in shares {
let key_package = frost::keys::KeyPackage::try_from(v).unwrap(); // TODO: handle error
let key_package = frost::keys::KeyPackage::try_from(v)?;
key_packages.insert(k, key_package);
}

(key_packages, pubkeys)
Ok((key_packages, pubkeys))
}