@@ -375,8 +375,9 @@ impl PartiallySignedTransaction {
375375
376376 // Common pset blinding checks
377377 #[ allow( clippy:: type_complexity) ] // FIXME we probably should actually factor out this return type
378- fn blind_checks (
378+ fn blind_checks < C : secp256k1_zkp :: Signing > (
379379 & self ,
380+ secp : & secp256k1_zkp:: Secp256k1 < C > ,
380381 inp_txout_sec : & HashMap < usize , TxOutSecrets > ,
381382 ) -> Result <
382383 (
@@ -385,11 +386,23 @@ impl PartiallySignedTransaction {
385386 ) ,
386387 PsetBlindError ,
387388 > {
389+ if let Some ( i) = inp_txout_sec. keys ( ) . copied ( ) . filter ( |& i| i >= self . inputs . len ( ) ) . min ( ) {
390+ return Err ( PsetBlindError :: InputSecretsOutOfBounds ( i) ) ;
391+ }
392+
388393 for ( i, inp) in self . inputs . iter ( ) . enumerate ( ) {
389394 if inp. has_issuance ( ) && inp. blinded_issuance . unwrap_or ( 1 ) == 1 {
390395 return Err ( PsetBlindError :: BlindingIssuanceUnsupported ( i) ) ;
391396 }
397+
398+ if let Some ( secrets) = inp_txout_sec. get ( & i) {
399+ let utxo = inp. witness_utxo . as_ref ( ) . ok_or ( PsetBlindError :: MissingWitnessUtxo ( i) ) ?;
400+ if !secrets. opens ( secp, utxo) {
401+ return Err ( PsetBlindError :: InputSecretsMismatch ( i) ) ;
402+ }
403+ }
392404 }
405+
393406 let mut blind_out_indices = Vec :: new ( ) ;
394407 for ( i, out) in self . outputs . iter ( ) . enumerate ( ) {
395408 if out. blinding_key . is_none ( ) {
@@ -477,6 +490,8 @@ impl PartiallySignedTransaction {
477490 /// 1. The `blinder_index` must be set in pset output field
478491 /// 2. the corresponding `inp_secrets`\[`out.blinder_index`\] must be present
479492 ///
493+ /// Each of `inp_secrets` must open the commitments of its input's `witness_utxo`.
494+ ///
480495 /// Issuances and re-issuance inputs are not blinded.
481496 /// # Parameters
482497 ///
@@ -490,7 +505,7 @@ impl PartiallySignedTransaction {
490505 secp : & secp256k1_zkp:: Secp256k1 < C > ,
491506 inp_txout_sec : & HashMap < usize , TxOutSecrets > ,
492507 ) -> Result < BTreeMap < CtLocation , ( AssetBlindingFactor , ValueBlindingFactor , SecretKey ) > , PsetBlindError > {
493- let ( inp_secrets, outs_to_blind) = self . blind_checks ( inp_txout_sec) ?;
508+ let ( inp_secrets, outs_to_blind) = self . blind_checks ( secp , inp_txout_sec) ?;
494509
495510 let mut ret = BTreeMap :: new ( ) ; // return all the random values used
496511 if outs_to_blind. is_empty ( ) {
@@ -582,6 +597,7 @@ impl PartiallySignedTransaction {
582597 /// blinding inputs and scalars from [`Global`] scalars field.
583598 /// `inp_secrets` and `out_secrets` must be consistent by [`Output`] `blinder_index` field
584599 /// For each output, the corresponding `inp_secrets`\[`out.blinder_index`\] must be present
600+ /// Each of `inp_secrets` must open the commitments of its input's `witness_utxo`.
585601 /// # Parameters
586602 ///
587603 /// * `inp_secrets`: [`TxOutSecrets`] corresponding to owned inputs. Use [`None`] for non-owned outputs
@@ -592,7 +608,7 @@ impl PartiallySignedTransaction {
592608 secp : & secp256k1_zkp:: Secp256k1 < C > ,
593609 inp_txout_sec : & HashMap < usize , TxOutSecrets > ,
594610 ) -> Result < BTreeMap < CtLocation , ( AssetBlindingFactor , ValueBlindingFactor , SecretKey ) > , PsetBlindError > {
595- let ( mut inp_secrets, mut outs_to_blind) = self . blind_checks ( inp_txout_sec) ?;
611+ let ( mut inp_secrets, mut outs_to_blind) = self . blind_checks ( secp , inp_txout_sec) ?;
596612
597613 let mut ret = BTreeMap :: new ( ) ;
598614 if outs_to_blind. is_empty ( ) {
@@ -1053,7 +1069,18 @@ mod tests {
10531069 value : sats_in,
10541070 asset : policy,
10551071 } ;
1056- let previous_output = TxOut :: default ( ) ; // Does not match btc_txout_secrets
1072+ let previous_output = TxOut {
1073+ asset : confidential:: Asset :: new_confidential ( & secp, policy, btc_txout_secrets. asset_bf ) ,
1074+ value : confidential:: Value :: new_confidential_from_assetid (
1075+ & secp,
1076+ sats_in,
1077+ policy,
1078+ btc_txout_secrets. value_bf ,
1079+ btc_txout_secrets. asset_bf ,
1080+ ) ,
1081+ script_pubkey : script. clone ( ) ,
1082+ ..TxOut :: default ( )
1083+ } ;
10571084 let prevout = OutPoint :: default ( ) ;
10581085 let sats_asset = 10 ;
10591086 let sats_token = 1 ;
@@ -1100,4 +1127,25 @@ mod tests {
11001127 let pset_des = encode:: deserialize ( & pset_bytes) . unwrap ( ) ;
11011128 assert_eq ! ( pset, pset_des) ;
11021129 }
1130+
1131+ #[ test]
1132+ fn pset_blind_input_secrets_checks ( ) {
1133+ let secp = secp256k1_zkp:: Secp256k1 :: new ( ) ;
1134+ let mut pset = PartiallySignedTransaction :: new_v2 ( ) ;
1135+ let mut input = Input :: from_prevout ( OutPoint :: default ( ) ) ;
1136+ input. witness_utxo = Some ( TxOut :: default ( ) ) ;
1137+ pset. add_input ( input) ;
1138+ let secrets = TxOutSecrets :: new (
1139+ crate :: AssetId :: default ( ) ,
1140+ AssetBlindingFactor :: zero ( ) ,
1141+ 0 ,
1142+ ValueBlindingFactor :: zero ( ) ,
1143+ ) ;
1144+
1145+ let err = pset. blind_checks ( & secp, & HashMap :: from ( [ ( 0 , secrets) ] ) ) . unwrap_err ( ) ;
1146+ assert_eq ! ( err, PsetBlindError :: InputSecretsMismatch ( 0 ) ) ;
1147+
1148+ let err = pset. blind_checks ( & secp, & HashMap :: from ( [ ( 1 , secrets) ] ) ) . unwrap_err ( ) ;
1149+ assert_eq ! ( err, PsetBlindError :: InputSecretsOutOfBounds ( 1 ) ) ;
1150+ }
11031151}
0 commit comments