@@ -2,10 +2,10 @@ use alloc::collections::{BTreeMap, BTreeSet};
2
2
use core:: fmt:: Debug ;
3
3
4
4
use manul:: protocol:: {
5
- Artifact , BoxedRound , CommunicationInfo , Deserializer , DirectMessage , EchoBroadcast , EntryPoint , FinalizeOutcome ,
5
+ Artifact , BoxedFormat , BoxedRound , CommunicationInfo , DirectMessage , EchoBroadcast , EntryPoint , FinalizeOutcome ,
6
6
LocalError , MessageValidationError , NormalBroadcast , PartyId , Payload , Protocol , ProtocolError , ProtocolMessage ,
7
7
ProtocolMessagePart , ProtocolValidationError , ReceiveError , RequiredMessageParts , RequiredMessages , Round , RoundId ,
8
- Serializer , TransitionInfo ,
8
+ TransitionInfo ,
9
9
} ;
10
10
use rand_core:: CryptoRngCore ;
11
11
use serde:: { Deserialize , Serialize } ;
@@ -39,7 +39,7 @@ impl<Id> ProtocolError<Id> for SimpleProtocolError {
39
39
40
40
fn verify_messages_constitute_error (
41
41
& self ,
42
- deserializer : & Deserializer ,
42
+ format : & BoxedFormat ,
43
43
_guilty_party : & Id ,
44
44
_shared_randomness : & [ u8 ] ,
45
45
_associated_data : & Self :: AssociatedData ,
@@ -49,20 +49,20 @@ impl<Id> ProtocolError<Id> for SimpleProtocolError {
49
49
) -> Result < ( ) , ProtocolValidationError > {
50
50
match self {
51
51
SimpleProtocolError :: Round1InvalidPosition => {
52
- let _message = message. direct_message . deserialize :: < Round1Message > ( deserializer ) ?;
52
+ let _message = message. direct_message . deserialize :: < Round1Message > ( format ) ?;
53
53
// Message contents would be checked here
54
54
Ok ( ( ) )
55
55
}
56
56
SimpleProtocolError :: Round2InvalidPosition => {
57
- let _r1_message = message. direct_message . deserialize :: < Round1Message > ( deserializer ) ?;
57
+ let _r1_message = message. direct_message . deserialize :: < Round1Message > ( format ) ?;
58
58
let r1_echos_serialized = combined_echos
59
59
. get ( & 1 . into ( ) )
60
60
. ok_or_else ( || LocalError :: new ( "Could not find combined echos for Round 1" ) ) ?;
61
61
62
62
// Deserialize the echos
63
63
let _r1_echos = r1_echos_serialized
64
64
. iter ( )
65
- . map ( |( _id, echo) | echo. deserialize :: < Round1Echo > ( deserializer ) )
65
+ . map ( |( _id, echo) | echo. deserialize :: < Round1Echo > ( format ) )
66
66
. collect :: < Result < Vec < _ > , _ > > ( ) ?;
67
67
68
68
// Message contents would be checked here
@@ -77,36 +77,36 @@ impl<Id> Protocol<Id> for SimpleProtocol {
77
77
type ProtocolError = SimpleProtocolError ;
78
78
79
79
fn verify_direct_message_is_invalid (
80
- deserializer : & Deserializer ,
80
+ format : & BoxedFormat ,
81
81
round_id : & RoundId ,
82
82
message : & DirectMessage ,
83
83
) -> Result < ( ) , MessageValidationError > {
84
84
match round_id {
85
- r if r == & 1 => message. verify_is_not :: < Round1Message > ( deserializer ) ,
86
- r if r == & 2 => message. verify_is_not :: < Round2Message > ( deserializer ) ,
85
+ r if r == & 1 => message. verify_is_not :: < Round1Message > ( format ) ,
86
+ r if r == & 2 => message. verify_is_not :: < Round2Message > ( format ) ,
87
87
_ => Err ( MessageValidationError :: InvalidEvidence ( "Invalid round number" . into ( ) ) ) ,
88
88
}
89
89
}
90
90
91
91
fn verify_echo_broadcast_is_invalid (
92
- deserializer : & Deserializer ,
92
+ format : & BoxedFormat ,
93
93
round_id : & RoundId ,
94
94
message : & EchoBroadcast ,
95
95
) -> Result < ( ) , MessageValidationError > {
96
96
match round_id {
97
- r if r == & 1 => message. verify_is_not :: < Round1Echo > ( deserializer ) ,
97
+ r if r == & 1 => message. verify_is_not :: < Round1Echo > ( format ) ,
98
98
r if r == & 2 => message. verify_is_some ( ) ,
99
99
_ => Err ( MessageValidationError :: InvalidEvidence ( "Invalid round number" . into ( ) ) ) ,
100
100
}
101
101
}
102
102
103
103
fn verify_normal_broadcast_is_invalid (
104
- deserializer : & Deserializer ,
104
+ format : & BoxedFormat ,
105
105
round_id : & RoundId ,
106
106
message : & NormalBroadcast ,
107
107
) -> Result < ( ) , MessageValidationError > {
108
108
match round_id {
109
- r if r == & 1 => message. verify_is_not :: < Round1Broadcast > ( deserializer ) ,
109
+ r if r == & 1 => message. verify_is_not :: < Round1Broadcast > ( format ) ,
110
110
r if r == & 2 => message. verify_is_some ( ) ,
111
111
_ => Err ( MessageValidationError :: InvalidEvidence ( "Invalid round number" . into ( ) ) ) ,
112
112
}
@@ -166,7 +166,7 @@ impl<Id: PartyId> EntryPoint<Id> for SimpleProtocolEntryPoint<Id> {
166
166
167
167
fn make_round (
168
168
self ,
169
- _rng : & mut impl CryptoRngCore ,
169
+ _rng : & mut dyn CryptoRngCore ,
170
170
_shared_randomness : & [ u8 ] ,
171
171
id : & Id ,
172
172
) -> Result < BoxedRound < Id , Self :: Protocol > , LocalError > {
@@ -205,8 +205,8 @@ impl<Id: PartyId> Round<Id> for Round1<Id> {
205
205
206
206
fn make_normal_broadcast (
207
207
& self ,
208
- _rng : & mut impl CryptoRngCore ,
209
- serializer : & Serializer ,
208
+ _rng : & mut dyn CryptoRngCore ,
209
+ format : & BoxedFormat ,
210
210
) -> Result < NormalBroadcast , LocalError > {
211
211
debug ! ( "{:?}: making normal broadcast" , self . context. id) ;
212
212
@@ -215,27 +215,27 @@ impl<Id: PartyId> Round<Id> for Round1<Id> {
215
215
my_position : self . context . ids_to_positions [ & self . context . id ] ,
216
216
} ;
217
217
218
- NormalBroadcast :: new ( serializer , message)
218
+ NormalBroadcast :: new ( format , message)
219
219
}
220
220
221
221
fn make_echo_broadcast (
222
222
& self ,
223
- _rng : & mut impl CryptoRngCore ,
224
- serializer : & Serializer ,
223
+ _rng : & mut dyn CryptoRngCore ,
224
+ format : & BoxedFormat ,
225
225
) -> Result < EchoBroadcast , LocalError > {
226
226
debug ! ( "{:?}: making echo broadcast" , self . context. id) ;
227
227
228
228
let message = Round1Echo {
229
229
my_position : self . context . ids_to_positions [ & self . context . id ] ,
230
230
} ;
231
231
232
- EchoBroadcast :: new ( serializer , message)
232
+ EchoBroadcast :: new ( format , message)
233
233
}
234
234
235
235
fn make_direct_message (
236
236
& self ,
237
- _rng : & mut impl CryptoRngCore ,
238
- serializer : & Serializer ,
237
+ _rng : & mut dyn CryptoRngCore ,
238
+ format : & BoxedFormat ,
239
239
destination : & Id ,
240
240
) -> Result < ( DirectMessage , Option < Artifact > ) , LocalError > {
241
241
debug ! ( "{:?}: making direct message for {:?}" , self . context. id, destination) ;
@@ -244,21 +244,21 @@ impl<Id: PartyId> Round<Id> for Round1<Id> {
244
244
my_position : self . context . ids_to_positions [ & self . context . id ] ,
245
245
your_position : self . context . ids_to_positions [ destination] ,
246
246
} ;
247
- let dm = DirectMessage :: new ( serializer , message) ?;
247
+ let dm = DirectMessage :: new ( format , message) ?;
248
248
Ok ( ( dm, None ) )
249
249
}
250
250
251
251
fn receive_message (
252
252
& self ,
253
- deserializer : & Deserializer ,
253
+ format : & BoxedFormat ,
254
254
from : & Id ,
255
255
message : ProtocolMessage ,
256
256
) -> Result < Payload , ReceiveError < Id , Self :: Protocol > > {
257
257
debug ! ( "{:?}: receiving message from {:?}" , self . context. id, from) ;
258
258
259
- let _echo = message. echo_broadcast . deserialize :: < Round1Echo > ( deserializer ) ?;
260
- let _normal = message. normal_broadcast . deserialize :: < Round1Broadcast > ( deserializer ) ?;
261
- let message = message. direct_message . deserialize :: < Round1Message > ( deserializer ) ?;
259
+ let _echo = message. echo_broadcast . deserialize :: < Round1Echo > ( format ) ?;
260
+ let _normal = message. normal_broadcast . deserialize :: < Round1Broadcast > ( format ) ?;
261
+ let message = message. direct_message . deserialize :: < Round1Message > ( format ) ?;
262
262
263
263
debug ! ( "{:?}: received message: {:?}" , self . context. id, message) ;
264
264
@@ -270,8 +270,8 @@ impl<Id: PartyId> Round<Id> for Round1<Id> {
270
270
}
271
271
272
272
fn finalize (
273
- self ,
274
- _rng : & mut impl CryptoRngCore ,
273
+ self : Box < Self > ,
274
+ _rng : & mut dyn CryptoRngCore ,
275
275
payloads : BTreeMap < Id , Payload > ,
276
276
_artifacts : BTreeMap < Id , Artifact > ,
277
277
) -> Result < FinalizeOutcome < Id , Self :: Protocol > , LocalError > {
@@ -283,7 +283,7 @@ impl<Id: PartyId> Round<Id> for Round1<Id> {
283
283
284
284
let typed_payloads = payloads
285
285
. into_values ( )
286
- . map ( |payload| payload. try_to_typed :: < Round1Payload > ( ) )
286
+ . map ( |payload| payload. downcast :: < Round1Payload > ( ) )
287
287
. collect :: < Result < Vec < _ > , _ > > ( ) ?;
288
288
let sum = self . context . ids_to_positions [ & self . context . id ]
289
289
+ typed_payloads. iter ( ) . map ( |payload| payload. x ) . sum :: < u8 > ( ) ;
@@ -321,8 +321,8 @@ impl<Id: PartyId> Round<Id> for Round2<Id> {
321
321
322
322
fn make_direct_message (
323
323
& self ,
324
- _rng : & mut impl CryptoRngCore ,
325
- serializer : & Serializer ,
324
+ _rng : & mut dyn CryptoRngCore ,
325
+ format : & BoxedFormat ,
326
326
destination : & Id ,
327
327
) -> Result < ( DirectMessage , Option < Artifact > ) , LocalError > {
328
328
debug ! ( "{:?}: making direct message for {:?}" , self . context. id, destination) ;
@@ -331,13 +331,13 @@ impl<Id: PartyId> Round<Id> for Round2<Id> {
331
331
my_position : self . context . ids_to_positions [ & self . context . id ] ,
332
332
your_position : self . context . ids_to_positions [ destination] ,
333
333
} ;
334
- let dm = DirectMessage :: new ( serializer , message) ?;
334
+ let dm = DirectMessage :: new ( format , message) ?;
335
335
Ok ( ( dm, None ) )
336
336
}
337
337
338
338
fn receive_message (
339
339
& self ,
340
- deserializer : & Deserializer ,
340
+ format : & BoxedFormat ,
341
341
from : & Id ,
342
342
message : ProtocolMessage ,
343
343
) -> Result < Payload , ReceiveError < Id , Self :: Protocol > > {
@@ -346,7 +346,7 @@ impl<Id: PartyId> Round<Id> for Round2<Id> {
346
346
message. echo_broadcast . assert_is_none ( ) ?;
347
347
message. normal_broadcast . assert_is_none ( ) ?;
348
348
349
- let message = message. direct_message . deserialize :: < Round1Message > ( deserializer ) ?;
349
+ let message = message. direct_message . deserialize :: < Round1Message > ( format ) ?;
350
350
351
351
debug ! ( "{:?}: received message: {:?}" , self . context. id, message) ;
352
352
@@ -358,8 +358,8 @@ impl<Id: PartyId> Round<Id> for Round2<Id> {
358
358
}
359
359
360
360
fn finalize (
361
- self ,
362
- _rng : & mut impl CryptoRngCore ,
361
+ self : Box < Self > ,
362
+ _rng : & mut dyn CryptoRngCore ,
363
363
payloads : BTreeMap < Id , Payload > ,
364
364
_artifacts : BTreeMap < Id , Artifact > ,
365
365
) -> Result < FinalizeOutcome < Id , Self :: Protocol > , LocalError > {
@@ -371,7 +371,7 @@ impl<Id: PartyId> Round<Id> for Round2<Id> {
371
371
372
372
let typed_payloads = payloads
373
373
. into_values ( )
374
- . map ( |payload| payload. try_to_typed :: < Round1Payload > ( ) )
374
+ . map ( |payload| payload. downcast :: < Round1Payload > ( ) )
375
375
. collect :: < Result < Vec < _ > , _ > > ( ) ?;
376
376
let sum = self . context . ids_to_positions [ & self . context . id ]
377
377
+ typed_payloads. iter ( ) . map ( |payload| payload. x ) . sum :: < u8 > ( ) ;
0 commit comments