@@ -481,12 +481,13 @@ pub mod testing {
481481
482482#[ cfg( test) ]
483483mod tests {
484+ use alloc:: collections:: BTreeMap ;
484485 use alloc:: vec:: Vec ;
485486 use proptest:: prelude:: * ;
486487
487488 use super :: {
488489 testing:: { arb_note_value_bounded, arb_trapdoor, arb_value_sum_bounded} ,
489- OverflowError , ValueCommitTrapdoor , ValueCommitment , ValueSum , MAX_NOTE_VALUE ,
490+ ValueCommitTrapdoor , ValueCommitment , ValueSum , MAX_NOTE_VALUE ,
490491 } ;
491492 use crate :: {
492493 note:: asset_base:: testing:: arb_asset_base, note:: AssetBase , primitives:: redpallas,
@@ -506,59 +507,72 @@ mod tests {
506507 fn check_binding_signature (
507508 native_values : & [ ( ValueSum , ValueCommitTrapdoor ) ] ,
508509 arb_values : & [ ( ValueSum , ValueCommitTrapdoor , AssetBase ) ] ,
509- neg_trapdoors : & [ ValueCommitTrapdoor ] ,
510510 arb_values_to_burn : & [ ( ValueSum , ValueCommitTrapdoor , AssetBase ) ] ,
511511 ) {
512- // for each arb value, create a negative value with a different trapdoor
513- let neg_arb_values: Vec < _ > = arb_values
514- . iter ( )
515- . cloned ( )
516- . zip ( neg_trapdoors. iter ( ) . cloned ( ) )
517- . map ( |( ( value, _, asset) , rcv) | ( negate_value_sum ( value) , rcv, asset) )
518- . collect ( ) ;
519-
520- let native_value_balance = native_values
521- . iter ( )
522- . map ( |( value, _) | value)
523- . sum :: < Result < ValueSum , OverflowError > > ( )
524- . expect ( "we generate values that won't overflow" ) ;
525-
526512 let native_values_with_asset: Vec < ( ValueSum , ValueCommitTrapdoor , AssetBase ) > =
527513 native_values
528514 . iter ( )
529515 . map ( |( value_sum, trapdoor) | ( * value_sum, trapdoor. clone ( ) , AssetBase :: native ( ) ) )
530516 . collect ( ) ;
531517
532518 let values = [
533- & native_values_with_asset,
519+ & native_values_with_asset[ .. ] ,
534520 arb_values,
535- & neg_arb_values,
536521 arb_values_to_burn,
537522 ]
538523 . concat ( ) ;
539524
525+ // bsk = Sum(rcv)
540526 let bsk = values
541527 . iter ( )
542528 . map ( |( _, rcv, _) | rcv)
543529 . sum :: < ValueCommitTrapdoor > ( )
544530 . into_bsk ( ) ;
545531
546- let bvk = ( values
547- . into_iter ( )
532+ // total_commitments = Sum(ValueCommit^{OrchardZSA}_{rcv}(asset, value))
533+ let total_commitments = values
534+ . iter ( )
535+ . cloned ( )
548536 . map ( |( value, rcv, asset) | ValueCommitment :: derive ( value, rcv, asset) )
549- . sum :: < ValueCommitment > ( )
550- - ValueCommitment :: derive (
551- native_value_balance,
552- ValueCommitTrapdoor :: zero ( ) ,
553- AssetBase :: native ( ) ,
554- )
555- - arb_values_to_burn
556- . iter ( )
557- . map ( |( value, _, asset) | {
558- ValueCommitment :: derive ( * value, ValueCommitTrapdoor :: zero ( ) , * asset)
559- } )
560- . sum :: < ValueCommitment > ( ) )
561- . into_bvk ( ) ;
537+ . sum :: < ValueCommitment > ( ) ;
538+
539+ // Expected value balance per asset
540+ let mut expected_balance_by_asset: BTreeMap < AssetBase , ValueSum > = BTreeMap :: new ( ) ;
541+
542+ // Add native_values into expected_balance_by_asset
543+ for ( value, _) in native_values. iter ( ) {
544+ let entry = expected_balance_by_asset
545+ . entry ( AssetBase :: native ( ) )
546+ . or_insert ( ValueSum :: zero ( ) ) ;
547+ * entry = ( * entry + * value) . expect ( "we generate values that won't overflow" ) ;
548+ }
549+
550+ // Add arb_values into expected_balance_by_asset
551+ for ( value, _, asset) in arb_values. iter ( ) . cloned ( ) {
552+ let entry = expected_balance_by_asset
553+ . entry ( asset)
554+ . or_insert ( ValueSum :: zero ( ) ) ;
555+ * entry = ( * entry + value) . expect ( "we generate values that won't overflow" ) ;
556+ }
557+
558+ // Subtract burned values into expected_balance_by_asset
559+ for ( value, _, asset) in arb_values_to_burn. iter ( ) . cloned ( ) {
560+ let entry = expected_balance_by_asset
561+ . entry ( asset)
562+ . or_insert ( ValueSum :: zero ( ) ) ;
563+ * entry =
564+ ( * entry + negate_value_sum ( value) ) . expect ( "we generate values that won't overflow" ) ;
565+ }
566+
567+ // Build the expected commitments using a zero trapdoor
568+ let expected_commitments = expected_balance_by_asset
569+ . into_iter ( )
570+ . map ( |( asset, balance) | {
571+ ValueCommitment :: derive ( balance, ValueCommitTrapdoor :: zero ( ) , asset)
572+ } )
573+ . sum :: < ValueCommitment > ( ) ;
574+
575+ let bvk = ( total_commitments - expected_commitments) . into_bvk ( ) ;
562576
563577 assert_eq ! ( redpallas:: VerificationKey :: from( & bsk) , bvk) ;
564578 }
@@ -571,20 +585,20 @@ mod tests {
571585 prop:: collection:: vec( ( arb_value_sum_bounded( bound) , arb_trapdoor( ) ) , n_values)
572586 )
573587 ) ,
574- ( asset_values, neg_trapdoors ) in ( 1usize ..10 ) . prop_flat_map( |n_values|
575- ( arb_note_value_bounded( MAX_NOTE_VALUE / n_values as u64 ) . prop_flat_map( move |bound|
588+ asset_values in ( 1usize ..10 ) . prop_flat_map( |n_values|
589+ arb_note_value_bounded( MAX_NOTE_VALUE / n_values as u64 ) . prop_flat_map( move |bound|
576590 prop:: collection:: vec( ( arb_value_sum_bounded( bound) , arb_trapdoor( ) , arb_asset_base( ) ) , n_values)
577- ) , prop :: collection :: vec ( arb_trapdoor ( ) , n_values ) )
591+ )
578592 ) ,
579593 burn_values in ( 1usize ..10 ) . prop_flat_map( |n_values|
580594 arb_note_value_bounded( MAX_NOTE_VALUE / n_values as u64 )
581595 . prop_flat_map( move |bound| prop:: collection:: vec( ( arb_value_sum_bounded( bound) , arb_trapdoor( ) , arb_asset_base( ) ) , n_values) )
582596 )
583597 ) {
584- check_binding_signature( & native_values, & [ ] , & [ ] , & [ ] ) ;
585- check_binding_signature( & native_values, & [ ] , & [ ] , & burn_values) ;
586- check_binding_signature( & native_values, & asset_values, & neg_trapdoors , & [ ] ) ;
587- check_binding_signature( & native_values, & asset_values, & neg_trapdoors , & burn_values) ;
598+ check_binding_signature( & native_values, & [ ] , & [ ] ) ;
599+ check_binding_signature( & native_values, & [ ] , & burn_values) ;
600+ check_binding_signature( & native_values, & asset_values, & [ ] ) ;
601+ check_binding_signature( & native_values, & asset_values, & burn_values) ;
588602 }
589603 }
590604}
0 commit comments