Skip to content

Commit

Permalink
Improve consistency of testing across projects (#76)
Browse files Browse the repository at this point in the history
* Update participant tests to use DKG format (#37)

Update Makefile to not run --all-features in tests
cli tests were not touched

* Update tests in trusted dealer (#37)

* Refactor test files structure to be consistent across projects (#37)

* Add cross project integration test (#37)

* Remove empty test files (#37)

* Remove reference to old test (#37)

* print entire identifier instead of converting back to integer

* Remove commented code (#37)

* Add signature verification step to participant demo (#78)

* Add verification step to participant demo (#56)

* Add cli test to participant (#56)

Clean up some comments and prints

---------

Co-authored-by: Conrado Gouvea <[email protected]>
  • Loading branch information
natalieesk and conradoplg authored Sep 27, 2023
1 parent c6056d0 commit 0c52892
Show file tree
Hide file tree
Showing 36 changed files with 652 additions and 716 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ members = [
"participant",
"trusted-dealer",
"dkg",
"coordinator"
"coordinator", "tests"
]
default-members = ["participant",
"trusted-dealer",
"dkg",
"coordinator", "tests"]

2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = true
script = "cargo fmt"

[tasks.clippy-full]
script = "cargo clippy --all-features --all-targets -- -D warnings"
script = "cargo clippy --all-targets -- -D warnings"

[tasks.all]
dependencies = [
Expand Down
9 changes: 3 additions & 6 deletions coordinator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
pub mod cli;

mod step_1;
mod step_2;
mod step_3;

#[cfg(test)]
mod tests;
pub mod step_1;
pub mod step_2;
pub mod step_3;
3 changes: 2 additions & 1 deletion coordinator/src/step_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn step_3(
participants: ParticipantsConfig,
signing_package: SigningPackage,
#[cfg(feature = "redpallas")] randomizer: frost::round2::Randomizer,
) {
) -> Signature {
let group_signature = request_inputs_signature_shares(
input,
logger,
Expand All @@ -46,6 +46,7 @@ pub fn step_3(
)
.unwrap();
print_signature(logger, group_signature);
group_signature
}

// Input required:
Expand Down
1 change: 0 additions & 1 deletion coordinator/src/tests.rs

This file was deleted.

96 changes: 0 additions & 96 deletions coordinator/tests/integration_tests.rs

This file was deleted.

2 changes: 2 additions & 0 deletions dkg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cli;
pub mod inputs;
2 changes: 1 addition & 1 deletion dkg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests;

use std::io;

use crate::cli::cli;
use cli::cli;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut reader = Box::new(io::stdin().lock());
Expand Down
3 changes: 1 addition & 2 deletions dkg/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
mod inputs_tests;
mod integration_test;
mod inputs;
1 change: 0 additions & 1 deletion dkg/src/tests/inputs_tests.rs → dkg/src/tests/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ fn return_malformed_identifier_error_if_identifier_invalid() {
let mut buf = BufWriter::new(Vec::new());
let expected = request_inputs(&mut invalid_input, &mut buf).unwrap_err();

println!("{:?}", expected);
assert_eq!(
*expected.downcast::<Error>().unwrap(),
Error::MalformedIdentifier
Expand Down
2 changes: 2 additions & 0 deletions dkg/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(test)]
mod integration_tests;
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use dkg::cli::cli;

use std::collections::HashMap;
use std::io::{BufRead, Write};
use std::thread;

use frost_ed25519::keys::{KeyPackage, PublicKeyPackage};
use frost_ed25519::Identifier;

use crate::cli::cli;

// Read a single line from the given reader.
fn read_line(mut reader: impl BufRead) -> Result<String, std::io::Error> {
let mut s = String::new();
Expand Down
86 changes: 39 additions & 47 deletions participant/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,63 @@
use frost::{Error, Signature};
#[cfg(not(feature = "redpallas"))]
use frost_ed25519 as frost;
#[cfg(feature = "redpallas")]
use reddsa::frost::redpallas as frost;

use frost::{round1, Error};
use participant::round1::{print_values, request_inputs};
use participant::round2::{generate_signature, print_values_round_2, round_2_request_inputs};
use participant::Logger;
use crate::round1::{print_values, request_inputs};
use crate::round2::{generate_signature, print_values_round_2, round_2_request_inputs};
use rand::thread_rng;
use std::io::BufRead;
use std::io::{BufRead, Write};

#[derive(PartialEq)]
pub enum CliError {
Config,
Signing,
}
pub fn cli(
input: &mut impl BufRead,
logger: &mut impl Write,
) -> Result<(), Box<dyn std::error::Error>> {
let round_1_config = request_inputs(input, logger)?;

pub struct ParticipantError {
pub frost_error: Error,
pub cli_error: CliError,
}
let key_package = round_1_config.key_package;

// This is a little messy because of the use of unwrap(). This can be improved.
pub fn cli(input: &mut impl BufRead, logger: &mut dyn Logger) -> Result<(), ParticipantError> {
let round_1_config = request_inputs(input, logger);
writeln!(logger, "Key Package succesfully created.")?;

if let Err(e) = round_1_config {
return Err(ParticipantError {
frost_error: e,
cli_error: CliError::Config,
});
}
let mut rng = thread_rng();
let (nonces, commitments) = frost::round1::commit(key_package.secret_share(), &mut rng);

let round_1_config_ok = round_1_config.unwrap();
print_values(commitments, logger)?;

let key_package_ok = round_1_config_ok.key_package;
let round_2_config = round_2_request_inputs(input, logger)?;

logger.log("Key Package succesfully created.".to_string());
let config_message = round_2_config.clone();

let mut rng = thread_rng();
let (nonces, commitments) = round1::commit(key_package_ok.secret_share(), &mut rng);
// Sign

print_values(commitments, logger);
let signature = generate_signature(round_2_config, &key_package, &nonces)?;

let round_2_config = round_2_request_inputs(input, logger); // TODO: handle errors
print_values_round_2(signature, logger)?;

if let Err(e) = round_2_config {
return Err(ParticipantError {
frost_error: e,
cli_error: CliError::Config,
});
}
let group_signature = request_signature(input, logger)?;
key_package
.group_public()
.verify(config_message.signing_package.message(), &group_signature)?;

let round_2_config_ok = round_2_config.unwrap();
writeln!(logger, "Group Signature verified.")?;

// Sign
Ok(())
}

let signature = generate_signature(round_2_config_ok, &key_package_ok, &nonces); // TODO: handle errors
fn request_signature(
input: &mut impl BufRead,
logger: &mut impl Write,
) -> Result<Signature, Box<dyn std::error::Error>> {
writeln!(logger, "The group signature:")?;

if let Err(e) = signature {
return Err(ParticipantError {
frost_error: e,
cli_error: CliError::Signing,
});
}
let mut signature_input = String::new();

print_values_round_2(signature.unwrap(), logger);
input.read_line(&mut signature_input)?;

Ok(())
let group_signature =
serde_json::from_str(signature_input.trim()).map_err(|_| Error::InvalidSignature)?;

// TODO: add redpallas feature

Ok(group_signature)
}
5 changes: 1 addition & 4 deletions participant/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pub mod cli;
pub mod round1;
pub mod round2;

pub trait Logger {
fn log(&mut self, value: String);
}
35 changes: 8 additions & 27 deletions participant/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
mod cli;
mod round1;
mod round2;

#[cfg(test)]
mod tests;

use cli::{cli, CliError};
use participant::Logger;
use cli::cli;

use std::io;

fn main() -> io::Result<()> {
// TODO: Update to use exit codes
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut reader = Box::new(io::stdin().lock());
let mut logger = ConsoleLogger;
let out = cli(&mut reader, &mut logger);

if let Err(e) = out {
if e.cli_error == CliError::Config {
{
eprintln!("Error: {}", e.frost_error);
std::process::exit(exitcode::DATAERR)
};
};
if e.cli_error == CliError::Signing {
eprintln!("Error: {}", e.frost_error);
std::process::exit(1)
};
}
let mut logger = io::stdout();
cli(&mut reader, &mut logger)?;

Ok(())
}

#[derive(Default)]
pub struct ConsoleLogger;

impl Logger for ConsoleLogger {
fn log(&mut self, value: String) {
println!("{}", value);
}
}
Loading

0 comments on commit 0c52892

Please sign in to comment.