-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
secp256k1-bitcoinj: Move WitnessMaker to src/main from src/test
- Loading branch information
1 parent
5e15025
commit bf0b94c
Showing
3 changed files
with
71 additions
and
49 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
secp256k1-bitcoinj/src/main/java/org/bitcoinj/secp256k1/bitcoinj/WitnessMaker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.bitcoinj.secp256k1.bitcoinj; | ||
|
||
import org.bitcoinj.secp256k1.api.P256K1XOnlyPubKey; | ||
import org.bitcoinj.secp256k1.api.P256k1PubKey; | ||
import org.bitcoinj.secp256k1.api.Secp256k1; | ||
import org.bitcoinj.secp256k1.foreign.PubKeyPojo; | ||
|
||
import java.math.BigInteger; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
/** | ||
* Experimental class for making P2TR witness programs | ||
*/ | ||
public class WitnessMaker { | ||
/** | ||
* 64-byte concatenation of two 32-byte hashes of "TapTweak" | ||
*/ | ||
private static final byte[] tweakPrefix = calcTagPrefix64("TapTweak"); | ||
private final Secp256k1 secp; | ||
|
||
public WitnessMaker(Secp256k1 secp) { | ||
this.secp = secp; | ||
} | ||
|
||
public byte[] calcWitnessProgram(P256k1PubKey pubKey) { | ||
P256K1XOnlyPubKey xOnlyKey = pubKey.getXOnly(); | ||
BigInteger tweakInt = calcTweak(xOnlyKey); | ||
P256k1PubKey G = new PubKeyPojo(Secp256k1.EC_PARAMS.getGenerator()); | ||
P256k1PubKey P2 = secp.ecPubKeyTweakMul(G, tweakInt); | ||
P256k1PubKey Q = secp.ecPubKeyCombine(pubKey, P2); | ||
return Q.getXOnly().getSerialized(); | ||
} | ||
|
||
public static BigInteger calcTweak(P256K1XOnlyPubKey xOnlyPubKey) { | ||
var digest = newDigest(); | ||
digest.update(tweakPrefix); | ||
byte[] hash = digest.digest(xOnlyPubKey.getSerialized()); | ||
return new BigInteger(1, hash); | ||
} | ||
|
||
public static byte[] calcTagPrefix64(String tag) { | ||
byte[] hash = hash256(tag.getBytes(StandardCharsets.UTF_8)); | ||
return ByteBuffer.allocate(64) | ||
.put(hash) | ||
.put(hash) | ||
.array(); | ||
} | ||
|
||
private static byte[] hash256(byte[] message) { | ||
return newDigest().digest(message); | ||
} | ||
|
||
private static MessageDigest newDigest() { | ||
try { | ||
return MessageDigest.getInstance("SHA-256"); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); // Can't happen. | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
secp256k1-bitcoinj/src/main/java/org/bitcoinj/secp256k1/bitcoinj/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* This module contains experimental utility classes for integrating {@code secp256k1-jdk} with <b>bitcoinj</b>. | ||
* If/when a future version of bitcoinj supports secp256k1-jdk and/or Schnorr signatures, this package will likely | ||
* no longer be needed and will be deprecated. | ||
*/ | ||
package org.bitcoinj.secp256k1.bitcoinj; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters