Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
oxf71 committed Jan 9, 2024
1 parent f3e2b3c commit b405a25
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 50 deletions.
125 changes: 81 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,97 @@ sig opt

## usage


early nonce gen

```go
privKey1, _ := btcec.PrivKeyFromBytes(decodeHex(bip340TestVectors[0].secretKey))
privKey2, _ := btcec.PrivKeyFromBytes(decodeHex(bip340TestVectors[1].secretKey))

musig2_early_demo()
msg := sha256.Sum256([]byte("msg hello"))
sign, hash, err := musig2demo.TwoPrivSign2(privKey1, privKey2, msg)
if err != nil {
log.Fatal(err)
}
fmt.Println("sign: ", hex.EncodeToString(sign.Serialize()))
fmt.Println("sign hash", hex.EncodeToString(hash))

btcAddress := musig2demo.TwoBtcAddress(privKey1, privKey2)
fmt.Println("btcAddress: ", btcAddress)

taprootAddress := musig2demo.TwoBtcTaprootAddress(privKey1, privKey2)
fmt.Println("taprootAddress: ", taprootAddress)

// verify sign

combinedKey, err := musig2demo.TwoCombinedKey(privKey1, privKey2)
if err != nil {
log.Fatal(err)
}

fmt.Println("combinedKey", combinedKey.SerializeCompressed())

fmt.Println("sign verify: ", sign.Verify(hash, combinedKey))

```


taproot_tweaked_x_only

```go
privKey1, _ := btcec.PrivKeyFromBytes(decodeHex("440bb3ec56d213e90d006d344d74f6478db4f7fa4cdd388095d8f4edef0c5156"))
privKey2, _ := btcec.PrivKeyFromBytes(decodeHex("e0087817fd1d1154a781c11b394a0dcec82f076bbf026df9d61667ead16fa778"))
testTweak := [32]byte{
privKey1, publicKey1 := btcec.PrivKeyFromBytes(decodeHex("440bb3ec56d213e90d006d344d74f6478db4f7fa4cdd388095d8f4edef0c5156"))
privKey2, publicKey2 := btcec.PrivKeyFromBytes(decodeHex("e0087817fd1d1154a781c11b394a0dcec82f076bbf026df9d61667ead16fa778"))
_ = [32]byte{
0xE8, 0xF7, 0x91, 0xFF, 0x92, 0x25, 0xA2, 0xAF,
0x01, 0x02, 0xAF, 0xFF, 0x4A, 0x9A, 0x72, 0x3D,
0x96, 0x12, 0xA6, 0x82, 0xA2, 0x5E, 0xBE, 0x79,
0x80, 0x2B, 0x26, 0x3C, 0xDF, 0xCD, 0x83, 0xBB,
}

priv := []*btcec.PrivateKey{privKey1, privKey2}
msg := sha256.Sum256([]byte("msg hello"))
sign, combinedKey, hash, err := musig2demo.MultiPartySign(priv, testTweak[:], msg)
allSignerPubKeys := []*btcec.PublicKey{publicKey1, publicKey2}

muSig2Tweaks := musig2demo.MuSig2Tweaks{
TaprootBIP0086Tweak: false,
// TaprootTweak: testTweak[:],
GenericTweaks: []musig2.KeyTweakDesc{},
}

// sign msg
msg := sha256.Sum256([]byte("msg hello"))
nonce2chan := make(chan [musig2.PubNonceSize]byte, 10)
nonce1chan := make(chan [musig2.PubNonceSize]byte, 10)
partialSignature2 := make(chan musig2.PartialSignature)
finalSig := make(chan schnorr.Signature, 10)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
// priv1
_, session1, err := musig2demo.MuSig2CreateContext(privKey1, allSignerPubKeys, &muSig2Tweaks)
if err != nil {
t.Fatal(err)
}
nonce1 := session1.PublicNonce()

nonce1chan <- nonce1

nonce2 := <-nonce2chan
_, err = session1.RegisterPubNonce(nonce2)
if err != nil {
t.Fatal(err)
}

_, err = musig2demo.MuSig2Sign(session1, msg, false)
if err != nil {
t.Fatal(err)
}
partial2 := <-partialSignature2

_, err = musig2demo.MuSig2CombineSig(session1, &partial2)
if err != nil {
t.Fatal(err)
}
finalSig <- *musig2demo.MuSig2FinalSig(session1)
}()

go func() {
// priv2
_, session2, err := musig2demo.MuSig2CreateContext(privKey2, allSignerPubKeys, &muSig2Tweaks)
if err != nil {
t.Fatal(err)
}
nonce2 := session2.PublicNonce()

nonce2chan <- nonce2

nonce1 := <-nonce1chan
_, err = session2.RegisterPubNonce(nonce1)
if err != nil {
t.Fatal(err)
}

partial2, err := musig2demo.MuSig2Sign(session2, msg, false)
if err != nil {
t.Fatal(err)
}

partialSignature2 <- *partial2
wg.Done()
}()
wg.Wait()

sig := <-finalSig

combinedKey, err := musig2demo.MuSig2CombineKeys(allSignerPubKeys, false, &muSig2Tweaks)
if err != nil {
t.Fatal(err)
}
if !sig.Verify(msg[:], combinedKey.FinalKey) {
t.Fatal("invalid signature")
}
```


Expand Down
100 changes: 94 additions & 6 deletions lib/go-ord-tx/pkg/ord/ord.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"encoding/json"
"fmt"
"log"
"sync"

"github.com/oxf71/musig2-demo/lib/go-ord-tx/pkg/btcapi"
extRpcClient "github.com/oxf71/musig2-demo/lib/go-ord-tx/pkg/rpcclient"
"github.com/oxf71/musig2-demo/musig2"
musig2demo "github.com/oxf71/musig2-demo/musig2"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
Expand Down Expand Up @@ -155,8 +157,17 @@ func createInscriptionTxCtxData(net *chaincfg.Params,
// return nil, err
// }
//

publicKey, _ := musig2.TwoCombinedKey(musigPriv[0], musigPriv[1])
muSig2Tweaks := musig2demo.MuSig2Tweaks{
TaprootBIP0086Tweak: false,
// TaprootTweak: testTweak[:],
// GenericTweaks: []musig2.KeyTweakDesc{},
}
allSignerPubKeys := []*btcec.PublicKey{musigPriv[0].PubKey(), musigPriv[1].PubKey()}
combinedKey, err := musig2demo.MuSig2CombineKeys(allSignerPubKeys, false, &muSig2Tweaks)
if err != nil {
return nil, err
}
publicKey := combinedKey.FinalKey

inscriptionBuilder := txscript.NewScriptBuilder().
AddData(schnorr.SerializePubKey(publicKey)).
Expand Down Expand Up @@ -391,12 +402,89 @@ func (tool *InscriptionTool) completeRevealTx() error {
revealTx = tool.revealTx[i]
idx = 0
}
witnessArray, err := txscript.CalcTapscriptSignaturehash(txscript.NewTxSigHashes(revealTx, tool.revealTxPrevOutputFetcher),
txscript.SigHashDefault, revealTx, idx, tool.revealTxPrevOutputFetcher, txscript.NewBaseTapLeaf(tool.txCtxDataList[i].inscriptionScript))
witnessArray, err := txscript.CalcTapscriptSignaturehash(
txscript.NewTxSigHashes(revealTx, tool.revealTxPrevOutputFetcher),
txscript.SigHashDefault,
revealTx,
idx,
tool.revealTxPrevOutputFetcher,
txscript.NewBaseTapLeaf(tool.txCtxDataList[i].inscriptionScript))
if err != nil {
return err
}
signature, _, _ := musig2.TwoPrivSign2(tool.txCtxDataList[i].privateKey[0], tool.txCtxDataList[i].privateKey[1], [32]byte(witnessArray))

muSig2Tweaks := musig2demo.MuSig2Tweaks{
TaprootBIP0086Tweak: false,
// TaprootTweak: testTweak[:],
// GenericTweaks: []musig2.KeyTweakDesc{},
}
allSignerPubKeys := []*btcec.PublicKey{tool.txCtxDataList[i].privateKey[0].PubKey(), tool.txCtxDataList[i].privateKey[1].PubKey()}
// sign msg
nonce2chan := make(chan [musig2.PubNonceSize]byte, 10)
nonce1chan := make(chan [musig2.PubNonceSize]byte, 10)
partialSignature2 := make(chan musig2.PartialSignature)
finalSig := make(chan schnorr.Signature, 10)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
// priv1
_, session1, err := musig2demo.MuSig2CreateContext(tool.txCtxDataList[i].privateKey[0], allSignerPubKeys, &muSig2Tweaks)
if err != nil {
log.Fatal(err)
}
nonce1 := session1.PublicNonce()

nonce1chan <- nonce1

nonce2 := <-nonce2chan
_, err = session1.RegisterPubNonce(nonce2)
if err != nil {
log.Fatal(err)
}

_, err = musig2demo.MuSig2Sign(session1, [32]byte(witnessArray), false)
if err != nil {
log.Fatal(err)
}
partial2 := <-partialSignature2

_, err = musig2demo.MuSig2CombineSig(session1, &partial2)
if err != nil {
log.Fatal(err)
}
finalSig <- *musig2demo.MuSig2FinalSig(session1)
}()

go func() {
// priv2
_, session2, err := musig2demo.MuSig2CreateContext(tool.txCtxDataList[i].privateKey[1], allSignerPubKeys, &muSig2Tweaks)
if err != nil {
log.Fatal(err)
}
nonce2 := session2.PublicNonce()

nonce2chan <- nonce2

nonce1 := <-nonce1chan
_, err = session2.RegisterPubNonce(nonce1)
if err != nil {
log.Fatal(err)
}

partial2, err := musig2demo.MuSig2Sign(session2, [32]byte(witnessArray), false)
if err != nil {
log.Fatal(err)
}

partialSignature2 <- *partial2
wg.Done()
}()
wg.Wait()

signature := <-finalSig

// signature, _, _ := musig2.TwoPrivSign2(tool.txCtxDataList[i].privateKey[0], tool.txCtxDataList[i].privateKey[1], [32]byte(witnessArray))

// signature, err := schnorr.Sign(tool.txCtxDataList[i].privateKey, witnessArray)
// if err != nil {
Expand Down

0 comments on commit b405a25

Please sign in to comment.