Merge 6eed792d43 into merged_master (Elements PR ElementsProject/elements#1050)

Conflicts were mostly obvious, except for those in CreateTransactionInternal.
(This function was moved from wallet.cpp to spend.cpp, and substantially
rewritten, between 0.21 and 22.0.) For those I manually applied the changes
from the diff, which wound up taking a slightly different form.

Also had to update the new test because the `addresses` field of the RPC
output was removed.
This commit is contained in:
Andrew Poelstra 2021-10-02 15:28:13 +00:00
commit b64ca7f411
7 changed files with 175 additions and 16 deletions

View file

@ -263,6 +263,7 @@ bool SubtractScalars(uint256& a, const uint256& b)
// Compute the scalar offset used for the final blinder computation
// value * asset_blinder + value_blinder
// FIXME this method should be in libsecp, as should `ComputeAndAddToScalarOffset`
bool CalculateScalarOffset(uint256& out, CAmount value, const uint256& asset_blinder, const uint256& value_blinder)
{
// If the asset_blinder is 0, then the equation resolves to just the value_blinder
@ -276,8 +277,24 @@ bool CalculateScalarOffset(uint256& out, CAmount value, const uint256& asset_bli
// tweak_mul expects a 32 byte, big endian tweak.
// We need to pack the 8 byte CAmount into a uint256 with the correct padding, so start it at 24 bytes from the front
WriteBE64(val.begin() + 24, value);
if (secp256k1_ec_privkey_tweak_mul(secp256k1_blind_context, out.begin(), val.begin()) != 1) return false;
if (!value_blinder.IsNull() && secp256k1_ec_privkey_tweak_add(secp256k1_blind_context, out.begin(), value_blinder.begin()) != 1) return false;
if (value > 0) {
if (secp256k1_ec_privkey_tweak_mul(secp256k1_blind_context, out.begin(), val.begin()) != 1) return false;
} else {
out = value_blinder;
return true;
}
if (!value_blinder.IsNull()) {
uint256 value_negated = value_blinder;
if (secp256k1_ec_seckey_negate(secp256k1_blind_context, value_negated.begin()) != 1) {
return false;
}
// Special-case zero, which would otherwise cause `secp256k1_ec_privkey_tweak_add` to fail
if (value_negated == out) {
out = uint256{};
return true;
}
if (secp256k1_ec_privkey_tweak_add(secp256k1_blind_context, out.begin(), value_blinder.begin()) != 1) return false;
}
return true;
}
@ -294,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;
}