@@ -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,6 +612,7 @@ impl SessionResultComputer for LargestSupportResultComputer {
590
612
mod tests {
591
613
use std:: sync:: Arc ;
592
614
use std:: collections:: { VecDeque , BTreeMap , BTreeSet } ;
615
+ use ethereum_types:: { H512 , H160 , Address } ;
593
616
use ethkey:: public_to_address;
594
617
use key_server_cluster:: { NodeId , SessionId , Error , KeyStorage , DummyKeyStorage ,
595
618
DocumentKeyShare , DocumentKeyShareVersion } ;
@@ -599,9 +622,14 @@ mod tests {
599
622
use key_server_cluster:: cluster_sessions:: ClusterSession ;
600
623
use key_server_cluster:: admin_sessions:: ShareChangeSessionMeta ;
601
624
use key_server_cluster:: decryption_session:: create_default_decryption_session;
602
- use key_server_cluster:: message:: { Message , KeyVersionNegotiationMessage , RequestKeyVersions , KeyVersions } ;
603
- use super :: { SessionImpl , SessionTransport , SessionParams , FastestResultComputer , LargestSupportResultComputer ,
604
- SessionResultComputer , SessionState , ContinueAction , FailedContinueAction } ;
625
+ use key_server_cluster:: message:: {
626
+ Message , KeyVersionNegotiationMessage , RequestKeyVersions ,
627
+ CommonKeyData , KeyVersions ,
628
+ } ;
629
+ use super :: {
630
+ SessionImpl , SessionTransport , SessionParams , FastestResultComputer , LargestSupportResultComputer ,
631
+ SessionResultComputer , SessionState , ContinueAction , FailedContinueAction ,
632
+ } ;
605
633
606
634
struct DummyTransport {
607
635
cluster : Arc < DummyCluster > ,
@@ -756,7 +784,11 @@ mod tests {
756
784
session: Default :: default ( ) ,
757
785
sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
758
786
session_nonce: 0 ,
759
- threshold: Some ( 10 ) ,
787
+ key_common: Some ( CommonKeyData {
788
+ threshold: 10 ,
789
+ author: Default :: default ( ) ,
790
+ public: Default :: default ( ) ,
791
+ } ) ,
760
792
versions: Vec :: new( ) ,
761
793
} ) ) , Err ( Error :: InvalidStateForRequest ) ) ;
762
794
}
@@ -772,7 +804,12 @@ mod tests {
772
804
session: Default :: default ( ) ,
773
805
sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
774
806
session_nonce: 0 ,
775
- threshold: Some ( 0 ) ,
807
+ key_common: Some ( CommonKeyData {
808
+ threshold: 0 ,
809
+ author: Default :: default ( ) ,
810
+ public: Default :: default ( ) ,
811
+ } ) ,
812
+
776
813
versions: vec![ version_id. clone( ) . into( ) ]
777
814
} ) ) , Ok ( ( ) ) ) ;
778
815
assert_eq ! ( ml. session( 0 ) . data. lock( ) . state, SessionState :: Finished ) ;
@@ -781,32 +818,61 @@ mod tests {
781
818
session: Default :: default ( ) ,
782
819
sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
783
820
session_nonce: 0 ,
784
- threshold: Some ( 0 ) ,
821
+ key_common: Some ( CommonKeyData {
822
+ threshold: 0 ,
823
+ author: Default :: default ( ) ,
824
+ public: Default :: default ( ) ,
825
+ } ) ,
826
+
785
827
versions: vec![ version_id. clone( ) . into( ) ]
786
828
} ) ) , Ok ( ( ) ) ) ;
787
829
assert_eq ! ( ml. session( 0 ) . data. lock( ) . state, SessionState :: Finished ) ;
788
830
}
789
831
790
832
#[ test]
791
- fn negotiation_fails_if_wrong_threshold_sent ( ) {
792
- let ml = MessageLoop :: empty ( 3 ) ;
793
- 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
+ } ) ;
794
864
795
- let version_id = ( * math:: generate_random_scalar ( ) . unwrap ( ) ) . clone ( ) ;
796
- assert_eq ! ( ml. session( 0 ) . process_message( ml. node_id( 1 ) , & KeyVersionNegotiationMessage :: KeyVersions ( KeyVersions {
797
- session: Default :: default ( ) ,
798
- sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
799
- session_nonce: 0 ,
800
- threshold: Some ( 1 ) ,
801
- versions: vec![ version_id. clone( ) . into( ) ]
802
- } ) ) , Ok ( ( ) ) ) ;
803
- assert_eq ! ( ml. session( 0 ) . process_message( ml. node_id( 2 ) , & KeyVersionNegotiationMessage :: KeyVersions ( KeyVersions {
804
- session: Default :: default ( ) ,
805
- sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
806
- session_nonce: 0 ,
807
- threshold: Some ( 2 ) ,
808
- versions: vec![ version_id. clone( ) . into( ) ]
809
- } ) ) , 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
+ } ) ;
810
876
}
811
877
812
878
#[ test]
@@ -819,7 +885,7 @@ mod tests {
819
885
session: Default :: default ( ) ,
820
886
sub_session: math:: generate_random_scalar( ) . unwrap( ) . into( ) ,
821
887
session_nonce: 0 ,
822
- threshold : None ,
888
+ key_common : None ,
823
889
versions: vec![ version_id. clone( ) . into( ) ]
824
890
} ) ) , Err ( Error :: InvalidMessage ) ) ;
825
891
}
@@ -829,9 +895,9 @@ mod tests {
829
895
let nodes = MessageLoop :: prepare_nodes ( 2 ) ;
830
896
let version_id = ( * math:: generate_random_scalar ( ) . unwrap ( ) ) . clone ( ) ;
831
897
nodes. values ( ) . nth ( 0 ) . unwrap ( ) . insert ( Default :: default ( ) , DocumentKeyShare {
832
- author : Default :: default ( ) ,
898
+ author : H160 :: from_low_u64_be ( 2 ) ,
833
899
threshold : 1 ,
834
- public : Default :: default ( ) ,
900
+ public : H512 :: from_low_u64_be ( 3 ) ,
835
901
common_point : None ,
836
902
encrypted_point : None ,
837
903
versions : vec ! [ DocumentKeyShareVersion {
@@ -845,8 +911,13 @@ mod tests {
845
911
// we can't be sure that node has given key version because previous ShareAdd session could fail
846
912
assert ! ( ml. session( 0 ) . data. lock( ) . state != SessionState :: Finished ) ;
847
913
848
- // check that upon completion, threshold is known
849
- 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
+ } ) ) ;
850
921
}
851
922
852
923
#[ test]
0 commit comments