Skip to content

Commit 9965591

Browse files
authored
frost-client: add message confirmation step to Comms trait (#491)
* frost-client: add message confirmation step to Comms trait * actually call the new comms method
1 parent 0cfe769 commit 9965591

File tree

8 files changed

+85
-81
lines changed

8 files changed

+85
-81
lines changed

Diff for: participant/src/cli.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::comms::Comms;
88

99
use crate::round1::{generate_nonces_and_commitments, print_values};
1010
use crate::round2::{generate_signature, print_values_round_2, round_2_request_inputs};
11-
use eyre::eyre;
11+
1212
use frost_core::Ciphersuite;
1313
use frost_ed25519::Ed25519Sha512;
1414
use frost_rerandomized::RandomizedCiphersuite;
@@ -69,16 +69,9 @@ pub async fn cli_for_processed_args<C: RandomizedCiphersuite + 'static>(
6969
)
7070
.await?;
7171

72-
writeln!(
73-
logger,
74-
"Message to be signed (hex-encoded):\n{}\nDo you want to sign it? (y/n)",
75-
hex::encode(round_2_config.signing_package.message())
76-
)?;
77-
let mut sign_it = String::new();
78-
input.read_line(&mut sign_it)?;
79-
if sign_it.trim() != "y" {
80-
return Err(eyre!("signing cancelled").into());
81-
}
72+
comms
73+
.confirm_message(input, logger, &round_2_config)
74+
.await?;
8275

8376
let signature = generate_signature(round_2_config, &key_package, &nonces)?;
8477

Diff for: participant/src/comms.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ pub mod http;
33
pub mod socket;
44

55
use async_trait::async_trait;
6+
use eyre::eyre;
67

78
use frost_core::{self as frost, Ciphersuite};
9+
use frostd::SendSigningPackageArgs;
810

911
use std::{
1012
error::Error,
@@ -43,13 +45,33 @@ pub trait Comms<C: Ciphersuite> {
4345
commitments: SigningCommitments<C>,
4446
identifier: Identifier<C>,
4547
rerandomized: bool,
46-
) -> Result<
47-
(
48-
frost::SigningPackage<C>,
49-
Option<frost_rerandomized::Randomizer<C>>,
50-
),
51-
Box<dyn Error>,
52-
>;
48+
) -> Result<SendSigningPackageArgs<C>, Box<dyn Error>>;
49+
50+
/// Ask the user if they want to sign the message.
51+
///
52+
/// Implementations should show the message to the user (or auxiliary data
53+
/// that maps to the message) and ask for confirmation.
54+
///
55+
/// The default implementation prints the message to output and reads
56+
/// confirmation from input.
57+
async fn confirm_message(
58+
&mut self,
59+
input: &mut dyn BufRead,
60+
output: &mut dyn Write,
61+
signing_package: &SendSigningPackageArgs<C>,
62+
) -> Result<(), Box<dyn Error>> {
63+
writeln!(
64+
output,
65+
"Message to be signed (hex-encoded):\n{}\nDo you want to sign it? (y/n)",
66+
hex::encode(signing_package.signing_package[0].message())
67+
)?;
68+
let mut sign_it = String::new();
69+
input.read_line(&mut sign_it)?;
70+
if sign_it.trim() != "y" {
71+
return Err(eyre!("signing cancelled").into());
72+
}
73+
Ok(())
74+
}
5375

5476
async fn send_signature_share(
5577
&mut self,

Diff for: participant/src/comms/cli.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use frost::{
1010
keys::PublicKeyPackage, round1::SigningCommitments, round2::SignatureShare, Identifier,
1111
SigningPackage,
1212
};
13+
use frostd::SendSigningPackageArgs;
1314

1415
use std::{
1516
error::Error,
@@ -47,13 +48,7 @@ where
4748
_commitments: SigningCommitments<C>,
4849
_identifier: Identifier<C>,
4950
rerandomized: bool,
50-
) -> Result<
51-
(
52-
frost::SigningPackage<C>,
53-
Option<frost_rerandomized::Randomizer<C>>,
54-
),
55-
Box<dyn Error>,
56-
> {
51+
) -> Result<SendSigningPackageArgs<C>, Box<dyn Error>> {
5752
writeln!(output, "Enter the JSON-encoded SigningPackage:")?;
5853

5954
let mut signing_package_json = String::new();
@@ -71,9 +66,19 @@ where
7166

7267
let randomizer =
7368
frost_rerandomized::Randomizer::<C>::deserialize(&hex::decode(json.trim())?)?;
74-
Ok((signing_package, Some(randomizer)))
69+
let r = frostd::SendSigningPackageArgs::<C> {
70+
signing_package: vec![signing_package],
71+
randomizer: vec![randomizer],
72+
aux_msg: vec![],
73+
};
74+
Ok(r)
7575
} else {
76-
Ok((signing_package, None))
76+
let r = frostd::SendSigningPackageArgs::<C> {
77+
signing_package: vec![signing_package],
78+
randomizer: vec![],
79+
aux_msg: vec![],
80+
};
81+
Ok(r)
7782
}
7883
}
7984

Diff for: participant/src/comms/http.rs

+4-25
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ use std::{
99

1010
use async_trait::async_trait;
1111
use eyre::{eyre, OptionExt};
12-
use frost_core::{
13-
self as frost, round1::SigningCommitments, round2::SignatureShare, Ciphersuite, Identifier,
14-
};
12+
use frost_core::{round1::SigningCommitments, round2::SignatureShare, Ciphersuite, Identifier};
1513
use rand::thread_rng;
1614
use snow::{HandshakeState, TransportState};
1715
use xeddsa::{xed25519, Sign as _};
@@ -169,14 +167,8 @@ where
169167
_output: &mut dyn Write,
170168
commitments: SigningCommitments<C>,
171169
_identifier: Identifier<C>,
172-
rerandomized: bool,
173-
) -> Result<
174-
(
175-
frost::SigningPackage<C>,
176-
Option<frost_rerandomized::Randomizer<C>>,
177-
),
178-
Box<dyn Error>,
179-
> {
170+
_rerandomized: bool,
171+
) -> Result<SendSigningPackageArgs<C>, Box<dyn Error>> {
180172
let mut rng = thread_rng();
181173

182174
eprintln!("Logging in...");
@@ -334,20 +326,7 @@ where
334326
}
335327
};
336328

337-
if rerandomized {
338-
let signing_package = r
339-
.signing_package
340-
.first()
341-
.ok_or(eyre!("missing signing package"))?;
342-
let randomizer = r.randomizer.first().ok_or(eyre!("missing randomizer"))?;
343-
Ok((signing_package.clone(), Some(*randomizer)))
344-
} else {
345-
let signing_package = r
346-
.signing_package
347-
.first()
348-
.ok_or(eyre!("missing signing package"))?;
349-
Ok((signing_package.clone(), None))
350-
}
329+
Ok(r)
351330
}
352331

353332
async fn send_signature_share(

Diff for: participant/src/comms/socket.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use async_trait::async_trait;
55
use frost_core::{self as frost, Ciphersuite};
66

77
use eyre::eyre;
8+
use frostd::SendSigningPackageArgs;
89
use message_io::{
910
network::{Endpoint, NetEvent, Transport},
1011
node::{self, NodeHandler, NodeListener},
@@ -89,13 +90,7 @@ where
8990
commitments: SigningCommitments<C>,
9091
identifier: Identifier<C>,
9192
_rerandomized: bool,
92-
) -> Result<
93-
(
94-
frost::SigningPackage<C>,
95-
Option<frost_rerandomized::Randomizer<C>>,
96-
),
97-
Box<dyn Error>,
98-
> {
93+
) -> Result<SendSigningPackageArgs<C>, Box<dyn Error>> {
9994
// Send Commitments to Coordinator
10095
let data = serde_json::to_vec(&Message::<C>::IdentifiedCommitments {
10196
identifier,
@@ -116,7 +111,11 @@ where
116111
randomizer,
117112
} = message
118113
{
119-
Ok((signing_package, randomizer))
114+
Ok(SendSigningPackageArgs::<C> {
115+
signing_package: vec![signing_package],
116+
randomizer: randomizer.map(|r| vec![r]).unwrap_or_default(),
117+
aux_msg: vec![],
118+
})
120119
} else {
121120
Err(eyre!("Expected SigningPackage message"))?
122121
}

Diff for: participant/src/round2.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use frost_core::{self as frost, Ciphersuite};
2+
use frostd::SendSigningPackageArgs;
23

34
use crate::comms::Comms;
45
use frost::{
@@ -24,28 +25,28 @@ pub async fn round_2_request_inputs<C: Ciphersuite>(
2425
commitments: SigningCommitments<C>,
2526
identifier: Identifier<C>,
2627
rerandomized: bool,
27-
) -> Result<Round2Config<C>, Box<dyn std::error::Error>> {
28-
let r = comms
28+
) -> Result<SendSigningPackageArgs<C>, Box<dyn std::error::Error>> {
29+
comms
2930
.get_signing_package(input, logger, commitments, identifier, rerandomized)
30-
.await?;
31-
32-
Ok(Round2Config {
33-
signing_package: r.0,
34-
randomizer: r.1,
35-
})
31+
.await
3632
}
3733

3834
pub fn generate_signature<C: frost_rerandomized::RandomizedCiphersuite>(
39-
config: Round2Config<C>,
35+
config: SendSigningPackageArgs<C>,
4036
key_package: &KeyPackage<C>,
4137
signing_nonces: &SigningNonces<C>,
4238
) -> Result<SignatureShare<C>, Error<C>> {
43-
let signing_package = config.signing_package;
39+
let signing_package = config.signing_package.first().unwrap();
4440

45-
let signature = if let Some(randomizer) = config.randomizer {
46-
frost_rerandomized::sign::<C>(&signing_package, signing_nonces, key_package, randomizer)?
41+
let signature = if !config.randomizer.is_empty() {
42+
frost_rerandomized::sign::<C>(
43+
signing_package,
44+
signing_nonces,
45+
key_package,
46+
config.randomizer[0],
47+
)?
4748
} else {
48-
round2::sign(&signing_package, signing_nonces, key_package)?
49+
round2::sign(signing_package, signing_nonces, key_package)?
4950
};
5051
Ok(signature)
5152
}

Diff for: participant/src/tests/round2.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use frost::{
1111
round2::SignatureShare,
1212
SigningPackage, VerifyingKey,
1313
};
14+
use frostd::SendSigningPackageArgs;
1415
use hex::FromHex;
1516
use participant::comms::cli::CLIComms;
1617
use participant::round2::print_values_round_2;
@@ -82,7 +83,7 @@ async fn check_valid_round_2_inputs() {
8283
assert!(round_2_config.is_ok());
8384
assert_eq!(
8485
expected.signing_package,
85-
round_2_config.unwrap().signing_package
86+
round_2_config.unwrap().signing_package[0]
8687
)
8788
}
8889

@@ -120,9 +121,10 @@ async fn check_sign() {
120121

121122
let signing_package = SigningPackage::new(signer_commitments, message);
122123

123-
let config = Round2Config {
124-
signing_package,
125-
randomizer: None,
124+
let config = SendSigningPackageArgs {
125+
signing_package: vec![signing_package],
126+
randomizer: vec![],
127+
aux_msg: vec![],
126128
};
127129

128130
let signature = generate_signature(config, &key_package, &nonces);

Diff for: participant/tests/integration_tests.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::collections::{BTreeMap, HashMap};
2+
use std::vec;
23

34
use frost_ed25519 as frost;
45

56
use frost::keys::IdentifierList;
67
use frost::{aggregate, SigningPackage};
7-
use participant::round2::{generate_signature, Round2Config};
8+
use frostd::SendSigningPackageArgs;
9+
use participant::round2::generate_signature;
810
use rand::thread_rng;
911

1012
#[test]
@@ -40,9 +42,10 @@ fn check_participant() {
4042
let mut signature_shares = BTreeMap::new();
4143

4244
for participant_identifier in nonces.keys() {
43-
let config = Round2Config {
44-
signing_package: SigningPackage::new(commitments.clone(), &message),
45-
randomizer: None,
45+
let config = SendSigningPackageArgs {
46+
signing_package: vec![SigningPackage::new(commitments.clone(), &message)],
47+
randomizer: vec![],
48+
aux_msg: vec![],
4649
};
4750
let signature = generate_signature(
4851
config,

0 commit comments

Comments
 (0)