From a67a2df3304d3d02e6d83d5fbf501ffee41a8624 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Fri, 1 Oct 2021 18:12:36 +0000 Subject: [PATCH] pset: remove one more intermediate-zero check from the blinding logic --- src/blindpsbt.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/blindpsbt.cpp b/src/blindpsbt.cpp index 1b4a30f3a8..eae28f8562 100644 --- a/src/blindpsbt.cpp +++ b/src/blindpsbt.cpp @@ -311,8 +311,17 @@ bool ComputeAndAddToScalarOffset(uint256& a, CAmount value, const uint256& asset if (a.IsNull()) { a = scalar; } else { - // If we have a, then add the scalar to it. - if (secp256k1_ec_privkey_tweak_add(secp256k1_blind_context, a.begin(), scalar.begin()) != 1) return false; + uint256 scalar_negated = scalar; + if (secp256k1_ec_seckey_negate(secp256k1_blind_context, scalar_negated.begin()) != 1) { + return false; + } + // Special-case zero, which would otherwise cause `secp256k1_ec_privkey_tweak_add` to fail + if (scalar_negated == a) { + a = uint256{}; + } else { + // If we have a, then add the scalar to it. + if (secp256k1_ec_privkey_tweak_add(secp256k1_blind_context, a.begin(), scalar.begin()) != 1) return false; + } } return true; }