@@ -309,7 +309,9 @@ where
309
309
#[ cfg( test) ]
310
310
pub ( crate ) mod testing {
311
311
use super :: * ;
312
- use crate :: testing:: * ;
312
+ use crate :: { pedersen, testing as common} ;
313
+
314
+ pub const TEST_RING_SIZE : usize = 8 ;
313
315
314
316
pub fn prove_verify < S : RingSuite > ( )
315
317
where
@@ -318,17 +320,17 @@ pub(crate) mod testing {
318
320
AffinePoint < S > : utils:: te_sw_map:: SWMapping < CurveConfig < S > > ,
319
321
{
320
322
let rng = & mut ark_std:: test_rng ( ) ;
321
- let ring_ctx = RingContext :: < S > :: from_rand ( 512 , rng) ;
323
+ let ring_ctx = RingContext :: < S > :: from_rand ( TEST_RING_SIZE , rng) ;
322
324
323
- let secret = Secret :: < S > :: from_seed ( TEST_SEED ) ;
325
+ let secret = Secret :: < S > :: from_seed ( common :: TEST_SEED ) ;
324
326
let public = secret. public ( ) ;
325
- let input = Input :: from ( random_val ( Some ( rng) ) ) ;
327
+ let input = Input :: from ( common :: random_val ( Some ( rng) ) ) ;
326
328
let output = secret. output ( input) ;
327
329
328
330
let ring_size = ring_ctx. max_ring_size ( ) ;
329
331
330
332
let prover_idx = 3 ;
331
- let mut pks = random_vec :: < AffinePoint < S > > ( ring_size, Some ( rng) ) ;
333
+ let mut pks = common :: random_vec :: < AffinePoint < S > > ( ring_size, Some ( rng) ) ;
332
334
pks[ prover_idx] = public. 0 ;
333
335
334
336
let prover_key = ring_ctx. prover_key ( & pks) ;
@@ -370,4 +372,167 @@ pub(crate) mod testing {
370
372
} ;
371
373
( $suite: ident, false ) => { } ;
372
374
}
375
+
376
+ pub trait RingSuiteExt : RingSuite
377
+ where
378
+ BaseField < Self > : ark_ff:: PrimeField ,
379
+ CurveConfig < Self > : SWCurveConfig + Clone ,
380
+ AffinePoint < Self > : SWMapping < CurveConfig < Self > > ,
381
+ {
382
+ fn ring_context ( ) -> & ' static RingContext < Self > ;
383
+ }
384
+
385
+ pub struct TestVector < S : RingSuite >
386
+ where
387
+ BaseField < S > : ark_ff:: PrimeField ,
388
+ CurveConfig < S > : SWCurveConfig + Clone ,
389
+ AffinePoint < S > : SWMapping < CurveConfig < S > > ,
390
+ {
391
+ pub pedersen : pedersen:: testing:: TestVector < S > ,
392
+ pub ring_pks : Box < [ AffinePoint < S > ; TEST_RING_SIZE ] > ,
393
+ pub ring_proof : RingProof < S > ,
394
+ }
395
+
396
+ impl < S : RingSuite > core:: fmt:: Debug for TestVector < S >
397
+ where
398
+ BaseField < S > : ark_ff:: PrimeField ,
399
+ CurveConfig < S > : SWCurveConfig + Clone ,
400
+ AffinePoint < S > : SWMapping < CurveConfig < S > > ,
401
+ {
402
+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
403
+ f. debug_struct ( "TestVector" )
404
+ . field ( "pedersen" , & self . pedersen )
405
+ . field ( "ring_proof" , & "..." )
406
+ . finish ( )
407
+ }
408
+ }
409
+
410
+ impl < S : RingSuiteExt + std:: fmt:: Debug + ' static > common:: TestVectorTrait for TestVector < S >
411
+ where
412
+ BaseField < S > : ark_ff:: PrimeField ,
413
+ CurveConfig < S > : SWCurveConfig + Clone ,
414
+ AffinePoint < S > : SWMapping < CurveConfig < S > > ,
415
+ {
416
+ fn new ( comment : & str , seed : & [ u8 ] , alpha : & [ u8 ] , salt : Option < & [ u8 ] > , ad : & [ u8 ] ) -> Self {
417
+ use super :: Prover ;
418
+ let pedersen = pedersen:: testing:: TestVector :: new ( comment, seed, alpha, salt, ad) ;
419
+
420
+ let secret = Secret :: < S > :: from_scalar ( pedersen. base . sk ) ;
421
+ let public = secret. public ( ) ;
422
+
423
+ let input = Input :: < S > :: from ( pedersen. base . h ) ;
424
+ let output = Output :: from ( pedersen. base . gamma ) ;
425
+
426
+ let ring_ctx = <S as RingSuiteExt >:: ring_context ( ) ;
427
+
428
+ use ark_std:: rand:: SeedableRng ;
429
+ let rng = & mut rand_chacha:: ChaCha20Rng :: from_seed ( [ 0x11 ; 32 ] ) ;
430
+ let prover_idx = 3 ;
431
+ let mut ring_pks = common:: random_vec :: < AffinePoint < S > > ( TEST_RING_SIZE , Some ( rng) ) ;
432
+ ring_pks[ prover_idx] = public. 0 ;
433
+
434
+ let prover_key = ring_ctx. prover_key ( & ring_pks) ;
435
+ let prover = ring_ctx. prover ( prover_key, prover_idx) ;
436
+ let proof = secret. prove ( input, output, ad, & prover) ;
437
+
438
+ {
439
+ // Just in case...
440
+ let mut p = ( Vec :: new ( ) , Vec :: new ( ) ) ;
441
+ pedersen. proof . serialize_compressed ( & mut p. 0 ) . unwrap ( ) ;
442
+ proof. pedersen_proof . serialize_compressed ( & mut p. 1 ) . unwrap ( ) ;
443
+ assert_eq ! ( p. 0 , p. 1 ) ;
444
+ }
445
+
446
+ // TODO: also dump the verifier pks commitmet
447
+ Self {
448
+ pedersen,
449
+ ring_pks : crate :: testing:: vec_to_array ( ring_pks) . unwrap ( ) ,
450
+ ring_proof : proof. ring_proof ,
451
+ }
452
+ }
453
+
454
+ fn from_map ( map : & common:: TestVectorMap ) -> Self {
455
+ let pedersen = pedersen:: testing:: TestVector :: from_map ( map) ;
456
+
457
+ let ring_pks_raw = map. item_bytes ( "ring_pks" ) ;
458
+ let ring_pks =
459
+ <[ AffinePoint < S > ; TEST_RING_SIZE ] >:: deserialize_compressed ( & ring_pks_raw[ ..] )
460
+ . map ( Box :: new)
461
+ . unwrap ( ) ;
462
+
463
+ let ring_proof_raw = map. item_bytes ( "ring_proof" ) ;
464
+ let ring_proof = RingProof :: < S > :: deserialize_compressed ( & ring_proof_raw[ ..] ) . unwrap ( ) ;
465
+
466
+ Self {
467
+ pedersen,
468
+ ring_pks,
469
+ ring_proof,
470
+ }
471
+ }
472
+
473
+ fn to_map ( & self ) -> common:: TestVectorMap {
474
+ let mut map = self . pedersen . to_map ( ) ;
475
+
476
+ let mut ring_pks_raw = Vec :: new ( ) ;
477
+ self . ring_pks
478
+ . serialize_compressed ( & mut ring_pks_raw)
479
+ . unwrap ( ) ;
480
+ let ring_pks_hex = hex:: encode ( ring_pks_raw) ;
481
+ map. 0 . insert ( "ring_pks" . to_string ( ) , ring_pks_hex) ;
482
+
483
+ let mut ring_proof_raw = Vec :: new ( ) ;
484
+ self . ring_proof
485
+ . serialize_compressed ( & mut ring_proof_raw)
486
+ . unwrap ( ) ;
487
+ let ring_proof_hex = hex:: encode ( ring_proof_raw) ;
488
+ map. 0 . insert ( "ring_proof" . to_string ( ) , ring_proof_hex) ;
489
+
490
+ map
491
+ }
492
+
493
+ fn run ( & self ) {
494
+ self . pedersen . run ( ) ;
495
+
496
+ let input = Input :: < S > :: from ( self . pedersen . base . h ) ;
497
+ let output = Output :: from ( self . pedersen . base . gamma ) ;
498
+ let secret = Secret :: from_scalar ( self . pedersen . base . sk ) ;
499
+ let public = secret. public ( ) ;
500
+ assert_eq ! ( public. 0 , self . pedersen. base. pk) ;
501
+
502
+ let ring_ctx = <S as RingSuiteExt >:: ring_context ( ) ;
503
+
504
+ let ring_pks = & * self . ring_pks ;
505
+ let prover_idx = ring_pks. iter ( ) . position ( |& pk| pk == public. 0 ) . unwrap ( ) ;
506
+
507
+ let prover_key = ring_ctx. prover_key ( ring_pks) ;
508
+ let prover = ring_ctx. prover ( prover_key, prover_idx) ;
509
+
510
+ let verifier_key = ring_ctx. verifier_key ( ring_pks) ;
511
+ let verifier = ring_ctx. verifier ( verifier_key) ;
512
+
513
+ let proof = secret. prove ( input, output, & self . pedersen . base . ad , & prover) ;
514
+
515
+ {
516
+ // Check if Pedersen proof matches
517
+ let mut p = ( Vec :: new ( ) , Vec :: new ( ) ) ;
518
+ self . pedersen . proof . serialize_compressed ( & mut p. 0 ) . unwrap ( ) ;
519
+ proof. pedersen_proof . serialize_compressed ( & mut p. 1 ) . unwrap ( ) ;
520
+ assert_eq ! ( p. 0 , p. 1 ) ;
521
+ }
522
+
523
+ // TODO
524
+ #[ cfg( feature = "test-vectors" ) ]
525
+ {
526
+ // Check if Ring proof matches
527
+ let mut p = ( Vec :: new ( ) , Vec :: new ( ) ) ;
528
+ self . ring_proof . serialize_compressed ( & mut p. 0 ) . unwrap ( ) ;
529
+ proof. ring_proof . serialize_compressed ( & mut p. 1 ) . unwrap ( ) ;
530
+ assert_eq ! ( p. 0 , p. 1 ) ;
531
+ }
532
+
533
+ assert ! (
534
+ Public :: verify( input, output, & self . pedersen. base. ad, & proof, & verifier) . is_ok( )
535
+ ) ;
536
+ }
537
+ }
373
538
}
0 commit comments