Skip to content

Commit c6b343c

Browse files
committed
Write docs
1 parent 82edefc commit c6b343c

File tree

4 files changed

+123
-12
lines changed

4 files changed

+123
-12
lines changed

cggmp21/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub mod keygen;
1616
pub mod progress;
1717
pub mod security_level;
1818
pub mod signing;
19+
pub mod supported_curves;
1920
pub mod trusted_dealer;
2021
mod utils;
2122

cggmp21/src/progress.rs

+85-6
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,115 @@
11
//! Traces progress of protocol execution
2+
//!
3+
//! Provides [`Tracer`] trait that can be used to trace progress of ongoing MPC protocol execution.
4+
//! For instance, it can be implemented to report progress to the end user.
5+
//!
6+
//! Out of box, there's [`PerfProfiler`] which can be used to bechmark a protocol.
7+
//!
8+
//! ## Usage example
9+
//! Provide tracer to the protocol builder and obtain results after protocol is completed:
10+
//!
11+
//! ```rust,no_run
12+
//! # use cggmp21::key_share::{Valid, KeyShare};
13+
//! # type E = cggmp21::supported_curves::Secp256r1;
14+
//! # type L = cggmp21::security_level::ReasonablySecure;
15+
//! # fn load_key_share() -> Result<Valid<KeyShare<E, L>>, std::convert::Infallible> { unimplemented!() }
16+
//! # async fn connect_to_network<M>() -> Result<round_based::MpcParty<M, round_based::simulation::MockedDelivery<M>>, std::convert::Infallible> { unimplemented!() }
17+
//! # async fn doc() -> Result<(), Box<dyn std::error::Error>> {
18+
//! use cggmp21::progress::PerfProfiler;
19+
//!
20+
//! let mut tracer = PerfProfiler::new();
21+
//!
22+
//! let party = connect_to_network().await?;
23+
//! let key_share = load_key_share()?;
24+
//! cggmp21::signing(&key_share)
25+
//! .set_progress_tracer(&mut tracer)
26+
//! .generate_presignature(&mut rand::rngs::OsRng, party)
27+
//! .await?;
28+
//! # Ok(()) }
29+
//!```
230
331
use std::fmt;
432
use std::time::{Duration, Instant};
533

634
use thiserror::Error;
735

36+
/// Traces progress of protocol execution
37+
///
38+
/// See [module level documentation](self) for more details
839
pub trait Tracer: Send + Sync {
40+
/// Traces occurred event
941
fn trace_event(&mut self, event: Event);
1042

43+
/// Traces [`Event::ProtocolBegins`] event
1144
fn protocol_begins(&mut self) {
1245
self.trace_event(Event::ProtocolBegins)
1346
}
47+
/// Traces [`Event::RoundBegins`] event
1448
fn round_begins(&mut self) {
1549
self.trace_event(Event::RoundBegins { name: None })
1650
}
51+
/// Traces [`Event::RoundBegins`] event
1752
fn named_round_begins(&mut self, round_name: &'static str) {
1853
self.trace_event(Event::RoundBegins {
1954
name: Some(round_name),
2055
})
2156
}
57+
/// Traces [`Event::Stage`] event
2258
fn stage(&mut self, stage: &'static str) {
2359
self.trace_event(Event::Stage { name: stage })
2460
}
61+
/// Traces [`Event::ReceiveMsgs`] event
2562
fn receive_msgs(&mut self) {
2663
self.trace_event(Event::ReceiveMsgs)
2764
}
65+
/// Traces [`Event::MsgsReceived`] event
2866
fn msgs_received(&mut self) {
2967
self.trace_event(Event::MsgsReceived)
3068
}
69+
/// Traces [`Event::SendMsg`] event
3170
fn send_msg(&mut self) {
3271
self.trace_event(Event::SendMsg)
3372
}
73+
/// Traces [`Event::MsgSent`] event
3474
fn msg_sent(&mut self) {
3575
self.trace_event(Event::MsgSent)
3676
}
77+
/// Traces [`Event::ProtocolEnds`] event
3778
fn protocol_ends(&mut self) {
3879
self.trace_event(Event::ProtocolEnds)
3980
}
4081
}
4182

83+
/// Event occurred during the protocol execution
4284
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
4385
pub enum Event {
86+
/// Protocol begins
87+
///
88+
/// This event is always emitted before any other events
4489
ProtocolBegins,
4590

46-
RoundBegins { name: Option<&'static str> },
47-
Stage { name: &'static str },
48-
91+
/// Round begins
92+
RoundBegins {
93+
/// Optional name of the round
94+
name: Option<&'static str>,
95+
},
96+
/// Stage begins
97+
Stage {
98+
/// Name of the stage
99+
name: &'static str,
100+
},
101+
102+
/// Protocol waits for some messages to be received
49103
ReceiveMsgs,
104+
/// Protocol received messages, round continues
50105
MsgsReceived,
51106

107+
/// Protocol starts sending a message
52108
SendMsg,
109+
/// Protocol sent a message, round continues
53110
MsgSent,
54111

112+
/// Protocol completed
55113
ProtocolEnds,
56114
}
57115

@@ -78,6 +136,11 @@ impl<T: Tracer> Tracer for Option<T> {
78136
}
79137
}
80138

139+
/// Profiles performance of the protocol
140+
///
141+
/// Implements [`Tracer`] trait so it can be embedded into protocol execution. `PerfProfiler` keeps track of time
142+
/// passed between each step of protocol. After protocol is completed, you can obtain a [`PerfReport`] via
143+
/// [`.get_report()`](PerfProfiler::get_report) method that contains all the measurements.
81144
pub struct PerfProfiler {
82145
last_timestamp: Option<Instant>,
83146
ongoing_stage: Option<usize>,
@@ -86,29 +149,43 @@ pub struct PerfProfiler {
86149
error: Option<ProfileError>,
87150
}
88151

152+
/// Performance report generated by [`PerfProfiler`]
89153
#[derive(Debug, Clone)]
90154
pub struct PerfReport {
155+
/// Duration of setup phase (time after protocol began and before first round started)
91156
pub setup: Duration,
157+
/// Stages of setup phase
92158
pub setup_stages: Vec<StageDuration>,
159+
/// Performance report for each round
93160
pub rounds: Vec<RoundDuration>,
94161
display_io: bool,
95162
}
96163

164+
/// Performance of specific round (part of [`PerfReport`])
97165
#[derive(Debug, Clone)]
98166
pub struct RoundDuration {
167+
/// Round name (if provided)
99168
pub round_name: Option<&'static str>,
169+
/// Stages of the round
100170
pub stages: Vec<StageDuration>,
171+
/// Total duration of pure computation performed during the round
101172
pub computation: Duration,
173+
/// Total time we spent during this round on sending messages
102174
pub sending: Duration,
175+
/// Total time we spent during this round on receiving messages
103176
pub receiving: Duration,
104177
}
105178

179+
/// Performance of specific stage (part of [`PerfReport`])
106180
#[derive(Debug, Clone)]
107181
pub struct StageDuration {
182+
/// Stage name
108183
pub name: &'static str,
184+
/// Duration of the stage
109185
pub duration: Duration,
110186
}
111187

188+
/// Protocol profiling resulted into error
112189
#[derive(Debug, Error, Clone)]
113190
#[error("profiler failed to trace protocol: it behaved unexpectedly")]
114191
pub struct ProfileError(
@@ -118,13 +195,11 @@ pub struct ProfileError(
118195
);
119196

120197
#[derive(Debug, Error, Clone)]
121-
pub enum ErrorReason {
198+
enum ErrorReason {
122199
#[error("protocol has never began")]
123200
ProtocolNeverBegan,
124201
#[error("tracing stage or sending/receiving message but round never began")]
125202
RoundNeverBegan,
126-
#[error("bug: unreachable condition happened")]
127-
Unreachable,
128203
#[error("stage is ongoing, but it can't be finished with that event: {event:?}")]
129204
CantFinishStage { event: Event },
130205
}
@@ -140,6 +215,7 @@ impl Tracer for PerfProfiler {
140215
}
141216

142217
impl PerfProfiler {
218+
/// Constructs new [`PerfProfiler`]
143219
pub fn new() -> Self {
144220
Self {
145221
last_timestamp: None,
@@ -155,6 +231,9 @@ impl PerfProfiler {
155231
}
156232
}
157233

234+
/// Obtains a report
235+
///
236+
/// Returns error if protocol behaved unexpectedly
158237
pub fn get_report(&self) -> Result<PerfReport, ProfileError> {
159238
if let Some(err) = self.error.clone() {
160239
Err(err)

cggmp21/src/security_level.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Security level of CGGMP protocol
22
//!
3-
//! Security level is measured in bits and defined as $\kappa$ and $\varepsilon$ in paper. Higher
4-
//! security level gives more security but makes protocol execution slower.
3+
//! Security level is defined as set of parameters in the CGGMP paper. Higher security level gives more
4+
//! security but makes protocol execution slower.
55
//!
6-
//! We provide two predefined security levels: [ReasonablySecure], which should be used in production,
7-
//! and [DevelopmentOnly], which is insecure but fast and can be used for testing.
6+
//! We provide a predefined [ReasonablySecure] security level which is recommended to use for most use-cases.
87
//!
98
//! You can define your own security level using macro [define_security_level]. Be sure that you properly
10-
//! analyzed the paper and you understand implications.
9+
//! analyzed the CGGMP paper and you understand implications. Inconsistent security level may cause unexpected
10+
//! unverbose runtime error or reduced security of the protocol.
1111
1212
use paillier_zk::libpaillier::unknown_order::BigNumber;
1313

@@ -90,7 +90,7 @@ pub mod _internal {
9090
///
9191
/// ## Example
9292
///
93-
/// Let's define security level corresponding to $\kappa=1024$, $\varepsilon=128$, $\ell = \ell' = 1024$,
93+
/// This code defines security level corresponding to $\kappa=1024$, $\varepsilon=128$, $\ell = \ell' = 1024$,
9494
/// $m = 50$, and $q = 2^{48}-1$:
9595
/// ```rust
9696
/// use cggmp21::security_level::define_security_level;

cggmp21/src/supported_curves.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! Curves supported by this crate
2+
//!
3+
//! This crate re-exports curves that are checked to work correctly with our CGGMP implementation.
4+
//! Generally, this crate can work with any curve as long as it satisfies constraints (check out
5+
//! [`SigningBuilder`](crate::signing::SigningBuilder) generic constraints), but it might have
6+
//! unexpected consequences: for instance, [default security level](crate::security_level::ReasonablySecure)
7+
//! might not be compatible with another curve, which might result into unexpected runtime error or
8+
//! reduced security of the protocol.
9+
10+
pub use generic_ec::curves::Secp256r1;
11+
pub use generic_ec::Curve;
12+
13+
#[cfg(test)]
14+
#[allow(dead_code)]
15+
mod check_compatibility {
16+
use generic_ec::{
17+
coords::AlwaysHasAffineX, hash_to_curve::FromHash, Curve, NonZero, Point, Scalar,
18+
};
19+
20+
fn curve_is_compatible<E: Curve>()
21+
where
22+
Scalar<E>: FromHash,
23+
NonZero<Point<E>>: AlwaysHasAffineX<E>,
24+
{
25+
}
26+
27+
fn supported_curves_are_compatible() {
28+
// curve_is_compatible::<super::Secp256k1>(); // secp256k1 is not yet supported
29+
curve_is_compatible::<super::Secp256r1>();
30+
}
31+
}

0 commit comments

Comments
 (0)