From f5ab7fdeb765ac46f3c333cd27ce8a720debc45d Mon Sep 17 00:00:00 2001 From: kngako Date: Tue, 20 Aug 2024 01:39:22 +0200 Subject: [PATCH] Improve JNI handling of frost share gen --- .../fr_acinq_secp256k1_Secp256k1CFunctions.c | 22 ++++++++++++------- .../fr/acinq/secp256k1/NativeSecp256k1.kt | 12 ++++++---- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c b/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c index 0e03ff4..4171a23 100644 --- a/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c +++ b/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c @@ -1379,7 +1379,7 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 jbyte *pubkeyBytes; unsigned char seed32[32]; - unsigned char *ids33[jn_participants]; + const jbyte *ids33[jn_participants]; if (jctx == 0) return NULL; @@ -1399,17 +1399,23 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 copy_bytes_from_java(penv, jseed32, size, seed32); size = (*penv)->GetArrayLength(penv, jids33); - CHECKRESULT(size != jn_participants, "invalid ids33 size"); + CHECKRESULT(size != jn_participants, "ids33 needs to be of size jn_participants"); CHECKRESULT((*penv)->GetArrayLength(penv, jpok64) != 64, "pok64 length must be 64 bytes"); +// for (i = 0; i < jn_participants; i++) +// { +// jbyteArray jid33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i); +// size = (*penv)->GetArrayLength(penv, jid33); +// CHECKRESULT(size != 33, "invalid id33 size"); +// copy_bytes_from_java(penv, jid33, 33, ids33[i]); +// } for (i = 0; i < jn_participants; i++) - { - jbyteArray jid33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i); - size = (*penv)->GetArrayLength(penv, jid33); - CHECKRESULT(size != 33, "invalid id33 size"); - copy_bytes_from_java(penv, jid33, 33, ids33[i]); - } + { + jbyteArray id33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i); + // TODO: Check id33 size is 33... + ids33[i] = (*penv)->GetByteArrayElements(penv, id33, 0); + } int sharesLength = jn_participants * 32; int vssCommitmentLength = jthreshold * 65; diff --git a/jni/src/main/kotlin/fr/acinq/secp256k1/NativeSecp256k1.kt b/jni/src/main/kotlin/fr/acinq/secp256k1/NativeSecp256k1.kt index 5c704d8..a79125d 100644 --- a/jni/src/main/kotlin/fr/acinq/secp256k1/NativeSecp256k1.kt +++ b/jni/src/main/kotlin/fr/acinq/secp256k1/NativeSecp256k1.kt @@ -145,18 +145,22 @@ public object NativeSecp256k1 : Secp256k1 { totalSigners, ids33 ) + println(Hex.encode(result)) + val shares = Array(totalSigners) { index -> val startIndex = 0 + (index*32); - val endIndex = 32 + (index*32); + val endIndex = 31 + (index*32); result.sliceArray( startIndex..endIndex ) } - val vssCommitment = Array(totalSigners) { index -> - val startIndex = 0 + (index*65); - val endIndex = 65 + (index*65); + val sharesOffset = totalSigners*32; + + val vssCommitment = Array(threshold) { index -> + val startIndex = sharesOffset + (index*65); + val endIndex = sharesOffset + 64 + (index*65); result.sliceArray( startIndex..endIndex