Skip to content

Commit 4203b06

Browse files
committed
Implement secp256k1_musig_nonce_gen_counter()
1 parent 5b32d69 commit 4203b06

File tree

8 files changed

+152
-14
lines changed

8 files changed

+152
-14
lines changed

jni/c/headers/java/fr_acinq_secp256k1_Secp256k1CFunctions.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,9 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
549549
if (jpubkeys == NULL)
550550
return NULL;
551551

552-
count = (*penv)->GetArrayLength(penv, jpubkeys);
553-
CHECKRESULT(count < 1, "pubkey array cannot be empty")
554-
pubkeys = calloc(count, sizeof(secp256k1_pubkey *));
552+
count = (*penv)->GetArrayLength(penv, jpubkeys);
553+
CHECKRESULT(count < 1, "pubkey array cannot be empty")
554+
pubkeys = calloc(count, sizeof(secp256k1_pubkey *));
555555

556556
for (i = 0; i < count; i++)
557557
{
@@ -907,6 +907,80 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
907907
return jnonce;
908908
}
909909

910+
JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1musig_1nonce_1gen_1counter(JNIEnv *penv, jclass clazz, jlong jctx, jlong jcounter, jbyteArray jseckey, jbyteArray jpubkey, jbyteArray jmsg32, jbyteArray jkeyaggcache, jbyteArray jextra_input32)
911+
{
912+
secp256k1_context *ctx = (secp256k1_context *)jctx;
913+
int result = 0;
914+
size_t size;
915+
secp256k1_musig_pubnonce pubnonce;
916+
secp256k1_musig_secnonce secnonce;
917+
jbyte *pubkey_ptr;
918+
secp256k1_pubkey pubkey;
919+
unsigned char seckey[32];
920+
unsigned char msg32[32];
921+
secp256k1_musig_keyagg_cache keyaggcache;
922+
unsigned char extra_input32[32];
923+
jbyteArray jnonce;
924+
jbyte *nonce_ptr = NULL;
925+
unsigned char nonce[fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE + fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE];
926+
927+
if (jctx == 0)
928+
return NULL;
929+
930+
if (jseckey == NULL)
931+
return NULL;
932+
933+
size = (*penv)->GetArrayLength(penv, jseckey);
934+
CHECKRESULT(size != 32, "invalid private key size");
935+
copy_bytes_from_java(penv, jseckey, size, seckey);
936+
937+
if (jpubkey == NULL)
938+
return NULL;
939+
940+
size = (*penv)->GetArrayLength(penv, jpubkey);
941+
CHECKRESULT((size != 33) && (size != 65), "invalid public key size");
942+
pubkey_ptr = (*penv)->GetByteArrayElements(penv, jpubkey, 0);
943+
result = secp256k1_ec_pubkey_parse(ctx, &pubkey, (unsigned char *)pubkey_ptr, size);
944+
(*penv)->ReleaseByteArrayElements(penv, jpubkey, pubkey_ptr, 0);
945+
CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed");
946+
947+
if (jmsg32 != NULL)
948+
{
949+
size = (*penv)->GetArrayLength(penv, jmsg32);
950+
CHECKRESULT(size != 32, "invalid message size");
951+
copy_bytes_from_java(penv, jmsg32, size, msg32);
952+
}
953+
954+
if (jkeyaggcache != NULL)
955+
{
956+
size = (*penv)->GetArrayLength(penv, jkeyaggcache);
957+
CHECKRESULT(size != sizeof(secp256k1_musig_keyagg_cache), "invalid keyagg cache size");
958+
copy_bytes_from_java(penv, jkeyaggcache, size, keyaggcache.data);
959+
}
960+
961+
if (jextra_input32 != NULL)
962+
{
963+
size = (*penv)->GetArrayLength(penv, jextra_input32);
964+
CHECKRESULT(size != 32, "invalid extra input size");
965+
copy_bytes_from_java(penv, jextra_input32, size, extra_input32);
966+
}
967+
968+
result = secp256k1_musig_nonce_gen_counter(ctx, &secnonce, &pubnonce, jcounter,
969+
seckey, &pubkey,
970+
jmsg32 == NULL ? NULL : msg32, jkeyaggcache == NULL ? NULL : &keyaggcache, jextra_input32 == NULL ? NULL : extra_input32);
971+
CHECKRESULT(!result, "secp256k1_musig_nonce_gen failed");
972+
973+
memcpy(nonce, secnonce.data, fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE);
974+
result = secp256k1_musig_pubnonce_serialize(ctx, nonce + fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE, &pubnonce);
975+
CHECKRESULT(!result, "secp256k1_musig_pubnonce_serialize failed");
976+
977+
jnonce = (*penv)->NewByteArray(penv, sizeof(nonce));
978+
nonce_ptr = (*penv)->GetByteArrayElements(penv, jnonce, 0);
979+
memcpy(nonce_ptr, nonce, sizeof(nonce));
980+
(*penv)->ReleaseByteArrayElements(penv, jnonce, nonce_ptr, 0);
981+
return jnonce;
982+
}
983+
910984
void free_nonces(secp256k1_musig_pubnonce **nonces, size_t count)
911985
{
912986
size_t i;

jni/src/main/java/fr/acinq/secp256k1/Secp256k1CFunctions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public class Secp256k1CFunctions {
8989

9090
public static native int secp256k1_schnorrsig_verify(long ctx, byte[] sig, byte[] msg, byte[] pubkey);
9191

92-
public static native byte[] secp256k1_musig_nonce_gen(long ctx, byte[] session_id32, byte[] seckey, byte[] pubkey, byte[] msg32, byte[] keyagg_cache, byte[] extra_input32);
92+
public static native byte[] secp256k1_musig_nonce_gen(long ctx, byte[] session_rand32, byte[] seckey, byte[] pubkey, byte[] msg32, byte[] keyagg_cache, byte[] extra_input32);
93+
94+
public static native byte[] secp256k1_musig_nonce_gen_counter(long ctx, long nonrepeating_cnt, byte[] seckey, byte[] pubkey, byte[] msg32, byte[] keyagg_cache, byte[] extra_input32);
9395

9496
public static native byte[] secp256k1_musig_nonce_agg(long ctx, byte[][] nonces);
9597

jni/src/main/kotlin/fr/acinq/secp256k1/NativeSecp256k1.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ public object NativeSecp256k1 : Secp256k1 {
9292
return Secp256k1CFunctions.secp256k1_schnorrsig_sign(Secp256k1Context.getContext(), data, sec, auxrand32)
9393
}
9494

95-
override fun musigNonceGen(sessionId32: ByteArray, privkey: ByteArray?, aggpubkey: ByteArray, msg32: ByteArray?, keyaggCache: ByteArray?, extraInput32: ByteArray?): ByteArray {
96-
return Secp256k1CFunctions.secp256k1_musig_nonce_gen(Secp256k1Context.getContext(), sessionId32, privkey, aggpubkey, msg32, keyaggCache, extraInput32)
95+
override fun musigNonceGen(sessionRandom32: ByteArray, privkey: ByteArray?, pubkey: ByteArray, msg32: ByteArray?, keyaggCache: ByteArray?, extraInput32: ByteArray?): ByteArray {
96+
return Secp256k1CFunctions.secp256k1_musig_nonce_gen(Secp256k1Context.getContext(), sessionRandom32, privkey, pubkey, msg32, keyaggCache, extraInput32)
97+
}
98+
99+
override fun musigNonceGenCounter(nonRepeatingCounter: ULong, privkey: ByteArray, pubkey: ByteArray, msg32: ByteArray?, keyaggCache: ByteArray?, extraInput32: ByteArray?): ByteArray {
100+
return Secp256k1CFunctions.secp256k1_musig_nonce_gen_counter(Secp256k1Context.getContext(), nonRepeatingCounter.toLong(), privkey, pubkey, msg32, keyaggCache, extraInput32)
97101
}
98102

99103
override fun musigNonceAgg(pubnonces: Array<ByteArray>): ByteArray {

native/secp256k1

src/commonMain/kotlin/fr/acinq/secp256k1/Secp256k1.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,30 @@ public interface Secp256k1 {
159159
* This nonce must never be persisted or reused across signing sessions.
160160
* All optional arguments exist to enrich the quality of the randomness used, which is critical for security.
161161
*
162-
* @param sessionId32 unique 32-byte session ID.
162+
* @param sessionRandom32 unique 32-byte random data that must not be reused to generate other nonces
163163
* @param privkey (optional) signer's private key.
164-
* @param aggpubkey aggregated public key of all participants in the signing session.
164+
* @param pubkey signer's public key
165165
* @param msg32 (optional) 32-byte message that will be signed, if already known.
166166
* @param keyaggCache (optional) key aggregation cache data from the signing session.
167167
* @param extraInput32 (optional) additional 32-byte random data.
168168
* @return serialized version of the secret nonce and the corresponding public nonce.
169169
*/
170-
public fun musigNonceGen(sessionId32: ByteArray, privkey: ByteArray?, aggpubkey: ByteArray, msg32: ByteArray?, keyaggCache: ByteArray?, extraInput32: ByteArray?): ByteArray
170+
public fun musigNonceGen(sessionRandom32: ByteArray, privkey: ByteArray?, pubkey: ByteArray, msg32: ByteArray?, keyaggCache: ByteArray?, extraInput32: ByteArray?): ByteArray
171+
172+
/**
173+
* Alternative counter-based method for generating nonce.
174+
* This nonce must never be persisted or reused across signing sessions.
175+
* All optional arguments exist to enrich the quality of the randomness used, which is critical for security.
176+
*
177+
* @param nonRepeatingCounter non-repeating counter that must never be reused with the same private key
178+
* @param privkey signer's private key.
179+
* @param pubkey signer's public key
180+
* @param msg32 (optional) 32-byte message that will be signed, if already known.
181+
* @param keyaggCache (optional) key aggregation cache data from the signing session.
182+
* @param extraInput32 (optional) additional 32-byte random data.
183+
* @return serialized version of the secret nonce and the corresponding public nonce.
184+
*/
185+
public fun musigNonceGenCounter(nonRepeatingCounter: ULong, privkey: ByteArray, pubkey: ByteArray, msg32: ByteArray?, keyaggCache: ByteArray?, extraInput32: ByteArray?): ByteArray
171186

172187
/**
173188
* Aggregate public nonces from all participants of a signing session.

src/nativeMain/kotlin/fr/acinq/secp256k1/Secp256k1Native.kt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fr.acinq.secp256k1
33
import kotlinx.cinterop.*
44
import platform.posix.memcpy
55
import platform.posix.size_tVar
6+
import platform.posix.uint64_t
67
import secp256k1.*
78

89
@OptIn(ExperimentalUnsignedTypes::class, ExperimentalForeignApi::class)
@@ -291,8 +292,8 @@ public object Secp256k1Native : Secp256k1 {
291292
}
292293
}
293294

294-
override fun musigNonceGen(sessionId32: ByteArray, privkey: ByteArray?, aggpubkey: ByteArray, msg32: ByteArray?, keyaggCache: ByteArray?, extraInput32: ByteArray?): ByteArray {
295-
require(sessionId32.size == 32)
295+
override fun musigNonceGen(sessionRandom32: ByteArray, privkey: ByteArray?, pubkey: ByteArray, msg32: ByteArray?, keyaggCache: ByteArray?, extraInput32: ByteArray?): ByteArray {
296+
require(sessionRandom32.size == 32)
296297
privkey?.let { require(it.size == 32) }
297298
msg32?.let { require(it.size == 32) }
298299
keyaggCache?.let { require(it.size == Secp256k1.MUSIG2_PUBLIC_KEYAGG_CACHE_SIZE) }
@@ -301,7 +302,7 @@ public object Secp256k1Native : Secp256k1 {
301302
val nonce = memScoped {
302303
val secnonce = alloc<secp256k1_musig_secnonce>()
303304
val pubnonce = alloc<secp256k1_musig_pubnonce>()
304-
val nPubkey = allocPublicKey(aggpubkey)
305+
val nPubkey = allocPublicKey(pubkey)
305306
val nKeyAggCache = keyaggCache?.let {
306307
val n = alloc<secp256k1_musig_keyagg_cache>()
307308
memcpy(n.ptr, toNat(it), Secp256k1.MUSIG2_PUBLIC_KEYAGG_CACHE_SIZE.toULong())
@@ -311,7 +312,7 @@ public object Secp256k1Native : Secp256k1 {
311312
ctx,
312313
secnonce.ptr,
313314
pubnonce.ptr,
314-
toNat(sessionId32),
315+
toNat(sessionRandom32),
315316
privkey?.let { toNat(it) },
316317
nPubkey.ptr,
317318
msg32?.let { toNat(it) },
@@ -324,6 +325,29 @@ public object Secp256k1Native : Secp256k1 {
324325
return nonce
325326
}
326327

328+
override fun musigNonceGenCounter(nonRepeatingCounter: ULong, privkey: ByteArray, pubkey: ByteArray, msg32: ByteArray?, keyaggCache: ByteArray?, extraInput32: ByteArray?): ByteArray {
329+
require(privkey.size ==32)
330+
require(pubkey.size == 33 || pubkey.size == 65)
331+
msg32?.let { require(it.size == 32) }
332+
keyaggCache?.let { require(it.size == Secp256k1.MUSIG2_PUBLIC_KEYAGG_CACHE_SIZE) }
333+
extraInput32?.let { require(it.size == 32) }
334+
val nonce = memScoped {
335+
val secnonce = alloc<secp256k1_musig_secnonce>()
336+
val pubnonce = alloc<secp256k1_musig_pubnonce>()
337+
val nPubkey = allocPublicKey(pubkey)
338+
val nKeyAggCache = keyaggCache?.let {
339+
val n = alloc<secp256k1_musig_keyagg_cache>()
340+
memcpy(n.ptr, toNat(it), Secp256k1.MUSIG2_PUBLIC_KEYAGG_CACHE_SIZE.toULong())
341+
n
342+
}
343+
secp256k1_musig_nonce_gen_counter(ctx, secnonce.ptr, pubnonce.ptr, nonRepeatingCounter, toNat(privkey), nPubkey.ptr, msg32?.let { toNat(it) },nKeyAggCache?.ptr, extraInput32?.let { toNat(it) }).requireSuccess("secp256k1_musig_nonce_gen_counter() failed")
344+
val nPubnonce = allocArray<UByteVar>(Secp256k1.MUSIG2_PUBLIC_NONCE_SIZE)
345+
secp256k1_musig_pubnonce_serialize(ctx, nPubnonce, pubnonce.ptr).requireSuccess("secp256k1_musig_pubnonce_serialize failed")
346+
secnonce.ptr.readBytes(Secp256k1.MUSIG2_SECRET_NONCE_SIZE) + nPubnonce.readBytes(Secp256k1.MUSIG2_PUBLIC_NONCE_SIZE)
347+
}
348+
return nonce
349+
}
350+
327351
override fun musigNonceAgg(pubnonces: Array<ByteArray>): ByteArray {
328352
require(pubnonces.isNotEmpty())
329353
pubnonces.forEach { require(it.size == Secp256k1.MUSIG2_PUBLIC_NONCE_SIZE) }

tests/src/commonTest/kotlin/fr/acinq/secp256k1/Musig2Test.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ class Musig2Test {
108108
}
109109
}
110110

111+
@Test
112+
fun `generate secret nonce from counter`() {
113+
val sk = Hex.decode("EEC1CB7D1B7254C5CAB0D9C61AB02E643D464A59FE6C96A7EFE871F07C5AEF54")
114+
val pk = Secp256k1.pubkeyCreate(sk)
115+
val nonce = Secp256k1.musigNonceGenCounter(0UL, sk, pk, null, null, null)
116+
val secnonce = nonce.copyOfRange(0, Secp256k1.MUSIG2_SECRET_NONCE_SIZE)
117+
val pubnonce = nonce.copyOfRange(Secp256k1.MUSIG2_SECRET_NONCE_SIZE, Secp256k1.MUSIG2_SECRET_NONCE_SIZE + Secp256k1.MUSIG2_PUBLIC_NONCE_SIZE)
118+
assertContentEquals(secnonce.copyOfRange(4, 4 + 64), Hex.decode("842F1380CD17A198FC3DAD3B7DA7492941F46976F2702FF7C66F24F472036AF1DA3F952DDE4A2DA6B6325707CE87A4E3616D06FC5F81A9C99386D20A99CECF99"))
119+
assertContentEquals(pubnonce, Hex.decode("03A5B9B6907942EACDDA49A366016EC2E62404A1BF4AB6D4DB82067BC3ADF086D7033205DB9EB34D5C7CE02848CAC68A83ED73E3883477F563F23CE9A11A7721EC64"))
120+
}
121+
111122
@Test
112123
fun `aggregate nonces`() {
113124
val tests = readData("musig2/nonce_agg_vectors.json")

0 commit comments

Comments
 (0)