add public key negation support

This commit is contained in:
Craig Raw 2025-09-11 18:16:25 +02:00
parent 6c7662ca09
commit 1623f923b3
2 changed files with 8 additions and 4 deletions

View file

@ -348,6 +348,11 @@ public class ECKey {
return ECKey.fromPublicOnly(point, compressed);
}
/** Negate the provided public key */
public ECKey negate() {
return ECKey.fromPublicOnly(getPubKeyPoint().negate().normalize(), isCompressed());
}
/** Add to the private key by the provided private key using modular arithmetic */
public ECKey addPrivate(ECKey privKey) {
if(this.priv == null || privKey.priv == null) {
@ -358,7 +363,7 @@ public class ECKey {
}
/** Negate the provided private key */
public ECKey negate() {
public ECKey negatePrivate() {
if(priv == null) {
throw new IllegalStateException("Key did not contain a private key");
}

View file

@ -241,7 +241,7 @@ public class SilentPaymentUtils {
try {
ECKey privateKey = walletNode.getWallet().getKeystores().getFirst().getKey(walletNode);
if(walletNode.getWallet().getScriptType() == P2TR && !privateKey.getPubKeyPoint().getYCoord().toBigInteger().mod(BigInteger.TWO).equals(BigInteger.ZERO)) {
privateKey = privateKey.negate();
privateKey = privateKey.negatePrivate();
}
if(summedPrivateKey == null) {
summedPrivateKey = privateKey;
@ -266,8 +266,7 @@ public class SilentPaymentUtils {
public static byte[] getSmallestOutpoint(Set<BlockTransactionHashIndex> outpoints) {
return outpoints.stream().map(outpoint -> new TransactionOutPoint(outpoint.getHash(), outpoint.getIndex())).map(TransactionOutPoint::bitcoinSerialize)
.sorted(new Utils.LexicographicByteArrayComparator())
.findFirst().orElseThrow(() -> new IllegalArgumentException("No inputs provided to calculate silent payments input hash"));
.min(new Utils.LexicographicByteArrayComparator()).orElseThrow(() -> new IllegalArgumentException("No inputs provided to calculate silent payments input hash"));
}
public static ECKey getLabelledSpendKey(ECKey scanPrivateKey, ECKey spendPublicKey, int labelIndex) {