mirror of
https://github.com/sparrowwallet/drongo.git
synced 2025-11-05 11:56:38 +00:00
Clean code and remove debug logs from ECKey.java
This commit is contained in:
parent
77901915b7
commit
2c2db1d644
1 changed files with 8 additions and 86 deletions
|
|
@ -307,7 +307,9 @@ public class ECKey {
|
||||||
return pub.getEncoded();
|
return pub.getEncoded();
|
||||||
}
|
}
|
||||||
|
|
||||||
// SATOCHIP
|
/**
|
||||||
|
* Gets the raw public key value in compressed or uncompressed form. This is needed by Satochip
|
||||||
|
*/
|
||||||
public byte[] getPubKey(Boolean compressed) {
|
public byte[] getPubKey(Boolean compressed) {
|
||||||
return pub.getEncoded(compressed);
|
return pub.getEncoded(compressed);
|
||||||
}
|
}
|
||||||
|
|
@ -433,39 +435,17 @@ public class ECKey {
|
||||||
return verify(sigHash.getBytes(), signature);
|
return verify(sigHash.getBytes(), signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ECKey getTweakedOutputKeyNEW() {
|
||||||
public ECKey getTweakedOutputKey() {
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey START");
|
|
||||||
TaprootPubKey taprootPubKey = liftX(getPubKeyXCoord());
|
TaprootPubKey taprootPubKey = liftX(getPubKeyXCoord());
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey taprootPubKey: " + taprootPubKey);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey taprootPubKey.ecPoint: " + taprootPubKey.ecPoint);
|
|
||||||
ECPoint internalKey = taprootPubKey.ecPoint;
|
ECPoint internalKey = taprootPubKey.ecPoint;
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey internalKey: " + internalKey);
|
|
||||||
//debug
|
|
||||||
ECKey tmp2 = ECKey.fromPublicOnly(internalKey, true);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey internalKey: " + Utils.bytesToHex(tmp2.getPubKey()));
|
|
||||||
//endbug
|
|
||||||
|
|
||||||
byte[] taggedHash = Utils.taggedHash("TapTweak", internalKey.getXCoord().getEncoded());
|
byte[] taggedHash = Utils.taggedHash("TapTweak", internalKey.getXCoord().getEncoded());
|
||||||
ECKey tweakValue = ECKey.fromPrivate(taggedHash);
|
ECKey tweakValue = ECKey.fromPrivate(taggedHash);
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey tweakValue: " + Utils.bytesToHex(tweakValue.getPubKey()));
|
|
||||||
ECPoint outputKey = internalKey.add(tweakValue.getPubKeyPoint());
|
ECPoint outputKey = internalKey.add(tweakValue.getPubKeyPoint());
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey: " + outputKey);
|
|
||||||
|
|
||||||
//debug
|
|
||||||
ECKey tmp = ECKey.fromPublicOnly(outputKey, true);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey: " + Utils.bytesToHex(tmp.getPubKey()));
|
|
||||||
//endbug
|
|
||||||
|
|
||||||
if(hasPrivKey()) {
|
if(hasPrivKey()) {
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey PRIVKEY NEW VERSION");
|
|
||||||
|
|
||||||
// isEven => used to determine private key for tweaking
|
// isEven => used to determine private key for tweaking
|
||||||
|
// see taproot_tweak_seckey(seckey0, h) in
|
||||||
|
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#constructing-and-spending-taproot-outputs
|
||||||
Boolean isEven = (getPubKey()[0] == 0x02);
|
Boolean isEven = (getPubKey()[0] == 0x02);
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey getPubKey(): " + Utils.bytesToHex(getPubKey()));
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey isEven getPubKey()[0]: " + getPubKey()[0]);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey isEven: " + isEven);
|
|
||||||
|
|
||||||
BigInteger taprootPriv;
|
BigInteger taprootPriv;
|
||||||
if (isEven){
|
if (isEven){
|
||||||
taprootPriv = priv;
|
taprootPriv = priv;
|
||||||
|
|
@ -473,79 +453,21 @@ public class ECKey {
|
||||||
taprootPriv = CURVE_PARAMS.getCurve().getOrder().subtract(priv);
|
taprootPriv = CURVE_PARAMS.getCurve().getOrder().subtract(priv);
|
||||||
}
|
}
|
||||||
BigInteger tweakedPrivKey = taprootPriv.add(tweakValue.getPrivKey()).mod(CURVE_PARAMS.getCurve().getOrder());
|
BigInteger tweakedPrivKey = taprootPriv.add(tweakValue.getPrivKey()).mod(CURVE_PARAMS.getCurve().getOrder());
|
||||||
|
|
||||||
//debug
|
|
||||||
ECKey tmp3 = new ECKey(tweakedPrivKey, outputKey, true);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey with private: " + Utils.bytesToHex(tmp3.getPubKey()));
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey private: " + tmp3.getPrivKey());
|
|
||||||
//endbug
|
|
||||||
|
|
||||||
|
|
||||||
/* log.debug("SATOCHIP ECKey getTweakedOutputKey PRIVKEY NEW VERSION SWITCH EVENNESS");
|
|
||||||
if (isEven){
|
|
||||||
//taprootPriv = priv;
|
|
||||||
taprootPriv = CURVE_PARAMS.getCurve().getOrder().subtract(priv);
|
|
||||||
} else {
|
|
||||||
taprootPriv = priv;
|
|
||||||
//taprootPriv = CURVE_PARAMS.getCurve().getOrder().subtract(priv);
|
|
||||||
}
|
|
||||||
tweakedPrivKey = taprootPriv.add(tweakValue.getPrivKey()).mod(CURVE_PARAMS.getCurve().getOrder());
|
|
||||||
|
|
||||||
//debug
|
|
||||||
ECKey tmp5 = new ECKey(tweakedPrivKey, outputKey, true);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey with private: " + Utils.bytesToHex(tmp5.getPubKey()));
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey private: " + tmp5.getPrivKey());
|
|
||||||
//endbug*/
|
|
||||||
|
|
||||||
|
|
||||||
/* // ORIGNAL VERSION
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey PRIVKEY OLD VERSION");
|
|
||||||
taprootPriv = priv;
|
|
||||||
tweakedPrivKey = taprootPriv.add(tweakValue.getPrivKey()).mod(CURVE_PARAMS.getCurve().getOrder());
|
|
||||||
//TODO: Improve on this hack. How do we know whether to negate the private key before tweaking it?
|
|
||||||
if(!ECKey.fromPrivate(tweakedPrivKey).getPubKeyPoint().equals(outputKey)) {
|
|
||||||
taprootPriv = CURVE_PARAMS.getCurve().getOrder().subtract(priv);
|
|
||||||
tweakedPrivKey = taprootPriv.add(tweakValue.getPrivKey()).mod(CURVE_PARAMS.getCurve().getOrder());
|
|
||||||
}
|
|
||||||
//debug
|
|
||||||
ECKey tmp4 = new ECKey(tweakedPrivKey, outputKey, true);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey with private: " + Utils.bytesToHex(tmp4.getPubKey()));
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey private: " + tmp4.getPrivKey());
|
|
||||||
//endbug*/
|
|
||||||
|
|
||||||
|
|
||||||
return new ECKey(tweakedPrivKey, outputKey, true);
|
return new ECKey(tweakedPrivKey, outputKey, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ECKey.fromPublicOnly(outputKey, true);
|
return ECKey.fromPublicOnly(outputKey, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ECKey getTweakedOutputKey() {
|
||||||
public ECKey getTweakedOutputKeyOLD() {
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey START");
|
|
||||||
TaprootPubKey taprootPubKey = liftX(getPubKeyXCoord());
|
TaprootPubKey taprootPubKey = liftX(getPubKeyXCoord());
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey taprootPubKey: " + taprootPubKey);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey taprootPubKey.ecPoint: " + taprootPubKey.ecPoint);
|
|
||||||
ECPoint internalKey = taprootPubKey.ecPoint;
|
ECPoint internalKey = taprootPubKey.ecPoint;
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey internalKey: " + internalKey);
|
|
||||||
//debug
|
|
||||||
ECKey tmp2 = ECKey.fromPublicOnly(internalKey, true);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey: " + Utils.bytesToHex(tmp2.getPubKey()));
|
|
||||||
//endbug
|
|
||||||
|
|
||||||
byte[] taggedHash = Utils.taggedHash("TapTweak", internalKey.getXCoord().getEncoded());
|
byte[] taggedHash = Utils.taggedHash("TapTweak", internalKey.getXCoord().getEncoded());
|
||||||
ECKey tweakValue = ECKey.fromPrivate(taggedHash);
|
ECKey tweakValue = ECKey.fromPrivate(taggedHash);
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey tweakValue: " + Utils.bytesToHex(tweakValue.getPubKey()));
|
|
||||||
ECPoint outputKey = internalKey.add(tweakValue.getPubKeyPoint());
|
ECPoint outputKey = internalKey.add(tweakValue.getPubKeyPoint());
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey: " + outputKey);
|
|
||||||
|
|
||||||
//debug
|
|
||||||
ECKey tmp = ECKey.fromPublicOnly(outputKey, true);
|
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey outputKey: " + Utils.bytesToHex(tmp.getPubKey()));
|
|
||||||
//endbug
|
|
||||||
|
|
||||||
if(hasPrivKey()) {
|
if(hasPrivKey()) {
|
||||||
log.debug("SATOCHIP ECKey getTweakedOutputKey hasPrivKey(): true");
|
|
||||||
BigInteger taprootPriv = priv;
|
BigInteger taprootPriv = priv;
|
||||||
BigInteger tweakedPrivKey = taprootPriv.add(tweakValue.getPrivKey()).mod(CURVE_PARAMS.getCurve().getOrder());
|
BigInteger tweakedPrivKey = taprootPriv.add(tweakValue.getPrivKey()).mod(CURVE_PARAMS.getCurve().getOrder());
|
||||||
//TODO: Improve on this hack. How do we know whether to negate the private key before tweaking it?
|
//TODO: Improve on this hack. How do we know whether to negate the private key before tweaking it?
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue