Add functionality for split_secret when a secret has been provided (#362)

This commit is contained in:
natalie 2023-06-05 11:25:43 +01:00
parent 32bf470df5
commit bc987bbba8
4 changed files with 133 additions and 14 deletions

View File

@ -7,6 +7,7 @@ use std::io;
use output::{print_values, Logger};
use rand::thread_rng;
use trusted_dealer_keygen::split_secret;
mod output;
use crate::inputs::{request_inputs, validate_inputs};
@ -30,14 +31,19 @@ fn main() -> io::Result<()> {
std::process::exit(exitcode::DATAERR)
}
// Print outputs
let out = trusted_dealer_keygen(&config, &mut rng);
if let Err(e) = out {
eprintln!("Error: {}", e);
std::process::exit(1)
}
let keygen = if config.secret.is_empty() {
trusted_dealer_keygen(&config, &mut rng).unwrap()
} else {
split_secret(&config, &mut rng)
};
let (key_packages, pubkeys) = out.unwrap();
// Print outputs
// if let Err(e) = keygen {
// eprintln!("Error: {}", e);
// std::process::exit(1)
// }
let (key_packages, pubkeys) = keygen;
let mut console_logger = ConsoleLogger::default();

View File

@ -1,8 +1,8 @@
use crate::inputs::Config;
use crate::trusted_dealer_keygen::split_secret;
use frost_ed25519 as frost;
use rand::thread_rng;
use crate::trusted_dealer_keygen::_split_secret;
use crate::trusted_dealer_keygen::trusted_dealer_keygen;
mod signature_gen;
@ -32,10 +32,29 @@ fn check_keygen_with_dealer() {
#[test]
fn check_keygen_with_dealer_with_secret() {
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 secret_config = Config {
min_signers: 2,
max_signers: 3,
secret: b"byte".to_vec(),
secret,
};
_split_secret(secret_config, &mut rng);
let (key_packages, pubkeys) = split_secret(&secret_config, &mut rng);
let (nonces, commitments) = signature_gen::generate_nonces_and_commitments(
secret_config.min_signers,
&key_packages,
&mut rng,
);
let message = "message to sign".as_bytes();
let comms = commitments.into_values().collect();
let signing_package = frost::SigningPackage::new(comms, message.to_vec());
let signature_shares =
signature_gen::generate_signature_shares(nonces, &key_packages, &signing_package);
let group_signature =
frost::aggregate(&signing_package, &signature_shares[..], &pubkeys).unwrap();
let verify_signature = pubkeys.group_public.verify(message, &group_signature);
assert!(verify_signature.is_ok());
}

View File

@ -4,7 +4,7 @@ use rand::thread_rng;
use crate::inputs::Config;
use crate::output::{print_values, Logger};
use crate::trusted_dealer_keygen::trusted_dealer_keygen;
use crate::trusted_dealer_keygen::{split_secret, trusted_dealer_keygen};
struct TestLogger(Vec<String>);
@ -15,7 +15,7 @@ impl Logger for TestLogger {
}
#[test]
fn check_output() {
fn check_output_without_secret() {
let mut test_logger = TestLogger(Vec::new());
let mut rng = thread_rng();
let config = Config {
@ -87,3 +87,81 @@ fn check_output() {
)
);
}
#[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 (key_packages, pubkeys) = split_secret(&config, &mut rng);
print_values(&key_packages, 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(key_packages[&signer_1].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(key_packages[&signer_1].secret_share.to_bytes())
)
);
assert_eq!(
test_logger.0[3],
format!(
"Public key: \"{}\"",
hex::encode(key_packages[&signer_1].public.to_bytes())
)
);
assert_eq!(test_logger.0[4], format!("Participant {:?}", signer_2));
assert_eq!(
test_logger.0[5],
format!(
"Secret share: \"{}\"",
hex::encode(key_packages[&signer_2].secret_share.to_bytes())
)
);
assert_eq!(
test_logger.0[6],
format!(
"Public key: \"{}\"",
hex::encode(key_packages[&signer_2].public.to_bytes())
)
);
assert_eq!(test_logger.0[7], format!("Participant {:?}", signer_3));
assert_eq!(
test_logger.0[8],
format!(
"Secret share: \"{}\"",
hex::encode(key_packages[&signer_3].secret_share.to_bytes())
)
);
assert_eq!(
test_logger.0[9],
format!(
"Public key: \"{}\"",
hex::encode(key_packages[&signer_3].public.to_bytes())
)
);
}

View File

@ -1,5 +1,5 @@
use frost::keys::{KeyPackage, PublicKeyPackage};
use frost::{Error, Identifier};
use frost::{Error, Identifier, SigningKey};
use frost_ed25519 as frost;
use rand::rngs::ThreadRng;
use std::collections::HashMap;
@ -23,4 +23,20 @@ pub fn trusted_dealer_keygen(
Ok((key_packages, pubkeys))
}
pub fn _split_secret(_config: Config, _rng: &mut ThreadRng) {}
pub fn split_secret(
config: &Config,
rng: &mut ThreadRng,
) -> (HashMap<Identifier, KeyPackage>, PublicKeyPackage) {
let sec = config.secret.clone();
let again = sec.try_into().unwrap();
let secret_key = SigningKey::from_bytes(again).unwrap();
let (shares, pubkeys) =
frost::keys::split(&secret_key, config.max_signers, config.min_signers, rng).unwrap();
let mut key_packages: HashMap<_, _> = HashMap::new();
for (k, v) in shares {
let key_package = frost::keys::KeyPackage::try_from(v).unwrap();
key_packages.insert(k, key_package);
}
(key_packages, pubkeys)
}