Add group public key to participant project (#30)

* Add text for group public key input for Participant CLI (#18)

* Refactor lib tests into own file (#18)

* Fix formatting (#18)

* Add group public key input to Participant (#18)

* Handle error for group public key (#18)

Update group_public_key field in config to use VerifyingKey type
This commit is contained in:
natalie 2023-06-20 19:42:57 +01:00 committed by GitHub
parent 7936c55e1e
commit f5e7f6b09f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 78 deletions

View File

@ -1,12 +1,13 @@
use frost::{Error, Identifier};
use frost::{Error, Identifier, VerifyingKey};
use frost_ed25519 as frost;
use hex::FromHex;
use std::io::BufRead;
#[derive(Debug, PartialEq)]
pub struct Config {
identifier: Identifier,
public_key: [u8; 32],
pub identifier: Identifier,
pub public_key: [u8; 32],
pub group_public_key: VerifyingKey,
}
pub trait Logger {
@ -34,79 +35,18 @@ pub fn request_inputs(input: &mut impl BufRead, logger: &mut dyn Logger) -> Resu
let public_key =
<[u8; 32]>::from_hex(public_key_input.trim()).map_err(|_| Error::MalformedVerifyingKey)?;
logger.log("The group public key:".to_string());
let mut group_public_key_input = String::new();
input.read_line(&mut group_public_key_input).unwrap();
let group_public_key = VerifyingKey::from_hex(group_public_key_input.trim())
.map_err(|_| Error::MalformedVerifyingKey)?; // TODO: Frost library needs to be updated with correct Error type
Ok(Config {
identifier: Identifier::try_from(identifier)?,
public_key,
group_public_key,
})
}
#[cfg(test)]
mod tests {
use frost::{Error, Identifier};
use frost_ed25519 as frost;
use hex::FromHex;
use crate::{request_inputs, Config, Logger};
pub struct TestLogger(Vec<String>);
impl Logger for TestLogger {
fn log(&mut self, value: String) {
self.0.push(value);
}
}
#[test]
fn check_valid_inputs() {
let public_key = <[u8; 32]>::from_hex(
"929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509",
)
.unwrap();
let identifier = Identifier::try_from(1).unwrap();
let config = Config {
identifier,
public_key,
};
let mut test_logger = TestLogger(Vec::new());
let mut valid_input =
"1\n929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509\n".as_bytes();
let expected = request_inputs(&mut valid_input, &mut test_logger).unwrap();
assert_eq!(expected, config);
}
#[test]
fn check_0_input_for_identifier() {
let mut test_logger = TestLogger(Vec::new());
let mut invalid_input =
"0\n929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509\n".as_bytes();
let expected = request_inputs(&mut invalid_input, &mut test_logger);
assert!(expected.is_err());
}
#[test]
fn check_non_u16_input_for_identifier() {
let mut test_logger = TestLogger(Vec::new());
let mut invalid_input = "-1\n".as_bytes();
let expected = request_inputs(&mut invalid_input, &mut test_logger);
assert!(expected.is_err());
}
#[test]
fn check_invalid_length_public_key() {
let mut test_logger = TestLogger(Vec::new());
let mut invalid_input = "1\n123456\n".as_bytes();
let expected = request_inputs(&mut invalid_input, &mut test_logger);
assert!(expected.is_err());
assert!(expected == Err(Error::MalformedVerifyingKey))
}
}

View File

@ -11,8 +11,11 @@ impl Logger for TestLogger {
#[test]
fn check_cli() {
let mut reader =
"1\n929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509\n".as_bytes();
let identifier = "1";
let pub_key = "929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509";
let group_pub_key = "15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673";
let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key);
let mut reader = input.as_bytes();
let mut test_logger = TestLogger(Vec::new());
cli(&mut reader, &mut test_logger);
@ -20,5 +23,6 @@ fn check_cli() {
test_logger.0[0],
format!("Your identifier (this should be an integer between 1 and 65535):")
);
assert_eq!(test_logger.0[1], format!("Your public key:"))
assert_eq!(test_logger.0[1], format!("Your public key:"));
assert_eq!(test_logger.0[2], format!("The group public key:"))
}

View File

@ -1,2 +1,102 @@
use frost::VerifyingKey;
#[cfg(test)]
use frost::{Error, Identifier};
use frost_ed25519 as frost;
use hex::FromHex;
use participant::{request_inputs, Config};
use crate::Logger;
pub struct TestLogger(Vec<String>);
impl Logger for TestLogger {
fn log(&mut self, value: String) {
self.0.push(value);
}
}
#[test]
fn check_test() {}
fn check_valid_inputs() {
let public_key =
<[u8; 32]>::from_hex("929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509")
.unwrap();
let identifier = Identifier::try_from(1).unwrap();
let group_public_key =
VerifyingKey::from_hex("15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673")
.unwrap();
let config = Config {
identifier,
public_key,
group_public_key,
};
let mut test_logger = TestLogger(Vec::new());
let identifier = "1";
let pub_key = "929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509";
let group_pub_key = "15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673";
let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key);
let mut valid_input = input.as_bytes();
let expected = request_inputs(&mut valid_input, &mut test_logger).unwrap();
assert_eq!(expected, config);
}
#[test]
fn check_0_input_for_identifier() {
let mut test_logger = TestLogger(Vec::new());
let identifier = "0";
let pub_key = "929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509";
let group_pub_key = "15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673";
let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key);
let mut invalid_input = input.as_bytes();
let expected = request_inputs(&mut invalid_input, &mut test_logger);
assert!(expected.is_err());
}
#[test]
fn check_non_u16_input_for_identifier() {
let mut test_logger = TestLogger(Vec::new());
let mut invalid_input = "-1\n".as_bytes();
let expected = request_inputs(&mut invalid_input, &mut test_logger);
assert!(expected.is_err());
}
#[test]
fn check_invalid_length_public_key() {
let mut test_logger = TestLogger(Vec::new());
let identifier = "1";
let pub_key = "123456";
let group_pub_key = "15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673";
let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key);
let mut invalid_input = input.as_bytes();
let expected = request_inputs(&mut invalid_input, &mut test_logger);
assert!(expected.is_err());
assert!(expected == Err(Error::MalformedVerifyingKey))
}
#[test]
fn check_invalid_length_group_public_key() {
let mut test_logger = TestLogger(Vec::new());
let identifier = "1";
let pub_key = "929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509";
let group_pub_key = "123456";
let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key);
let mut invalid_input = input.as_bytes();
let expected = request_inputs(&mut invalid_input, &mut test_logger);
assert!(expected.is_err());
assert!(expected == Err(Error::MalformedVerifyingKey))
}