@@ -26,7 +26,7 @@ use key_server_cluster::decryption_session::SessionImpl as DecryptionSession;
26
26
use key_server_cluster:: signing_session_ecdsa:: SessionImpl as EcdsaSigningSession ;
27
27
use key_server_cluster:: signing_session_schnorr:: SessionImpl as SchnorrSigningSession ;
28
28
use key_server_cluster:: message:: { Message , KeyVersionNegotiationMessage , RequestKeyVersions ,
29
- KeyVersions , KeyVersionsError , FailedKeyVersionContinueAction } ;
29
+ KeyVersions , KeyVersionsError , FailedKeyVersionContinueAction , CommonKeyData } ;
30
30
use key_server_cluster:: admin_sessions:: ShareChangeSessionMeta ;
31
31
32
32
// TODO [Opt]: change sessions so that versions are sent by chunks.
@@ -97,8 +97,8 @@ struct SessionData {
97
97
pub state : SessionState ,
98
98
/// Initialization confirmations.
99
99
pub confirmations : Option < BTreeSet < NodeId > > ,
100
- /// Key threshold .
101
- pub threshold : Option < usize > ,
100
+ /// Common key data that nodes have agreed upon .
101
+ pub key_share : Option < DocumentKeyShare > ,
102
102
/// { Version => Nodes }
103
103
pub versions : Option < BTreeMap < H256 , BTreeSet < NodeId > > > ,
104
104
/// Session result.
@@ -167,12 +167,11 @@ pub struct LargestSupportResultComputer;
167
167
impl < T > SessionImpl < T > where T : SessionTransport {
168
168
/// Create new session.
169
169
pub fn new ( params : SessionParams < T > ) -> Self {
170
- let threshold = params. key_share . as_ref ( ) . map ( |key_share| key_share. threshold ) ;
171
170
SessionImpl {
172
171
core : SessionCore {
173
172
meta : params. meta ,
174
173
sub_session : params. sub_session ,
175
- key_share : params. key_share ,
174
+ key_share : params. key_share . clone ( ) ,
176
175
result_computer : params. result_computer ,
177
176
transport : params. transport ,
178
177
nonce : params. nonce ,
@@ -181,7 +180,12 @@ impl<T> SessionImpl<T> where T: SessionTransport {
181
180
data : Mutex :: new ( SessionData {
182
181
state : SessionState :: WaitingForInitialization ,
183
182
confirmations : None ,
184
- threshold : threshold,
183
+ key_share : params. key_share . map ( |key_share| DocumentKeyShare {
184
+ threshold : key_share. threshold ,
185
+ author : key_share. author ,
186
+ public : key_share. public ,
187
+ ..Default :: default ( )
188
+ } ) ,
185
189
versions : None ,
186
190
result : None ,
187
191
continue_with : None ,
@@ -195,12 +199,6 @@ impl<T> SessionImpl<T> where T: SessionTransport {
195
199
& self . core . meta
196
200
}
197
201
198
- /// Return key threshold.
199
- pub fn key_threshold ( & self ) -> Result < usize , Error > {
200
- self . data . lock ( ) . threshold . clone ( )
201
- . ok_or ( Error :: InvalidStateForRequest )
202
- }
203
-
204
202
/// Return result computer reference.
205
203
pub fn version_holders ( & self , version : & H256 ) -> Result < BTreeSet < NodeId > , Error > {
206
204
Ok ( self . data . lock ( ) . versions . as_ref ( ) . ok_or ( Error :: InvalidStateForRequest ) ?
@@ -229,6 +227,12 @@ impl<T> SessionImpl<T> where T: SessionTransport {
229
227
. expect ( "wait_session returns Some if called without timeout; qed" )
230
228
}
231
229
230
+ /// Retrieve common key data (author, threshold, public), if available.
231
+ pub fn common_key_data ( & self ) -> Result < DocumentKeyShare , Error > {
232
+ self . data . lock ( ) . key_share . clone ( )
233
+ . ok_or ( Error :: InvalidStateForRequest )
234
+ }
235
+
232
236
/// Initialize session.
233
237
pub fn initialize ( & self , connected_nodes : BTreeSet < NodeId > ) -> Result < ( ) , Error > {
234
238
// check state
@@ -322,7 +326,11 @@ impl<T> SessionImpl<T> where T: SessionTransport {
322
326
session : self . core . meta . id . clone ( ) . into ( ) ,
323
327
sub_session : self . core . sub_session . clone ( ) . into ( ) ,
324
328
session_nonce : self . core . nonce ,
325
- threshold : self . core . key_share . as_ref ( ) . map ( |key_share| key_share. threshold ) ,
329
+ key_common : self . core . key_share . as_ref ( ) . map ( |key_share| CommonKeyData {
330
+ threshold : key_share. threshold ,
331
+ author : key_share. author . into ( ) ,
332
+ public : key_share. public . into ( ) ,
333
+ } ) ,
326
334
versions : self . core . key_share . as_ref ( ) . map ( |key_share|
327
335
key_share. versions . iter ( ) . rev ( )
328
336
. filter ( |v| v. id_numbers . contains_key ( sender) )
@@ -357,12 +365,25 @@ impl<T> SessionImpl<T> where T: SessionTransport {
357
365
358
366
// remember versions that sender have
359
367
{
360
- match message. threshold . clone ( ) {
361
- Some ( threshold) if data. threshold . is_none ( ) => {
362
- data. threshold = Some ( threshold) ;
368
+ match message. key_common . as_ref ( ) {
369
+ Some ( key_common) if data. key_share . is_none ( ) => {
370
+ data. key_share = Some ( DocumentKeyShare {
371
+ threshold : key_common. threshold ,
372
+ author : key_common. author . clone ( ) . into ( ) ,
373
+ public : key_common. public . clone ( ) . into ( ) ,
374
+ ..Default :: default ( )
375
+ } ) ;
376
+ } ,
377
+ Some ( key_common) => {
378
+ let prev_key_share = data. key_share . as_ref ( )
379
+ . expect ( "data.key_share.is_none() is matched by previous branch; qed" ) ;
380
+ if prev_key_share. threshold != key_common. threshold ||
381
+ prev_key_share. author . as_bytes ( ) != key_common. author . as_bytes ( ) ||
382
+ prev_key_share. public . as_bytes ( ) != key_common. public . as_bytes ( )
383
+ {
384
+ return Err ( Error :: InvalidMessage ) ;
385
+ }
363
386
} ,
364
- Some ( threshold) if data. threshold . as_ref ( ) == Some ( & threshold) => ( ) ,
365
- Some ( _) => return Err ( Error :: InvalidMessage ) ,
366
387
None if message. versions . is_empty ( ) => ( ) ,
367
388
None => return Err ( Error :: InvalidMessage ) ,
368
389
}
@@ -388,7 +409,8 @@ impl<T> SessionImpl<T> where T: SessionTransport {
388
409
let reason = "this field is filled on master node when initializing; try_complete is only called on initialized master node; qed" ;
389
410
let confirmations = data. confirmations . as_ref ( ) . expect ( reason) ;
390
411
let versions = data. versions . as_ref ( ) . expect ( reason) ;
391
- if let Some ( result) = core. result_computer . compute_result ( data. threshold . clone ( ) , confirmations, versions) {
412
+ let threshold = data. key_share . as_ref ( ) . map ( |key_share| key_share. threshold ) ;
413
+ if let Some ( result) = core. result_computer . compute_result ( threshold, confirmations, versions) {
392
414
// when the master node processing decryption service request, it starts with a key version negotiation session
393
415
// if the negotiation fails, only master node knows about it
394
416
// => if the error is fatal, only the master will know about it and report it to the contract && the request will never be rejected
@@ -590,7 +612,7 @@ impl SessionResultComputer for LargestSupportResultComputer {
590
612
mod tests {
591
613
use std:: sync:: Arc ;
592
614
use std:: collections:: { VecDeque , BTreeMap , BTreeSet } ;
593
- use ethereum_types:: { H512 , Address } ;
615
+ use ethereum_types:: { H512 , H160 , Address } ;
594
616
use ethkey:: public_to_address;
595
617
use key_server_cluster:: { NodeId , SessionId , Error , KeyStorage , DummyKeyStorage ,
596
618
DocumentKeyShare , DocumentKeyShareVersion } ;
@@ -600,7 +622,10 @@ mod tests {
600
622
use key_server_cluster:: cluster_sessions:: ClusterSession ;
601
623
use key_server_cluster:: admin_sessions:: ShareChangeSessionMeta ;
602
624
use key_server_cluster:: decryption_session:: create_default_decryption_session;
603
- use key_server_cluster:: message:: { Message , KeyVersionNegotiationMessage , RequestKeyVersions , KeyVersions } ;
625
+ use key_server_cluster:: message:: {
626
+ Message , KeyVersionNegotiationMessage , RequestKeyVersions ,
627
+ CommonKeyData , KeyVersions ,
628
+ } ;
604
629
use super :: {
605
630
SessionImpl , SessionTransport , SessionParams , FastestResultComputer , LargestSupportResultComputer ,
606
631
SessionResultComputer , SessionState , ContinueAction , FailedContinueAction ,
@@ -759,7 +784,11 @@ mod tests {
759
784
session: Default :: default ( ) ,
760
785
sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
761
786
session_nonce: 0 ,
762
- threshold: Some ( 10 ) ,
787
+ key_common: Some ( CommonKeyData {
788
+ threshold: 10 ,
789
+ author: Default :: default ( ) ,
790
+ public: Default :: default ( ) ,
791
+ } ) ,
763
792
versions: Vec :: new( ) ,
764
793
} ) ) , Err ( Error :: InvalidStateForRequest ) ) ;
765
794
}
@@ -775,7 +804,12 @@ mod tests {
775
804
session: Default :: default ( ) ,
776
805
sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
777
806
session_nonce: 0 ,
778
- threshold: Some ( 0 ) ,
807
+ key_common: Some ( CommonKeyData {
808
+ threshold: 0 ,
809
+ author: Default :: default ( ) ,
810
+ public: Default :: default ( ) ,
811
+ } ) ,
812
+
779
813
versions: vec![ version_id. clone( ) . into( ) ]
780
814
} ) ) , Ok ( ( ) ) ) ;
781
815
assert_eq ! ( ml. session( 0 ) . data. lock( ) . state, SessionState :: Finished ) ;
@@ -784,32 +818,61 @@ mod tests {
784
818
session: Default :: default ( ) ,
785
819
sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
786
820
session_nonce: 0 ,
787
- threshold: Some ( 0 ) ,
821
+ key_common: Some ( CommonKeyData {
822
+ threshold: 0 ,
823
+ author: Default :: default ( ) ,
824
+ public: Default :: default ( ) ,
825
+ } ) ,
826
+
788
827
versions: vec![ version_id. clone( ) . into( ) ]
789
828
} ) ) , Ok ( ( ) ) ) ;
790
829
assert_eq ! ( ml. session( 0 ) . data. lock( ) . state, SessionState :: Finished ) ;
791
830
}
792
831
793
832
#[ test]
794
- fn negotiation_fails_if_wrong_threshold_sent ( ) {
795
- let ml = MessageLoop :: empty ( 3 ) ;
796
- ml. session ( 0 ) . initialize ( ml. nodes . keys ( ) . cloned ( ) . collect ( ) ) . unwrap ( ) ;
833
+ fn negotiation_fails_if_wrong_common_data_sent ( ) {
834
+ fn run_test ( key_common : CommonKeyData ) {
835
+ let ml = MessageLoop :: empty ( 3 ) ;
836
+ ml. session ( 0 ) . initialize ( ml. nodes . keys ( ) . cloned ( ) . collect ( ) ) . unwrap ( ) ;
837
+
838
+ let version_id = ( * math:: generate_random_scalar ( ) . unwrap ( ) ) . clone ( ) ;
839
+ assert_eq ! ( ml. session( 0 ) . process_message( ml. node_id( 1 ) , & KeyVersionNegotiationMessage :: KeyVersions ( KeyVersions {
840
+ session: Default :: default ( ) ,
841
+ sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
842
+ session_nonce: 0 ,
843
+ key_common: Some ( CommonKeyData {
844
+ threshold: 1 ,
845
+ author: Default :: default ( ) ,
846
+ public: Default :: default ( ) ,
847
+ } ) ,
848
+ versions: vec![ version_id. clone( ) . into( ) ]
849
+ } ) ) , Ok ( ( ) ) ) ;
850
+ assert_eq ! ( ml. session( 0 ) . process_message( ml. node_id( 2 ) , & KeyVersionNegotiationMessage :: KeyVersions ( KeyVersions {
851
+ session: Default :: default ( ) ,
852
+ sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
853
+ session_nonce: 0 ,
854
+ key_common: Some ( key_common) ,
855
+ versions: vec![ version_id. clone( ) . into( ) ]
856
+ } ) ) , Err ( Error :: InvalidMessage ) ) ;
857
+ }
858
+
859
+ run_test ( CommonKeyData {
860
+ threshold : 2 ,
861
+ author : Default :: default ( ) ,
862
+ public : Default :: default ( ) ,
863
+ } ) ;
797
864
798
- let version_id = ( * math:: generate_random_scalar ( ) . unwrap ( ) ) . clone ( ) ;
799
- assert_eq ! ( ml. session( 0 ) . process_message( ml. node_id( 1 ) , & KeyVersionNegotiationMessage :: KeyVersions ( KeyVersions {
800
- session: Default :: default ( ) ,
801
- sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
802
- session_nonce: 0 ,
803
- threshold: Some ( 1 ) ,
804
- versions: vec![ version_id. clone( ) . into( ) ]
805
- } ) ) , Ok ( ( ) ) ) ;
806
- assert_eq ! ( ml. session( 0 ) . process_message( ml. node_id( 2 ) , & KeyVersionNegotiationMessage :: KeyVersions ( KeyVersions {
807
- session: Default :: default ( ) ,
808
- sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
809
- session_nonce: 0 ,
810
- threshold: Some ( 2 ) ,
811
- versions: vec![ version_id. clone( ) . into( ) ]
812
- } ) ) , Err ( Error :: InvalidMessage ) ) ;
865
+ run_test ( CommonKeyData {
866
+ threshold : 1 ,
867
+ author : H160 :: from_low_u64_be ( 1 ) . into ( ) ,
868
+ public : Default :: default ( ) ,
869
+ } ) ;
870
+
871
+ run_test ( CommonKeyData {
872
+ threshold : 1 ,
873
+ author : H160 :: from_low_u64_be ( 2 ) . into ( ) ,
874
+ public : Default :: default ( ) ,
875
+ } ) ;
813
876
}
814
877
815
878
#[ test]
@@ -822,7 +885,7 @@ mod tests {
822
885
session: Default :: default ( ) ,
823
886
sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
824
887
session_nonce: 0 ,
825
- threshold : None ,
888
+ key_common : None ,
826
889
versions: vec![ version_id. clone( ) . into( ) ]
827
890
} ) ) , Err ( Error :: InvalidMessage ) ) ;
828
891
}
@@ -832,9 +895,9 @@ mod tests {
832
895
let nodes = MessageLoop :: prepare_nodes ( 2 ) ;
833
896
let version_id = ( * math:: generate_random_scalar ( ) . unwrap ( ) ) . clone ( ) ;
834
897
nodes. values ( ) . nth ( 0 ) . unwrap ( ) . insert ( Default :: default ( ) , DocumentKeyShare {
835
- author : Default :: default ( ) ,
898
+ author : H160 :: from_low_u64_be ( 2 ) ,
836
899
threshold : 1 ,
837
- public : Default :: default ( ) ,
900
+ public : H512 :: from_low_u64_be ( 3 ) ,
838
901
common_point : None ,
839
902
encrypted_point : None ,
840
903
versions : vec ! [ DocumentKeyShareVersion {
@@ -848,8 +911,13 @@ mod tests {
848
911
// we can't be sure that node has given key version because previous ShareAdd session could fail
849
912
assert ! ( ml. session( 0 ) . data. lock( ) . state != SessionState :: Finished ) ;
850
913
851
- // check that upon completion, threshold is known
852
- assert_eq ! ( ml. session( 0 ) . key_threshold( ) , Ok ( 1 ) ) ;
914
+ // check that upon completion, commmon key data is known
915
+ assert_eq ! ( ml. session( 0 ) . common_key_data( ) , Ok ( DocumentKeyShare {
916
+ author: H160 :: from_low_u64_be( 2 ) ,
917
+ threshold: 1 ,
918
+ public: H512 :: from_low_u64_be( 3 ) ,
919
+ ..Default :: default ( )
920
+ } ) ) ;
853
921
}
854
922
855
923
#[ test]
0 commit comments