Fix for handling ids33 in JNI

This commit is contained in:
kngako
2024-08-21 21:35:27 +02:00
parent e6cba2b505
commit d74895079d
8 changed files with 79 additions and 50 deletions

View File

@@ -38,6 +38,8 @@ extern "C" {
#undef fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_FROST_SECRET_NONCE_SIZE
#define fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_FROST_SECRET_NONCE_SIZE 68L
#undef fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_FROST_PUBLIC_NONCE_SIZE
#define fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_FROST_PUBLIC_NONCE_SIZE 66L
#undef fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_FROST_AGGREGATE_SHARE_SIZE
#define fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_FROST_AGGREGATE_SHARE_SIZE 32L
#undef fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_FROST_SESSION_SIZE

View File

@@ -1379,7 +1379,7 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
jbyte *pubkeyBytes;
unsigned char seed32[32];
const jbyte *ids33[jn_participants];
unsigned char *ids33[jn_participants];
if (jctx == 0)
return NULL;
@@ -1403,14 +1403,24 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
CHECKRESULT((*penv)->GetArrayLength(penv, jpok64) != 64, "pok64 length must be 64 bytes");
// for (i = 0; i < jn_participants; i++)
// {
// jbyteArray id33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i);
// size = (*penv)->GetArrayLength(penv, id33);
// CHECKRESULT(size != 33, "invalid id33 size");
// ids33[i] = (*penv)->GetByteArrayElements(penv, id33, 0);
// (*penv)->ReleaseByteArrayElements(penv, id33, ids33[i], 0);
// }
for (i = 0; i < jn_participants; i++)
{
jbyteArray id33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i);
size = (*penv)->GetArrayLength(penv, id33);
CHECKRESULT(size != 33, "invalid id33 size");
ids33[i] = (*penv)->GetByteArrayElements(penv, id33, 0);
(*penv)->ReleaseByteArrayElements(penv, id33, ids33[i], 0);
}
{
jbyteArray jid33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i);
size = (*penv)->GetArrayLength(penv, jid33);
CHECKRESULT(size != 33, "invalid id33 size");
ids33[i] = (unsigned char *) calloc(1, size);
copy_bytes_from_java(penv, jid33, size, ids33[i]);
}
int sharesLength = jn_participants * 32;
int vssCommitmentLength = jthreshold * 65;
@@ -1922,7 +1932,6 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
jbyte *pubkey_ptr;
secp256k1_xonly_pubkey pubkey;
unsigned char msg32[32];
secp256k1_musig_keyagg_cache keyaggcache;
unsigned char extra_input32[32];
jbyteArray jnonce;
@@ -2012,7 +2021,7 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
secp256k1_frost_session session;
secp256k1_frost_pubnonce **pubnonces;
jbyte *in66, *pub, *id33;
jbyte *in66, *pub, *my_id33;
jbyteArray jpubnonce;
@@ -2020,7 +2029,7 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
secp256k1_xonly_pubkey public_key;
const jbyte *ids33[n_pubnonces];
unsigned char *ids33[n_pubnonces];
secp256k1_frost_tweak_cache tweak_cache;
secp256k1_pubkey adaptor;
@@ -2060,7 +2069,7 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
pubnonces[i] = calloc(1, sizeof(secp256k1_frost_pubnonce));
jpubnonce = (*penv)->GetObjectArrayElement(penv, jpubnonces, i);
size = (*penv)->GetArrayLength(penv, jpubnonce);
CHECKRESULT1(size != 66, "invalid public nonce size", free_frost_nonces(pubnonces, count));
CHECKRESULT1(size != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_FROST_PUBLIC_NONCE_SIZE, "invalid public nonce size", free_frost_nonces(pubnonces, count));
in66 = (*penv)->GetByteArrayElements(penv, jpubnonce, 0);
result = secp256k1_frost_pubnonce_parse(ctx, pubnonces[i], (unsigned char *)in66);
(*penv)->ReleaseByteArrayElements(penv, jpubnonce, in66, 0);
@@ -2077,19 +2086,20 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
(*penv)->ReleaseByteArrayElements(penv, jpubkey, pub, 0);
CHECKRESULT(!result, "secp256k1_xonly_pubkey_parse failed");
id33 = (*penv)->GetByteArrayElements(penv, jmy_id33, 0);
my_id33 = (*penv)->GetByteArrayElements(penv, jmy_id33, 0);
(*penv)->ReleaseByteArrayElements(penv, jmy_id33, my_id33, 0);
CHECKRESULT((*penv)->GetArrayLength(penv, jids33) != n_pubnonces, "invalid ids33 array size");
size = (*penv)->GetArrayLength(penv, jids33);
CHECKRESULT(size != n_pubnonces, "invalid ids33 size");
for (i = 0; i < n_pubnonces; i++)
{
jbyteArray id33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i);
size = (*penv)->GetArrayLength(penv, id33);
jbyteArray jid33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i);
size = (*penv)->GetArrayLength(penv, jid33);
CHECKRESULT(size != 33, "invalid id33 size");
ids33[i] = (*penv)->GetByteArrayElements(penv, id33, 0);
(*penv)->ReleaseByteArrayElements(penv, id33, ids33[i], 0);
}
ids33[i] = (unsigned char *) calloc(1, size);
copy_bytes_from_java(penv, jid33, size, ids33[i]);
}
if (jtweak_cache != NULL)
{
@@ -2097,7 +2107,6 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size");
copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data);
}
if (jadaptor != NULL)
{
@@ -2117,7 +2126,7 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
n_pubnonces,
msg32,
&public_key,
id33,
my_id33,
(const unsigned char * const*) ids33,
jtweak_cache == NULL ? NULL : &tweak_cache,
jadaptor == NULL ? NULL : &adaptor

View File

@@ -272,6 +272,7 @@ public object NativeSecp256k1 : Secp256k1 {
override fun frostNonceProcess(
publicNonces: Array<ByteArray>,
threshold: Int,
msg32: ByteArray,
publicKey: ByteArray,
id33: ByteArray,
@@ -282,7 +283,7 @@ public object NativeSecp256k1 : Secp256k1 {
return Secp256k1CFunctions.secp256k1_frost_nonce_process(
Secp256k1Context.getContext(),
publicNonces,
publicNonces.size,
threshold,
msg32,
publicKey,
id33,
@@ -324,12 +325,12 @@ public object NativeSecp256k1 : Secp256k1 {
)
}
override fun frostPartialSignatureAggregate(session: ByteArray, partialSignatures: Array<ByteArray>): ByteArray {
override fun frostPartialSignatureAggregate(session: ByteArray, partialSignatures: Array<ByteArray>, threshold: Int): ByteArray {
return Secp256k1CFunctions.secp256k1_frost_partial_sig_agg(
Secp256k1Context.getContext(),
session,
partialSignatures,
partialSignatures.size
threshold
)
}