|
1 | 1 | package fr.acinq.bitcoin.crypto.musig2 |
2 | 2 |
|
3 | 3 | import fr.acinq.bitcoin.* |
| 4 | +import fr.acinq.bitcoin.utils.Either |
4 | 5 | import fr.acinq.secp256k1.Hex |
5 | 6 | import kotlinx.serialization.json.* |
6 | 7 | import kotlin.random.Random |
@@ -76,7 +77,7 @@ class Musig2TestsCommon { |
76 | 77 | val expectedPubnonce = IndividualNonce(it.jsonObject["expected_pubnonce"]!!.jsonPrimitive.content) |
77 | 78 | // secp256k1 only supports signing 32-byte messages (when provided), which excludes some of the test vectors. |
78 | 79 | if (msg == null || msg.size == 32) { |
79 | | - val (secnonce, pubnonce) = SecretNonce.generate(randprime, sk, pk, msg?.byteVector32(), keyagg, extraInput?.byteVector32()) |
| 80 | + val (secnonce, pubnonce) = SecretNonce.generate(randprime, sk?.let { Either.Left(it) } ?: Either.Right(pk), msg?.byteVector32(), keyagg, extraInput?.byteVector32()) |
80 | 81 | assertEquals(expectedPubnonce, pubnonce) |
81 | 82 | assertEquals(expectedSecnonce, secnonce) |
82 | 83 | } |
@@ -278,7 +279,7 @@ class Musig2TestsCommon { |
278 | 279 | } |
279 | 280 |
|
280 | 281 | // Generate secret nonces for each participant. |
281 | | - val nonces = privkeys.map { SecretNonce.generate(Random.Default.nextBytes(32).byteVector32(), it, it.publicKey(), message = null, keyAggCache, extraInput = null) } |
| 282 | + val nonces = privkeys.map { SecretNonce.generate(Random.Default.nextBytes(32).byteVector32(), Either.Left(it), message = null, keyAggCache, extraInput = null) } |
282 | 283 | val secnonces = nonces.map { it.first } |
283 | 284 | val pubnonces = nonces.map { it.second } |
284 | 285 |
|
@@ -316,8 +317,8 @@ class Musig2TestsCommon { |
316 | 317 |
|
317 | 318 | // The first step of a musig2 signing session is to exchange nonces. |
318 | 319 | // If participants are disconnected before the end of the signing session, they must start again with fresh nonces. |
319 | | - val aliceNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), alicePrivKey, alicePrivKey.publicKey(), listOf(alicePubKey, bobPubKey), null, null) |
320 | | - val bobNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), bobPrivKey, bobPrivKey.publicKey(), listOf(alicePubKey, bobPubKey), null, null) |
| 320 | + val aliceNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), Either.Left(alicePrivKey), listOf(alicePubKey, bobPubKey), null, null) |
| 321 | + val bobNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), Either.Left(bobPrivKey), listOf(alicePubKey, bobPubKey), null, null) |
321 | 322 |
|
322 | 323 | // Once they have each other's public nonce, they can produce partial signatures. |
323 | 324 | val publicNonces = listOf(aliceNonce.second, bobNonce.second) |
@@ -354,8 +355,8 @@ class Musig2TestsCommon { |
354 | 355 | val tx = Transaction(2, listOf(), listOf(TxOut(10_000.sat(), Script.pay2tr(commonPubKey))), 0) |
355 | 356 | val spendingTx = Transaction(2, listOf(TxIn(OutPoint(tx, 0), sequence = 0)), listOf(TxOut(10_000.sat(), Script.pay2wpkh(alicePubKey))), 0) |
356 | 357 |
|
357 | | - val aliceNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), alicePrivKey, alicePrivKey.publicKey(),listOf(alicePubKey, bobPubKey), null, null) |
358 | | - val bobNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), bobPrivKey, bobPrivKey.publicKey(), listOf(alicePubKey, bobPubKey), null, null) |
| 358 | + val aliceNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), Either.Left(alicePrivKey), listOf(alicePubKey, bobPubKey), null, null) |
| 359 | + val bobNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), Either.Left(bobPrivKey), listOf(alicePubKey, bobPubKey), null, null) |
359 | 360 | val publicNonces = listOf(aliceNonce.second, bobNonce.second) |
360 | 361 |
|
361 | 362 | val aliceSig = Musig2.signTaprootInput(alicePrivKey, spendingTx, 0, listOf(tx.txOut[0]), listOf(alicePubKey, bobPubKey), aliceNonce.first, publicNonces, scriptTree = null).right |
@@ -411,8 +412,8 @@ class Musig2TestsCommon { |
411 | 412 | ) |
412 | 413 | // The first step of a musig2 signing session is to exchange nonces. |
413 | 414 | // If participants are disconnected before the end of the signing session, they must start again with fresh nonces. |
414 | | - val userNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), userPrivateKey, userPrivateKey.publicKey(), listOf(userPublicKey, serverPublicKey), null, null) |
415 | | - val serverNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), serverPrivateKey, serverPrivateKey.publicKey(), listOf(userPublicKey, serverPublicKey), null, null) |
| 415 | + val userNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), Either.Left(userPrivateKey), listOf(userPublicKey, serverPublicKey), null, null) |
| 416 | + val serverNonce = Musig2.generateNonce(Random.Default.nextBytes(32).byteVector32(), Either.Left(serverPrivateKey), listOf(userPublicKey, serverPublicKey), null, null) |
416 | 417 |
|
417 | 418 | // Once they have each other's public nonce, they can produce partial signatures. |
418 | 419 | val publicNonces = listOf(userNonce.second, serverNonce.second) |
|
0 commit comments