Skip to content

Commit

Permalink
Phase 1: also send randomizer (#140)
Browse files Browse the repository at this point in the history
* also send randomizer in socket comms

* participant: change default IP address
  • Loading branch information
conradoplg authored Feb 21, 2024
1 parent c1187ba commit cb77d35
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 41 deletions.
6 changes: 6 additions & 0 deletions coordinator/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ pub struct Args {
#[arg(short = 'm', long, default_value = "-")]
pub message: String,

/// The randomizer to use. Can be a file with the raw randomizer, or "-". If "-"
/// is specified, then it will be read from standard input as a hex string.
#[cfg(feature = "redpallas")]
#[arg(short = 'r', long, default_value = "-")]
pub randomizer: String,

/// Where to write the generated raw bytes signature. If "-", the
/// human-readable hex-string is printed to stdout.
#[arg(short = 's', long, default_value = "-")]
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub async fn cli(
.await?;

#[cfg(feature = "redpallas")]
let randomizer = request_randomizer(reader, logger)?;
let randomizer = request_randomizer(args, reader, logger)?;

writeln!(logger, "=== STEP 3: BUILD GROUP SIGNATURE ===\n")?;

Expand Down
6 changes: 6 additions & 0 deletions coordinator/src/comms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ pub enum Message {
identifier: Identifier,
commitments: SigningCommitments,
},
#[cfg(not(feature = "redpallas"))]
SigningPackage(SigningPackage),
#[cfg(feature = "redpallas")]
SigningPackageAndRandomizer {
signing_package: SigningPackage,
randomizer: frost::round2::Randomizer,
},
SignatureShare(SignatureShare),
}

Expand Down
13 changes: 11 additions & 2 deletions coordinator/src/comms/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,27 @@ impl Comms for SocketComms {
_input: &mut dyn BufRead,
_output: &mut dyn Write,
signing_package: &SigningPackage,
#[cfg(feature = "redpallas")] _randomizer: frost::round2::Randomizer,
#[cfg(feature = "redpallas")] randomizer: frost::round2::Randomizer,
) -> Result<BTreeMap<Identifier, SignatureShare>, Box<dyn Error>> {
eprintln!("Sending SigningPackage to participants...");
// Send SigningPackage to all participants
eprintln!("Sending SigningPackage to participants...");

#[cfg(not(feature = "redpallas"))]
let data = serde_json::to_vec(&Message::SigningPackage(signing_package.clone()))?;
#[cfg(feature = "redpallas")]
let data = serde_json::to_vec(&Message::SigningPackageAndRandomizer {
signing_package: signing_package.clone(),
randomizer,
})?;

for identifier in signing_package.signing_commitments().keys() {
let endpoint = self
.endpoints
.get(identifier)
.ok_or(eyre!("unknown identifier"))?;
self.handler.network().send(*endpoint, &data);
}

eprintln!("Waiting for participants to send their SignatureShares...");
// Read SignatureShare from all participants
let mut signature_shares = BTreeMap::new();
Expand Down
10 changes: 8 additions & 2 deletions coordinator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let mut reader = Box::new(io::stdin().lock());
let mut logger = io::stdout();
cli(&args, &mut reader, &mut logger).await?;
let r = cli(&args, &mut reader, &mut logger).await;

// Force process to exit; since socket comms spawn a thread, it will keep
// running forever. Ideally we should join() the thread but this works for
// now.
std::process::exit(0);
match r {
Ok(_) => std::process::exit(0),
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
}
}

// Choose participants -> send message to those participants - gen message to send
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/step_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn read_commitments(
)?;
let pub_key_package: PublicKeyPackage = serde_json::from_str(&pub_key_package)?;

let num_of_participants = if args.cli && args.num_signers == 0 {
let num_of_participants = if args.num_signers == 0 {
writeln!(logger, "The number of participants: ")?;

let mut participants = String::new();
Expand Down
3 changes: 2 additions & 1 deletion coordinator/src/step_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ fn request_message(
logger: &mut dyn Write,
commitments: BTreeMap<Identifier, SigningCommitments>,
) -> Result<SigningPackage, Box<dyn std::error::Error>> {
let message = if args.cli && args.message == "-" {
let message = if args.message == "-" {
writeln!(logger, "The message to be signed (hex encoded)")?;

let mut msg = String::new();
input.read_line(&mut msg)?;

hex::decode(msg.trim())?
} else {
eprintln!("Reading message from {}...", &args.message);
fs::read(&args.message)?
};

Expand Down
16 changes: 12 additions & 4 deletions coordinator/src/step_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ use crate::{args::Args, comms::Comms, step_1::ParticipantsConfig};

#[cfg(feature = "redpallas")]
pub fn request_randomizer(
args: &Args,
input: &mut impl BufRead,
logger: &mut dyn Write,
) -> Result<frost::round2::Randomizer, Box<dyn std::error::Error>> {
writeln!(logger, "Enter the randomizer (hex string):")?;
let randomizer = if args.randomizer == "-" {
writeln!(logger, "Enter the randomizer (hex string):")?;

let mut randomizer = String::new();
input.read_line(&mut randomizer)?;

let mut randomizer = String::new();
input.read_line(&mut randomizer)?;
hex::decode(randomizer.trim())?
} else {
eprintln!("Reading randomizer from {}...", &args.randomizer);
fs::read(&args.randomizer)?
};

Ok(frost::round2::Randomizer::deserialize(
&hex::decode(randomizer.trim())?
&randomizer
.try_into()
.map_err(|_| frost::Error::MalformedIdentifier)?,
)?)
Expand Down
6 changes: 3 additions & 3 deletions participant/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub struct Args {
#[arg(short = 'k', long, default_value = "key-package-1.json")]
pub key_package: String,

/// IP to bind to, if using online comms
#[arg(short, long, default_value = "0.0.0.0")]
/// IP to connect to, if using online comms
#[arg(short, long, default_value = "127.0.0.1")]
pub ip: String,

/// Port to bind to, if using online comms
/// Port to connect to, if using online comms
#[arg(short, long, default_value_t = 2744)]
pub port: u16,
}
17 changes: 14 additions & 3 deletions participant/src/comms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use frost::{
round1::SigningCommitments,
round2::SignatureShare,
serde::{self, Deserialize, Serialize},
Identifier, SigningPackage,
Identifier,
};

#[derive(Serialize, Deserialize)]
Expand All @@ -28,10 +28,21 @@ pub enum Message {
identifier: Identifier,
commitments: SigningCommitments,
},
SigningPackage(SigningPackage),
#[cfg(not(feature = "redpallas"))]
SigningPackage(frost::SigningPackage),
#[cfg(feature = "redpallas")]
SigningPackageAndRandomizer {
signing_package: frost::SigningPackage,
randomizer: frost::round2::Randomizer,
},
SignatureShare(SignatureShare),
}

#[cfg(not(feature = "redpallas"))]
pub type GenericSigningPackage = frost::SigningPackage;
#[cfg(feature = "redpallas")]
pub type GenericSigningPackage = (frost::SigningPackage, frost::round2::Randomizer);

#[async_trait(?Send)]
pub trait Comms {
async fn get_signing_package(
Expand All @@ -40,7 +51,7 @@ pub trait Comms {
output: &mut dyn Write,
commitments: SigningCommitments,
identifier: Identifier,
) -> Result<SigningPackage, Box<dyn Error>>;
) -> Result<GenericSigningPackage, Box<dyn Error>>;

async fn send_signature_share(
&mut self,
Expand Down
20 changes: 19 additions & 1 deletion participant/src/comms/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use std::{
};

use crate::comms::Comms;

use super::GenericSigningPackage;
// use super::Comms;

pub struct CLIComms {}
Expand All @@ -31,7 +33,7 @@ impl Comms for CLIComms {
output: &mut dyn Write,
_commitments: SigningCommitments,
_identifier: Identifier,
) -> Result<SigningPackage, Box<dyn Error>> {
) -> Result<GenericSigningPackage, Box<dyn Error>> {
writeln!(output, "Enter the JSON-encoded SigningPackage:")?;

let mut signing_package_json = String::new();
Expand All @@ -41,6 +43,22 @@ impl Comms for CLIComms {
// TODO: change to return a generic Error and use a better error
let signing_package: SigningPackage = serde_json::from_str(signing_package_json.trim())?;

#[cfg(feature = "redpallas")]
{
writeln!(output, "Enter the randomizer (hex string):")?;

let mut json = String::new();
input.read_line(&mut json).unwrap();

let randomizer = frost::round2::Randomizer::deserialize(
&hex::decode(json.trim())?
.try_into()
.map_err(|_| eyre!("Invalid randomizer"))?,
)?;
Ok((signing_package, randomizer))
}

#[cfg(not(feature = "redpallas"))]
Ok(signing_package)
}

Expand Down
17 changes: 14 additions & 3 deletions participant/src/comms/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use message_io::{
};
use tokio::sync::mpsc::{self, Receiver, Sender};

use frost::{round1::SigningCommitments, round2::SignatureShare, Identifier, SigningPackage};
use frost::{round1::SigningCommitments, round2::SignatureShare, Identifier};

use std::{
error::Error,
io::{BufRead, Write},
};

use super::{Comms, Message};
use super::{Comms, GenericSigningPackage, Message};
use crate::args::Args;

pub struct SocketComms {
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Comms for SocketComms {
_output: &mut dyn Write,
commitments: SigningCommitments,
identifier: Identifier,
) -> Result<SigningPackage, Box<dyn Error>> {
) -> Result<GenericSigningPackage, Box<dyn Error>> {
// Send Commitments to Coordinator
let data = serde_json::to_vec(&Message::IdentifiedCommitments {
identifier,
Expand All @@ -99,11 +99,22 @@ impl Comms for SocketComms {
.ok_or(eyre!("Did not receive signing package!"))?;

let message: Message = serde_json::from_slice(&data)?;
#[cfg(not(feature = "redpallas"))]
if let Message::SigningPackage(signing_package) = message {
Ok(signing_package)
} else {
Err(eyre!("Expected SigningPackage message"))?
}
#[cfg(feature = "redpallas")]
if let Message::SigningPackageAndRandomizer {
signing_package,
randomizer,
} = message
{
Ok((signing_package, randomizer))
} else {
Err(eyre!("Expected SigningPackage message"))?
}
}

async fn send_signature_share(
Expand Down
10 changes: 8 additions & 2 deletions participant/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let mut reader = Box::new(io::stdin().lock());
let mut logger = io::stdout();
cli(&args, &mut reader, &mut logger).await?;
let r = cli(&args, &mut reader, &mut logger).await;

// Force process to exit; since socket comms spawn a thread, it will keep
// running forever. Ideally we should join() the thread but this works for
// now.
std::process::exit(0);
match r {
Ok(_) => std::process::exit(0),
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
}
}
22 changes: 4 additions & 18 deletions participant/src/round2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "redpallas")]
use frost::FieldError;
#[cfg(not(feature = "redpallas"))]
use frost_ed25519 as frost;
#[cfg(feature = "redpallas")]
Expand Down Expand Up @@ -32,32 +30,20 @@ pub async fn round_2_request_inputs(
) -> Result<Round2Config, Box<dyn std::error::Error>> {
writeln!(logger, "=== Round 2 ===")?;

let signing_package = comms
let r = comms
.get_signing_package(input, logger, commitments, identifier)
.await?;

#[cfg(feature = "redpallas")]
{
writeln!(logger, "Enter the randomizer (hex string):")?;

let mut json = String::new();

input.read_line(&mut json).unwrap();

let randomizer = frost::round2::Randomizer::deserialize(
&hex::decode(json.trim())
.map_err(|_| Error::FieldError(FieldError::MalformedScalar))?
.try_into()
.map_err(|_| Error::FieldError(FieldError::MalformedScalar))?,
)?;
Ok(Round2Config {
signing_package,
randomizer,
signing_package: r.0,
randomizer: r.1,
})
}

#[cfg(not(feature = "redpallas"))]
Ok(Round2Config { signing_package })
Ok(Round2Config { signing_package: r })
}

pub fn generate_signature(
Expand Down

0 comments on commit cb77d35

Please sign in to comment.