11// Copyright © Aptos Foundation
22
33use crate :: {
4- dkg:: dummy_dkg:: DummyDKG , on_chain_config:: OnChainConfig ,
5- validator_verifier:: ValidatorConsensusInfoMoveStruct ,
4+ dkg:: real_dkg:: RealDKG ,
5+ on_chain_config:: OnChainConfig ,
6+ validator_verifier:: { ValidatorConsensusInfo , ValidatorConsensusInfoMoveStruct } ,
67} ;
78use anyhow:: Result ;
89use aptos_crypto:: Uniform ;
@@ -12,9 +13,12 @@ use move_core_types::{
1213 move_resource:: MoveStructType ,
1314} ;
1415use once_cell:: sync:: Lazy ;
15- use rand:: CryptoRng ;
16+ use rand:: { CryptoRng , RngCore } ;
1617use serde:: { Deserialize , Serialize } ;
17- use std:: { collections:: BTreeSet , fmt:: Debug } ;
18+ use std:: {
19+ collections:: BTreeSet ,
20+ fmt:: { Debug , Formatter } ,
21+ } ;
1822
1923#[ derive( Clone , Serialize , Deserialize , Debug , PartialEq , Eq , CryptoHasher , BCSCryptoHash ) ]
2024pub struct DKGTranscriptMetadata {
@@ -37,13 +41,22 @@ pub static DKG_START_EVENT_MOVE_TYPE_TAG: Lazy<TypeTag> =
3741 Lazy :: new ( || TypeTag :: Struct ( Box :: new ( DKGStartEvent :: struct_tag ( ) ) ) ) ;
3842
3943/// DKG transcript and its metadata.
40- #[ derive( Clone , Serialize , Deserialize , Debug , PartialEq , Eq ) ]
44+ #[ derive( Clone , Serialize , Deserialize , PartialEq , Eq ) ]
4145pub struct DKGTranscript {
4246 pub metadata : DKGTranscriptMetadata ,
4347 #[ serde( with = "serde_bytes" ) ]
4448 pub transcript_bytes : Vec < u8 > ,
4549}
4650
51+ impl Debug for DKGTranscript {
52+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
53+ f. debug_struct ( "DKGTranscript" )
54+ . field ( "metadata" , & self . metadata )
55+ . field ( "transcript_bytes_len" , & self . transcript_bytes . len ( ) )
56+ . finish ( )
57+ }
58+ }
59+
4760impl DKGTranscript {
4861 pub fn new ( epoch : u64 , author : AccountAddress , transcript_bytes : Vec < u8 > ) -> Self {
4962 Self {
@@ -63,52 +76,88 @@ impl DKGTranscript {
6376 }
6477}
6578
66- // The input of DKG .
79+ /// Reflection of `0x1::dkg::DKGSessionMetadata` in rust .
6780#[ derive( Clone , Debug , Eq , PartialEq , Serialize , Deserialize ) ]
6881pub struct DKGSessionMetadata {
6982 pub dealer_epoch : u64 ,
7083 pub dealer_validator_set : Vec < ValidatorConsensusInfoMoveStruct > ,
7184 pub target_validator_set : Vec < ValidatorConsensusInfoMoveStruct > ,
7285}
7386
74- // The input and the run state of DKG.
87+ impl DKGSessionMetadata {
88+ pub fn target_validator_consensus_infos_cloned ( & self ) -> Vec < ValidatorConsensusInfo > {
89+ self . target_validator_set
90+ . clone ( )
91+ . into_iter ( )
92+ . map ( |obj| obj. try_into ( ) . unwrap ( ) )
93+ . collect ( )
94+ }
95+
96+ pub fn dealer_consensus_infos_cloned ( & self ) -> Vec < ValidatorConsensusInfo > {
97+ self . dealer_validator_set
98+ . clone ( )
99+ . into_iter ( )
100+ . map ( |obj| obj. try_into ( ) . unwrap ( ) )
101+ . collect ( )
102+ }
103+ }
104+
75105/// Reflection of Move type `0x1::dkg::DKGSessionState`.
76106#[ derive( Clone , Debug , Eq , PartialEq , Serialize , Deserialize ) ]
77107pub struct DKGSessionState {
78108 pub metadata : DKGSessionMetadata ,
79109 pub start_time_us : u64 ,
80- pub result : Vec < u8 > ,
81- pub deadline_microseconds : u64 ,
110+ pub transcript : Vec < u8 > ,
82111}
83112
113+ impl DKGSessionState {
114+ pub fn target_epoch ( & self ) -> u64 {
115+ self . metadata . dealer_epoch + 1
116+ }
117+ }
84118/// Reflection of Move type `0x1::dkg::DKGState`.
85119#[ derive( Clone , Debug , Default , Eq , PartialEq , Serialize , Deserialize ) ]
86120pub struct DKGState {
87- pub last_complete : Option < DKGSessionState > ,
121+ pub last_completed : Option < DKGSessionState > ,
88122 pub in_progress : Option < DKGSessionState > ,
89123}
90124
125+ impl DKGState {
126+ pub fn maybe_last_complete ( & self , epoch : u64 ) -> Option < & DKGSessionState > {
127+ match & self . last_completed {
128+ Some ( session) if session. target_epoch ( ) == epoch => Some ( session) ,
129+ _ => None ,
130+ }
131+ }
132+
133+ pub fn last_complete ( & self ) -> & DKGSessionState {
134+ self . last_completed . as_ref ( ) . unwrap ( )
135+ }
136+ }
137+
91138impl OnChainConfig for DKGState {
92139 const MODULE_IDENTIFIER : & ' static str = "dkg" ;
93140 const TYPE_IDENTIFIER : & ' static str = "DKGState" ;
94141}
95142
143+ /// NOTE: this is a subset of the full scheme. Some data items/algorithms are not used in DKG and are omitted.
96144pub trait DKGTrait : Debug {
97145 type DealerPrivateKey ;
98146 type PublicParams : Clone + Debug + Send + Sync ;
99- type Transcript : Clone + Default + Send + Sync + Serialize + for < ' a > Deserialize < ' a > ;
147+ type Transcript : Clone + Send + Sync + Serialize + for < ' a > Deserialize < ' a > ;
100148 type InputSecret : Uniform ;
101149 type DealtSecret ;
102150 type DealtSecretShare ;
103- type NewValidatorDecryptKey ;
151+ type DealtPubKeyShare ;
152+ type NewValidatorDecryptKey : Uniform ;
104153
105154 fn new_public_params ( dkg_session_metadata : & DKGSessionMetadata ) -> Self :: PublicParams ;
106- fn generate_predictable_input_secret_for_testing (
107- dealer_sk : & Self :: DealerPrivateKey ,
108- ) -> Self :: InputSecret ;
109155 fn aggregate_input_secret ( secrets : Vec < Self :: InputSecret > ) -> Self :: InputSecret ;
110- fn dealt_secret_from_input ( input : & Self :: InputSecret ) -> Self :: DealtSecret ;
111- fn generate_transcript < R : CryptoRng > (
156+ fn dealt_secret_from_input (
157+ pub_params : & Self :: PublicParams ,
158+ input : & Self :: InputSecret ,
159+ ) -> Self :: DealtSecret ;
160+ fn generate_transcript < R : CryptoRng + RngCore > (
112161 rng : & mut R ,
113162 params : & Self :: PublicParams ,
114163 input_secret : & Self :: InputSecret ,
@@ -120,14 +169,15 @@ pub trait DKGTrait: Debug {
120169
121170 fn aggregate_transcripts (
122171 params : & Self :: PublicParams ,
123- transcripts : Vec < Self :: Transcript > ,
124- ) -> Self :: Transcript ;
172+ accumulator : & mut Self :: Transcript ,
173+ element : Self :: Transcript ,
174+ ) ;
125175 fn decrypt_secret_share_from_transcript (
126176 pub_params : & Self :: PublicParams ,
127177 trx : & Self :: Transcript ,
128178 player_idx : u64 ,
129179 dk : & Self :: NewValidatorDecryptKey ,
130- ) -> Result < Self :: DealtSecretShare > ;
180+ ) -> Result < ( Self :: DealtSecretShare , Self :: DealtPubKeyShare ) > ;
131181 fn reconstruct_secret_from_shares (
132182 pub_params : & Self :: PublicParams ,
133183 player_share_pairs : Vec < ( u64 , Self :: DealtSecretShare ) > ,
@@ -136,6 +186,6 @@ pub trait DKGTrait: Debug {
136186}
137187
138188pub mod dummy_dkg;
189+ pub mod real_dkg;
139190
140- // TODO: replace with RealDKG.
141- pub type DefaultDKG = DummyDKG ;
191+ pub type DefaultDKG = RealDKG ;
0 commit comments