3
3
#![ no_std]
4
4
#![ forbid( unused_crate_dependencies, missing_docs) ]
5
5
6
- #[ cfg( any ( feature = "std" , test) ) ]
6
+ #[ cfg( test) ]
7
7
extern crate std;
8
8
9
9
extern crate alloc;
10
10
11
11
mod _unused_deps {
12
- // We don't use it directy , but we need to enable `serde` feature
12
+ // We don't use it directly , but we need to enable `serde` feature
13
13
use generic_array as _;
14
14
}
15
15
@@ -132,28 +132,23 @@ where
132
132
}
133
133
134
134
/// Protocol error
135
- #[ derive( Debug , displaydoc:: Display ) ]
136
- #[ cfg_attr( feature = "std" , derive( thiserror:: Error ) ) ]
135
+ #[ derive( Debug , thiserror:: Error ) ]
137
136
pub enum Error < RecvErr , SendErr > {
138
137
/// Couldn't send a message in the first round
139
- #[ displaydoc ( "send a message at round 1" ) ]
140
- Round1Send ( #[ cfg_attr ( feature = "std" , source) ] SendErr ) ,
138
+ #[ error ( "send a message at round 1" ) ]
139
+ Round1Send ( #[ source] SendErr ) ,
141
140
/// Couldn't receive a message in the first round
142
- #[ displaydoc( "receive messages at round 1" ) ]
143
- Round1Receive (
144
- #[ cfg_attr( feature = "std" , source) ] CompleteRoundError < RoundInputError , RecvErr > ,
145
- ) ,
141
+ #[ error( "receive messages at round 1" ) ]
142
+ Round1Receive ( #[ source] CompleteRoundError < RoundInputError , RecvErr > ) ,
146
143
/// Couldn't send a message in the second round
147
- #[ displaydoc ( "send a message at round 2" ) ]
148
- Round2Send ( #[ cfg_attr ( feature = "std" , source) ] SendErr ) ,
144
+ #[ error ( "send a message at round 2" ) ]
145
+ Round2Send ( #[ source] SendErr ) ,
149
146
/// Couldn't receive a message in the second round
150
- #[ displaydoc( "receive messages at round 2" ) ]
151
- Round2Receive (
152
- #[ cfg_attr( feature = "std" , source) ] CompleteRoundError < RoundInputError , RecvErr > ,
153
- ) ,
147
+ #[ error( "receive messages at round 2" ) ]
148
+ Round2Receive ( #[ source] CompleteRoundError < RoundInputError , RecvErr > ) ,
154
149
155
150
/// Some of the parties cheated
156
- #[ displaydoc ( "malicious parties: {guilty_parties:?}" ) ]
151
+ #[ error ( "malicious parties: {guilty_parties:?}" ) ]
157
152
PartiesOpenedRandomnessDoesntMatchCommitment {
158
153
/// List of cheated parties
159
154
guilty_parties : Vec < Blame > ,
@@ -173,56 +168,43 @@ pub struct Blame {
173
168
174
169
#[ cfg( test) ]
175
170
mod tests {
176
- use alloc:: { vec, vec:: Vec } ;
177
-
178
171
use rand:: Rng ;
179
- use round_based:: simulation:: Simulation ;
180
172
use sha2:: { Digest , Sha256 } ;
181
173
182
- use super :: { protocol_of_random_generation, Msg } ;
174
+ use super :: protocol_of_random_generation;
183
175
184
- #[ tokio :: test]
185
- async fn simulation_async ( ) {
176
+ #[ test]
177
+ fn simulation ( ) {
186
178
let mut rng = rand_dev:: DevRng :: new ( ) ;
187
179
188
180
let n: u16 = 5 ;
189
181
190
- let mut simulation = Simulation :: < Msg > :: new ( ) ;
191
- let mut party_output = vec ! [ ] ;
192
-
193
- for i in 0 ..n {
194
- let party = simulation. add_party ( ) ;
195
- let output = protocol_of_random_generation ( party, i, n, rng. fork ( ) ) ;
196
- party_output. push ( output) ;
197
- }
198
-
199
- let output = futures:: future:: try_join_all ( party_output) . await . unwrap ( ) ;
200
-
201
- // Assert that all parties outputed the same randomness
202
- for i in 1 ..n {
203
- assert_eq ! ( output[ 0 ] , output[ usize :: from( i) ] ) ;
204
- }
182
+ let randomness = round_based:: sim:: run_with_setup (
183
+ core:: iter:: repeat_with ( || rng. fork ( ) ) . take ( n. into ( ) ) ,
184
+ |i, party, rng| protocol_of_random_generation ( party, i, n, rng) ,
185
+ )
186
+ . unwrap ( )
187
+ . expect_ok ( )
188
+ . expect_eq ( ) ;
205
189
206
- std:: println!( "Output randomness: {}" , hex:: encode( output [ 0 ] ) ) ;
190
+ std:: println!( "Output randomness: {}" , hex:: encode( randomness ) ) ;
207
191
}
208
192
209
- #[ test]
210
- fn simulation_sync ( ) {
193
+ #[ tokio :: test]
194
+ async fn simulation_async ( ) {
211
195
let mut rng = rand_dev:: DevRng :: new ( ) ;
212
196
213
- let simulation = round_based:: simulation:: SimulationSync :: from_async_fn ( 5 , |i, party| {
214
- protocol_of_random_generation ( party, i, 5 , rng. fork ( ) )
215
- } ) ;
197
+ let n: u16 = 5 ;
216
198
217
- let outputs = simulation
218
- . run ( )
219
- . unwrap ( )
220
- . into_iter ( )
221
- . collect :: < Result < Vec < _ > , _ > > ( )
222
- . unwrap ( ) ;
223
- for output_i in & outputs {
224
- assert_eq ! ( * output_i , outputs [ 0 ] ) ;
225
- }
199
+ let randomness = round_based :: sim :: async_env :: run_with_setup (
200
+ core :: iter :: repeat_with ( || rng . fork ( ) ) . take ( n . into ( ) ) ,
201
+ |i , party , rng| protocol_of_random_generation ( party , i , n , rng ) ,
202
+ )
203
+ . await
204
+ . expect_ok ( )
205
+ . expect_eq ( ) ;
206
+
207
+ std :: println! ( "Output randomness: {}" , hex :: encode ( randomness ) ) ;
226
208
}
227
209
228
210
// Emulate the protocol using the state machine interface
0 commit comments