Skip to content

Commit 1f720fe

Browse files
committed
Merge #307: pset: check input secrets against the inputs before blinding
7a951f1 pset: test the input secrets checks in blind_checks (Kyryl R) 1238c23 pset: check input secrets against the inputs before blinding (Kyryl R) Pull request description: Added checks mentioned here: #301 (comment) Now blind_last and blind_non_last would throw an error if the input secrets given for an input index is out of bounds or input secrets do not open the commitments of the input's witness utxo ACKs for top commit: apoelstra: ACK 7a951f1; successfully ran local tests Tree-SHA512: 38d7b2237e033d8ac2ff5ceb06070b10275af0aa3909824ac8d9da248338745ac266a96ea64bfd9322257692b71a7f044e1554d8d2579f9846b2412e07d44b93
2 parents c4736ee + 7a951f1 commit 1f720fe

3 files changed

Lines changed: 98 additions & 4 deletions

File tree

‎src/blind.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,42 @@ impl TxOutSecrets {
388388
pub fn value_blind_inputs(&self) -> (u64, AssetBlindingFactor, ValueBlindingFactor) {
389389
(self.value, self.asset_bf, self.value_bf)
390390
}
391+
392+
/// Checks if secrets "open" the asset and value commitments of `txout`.
393+
///
394+
/// An explicit asset or value is only opened by a zero blinding factor.
395+
pub(crate) fn opens<C: Signing>(&self, secp: &Secp256k1<C>, txout: &TxOut) -> bool {
396+
let asset_opens = match txout.asset {
397+
Asset::Null => false,
398+
Asset::Explicit(asset) => {
399+
asset == self.asset && self.asset_bf == AssetBlindingFactor::zero()
400+
}
401+
Asset::Confidential(_) => {
402+
txout.asset == Asset::new_confidential(secp, self.asset, self.asset_bf)
403+
}
404+
};
405+
406+
let value_opens = match txout.value {
407+
Value::Null => false,
408+
Value::Explicit(value) => {
409+
value == self.value && self.value_bf == ValueBlindingFactor::zero()
410+
}
411+
Value::Confidential(_) => {
412+
// Committing to a zero value with a zero blinding factor would panic
413+
(self.value != 0 || self.value_bf != ValueBlindingFactor::zero())
414+
&& txout.value
415+
== Value::new_confidential_from_assetid(
416+
secp,
417+
self.value,
418+
self.asset,
419+
self.value_bf,
420+
self.asset_bf,
421+
)
422+
}
423+
};
424+
425+
asset_opens && value_opens
426+
}
391427
}
392428

393429
/// Data structure used to provide inputs to [`SurjectionProof`] methods.

‎src/pset/error.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ pub enum PsetBlindError {
216216
BlindingProofsCreationError(usize, secp256k1_zkp::Error),
217217
/// Blinding issuance unsupported
218218
BlindingIssuanceUnsupported(usize),
219+
/// Input secrets given for an input index which is out of bounds
220+
InputSecretsOutOfBounds(usize),
221+
/// Input secrets do not open the commitments of the input's witness utxo
222+
InputSecretsMismatch(usize),
219223
}
220224

221225
impl fmt::Display for PsetBlindError {
@@ -260,6 +264,12 @@ impl fmt::Display for PsetBlindError {
260264
PsetBlindError::BlindingIssuanceUnsupported(i) => {
261265
write!(f, "Blinding issuance is not supported, set blinded_issuance to 0 at input index {}", i)
262266
}
267+
PsetBlindError::InputSecretsOutOfBounds(i) => {
268+
write!(f, "Secrets given for input index {}, which is out of bounds", i)
269+
}
270+
PsetBlindError::InputSecretsMismatch(i) => {
271+
write!(f, "Secrets for input index {} do not open its witness utxo", i)
272+
}
263273
}
264274
}
265275
}

‎src/pset/mod.rs‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)