@@ -29,17 +29,17 @@ use halo2_ecc::{
29
29
fields:: FieldChip ,
30
30
} ;
31
31
use itertools:: Itertools ;
32
- use serde:: { Deserialize , Serialize } ;
33
32
use ssz_rs:: Merkleized ;
34
- use std:: {
35
- env:: var,
36
- fs:: File ,
37
- io:: { Read , Result as IoResult , Write } ,
38
- iter,
39
- marker:: PhantomData ,
40
- vec,
41
- } ;
42
-
33
+ use std:: { env:: var, iter, marker:: PhantomData , vec} ;
34
+
35
+ /// `CommitteeUpdateCircuit` maps next sync committee SSZ root in the finalized state root to the corresponding Poseidon commitment to the public keys.
36
+ ///
37
+ /// Assumes that public keys are BLS12-381 points on G1; `sync_committee_branch` is exactly `S::SYNC_COMMITTEE_PUBKEYS_DEPTH` hashes in lenght.
38
+ ///
39
+ /// The circuit exposes two public inputs:
40
+ /// - `poseidon_commit` is a Poseidon "onion" commitment to the X coordinates of sync committee public keys. Coordinates are expressed as big-integer with two limbs of LIMB_BITS * 2 bits.
41
+ /// - `committee_root_ssz` is a Merkle SSZ root of the list of sync committee public keys.
42
+ /// - `finalized_header_root` is a Merkle SSZ root of the finalized header.
43
43
#[ derive( Clone , Debug , Default ) ]
44
44
pub struct CommitteeUpdateCircuit < S : Spec , F : Field > {
45
45
_f : PhantomData < F > ,
@@ -268,57 +268,25 @@ impl<S: Spec> AppCircuit for CommitteeUpdateCircuit<S, bn256::Fr> {
268
268
269
269
#[ cfg( test) ]
270
270
mod tests {
271
- fn write_struct_to_file < T : Serialize > ( data : & T , file_path : & str ) -> IoResult < ( ) > {
272
- let encoded: Vec < u8 > = bincode:: serialize ( data) . expect ( "Failed to serialize" ) ;
273
- let mut file = File :: create ( file_path) ?;
274
- file. write_all ( & encoded) ?;
275
- Ok ( ( ) )
276
- }
277
-
278
- fn read_struct_from_file < T : for < ' de > Deserialize < ' de > > ( file_path : & str ) -> IoResult < T > {
279
- let mut file = File :: open ( file_path) ?;
280
- let mut buffer = Vec :: new ( ) ;
281
- file. read_to_end ( & mut buffer) ?;
282
-
283
- // Deserialization can use the buffer's data, no lifetime issue
284
- let decoded: T = bincode:: deserialize ( & buffer)
285
- . map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidData , e) ) ?;
286
-
287
- Ok ( decoded)
288
- }
289
- use std:: { fs, io:: Cursor } ;
271
+ use std:: fs;
290
272
291
273
use crate :: {
292
- aggregation_circuit:: AggregationConfigPinning , gadget :: crypto :: ShaFlexGateManager ,
293
- util :: Halo2ConfigPinning , witness:: CommitteeUpdateArgs ,
274
+ aggregation_circuit:: AggregationConfigPinning , util :: Halo2ConfigPinning ,
275
+ witness:: CommitteeUpdateArgs ,
294
276
} ;
295
277
296
278
use super :: * ;
297
- use bn256 :: G1Affine ;
279
+ use ark_std :: { end_timer , start_timer } ;
298
280
use eth_types:: Testnet ;
299
- use ff:: Field ;
300
281
use halo2_base:: {
301
- gates:: circuit:: { builder:: BaseCircuitBuilder , BaseCircuitParams } ,
302
282
halo2_proofs:: {
283
+ dev:: MockProver ,
303
284
halo2curves:: bn256:: Fr ,
304
- plonk:: { create_proof, keygen_vk, verify_proof, Circuit , ProvingKey , VerifyingKey } ,
305
- poly:: {
306
- commitment:: Params ,
307
- kzg:: {
308
- commitment:: { KZGCommitmentScheme , ParamsKZG } ,
309
- multiopen:: { ProverSHPLONK , VerifierSHPLONK } ,
310
- strategy:: SingleStrategy ,
311
- } ,
312
- VerificationStrategy ,
313
- } ,
314
- transcript:: {
315
- Blake2bRead , Blake2bWrite , Challenge255 , TranscriptReadBuffer ,
316
- TranscriptWriterBuffer ,
317
- } ,
285
+ plonk:: ProvingKey ,
286
+ poly:: { commitment:: Params , kzg:: commitment:: ParamsKZG } ,
318
287
} ,
319
288
utils:: fs:: gen_srs,
320
289
} ;
321
- use rand:: rngs:: OsRng ;
322
290
use snark_verifier_sdk:: evm:: { evm_verify, gen_evm_proof_shplonk} ;
323
291
use snark_verifier_sdk:: { halo2:: aggregation:: AggregationCircuit , CircuitExt , Snark } ;
324
292
@@ -343,113 +311,25 @@ mod tests {
343
311
}
344
312
345
313
#[ test]
346
- fn test_prove_and_verify_plonk ( ) {
314
+ fn test_committee_update_circuit ( ) {
347
315
const K : u32 = 20 ;
348
- let mut file = File :: open ( "./params/kzg_bn254_20.srs" ) . expect ( "Failed to open params!" ) ;
349
-
350
- let mut buffer = Vec :: new ( ) ;
351
-
352
- file. read_to_end ( & mut buffer)
353
- . expect ( "Failed to read params!" ) ;
354
-
355
- let params: ParamsKZG < Bn256 > =
356
- ParamsKZG :: read ( & mut Cursor :: new ( & buffer) ) . expect ( "Failed to read buffer :(" ) ;
357
-
358
- const PINNING_PATH : & str = "./config/committee_update_20.json" ;
359
- const PKEY_PATH : & str = "../build/committee_update_20.pkey" ;
360
-
361
- let pk = CommitteeUpdateCircuit :: < Testnet , Fr > :: create_pk (
362
- & params,
363
- PKEY_PATH ,
364
- PINNING_PATH ,
365
- & CommitteeUpdateArgs :: < Testnet > :: default ( ) ,
366
- None ,
367
- ) ;
368
-
369
- println ! ( "[Ok] Proving Key" ) ;
370
-
371
316
let witness = load_circuit_args ( ) ;
317
+ let params: ParamsKZG < Bn256 > = gen_srs ( K ) ;
372
318
373
319
let circuit = CommitteeUpdateCircuit :: < Testnet , Fr > :: create_circuit (
374
- CircuitBuilderStage :: Prover ,
375
- Some ( Eth2ConfigPinning :: from_path ( PINNING_PATH ) ) ,
320
+ CircuitBuilderStage :: Mock ,
321
+ None ,
376
322
& witness,
377
323
& params,
378
324
)
379
- . expect ( "Failed to construct circuit" ) ;
380
-
381
- println ! ( "[Ok] Construct Circuit" ) ;
382
-
383
- let reader: Vec < u8 > = vec ! [ ] ;
384
- let mut transcript_writer: Blake2bWrite < Vec < u8 > , G1Affine , Challenge255 < G1Affine > > =
385
- Blake2bWrite :: init ( reader) ;
386
- println ! ( "[Ok] Read Transcript" ) ;
387
-
388
- let vk: & VerifyingKey < G1Affine > = pk. get_vk ( ) ;
389
-
390
- let circuit_params = circuit. params ( ) ;
391
- //let vk_serialized = vk.to_bytes(halo2_base::halo2_proofs::SerdeFormat::RawBytes);
392
-
393
- let witness_serialized = bincode:: serialize ( & witness) . unwrap ( ) ;
394
- let witness_deserialized: CommitteeUpdateArgs < Testnet > =
395
- bincode:: deserialize ( & witness_serialized) . unwrap ( ) ;
396
-
397
- /*
398
- let instances = circuit.instances();
399
- let instances_ref: Vec<&[Fr]> = instances.iter().map(|inner| inner.as_slice()).collect();
400
- let instances_nested_ref: Vec<&[&[Fr]]> = instances_ref
401
- .iter()
402
- .map(|inner| std::slice::from_ref(inner))
403
- .collect();
404
-
405
- create_proof::<
406
- KZGCommitmentScheme<Bn256>,
407
- ProverSHPLONK<Bn256>,
408
- Challenge255<G1Affine>,
409
- OsRng,
410
- Blake2bWrite<Vec<u8>, G1Affine, Challenge255<G1Affine>>,
411
- _,
412
- >(
413
- ¶ms,
414
- &pk,
415
- &[circuit],
416
- &instances_nested_ref,
417
- OsRng,
418
- &mut transcript_writer,
419
- )
420
- .expect("Failed to generate proof!");
421
-
422
- let reader_final = transcript_writer.finalize();
423
-
424
- println!("[Ok] Proof");
425
-
426
- let mut transcript_reader = Blake2bRead::init(&reader_final[..]);
325
+ . unwrap ( ) ;
427
326
428
- write_struct_to_file(&reader_final, "proof.txt").expect("Failed to write proof");
429
- write_struct_to_file(&instances, "instances.txt").expect("Failed to write instances");
430
- write_struct_to_file(
431
- &vk.to_bytes(halo2_base::halo2_proofs::SerdeFormat::RawBytes),
432
- "vk.txt",
433
- )
434
- .expect("Failed to write vk");
435
-
436
- verify_proof::<
437
- _,
438
- VerifierSHPLONK<Bn256>,
439
- Challenge255<G1Affine>,
440
- Blake2bRead<_, G1Affine, Challenge255<G1Affine>>,
441
- SingleStrategy<Bn256>,
442
- >(
443
- ¶ms,
444
- &vk,
445
- SingleStrategy::new(¶ms),
446
- &instances_nested_ref,
447
- &mut transcript_reader,
448
- )
449
- .expect("Failed to verify!");
327
+ let instance = CommitteeUpdateCircuit :: < Testnet , Fr > :: get_instances ( & witness, LIMB_BITS ) ;
450
328
451
- println!("[Yippie]");
452
- */
329
+ let timer = start_timer ! ( || "committee_update mock prover" ) ;
330
+ let prover = MockProver :: < Fr > :: run ( K , & circuit, instance) . unwrap ( ) ;
331
+ prover. assert_satisfied ( ) ;
332
+ end_timer ! ( timer) ;
453
333
}
454
334
455
335
#[ test]
0 commit comments