22
33sp1_zkvm:: entrypoint!( main) ;
44
5- use dkg:: { self , compute_initial_commitment_hash, for_each_raw_type , VerificationErrors } ;
5+ use dkg:: { self , compute_initial_commitment_hash} ;
66
77use chacha20:: cipher:: { KeyIvInit , StreamCipher } ;
88use chacha20:: { ChaCha20 , Key , Nonce } ;
99
10- use bls12_381:: { self , G1Affine , G2Affine } ;
1110use dkg:: crypto:: * ;
1211use dkg:: types:: * ;
1312use serde:: Deserialize ;
@@ -123,11 +122,21 @@ impl BinaryStream {
123122 Ok ( T :: from_bytes ( bytes) )
124123 }
125124
125+ pub fn remain_len ( & self ) -> usize {
126+ self . data . len ( ) - self . pos
127+ }
128+
126129 pub fn finalize ( & mut self ) {
130+ println ! (
131+ "Read {} bytes, {} remain" ,
132+ self . pos,
133+ self . data. len( ) - self . pos
134+ ) ;
127135 assert ! ( self . pos == self . data. len( ) ) ;
128136 }
129137}
130138
139+ #[ cfg( feature = "auth_commitment" ) ]
131140fn parse_message < Setup : dkg:: DkgSetup + dkg:: DkgSetupTypes < Setup > > (
132141 msg : Vec < u8 > ,
133142 settings : dkg:: GenerateSettings ,
@@ -141,22 +150,28 @@ fn parse_message<Setup: dkg::DkgSetup + dkg::DkgSetupTypes<Setup>>(
141150 let gen_id = stream
142151 . read :: < DkgGenId > ( )
143152 . map_err ( |e| format ! ( "Invalid gen_id: {e}" ) ) ?;
153+
154+ println ! ( "remain_len {}" , stream. remain_len( ) ) ;
144155 let msg_type = stream
145156 . read_byte_array :: < 1 > ( )
146157 . map_err ( |e| format ! ( "Invalid msg_type: {e}" ) ) ?[ 0 ] ;
147-
158+ println ! ( "remain_len {}" , stream . remain_len ( ) ) ;
148159 let secret = stream
149160 . read :: < RawBytes < Setup :: DkgSecretKey > > ( )
150161 . map_err ( |e| format ! ( "Invalid secret: {e}" ) ) ?;
162+ println ! ( "remain_len {}" , stream. remain_len( ) ) ;
151163 let commitment_hash = stream
152164 . read :: < SHA256Raw > ( )
153165 . map_err ( |e| format ! ( "Invalid commitment_hash: {e}" ) ) ?;
166+ println ! ( "remain_len {}" , stream. remain_len( ) ) ;
154167 let commitment_pubkey = stream
155168 . read :: < RawBytes < Setup :: CommitmentPubkey > > ( )
156169 . map_err ( |e| format ! ( "Invalid commitment_pubkey: {e}" ) ) ?;
170+ println ! ( "remain_len {}" , stream. remain_len( ) ) ;
157171 let commitment_signature = stream
158172 . read :: < RawBytes < Setup :: CommitmentSignature > > ( )
159173 . map_err ( |e| format ! ( "Invalid commitment_signature: {e}" ) ) ?;
174+ println ! ( "remain_len {}" , stream. remain_len( ) ) ;
160175
161176 stream. finalize ( ) ;
162177
@@ -172,7 +187,7 @@ fn parse_message<Setup: dkg::DkgSetup + dkg::DkgSetupTypes<Setup>>(
172187 return Err ( "Invalid msg_type" . to_string ( ) ) ;
173188 }
174189
175- let mut initial_commitment = dkg:: InitialCommitment :: < Setup > {
190+ let initial_commitment = dkg:: InitialCommitment :: < Setup > {
176191 settings : settings,
177192 base_pubkeys : base_pubkeys,
178193 hash : sender_commitment_hash. clone ( ) ,
@@ -196,6 +211,69 @@ fn parse_message<Setup: dkg::DkgSetup + dkg::DkgSetupTypes<Setup>>(
196211 } )
197212}
198213
214+ #[ cfg( not( feature = "auth_commitment" ) ) ]
215+ fn parse_message < Setup : dkg:: DkgSetup + dkg:: DkgSetupTypes < Setup > > (
216+ msg : Vec < u8 > ,
217+ settings : dkg:: GenerateSettings ,
218+ base_pubkeys : Vec < RawBytes < Setup :: Point > > ,
219+ commitment_hashes : Vec < SHA256Raw > ,
220+ receiver_commitment_hash : SHA256Raw ,
221+ sender_commitment_hash : SHA256Raw ,
222+ ) -> Result < dkg:: SharedData < Setup > , String > {
223+ let mut stream = BinaryStream { data : msg, pos : 0 } ;
224+
225+ let gen_id = stream
226+ . read :: < DkgGenId > ( )
227+ . map_err ( |e| format ! ( "Invalid gen_id: {e}" ) ) ?;
228+ //println!("remain_len {}", stream.remain_len());
229+ let msg_type = stream
230+ . read_byte_array :: < 1 > ( )
231+ . map_err ( |e| format ! ( "Invalid msg_type: {e}" ) ) ?[ 0 ] ;
232+ //println!("remain_len {}", stream.remain_len());
233+ let secret = stream
234+ . read :: < RawBytes < Setup :: DkgSecretKey > > ( )
235+ . map_err ( |e| format ! ( "Invalid secret: {e}" ) ) ?;
236+ //println!("remain_len {}", stream.remain_len());
237+ let commitment_pubkey = stream
238+ . read :: < RawBytes < Setup :: CommitmentPubkey > > ( )
239+ . map_err ( |e| format ! ( "Invalid commitment_pubkey: {e}" ) ) ?;
240+ //println!("remain_len {}", stream.remain_len());
241+ stream. finalize ( ) ;
242+
243+ if stream. bytes_left ( ) != 0 {
244+ return Err ( "Invalid message" . to_string ( ) ) ;
245+ }
246+
247+ if settings. gen_id != gen_id {
248+ return Err ( "Invalid gen_id" . to_string ( ) ) ;
249+ }
250+
251+ if msg_type != 3 {
252+ return Err ( "Invalid msg_type" . to_string ( ) ) ;
253+ }
254+
255+ let initial_commitment = dkg:: InitialCommitment :: < Setup > {
256+ settings : settings,
257+ base_pubkeys : base_pubkeys,
258+ hash : sender_commitment_hash. clone ( ) ,
259+ } ;
260+
261+ Ok ( dkg:: SharedData :: < Setup > {
262+ verification_hashes : commitment_hashes,
263+ initial_commitment : initial_commitment,
264+ seeds_exchange_commitment : dkg:: SeedExchangeCommitment {
265+ initial_commitment_hash : sender_commitment_hash,
266+ shared_secret : dkg:: ExchangedSecret {
267+ secret : secret,
268+ dst_base_hash : receiver_commitment_hash,
269+ } ,
270+ commitment : dkg:: Commitment {
271+ pubkey : commitment_pubkey,
272+ } ,
273+ } ,
274+ } )
275+ }
276+
199277pub fn main ( ) {
200278 run :: < BlsDkgWithSecp256kCommitment > ( ) ;
201279}
0 commit comments