diff --git a/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c b/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c index 0ed2034..44e7257 100644 --- a/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c +++ b/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c @@ -1357,36 +1357,38 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1shares_1gen(JNIEnv *penv, jclass clazz, jlong jctx, jbyteArray jseed32, jint jthreshold, jint jn_participants, jobjectArray jids33) { secp256k1_context *ctx = (secp256k1_context *)jctx; - // This is what gets returned - secp256k1_frost_share shares[jn_participants]; - secp256k1_pubkey vss_commitment[jthreshold]; + + secp256k1_frost_share *shares; + secp256k1_pubkey* vss_commitment; unsigned char pok64[64]; size_t size; - // This is what gets passed jbyte *pubkeyBytes; unsigned char seed32[32]; - if (jseed32 != NULL) - { - size = (*penv)->GetArrayLength(penv, jseed32); - CHECKRESULT(size != 32, "invalid seed32 size"); - copy_bytes_from_java(penv, jseed32, size, seed32); - } - const jbyte *ids33[jn_participants]; - // Copy over data from jids33 + + if (jctx == 0) + return NULL; + + if (jseed32 != NULL) + return NULL; + if (jids33 != NULL) + return NULL; + + size = (*penv)->GetArrayLength(penv, jseed32); + CHECKRESULT(size != 32, "invalid seed32 size"); + copy_bytes_from_java(penv, jseed32, size, seed32); + + size = (*penv)->GetArrayLength(penv, jids33); + CHECKRESULT(size != jn_participants, "invalid ids33 size"); + for (i = 0; i < jn_participants; i++) { - size = (*penv)->GetArrayLength(penv, jids33); - CHECKRESULT(size != jn_participants, "invalid ids33 size"); - for (i = 0; i < jn_participants; i++) - { - jbyteArray id33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i); - // TODO: Check id33 size is 33... - ids33[i] = (*penv)->GetByteArrayElements(penv, id33, 0); - } + jbyteArray id33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i); + // TODO: Check id33 size is 33... + ids33[i] = (*penv)->GetByteArrayElements(penv, id33, 0); } int result = 0; @@ -1437,90 +1439,93 @@ void free_shares(secp256k1_frost_share **shares, size_t count) JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1share_1agg (JNIEnv *penv, jclass clazz, jlong jctx, jobjectArray jshares, jobjectArray jvss_commitments, jint jtotalShareCount, jint jthreshold, jbyteArray jid33) { - secp256k1_context *ctx = (secp256k1_context *)jctx; + secp256k1_context *ctx = (secp256k1_context *)jctx; - secp256k1_frost_share aggregate_share; - jbyteArray jaggregate_share; - secp256k1_xonly_pubkey aggregate_public_key; - jbyteArray jaggregate_public_key; + secp256k1_frost_share aggregate_share; + jbyteArray jaggregate_share; + secp256k1_xonly_pubkey aggregate_public_key; + jbyteArray jaggregate_public_key; - const secp256k1_frost_share **shares; - jbyteArray jshare; - jbyte *in32; + const secp256k1_frost_share **shares; + jbyteArray jshare; + jbyte *in32; - secp256k1_xonly_pubkey **vss_commitments; - jbyteArray jvss_commitment; - jbyte *pub; + secp256k1_xonly_pubkey **vss_commitments; + jbyteArray jvss_commitment; + jbyte *pub; - jbyte *id33; + jbyte *id33; - size_t size, count; + size_t size, count; - count = (*penv)->GetArrayLength(penv, jnonces); - CHECKRESULT(count != jtotalShareCount, "jshares count should be total share count."); - shares = calloc(count, sizeof(secp256k1_frost_share*)); + if (jctx == 0) + return NULL; - for (i = 0; i < count; i++) - { - shares[i] = calloc(1, sizeof(secp256k1_frost_share)); - jshare = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jshares, i); - size = (*penv)->GetArrayLength(penv, jshare); - CHECKRESULT1(size != sizeof(secp256k1_frost_share), "invalid share size", free_shares(shares, count)); - in32 = (*penv)->GetByteArrayElements(penv, jshare, 0); - result = secp256k1_frost_share_parse(ctx, shares[i], (unsigned char *)in32); - (*penv)->ReleaseByteArrayElements(penv, jshare, in32, 0); - CHECKRESULT1(!result, "secp256k1_frost_share_parse failed", free_shares(shares, count)); - } + count = (*penv)->GetArrayLength(penv, jnonces); + CHECKRESULT(count != jtotalShareCount, "jshares count should be total share count."); + shares = calloc(count, sizeof(secp256k1_frost_share*)); - count = (*penv)->GetArrayLength(penv, jvss_commitments); - vss_commitments = calloc(count, sizeof(secp256k1_pubkey *)); + for (i = 0; i < count; i++) + { + shares[i] = calloc(1, sizeof(secp256k1_frost_share)); + jshare = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jshares, i); + size = (*penv)->GetArrayLength(penv, jshare); + CHECKRESULT1(size != sizeof(secp256k1_frost_share), "invalid share size", free_shares(shares, count)); + in32 = (*penv)->GetByteArrayElements(penv, jshare, 0); + result = secp256k1_frost_share_parse(ctx, shares[i], (unsigned char *)in32); + (*penv)->ReleaseByteArrayElements(penv, jshare, in32, 0); + CHECKRESULT1(!result, "secp256k1_frost_share_parse failed", free_shares(shares, count)); + } - for (i = 0; i < count; i++) - { - vss_commitments[i] = calloc(1, sizeof(secp256k1_pubkey)); - jvss_commitment = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jvss_commitments, i); - size = (*penv)->GetArrayLength(penv, jvss_commitment); - CHECKRESULT1((size != 33) && (size != 65), "invalid public key size", free_pubkeys(pubkeys, count)); - pub = (*penv)->GetByteArrayElements(penv, jvss_commitment, 0); - result = secp256k1_ec_pubkey_parse(ctx, vss_commitments[i], (unsigned char *)pub, size); - (*penv)->ReleaseByteArrayElements(penv, jvss_commitment, pub, 0); - CHECKRESULT1(!result, "secp256k1_ec_pubkey_parse failed", free_pubkeys(vss_commitments, count)); - } + count = (*penv)->GetArrayLength(penv, jvss_commitments); + vss_commitments = calloc(count, sizeof(secp256k1_pubkey *)); - id33 = (*penv)->GetByteArrayElements(penv, jid33, 0); + for (i = 0; i < count; i++) + { + vss_commitments[i] = calloc(1, sizeof(secp256k1_pubkey)); + jvss_commitment = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jvss_commitments, i); + size = (*penv)->GetArrayLength(penv, jvss_commitment); + CHECKRESULT1((size != 33) && (size != 65), "invalid public key size", free_pubkeys(pubkeys, count)); + pub = (*penv)->GetByteArrayElements(penv, jvss_commitment, 0); + result = secp256k1_ec_pubkey_parse(ctx, vss_commitments[i], (unsigned char *)pub, size); + (*penv)->ReleaseByteArrayElements(penv, jvss_commitment, pub, 0); + CHECKRESULT1(!result, "secp256k1_ec_pubkey_parse failed", free_pubkeys(vss_commitments, count)); + } + + id33 = (*penv)->GetByteArrayElements(penv, jid33, 0); - int result = 0; + int result = 0; - result = secp256k1_frost_share_agg( - ctx, - &aggregate_share, - &aggregate_public_key, - shares, - vss_commitments, - jtotalShareCount, - jthreshold, - (unsigned char *)id33 - ); + result = secp256k1_frost_share_agg( + ctx, + &aggregate_share, + &aggregate_public_key, + shares, + vss_commitments, + jtotalShareCount, + jthreshold, + (unsigned char *)id33 + ); - jobjectArray output = (*penv)->NewObjectArray(penv, 2, jbyteArray, NULL); + jobjectArray output = (*penv)->NewObjectArray(penv, 2, jbyteArray, NULL); - jaggregate_share = (*penv)->NewByteArray(penv, 64); - copy_bytes_to_java(penv, jaggregate_share, 64, aggregate_share); + jaggregate_share = (*penv)->NewByteArray(penv, 64); + copy_bytes_to_java(penv, jaggregate_share, 64, aggregate_share); - output[0] = (*penv)->NewByteArray(penv, 32); - pub = (*penv)->GetByteArrayElements(penv, output[0], 0); - result = secp256k1_frost_share_serialize(ctx, (unsigned char *)pub, &aggregate_share); - (*penv)->ReleaseByteArrayElements(penv, output[0], pub, 0); - CHECKRESULT(!result, "secp256k1_frost_share_serialize failed"); + output[0] = (*penv)->NewByteArray(penv, 32); + pub = (*penv)->GetByteArrayElements(penv, output[0], 0); + result = secp256k1_frost_share_serialize(ctx, (unsigned char *)pub, &aggregate_share); + (*penv)->ReleaseByteArrayElements(penv, output[0], pub, 0); + CHECKRESULT(!result, "secp256k1_frost_share_serialize failed"); - output[1] = (*penv)->NewByteArray(penv, 32); - pub = (*penv)->GetByteArrayElements(penv, output[1], 0); - result = secp256k1_xonly_pubkey_serialize(ctx, (unsigned char *)pub, &aggregate_public_key); - (*penv)->ReleaseByteArrayElements(penv, output[1], pub, 0); - CHECKRESULT(!result, "secp256k1_xonly_pubkey_serialize failed"); + output[1] = (*penv)->NewByteArray(penv, 32); + pub = (*penv)->GetByteArrayElements(penv, output[1], 0); + result = secp256k1_xonly_pubkey_serialize(ctx, (unsigned char *)pub, &aggregate_public_key); + (*penv)->ReleaseByteArrayElements(penv, output[1], pub, 0); + CHECKRESULT(!result, "secp256k1_xonly_pubkey_serialize failed"); - return output; + return output; } /* @@ -1541,6 +1546,11 @@ JNIEXPORT jint JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1fr secp256k1_xonly_pubkey *vss_commitment; jbyte *jpub; + size_t size, count; + + if (jctx == 0) + return NULL; + share = calloc(1, sizeof(secp256k1_frost_share)); size = (*penv)->GetArrayLength(penv, jshare); // TODO: CHECKRESULT1(size != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE, "invalid public nonce size", free_nonces(pubnonces, count)); @@ -1579,50 +1589,54 @@ JNIEXPORT jint JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1fr JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1compute_1pubshare (JNIEnv *penv, jclass clazz, jlong jctx, jint jthreshold, jbyteArray jid33, jobjectArray jvss_commitments, jint jtotalSignersCount) { - secp256k1_context *ctx = (secp256k1_context *)jctx; + secp256k1_context *ctx = (secp256k1_context *)jctx; - secp256k1_pubkey pubshare; + secp256k1_pubkey pubshare; - jbyte *id33; + jbyte *id33; - secp256k1_xonly_pubkey **vss_commitments; - jbyteArray jvss_commitment; - jbyte *jpubkey; + secp256k1_xonly_pubkey **vss_commitments; + jbyteArray jvss_commitment; + jbyte *jpubkey; - id33 = (*penv)->GetByteArrayElements(penv, jid33, 0); + size_t size, count; + if (jctx == 0) + return NULL; - count = (*penv)->GetArrayLength(penv, jvss_commitments); - vss_commitments = calloc(count, sizeof(secp256k1_pubkey *)); + id33 = (*penv)->GetByteArrayElements(penv, jid33, 0); - for (i = 0; i < count; i++) - { - vss_commitments[i] = calloc(1, sizeof(secp256k1_pubkey)); - jvss_commitment = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jvss_commitments, i); - size = (*penv)->GetArrayLength(penv, jvss_commitment); - CHECKRESULT1((size != 33) && (size != 65), "invalid public key size", free_pubkeys(pubkeys, count)); - jpubkey = (*penv)->GetByteArrayElements(penv, jvss_commitment, 0); - result = secp256k1_ec_pubkey_parse(ctx, vss_commitments[i], (unsigned char *)jpub, size); - (*penv)->ReleaseByteArrayElements(penv, jvss_commitment, jpub, 0); - CHECKRESULT1(!result, "secp256k1_ec_pubkey_parse failed", free_pubkeys(vss_commitments, count)); - } + count = (*penv)->GetArrayLength(penv, jvss_commitments); + vss_commitments = calloc(count, sizeof(secp256k1_pubkey *)); - int result = secp256k1_frost_compute_pubshare( - ctx, - &pubshare, - jthreshold, - (unsigned char *)id33 - vss_commitments, - jtotalSignersCount - ); + for (i = 0; i < count; i++) + { + vss_commitments[i] = calloc(1, sizeof(secp256k1_pubkey)); + jvss_commitment = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jvss_commitments, i); + size = (*penv)->GetArrayLength(penv, jvss_commitment); + CHECKRESULT1((size != 33) && (size != 65), "invalid public key size", free_pubkeys(pubkeys, count)); + jpubkey = (*penv)->GetByteArrayElements(penv, jvss_commitment, 0); + result = secp256k1_ec_pubkey_parse(ctx, vss_commitments[i], (unsigned char *)jpub, size); + (*penv)->ReleaseByteArrayElements(penv, jvss_commitment, jpub, 0); + CHECKRESULT1(!result, "secp256k1_ec_pubkey_parse failed", free_pubkeys(vss_commitments, count)); + } - jpubkey = (*penv)->NewByteArray(penv, 65); - jbyte *jpubkeyBytes = (*penv)->GetByteArrayElements(penv, jpubkey, 0); - result = secp256k1_ec_pubkey_serialize(ctx, (unsigned char *)jpubkeyBytes, &size, &pubkey, SECP256K1_EC_UNCOMPRESSED); - (*penv)->ReleaseByteArrayElements(penv, jpubkey, jpubkeyBytes, 0); - CHECKRESULT(!result, "secp256k1_ec_pubkey_serialize failed"); + int result = secp256k1_frost_compute_pubshare( + ctx, + &pubshare, + jthreshold, + (unsigned char *)id33 + vss_commitments, + jtotalSignersCount + ); - return jpubkey; + jpubkey = (*penv)->NewByteArray(penv, 65); + jbyte *jpubkeyBytes = (*penv)->GetByteArrayElements(penv, jpubkey, 0); + result = secp256k1_ec_pubkey_serialize(ctx, (unsigned char *)jpubkeyBytes, &size, &pubkey, SECP256K1_EC_UNCOMPRESSED); + (*penv)->ReleaseByteArrayElements(penv, jpubkey, jpubkeyBytes, 0); + CHECKRESULT(!result, "secp256k1_ec_pubkey_serialize failed"); + + return jpubkey; } /* @@ -1639,19 +1653,24 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 secp256k1_xonly_pubkey public_key; jbyte *pub; + size_t size, count; + + if (jctx == 0) + return NULL; + pub = (*penv)->GetByteArrayElements(penv, jpublicKey, 0); - result = secp256k1_xonly_pubkey_parse(ctx, &public_key, (unsigned char *)pub); - (*penv)->ReleaseByteArrayElements(penv, jpublicKey, pub, 0); - CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed"); + result = secp256k1_xonly_pubkey_parse(ctx, &public_key, (unsigned char *)pub); + (*penv)->ReleaseByteArrayElements(penv, jpublicKey, pub, 0); + CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed"); int result = secp256k1_frost_pubkey_tweak( - ctx, - &tweak_cache, - &public_key + ctx, + &tweak_cache, + &public_key ); CHECKRESULT(!result, "secp256k1_frost_pubkey_tweak failed"); - size = sizeof(secp256k1_frost_tweak_cache); + size = sizeof(secp256k1_frost_tweak_cache); jbyteArray jtweak_cache = (*penv)->NewByteArray(penv, size); copy_bytes_to_java(penv, jtweak_cache, size, tweak_cache.data); @@ -1666,49 +1685,52 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1pubkey_1ec_1tweak_1add (JNIEnv *penv, jclass clazz, jlong jctx, jbyteArray jtweak_cache, jbyteArray jtweak32) { - secp256k1_context *ctx = (secp256k1_context *)jctx; + secp256k1_context *ctx = (secp256k1_context *)jctx; - jbyte *tweak32, *pub; - secp256k1_pubkey pubkey; - jbyteArray jpubkey; + jbyte *tweak32, *pub; + secp256k1_pubkey pubkey; + jbyteArray jpubkey; - secp256k1_frost_tweak_cache tweak_cache; - jbyte *tweak32 = (*penv)->GetByteArrayElements(penv, jtweak32, 0);; + secp256k1_frost_tweak_cache tweak_cache; + jbyte *tweak32 = (*penv)->GetByteArrayElements(penv, jtweak32, 0);; - if (jctx == 0) - return NULL; - if (jtweak_cache == NULL) - return NULL; + size_t size, count; - size = (*penv)->GetArrayLength(penv, jtweak_cache); - CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); - copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data); - if (jtweak32 == NULL) + if (jctx == 0) + return NULL; + if (jtweak_cache == NULL) + return NULL; + if (jtweak32 == NULL) return NULL; - CHECKRESULT((*penv)->GetArrayLength(penv, jtweak32) != 32, "tweak must be 32 bytes"); - tweak32 = (*penv)->GetByteArrayElements(penv, jtweak32, 0); - int result = secp256k1_frost_pubkey_ec_tweak_add( - ctx, - pubkey, - &tweak_cache, - &public_key - ); - (*penv)->ReleaseByteArrayElements(penv, jtweak32, tweak32, 0); - CHECKRESULT(!result, "secp256k1_frost_pubkey_ec_tweak_add failed"); + size = (*penv)->GetArrayLength(penv, jtweak_cache); + CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); + copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data); + + CHECKRESULT((*penv)->GetArrayLength(penv, jtweak32) != 32, "tweak must be 32 bytes"); + tweak32 = (*penv)->GetByteArrayElements(penv, jtweak32, 0); - jpubkey = (*penv)->NewByteArray(penv, 65); - pub = (*penv)->GetByteArrayElements(penv, jpubkey, 0); - size = 65; - result = secp256k1_ec_pubkey_serialize(ctx, pub, &size, &pubkey, SECP256K1_EC_UNCOMPRESSED); - (*penv)->ReleaseByteArrayElements(penv, jpubkey, pub, 0); - CHECKRESULT(!result, "secp256k1_ec_pubkey_serialize failed"); + int result = secp256k1_frost_pubkey_ec_tweak_add( + ctx, + pubkey, + &tweak_cache, + &public_key + ); + (*penv)->ReleaseByteArrayElements(penv, jtweak32, tweak32, 0); + CHECKRESULT(!result, "secp256k1_frost_pubkey_ec_tweak_add failed"); - pub = (*penv)->GetByteArrayElements(penv, jtweak_cache, 0); - memcpy(pub, tweak_cache.data, sizeof(secp256k1_frost_tweak_cache)); - (*penv)->ReleaseByteArrayElements(penv, jtweak_cache, pub, 0); + jpubkey = (*penv)->NewByteArray(penv, 65); + pub = (*penv)->GetByteArrayElements(penv, jpubkey, 0); + size = 65; + result = secp256k1_ec_pubkey_serialize(ctx, pub, &size, &pubkey, SECP256K1_EC_UNCOMPRESSED); + (*penv)->ReleaseByteArrayElements(penv, jpubkey, pub, 0); + CHECKRESULT(!result, "secp256k1_ec_pubkey_serialize failed"); - return jpubkey; + pub = (*penv)->GetByteArrayElements(penv, jtweak_cache, 0); + memcpy(pub, tweak_cache.data, sizeof(secp256k1_frost_tweak_cache)); + (*penv)->ReleaseByteArrayElements(penv, jtweak_cache, pub, 0); + + return jpubkey; } /* @@ -1719,49 +1741,51 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1pubkey_1xonly_1tweak_1add (JNIEnv *penv, jclass clazz, jlong jctx, jbyteArray jtweak_cache, jbyteArray jtweak32) { - secp256k1_context *ctx = (secp256k1_context *)jctx; + secp256k1_context *ctx = (secp256k1_context *)jctx; - jbyte *tweak32, *pub; - secp256k1_pubkey pubkey; - jbyteArray jpubkey; + jbyte *tweak32, *pub; + secp256k1_pubkey pubkey; + jbyteArray jpubkey; - secp256k1_frost_tweak_cache tweak_cache; - jbyte *tweak32 = (*penv)->GetByteArrayElements(penv, jtweak32, 0);; + secp256k1_frost_tweak_cache tweak_cache; + jbyte *tweak32 = (*penv)->GetByteArrayElements(penv, jtweak32, 0);; - if (jctx == 0) - return NULL; - if (jtweak_cache == NULL) - return NULL; + size_t size, count; - size = (*penv)->GetArrayLength(penv, jtweak_cache); - CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); - copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data); - if (jtweak32 == NULL) - return NULL; - CHECKRESULT((*penv)->GetArrayLength(penv, jtweak32) != 32, "tweak must be 32 bytes"); - tweak32 = (*penv)->GetByteArrayElements(penv, jtweak32, 0); + if (jctx == 0) + return NULL; + if (jtweak_cache == NULL) + return NULL; - int result = secp256k1_frost_pubkey_ec_tweak_add( - ctx, - pubkey, - &tweak_cache, - &public_key - ); - (*penv)->ReleaseByteArrayElements(penv, jtweak32, tweak32, 0); - CHECKRESULT(!result, "secp256k1_frost_pubkey_ec_tweak_add failed"); + size = (*penv)->GetArrayLength(penv, jtweak_cache); + CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); + copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data); + if (jtweak32 == NULL) + return NULL; + CHECKRESULT((*penv)->GetArrayLength(penv, jtweak32) != 32, "tweak must be 32 bytes"); + tweak32 = (*penv)->GetByteArrayElements(penv, jtweak32, 0); - jpubkey = (*penv)->NewByteArray(penv, 65); - pub = (*penv)->GetByteArrayElements(penv, jpubkey, 0); - size = 65; - result = secp256k1_ec_pubkey_serialize(ctx, pub, &size, &pubkey, SECP256K1_EC_UNCOMPRESSED); - (*penv)->ReleaseByteArrayElements(penv, jpubkey, pub, 0); - CHECKRESULT(!result, "secp256k1_ec_pubkey_serialize failed"); + int result = secp256k1_frost_pubkey_ec_tweak_add( + ctx, + pubkey, + &tweak_cache, + &public_key + ); + (*penv)->ReleaseByteArrayElements(penv, jtweak32, tweak32, 0); + CHECKRESULT(!result, "secp256k1_frost_pubkey_ec_tweak_add failed"); - pub = (*penv)->GetByteArrayElements(penv, jtweak_cache, 0); - memcpy(pub, tweak_cache.data, sizeof(secp256k1_musig_keyagg_cache)); - (*penv)->ReleaseByteArrayElements(penv, jtweak_cache, pub, 0); + jpubkey = (*penv)->NewByteArray(penv, 65); + pub = (*penv)->GetByteArrayElements(penv, jpubkey, 0); + size = 65; + result = secp256k1_ec_pubkey_serialize(ctx, pub, &size, &pubkey, SECP256K1_EC_UNCOMPRESSED); + (*penv)->ReleaseByteArrayElements(penv, jpubkey, pub, 0); + CHECKRESULT(!result, "secp256k1_ec_pubkey_serialize failed"); - return jpubkey; + pub = (*penv)->GetByteArrayElements(penv, jtweak_cache, 0); + memcpy(pub, tweak_cache.data, sizeof(secp256k1_musig_keyagg_cache)); + (*penv)->ReleaseByteArrayElements(penv, jtweak_cache, pub, 0); + + return jpubkey; } /* @@ -1772,93 +1796,92 @@ JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp2 JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1nonce_1gen (JNIEnv *penv, jclass clazz, jlong jctx, jbyteArray jsession_id32, jbyteArray jshare, jbyteArray jmsg32, jbyteArray jpubkey, jbyteArray jextra_input32) { - secp256k1_context *ctx = (secp256k1_context *)jctx; + secp256k1_context *ctx = (secp256k1_context *)jctx; - int result = 0; + int result = 0; - size_t size; - secp256k1_frost_secnonce secnonce; - secp256k1_frost_pubnonce pubnonce; - unsigned char session_id32[32]; + secp256k1_frost_secnonce secnonce; + secp256k1_frost_pubnonce pubnonce; + unsigned char session_id32[32]; secp256k1_frost_share share; jbyte *in32; - jbyte *pubkey_ptr; - secp256k1_pubkey pubkey; - unsigned char msg32[32]; - secp256k1_musig_keyagg_cache keyaggcache; - unsigned char extra_input32[32]; + jbyte *pubkey_ptr; + secp256k1_pubkey pubkey; + unsigned char msg32[32]; + secp256k1_musig_keyagg_cache keyaggcache; + unsigned char extra_input32[32]; - if (jctx == 0) - return NULL; + size_t size, count; - if (jsession_id32 == 0) - return NULL; - size = (*penv)->GetArrayLength(penv, jsession_id32); - CHECKRESULT(size != 32, "invalid session_id size"); - copy_bytes_from_java(penv, jsession_id32, size, session_id32); + if (jctx == 0) + return NULL; - if (jshare != NULL) { - share = calloc(1, sizeof(secp256k1_frost_share)); - size = (*penv)->GetArrayLength(penv, jshare); - // TODO: CHECKRESULT1(size != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE, "invalid public nonce size", free_nonces(pubnonces, count)); - in32 = (*penv)->GetByteArrayElements(penv, jshare, 0); - result = secp256k1_frost_share_parse(ctx, share, (unsigned char *)in32); - (*penv)->ReleaseByteArrayElements(penv, jshare, in32, 0); - // TODO: CHECKRESULT1(!result, "secp256k1_frost_share_parse failed", free_shares(shares, count)); + if (jsession_id32 == 0) + return NULL; + size = (*penv)->GetArrayLength(penv, jsession_id32); + CHECKRESULT(size != 32, "invalid session_id size"); + copy_bytes_from_java(penv, jsession_id32, size, session_id32); - } + if (jshare != NULL) { + share = calloc(1, sizeof(secp256k1_frost_share)); + size = (*penv)->GetArrayLength(penv, jshare); + // TODO: CHECKRESULT1(size != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE, "invalid public nonce size", free_nonces(pubnonces, count)); + in32 = (*penv)->GetByteArrayElements(penv, jshare, 0); + result = secp256k1_frost_share_parse(ctx, share, (unsigned char *)in32); + (*penv)->ReleaseByteArrayElements(penv, jshare, in32, 0); + // TODO: CHECKRESULT1(!result, "secp256k1_frost_share_parse failed", free_shares(shares, count)); + + } + + if (jmsg32 != NULL) + { + size = (*penv)->GetArrayLength(penv, jmsg32); + CHECKRESULT(size != 32, "invalid message size"); + copy_bytes_from_java(penv, jmsg32, size, msg32); + } + + if (jpubkey != NULL) { + size = (*penv)->GetArrayLength(penv, jpubkey); + CHECKRESULT((size != 33) && (size != 65), "invalid public key size"); + pubkey_ptr = (*penv)->GetByteArrayElements(penv, jpubkey, 0); + result = secp256k1_ec_pubkey_parse(ctx, &pubkey, (unsigned char *)pubkey_ptr, size); + (*penv)->ReleaseByteArrayElements(penv, jpubkey, pubkey_ptr, 0); + CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed"); + } + if (jextra_input32 != NULL) + { + size = (*penv)->GetArrayLength(penv, jextra_input32); + CHECKRESULT(size != 32, "invalid extra input size"); + copy_bytes_from_java(penv, jextra_input32, size, extra_input32); + } - if (jmsg32 != NULL) - { - size = (*penv)->GetArrayLength(penv, jmsg32); - CHECKRESULT(size != 32, "invalid message size"); - copy_bytes_from_java(penv, jmsg32, size, msg32); - } + result = secp256k1_frost_nonce_gen( + ctx, + &secnonce, + &pubnonce, + session_id32, + jshare == NULL ? NULL : &share, + jmsg32 == NULL ? NULL : msg32, + jpubkey == NULL ? NULL :pubkey_ptr, + jextra_input32 == NULL ? NULL : extra_input32 + ); - if (jpubkey != NULL) { - size = (*penv)->GetArrayLength(penv, jpubkey); - CHECKRESULT((size != 33) && (size != 65), "invalid public key size"); - pubkey_ptr = (*penv)->GetByteArrayElements(penv, jpubkey, 0); - result = secp256k1_ec_pubkey_parse(ctx, &pubkey, (unsigned char *)pubkey_ptr, size); - (*penv)->ReleaseByteArrayElements(penv, jpubkey, pubkey_ptr, 0); - CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed"); - } - - - if (jextra_input32 != NULL) - { - size = (*penv)->GetArrayLength(penv, jextra_input32); - CHECKRESULT(size != 32, "invalid extra input size"); - copy_bytes_from_java(penv, jextra_input32, size, extra_input32); - } - - result = secp256k1_frost_nonce_gen( - ctx, - &secnonce, - &pubnonce, - session_id32, - jshare == NULL ? NULL : &share, - jmsg32 == NULL ? NULL : msg32, - jpubkey == NULL ? NULL :pubkey_ptr, - jextra_input32 == NULL ? NULL : extra_input32 - ); - - CHECKRESULT(!result, "secp256k1_frost_nonce_gen failed"); -// TODO: copy nonce result... -// memcpy(nonce, secnonce.data, fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE); -// result = secp256k1_musig_pubnonce_serialize(ctx, nonce + fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE, &pubnonce); -// CHECKRESULT(!result, "secp256k1_musig_pubnonce_serialize failed"); -// -// jnonce = (*penv)->NewByteArray(penv, sizeof(nonce)); -// nonce_ptr = (*penv)->GetByteArrayElements(penv, jnonce, 0); -// memcpy(nonce_ptr, nonce, sizeof(nonce)); -// (*penv)->ReleaseByteArrayElements(penv, jnonce, nonce_ptr, 0); -// -// return jnonce; + CHECKRESULT(!result, "secp256k1_frost_nonce_gen failed"); + // TODO: copy nonce result... + // memcpy(nonce, secnonce.data, fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE); + // result = secp256k1_musig_pubnonce_serialize(ctx, nonce + fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE, &pubnonce); + // CHECKRESULT(!result, "secp256k1_musig_pubnonce_serialize failed"); + // + // jnonce = (*penv)->NewByteArray(penv, sizeof(nonce)); + // nonce_ptr = (*penv)->GetByteArrayElements(penv, jnonce, 0); + // memcpy(nonce_ptr, nonce, sizeof(nonce)); + // (*penv)->ReleaseByteArrayElements(penv, jnonce, nonce_ptr, 0); + // + // return jnonce; } @@ -1870,92 +1893,97 @@ JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp2 JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1nonce_1process (JNIEnv *penv, jclass clazz, jlong jctx, jobjectArray jpubnonces, jint n_pubnonces, jbyteArray jmsg32, jbyteArray jpubkey, jbyteArray jmy_id33, jobjectArray jids33, jbyteArray jtweak_cache, jbyteArray jadaptor) { - secp256k1_context *ctx = (secp256k1_context *)jctx; + secp256k1_context *ctx = (secp256k1_context *)jctx; - secp256k1_frost_session session; + secp256k1_frost_session session; - secp256k1_frost_pubnonce **pubnonces; - jbyte *in66, *pub, *id33;; + secp256k1_frost_pubnonce **pubnonces; + jbyte *in66, *pub, *id33;; - unsigned char msg32[32]; + unsigned char msg32[32]; - secp256k1_xonly_pubkey public_key; + secp256k1_xonly_pubkey public_key; - const jbyte *ids33[jn_participants]; + const jbyte *ids33[jn_participants]; - secp256k1_frost_tweak_cache tweak_cache; - secp256k1_pubkey adaptor; + secp256k1_frost_tweak_cache tweak_cache; + secp256k1_pubkey adaptor; - count = (*penv)->GetArrayLength(penv, jpubnonces); - CHECKRESULT(count <= 0, "public nonces count cannot be 0"); + size_t size, count; - pubnonces = calloc(count, sizeof(secp256k1_frost_pubnonce *)); + if (jctx == 0) + return NULL; - for (i = 0; i < count; i++) - { - pubnonces[i] = calloc(1, sizeof(secp256k1_frost_pubnonce)); - jnonce = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jpubnonces, i); - size = (*penv)->GetArrayLength(penv, jnonce); - CHECKRESULT1(size != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE, "invalid public nonce size", free_nonces(pubnonces, count)); - in66 = (*penv)->GetByteArrayElements(penv, jnonce, 0); - result = secp256k1_frost_pubnonce_parse(ctx, pubnonces[i], (unsigned char *)in66); - (*penv)->ReleaseByteArrayElements(penv, jnonce, in66, 0); - CHECKRESULT1(!result, "secp256k1_frost_pubnonce_parse failed", free_nonces(pubnonces, count)); - } + count = (*penv)->GetArrayLength(penv, jpubnonces); + CHECKRESULT(count <= 0, "public nonces count cannot be 0"); - if (jmsg32 != NULL) + pubnonces = calloc(count, sizeof(secp256k1_frost_pubnonce *)); + + for (i = 0; i < count; i++) + { + pubnonces[i] = calloc(1, sizeof(secp256k1_frost_pubnonce)); + jnonce = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jpubnonces, i); + size = (*penv)->GetArrayLength(penv, jnonce); + CHECKRESULT1(size != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE, "invalid public nonce size", free_nonces(pubnonces, count)); + in66 = (*penv)->GetByteArrayElements(penv, jnonce, 0); + result = secp256k1_frost_pubnonce_parse(ctx, pubnonces[i], (unsigned char *)in66); + (*penv)->ReleaseByteArrayElements(penv, jnonce, in66, 0); + CHECKRESULT1(!result, "secp256k1_frost_pubnonce_parse failed", free_nonces(pubnonces, count)); + } + + if (jmsg32 != NULL) + { + size = (*penv)->GetArrayLength(penv, jmsg32); + CHECKRESULT(size != 32, "invalid message size"); + copy_bytes_from_java(penv, jmsg32, size, msg32); + } + + pub = (*penv)->GetByteArrayElements(penv, jpubkey, 0); + result = secp256k1_xonly_pubkey_parse(ctx, &public_key, (unsigned char *)pub); + (*penv)->ReleaseByteArrayElements(penv, jpubkey, pub, 0); + CHECKRESULT(!result, "secp256k1_xonly_pubkey_parse failed"); + + id33 = (*penv)->GetByteArrayElements(penv, jmy_id33, 0); + + // Copy over data from jids33 + if (jids33 != NULL) + { + size = (*penv)->GetArrayLength(penv, jids33); + CHECKRESULT(size != n_pubnonces, "invalid ids33 size"); + for (i = 0; i < n_pubnonces; i++) { - size = (*penv)->GetArrayLength(penv, jmsg32); - CHECKRESULT(size != 32, "invalid message size"); - copy_bytes_from_java(penv, jmsg32, size, msg32); - } - - pub = (*penv)->GetByteArrayElements(penv, jpubkey, 0); - result = secp256k1_xonly_pubkey_parse(ctx, &public_key, (unsigned char *)pub); - (*penv)->ReleaseByteArrayElements(penv, jpubkey, pub, 0); - CHECKRESULT(!result, "secp256k1_xonly_pubkey_parse failed"); - - id33 = (*penv)->GetByteArrayElements(penv, jmy_id33, 0); - - // Copy over data from jids33 - if (jids33 != NULL) - { - 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); - // TODO: Check id33 size is 33... - ids33[i] = (*penv)->GetByteArrayElements(penv, id33, 0); - } + jbyteArray id33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i); + // TODO: Check id33 size is 33... + ids33[i] = (*penv)->GetByteArrayElements(penv, id33, 0); } + } - size = (*penv)->GetArrayLength(penv, jtweak_cache); - CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); - copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data); + size = (*penv)->GetArrayLength(penv, jtweak_cache); + 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) - { - pub = (*penv)->GetByteArrayElements(penv, jadaptor, 0); - result = secp256k1_ec_pubkey_parse(ctx, &adaptor, (unsigned char *)pub, pubSize); - (*penv)->ReleaseByteArrayElements(penv, jadaptor, pub, 0); - CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed"); - } - int result = secp256k1_frost_nonce_process( - ctx, - &session, - (const secp256k1_frost_pubnonce *const *)pubnonces, - n_pubnonces, - msg32, - &public_key, - id33, - ids33, - &tweak_cache, - jadaptor == NULL ? NULL : adaptor - ); + if (jadaptor != NULL) + { + pub = (*penv)->GetByteArrayElements(penv, jadaptor, 0); + result = secp256k1_ec_pubkey_parse(ctx, &adaptor, (unsigned char *)pub, pubSize); + (*penv)->ReleaseByteArrayElements(penv, jadaptor, pub, 0); + CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed"); + } + int result = secp256k1_frost_nonce_process( + ctx, + &session, + (const secp256k1_frost_pubnonce *const *)pubnonces, + n_pubnonces, + msg32, + &public_key, + id33, + ids33, + &tweak_cache, + jadaptor == NULL ? NULL : adaptor + ); - CHECKRESULT(!result, "secp256k1_frost_nonce_process failed"); + CHECKRESULT(!result, "secp256k1_frost_nonce_process failed"); size = sizeof(secp256k1_frost_session); jbyteArray jsession = (*penv)->NewByteArray(penv, size); @@ -1972,56 +2000,61 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1partial_1sign (JNIEnv *penv, jclass clazz, jlong jctx, jbyteArray jsecnonce, jbyteArray jagg_share, jbyteArray jsession, jbyteArray jtweak_cache) { - secp256k1_context *ctx = (secp256k1_context *)jctx; + secp256k1_context *ctx = (secp256k1_context *)jctx; - secp256k1_frost_partial_sig partial_sig; - secp256k1_frost_secnonce secnonce; + secp256k1_frost_partial_sig partial_sig; + secp256k1_frost_secnonce secnonce; - secp256k1_frost_share agg_share; - jbyte *in32; + secp256k1_frost_share agg_share; + jbyte *in32; - secp256k1_frost_session session; + secp256k1_frost_session session; - secp256k1_frost_tweak_cache tweak_cache; + secp256k1_frost_tweak_cache tweak_cache; - jbyteArray jpsig; - jbyte *ptr; + jbyteArray jpsig; + jbyte *ptr; - copy_bytes_from_java(penv, jsecnonce, fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE, secnonce.data); + size_t size, count; - if (jagg_share != NULL) { - agg_share = calloc(1, sizeof(secp256k1_frost_share)); - size = (*penv)->GetArrayLength(penv, jagg_share); - CHECKRESULT1(size != sizeof(secp256k1_frost_share), "invalid agg_share size", free_shares(shares, count)); - in32 = (*penv)->GetByteArrayElements(penv, jagg_share, 0); - result = secp256k1_frost_share_parse(ctx, agg_share, (unsigned char *)in32); - (*penv)->ReleaseByteArrayElements(penv, jagg_share, in32, 0); - CHECKRESULT1(!result, "secp256k1_frost_share_parse failed", free_shares(shares, count)); - } + if (jctx == 0) + return NULL; - CHECKRESULT((*penv)->GetArrayLength(penv, jsession) != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SESSION_SIZE, "invalid session size"); - copy_bytes_from_java(penv, jsession, fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SESSION_SIZE, session.data); + copy_bytes_from_java(penv, jsecnonce, fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE, secnonce.data); - size = (*penv)->GetArrayLength(penv, jtweak_cache); - CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); - copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data); + if (jagg_share != NULL) { + agg_share = calloc(1, sizeof(secp256k1_frost_share)); + size = (*penv)->GetArrayLength(penv, jagg_share); + CHECKRESULT1(size != sizeof(secp256k1_frost_share), "invalid agg_share size", free_shares(shares, count)); + in32 = (*penv)->GetByteArrayElements(penv, jagg_share, 0); + result = secp256k1_frost_share_parse(ctx, agg_share, (unsigned char *)in32); + (*penv)->ReleaseByteArrayElements(penv, jagg_share, in32, 0); + CHECKRESULT1(!result, "secp256k1_frost_share_parse failed", free_shares(shares, count)); + } - int result = secp256k1_frost_partial_sign( - ctx, - &partial_sig, - &secnonce, - &agg_share, - &session, - &tweak_cache - ); - CHECKRESULT(!result, "secp256k1_frost_partial_sign failed"); + CHECKRESULT((*penv)->GetArrayLength(penv, jsession) != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SESSION_SIZE, "invalid session size"); + copy_bytes_from_java(penv, jsession, fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SESSION_SIZE, session.data); - jpsig = (*penv)->NewByteArray(penv, 32); - ptr = (*penv)->GetByteArrayElements(penv, jpsig, 0); - result = secp256k1_frost_partial_sig_serialize(ctx, (unsigned char *)ptr, &partial_sig); - CHECKRESULT(!result, "secp256k1_frost_partial_sig_serialize failed"); + size = (*penv)->GetArrayLength(penv, jtweak_cache); + CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); + copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data); - return jpsig; + int result = secp256k1_frost_partial_sign( + ctx, + &partial_sig, + &secnonce, + &agg_share, + &session, + &tweak_cache + ); + CHECKRESULT(!result, "secp256k1_frost_partial_sign failed"); + + jpsig = (*penv)->NewByteArray(penv, 32); + ptr = (*penv)->GetByteArrayElements(penv, jpsig, 0); + result = secp256k1_frost_partial_sig_serialize(ctx, (unsigned char *)ptr, &partial_sig); + CHECKRESULT(!result, "secp256k1_frost_partial_sig_serialize failed"); + + return jpsig; } /* @@ -2032,56 +2065,61 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 JNIEXPORT jint JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1partial_1sig_1verify (JNIEnv *penv, jclass clazz, jlong jctx, jbyteArray jpartial_sig, jbyteArray jpubnonce, jbyteArray jpubshare, jbyteArray jsession, jbyteArray jtweak_cache) { - secp256k1_context *ctx = (secp256k1_context *)jctx; + secp256k1_context *ctx = (secp256k1_context *)jctx; - secp256k1_frost_partial_sig partial_sig; + secp256k1_frost_partial_sig partial_sig; - secp256k1_frost_pubnonce pubnonce; + secp256k1_frost_pubnonce pubnonce; - secp256k1_pubkey pubshare; + secp256k1_pubkey pubshare; - secp256k1_frost_session session; - secp256k1_frost_tweak_cache tweak_cache; + secp256k1_frost_session session; + secp256k1_frost_tweak_cache tweak_cache; - jbyte *ptr, *pubkeyBytes; + jbyte *ptr, *pubkeyBytes; - ptr = (*penv)->GetByteArrayElements(penv, jpartial_sig, 0); - result = secp256k1_frost_partial_sig_parse(ctx, &partial_sig, ptr); - (*penv)->ReleaseByteArrayElements(penv, jpartial_sig, ptr, 0); - CHECKRESULT(!result, "secp256k1_frost_partial_sig_parse failed"); + size_t size, count; - ptr = (*penv)->GetByteArrayElements(penv, jpubnonce, 0); - result = secp256k1_frost_pubnonce_parse(ctx, &pubnonce, ptr); - (*penv)->ReleaseByteArrayElements(penv, jpubnonce, ptr, 0); - CHECKRESULT(!result, "secp256k1_frost_pubnonce_parse failed"); + if (jctx == 0) + return NULL; - size = (*penv)->GetArrayLength(penv, jpubkey); - CHECKRESULT((size != 33) && (size != 65), "invalid public key size"); + ptr = (*penv)->GetByteArrayElements(penv, jpartial_sig, 0); + result = secp256k1_frost_partial_sig_parse(ctx, &partial_sig, ptr); + (*penv)->ReleaseByteArrayElements(penv, jpartial_sig, ptr, 0); + CHECKRESULT(!result, "secp256k1_frost_partial_sig_parse failed"); - pubkeyBytes = (*penv)->GetByteArrayElements(penv, jpubshare, 0); - result = secp256k1_ec_pubkey_parse(ctx, &pubshare, (unsigned char *)pubkeyBytes, size); - (*penv)->ReleaseByteArrayElements(penv, jpubshare, pubkeyBytes, 0); - CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed"); + ptr = (*penv)->GetByteArrayElements(penv, jpubnonce, 0); + result = secp256k1_frost_pubnonce_parse(ctx, &pubnonce, ptr); + (*penv)->ReleaseByteArrayElements(penv, jpubnonce, ptr, 0); + CHECKRESULT(!result, "secp256k1_frost_pubnonce_parse failed"); + + size = (*penv)->GetArrayLength(penv, jpubkey); + CHECKRESULT((size != 33) && (size != 65), "invalid public key size"); + + pubkeyBytes = (*penv)->GetByteArrayElements(penv, jpubshare, 0); + result = secp256k1_ec_pubkey_parse(ctx, &pubshare, (unsigned char *)pubkeyBytes, size); + (*penv)->ReleaseByteArrayElements(penv, jpubshare, pubkeyBytes, 0); + CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed"); - size = (*penv)->GetArrayLength(penv, jsession); - CHECKRESULT(size != sizeof(secp256k1_frost_session), "invalid session size"); - copy_bytes_from_java(penv, jsession, size, session.data); + size = (*penv)->GetArrayLength(penv, jsession); + CHECKRESULT(size != sizeof(secp256k1_frost_session), "invalid session size"); + copy_bytes_from_java(penv, jsession, size, session.data); - size = (*penv)->GetArrayLength(penv, jtweak_cache); - CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); - copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data); + size = (*penv)->GetArrayLength(penv, jtweak_cache); + CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); + copy_bytes_from_java(penv, jtweak_cache, size, tweak_cache.data); - int result = secp256k1_frost_partial_sig_verify( - ctx, - &partial_sig, - &pubnonce, - &pubshare, - &session, - &tweak_cache - ); + int result = secp256k1_frost_partial_sig_verify( + ctx, + &partial_sig, + &pubnonce, + &pubshare, + &session, + &tweak_cache + ); - return result; + return result; } void free_partial_sigs(secp256k1_frost_partial_sig **psigs, size_t count) @@ -2103,59 +2141,59 @@ void free_partial_sigs(secp256k1_frost_partial_sig **psigs, size_t count) JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1partial_1sig_1agg (JNIEnv *penv, jclass clazz, jlong jctx, jbyteArray jsession, jobjectArray jpartial_sigs, jint jn_sigs) { - secp256k1_context *ctx = (secp256k1_context *)jctx; + secp256k1_context *ctx = (secp256k1_context *)jctx; - unsigned char sig64[64]; + unsigned char sig64[64]; - secp256k1_frost_session session; - secp256k1_frost_partial_sig **psigs; + secp256k1_frost_session session; + secp256k1_frost_partial_sig **psigs; - jbyteArray jpsig; - jbyte *ptr; - size_t size, count; - size_t i; - int result = 0; + jbyteArray jpsig; + jbyte *ptr; + size_t size, count; + size_t i; + int result = 0; - if (jctx == 0) - return NULL; - if (jsession == NULL) - return NULL; + if (jctx == 0) + return NULL; + if (jsession == NULL) + return NULL; - size = (*penv)->GetArrayLength(penv, jsession); - CHECKRESULT(size != sizeof(secp256k1_frost_session), "invalid session size"); - copy_bytes_from_java(penv, jsession, size, session.data); + size = (*penv)->GetArrayLength(penv, jsession); + CHECKRESULT(size != sizeof(secp256k1_frost_session), "invalid session size"); + copy_bytes_from_java(penv, jsession, size, session.data); - if (jpsigs == NULL) - return NULL; + if (jpsigs == NULL) + return NULL; - count = (*penv)->GetArrayLength(penv, jpsigs); - CHECKRESULT(count <= 0, "partial sigs count cannot be 0"); + count = (*penv)->GetArrayLength(penv, jpsigs); + CHECKRESULT(count <= 0, "partial sigs count cannot be 0"); - psigs = calloc(count, sizeof(secp256k1_frost_partial_sig *)); + psigs = calloc(count, sizeof(secp256k1_frost_partial_sig *)); - for (i = 0; i < count; i++) - { - psigs[i] = calloc(1, sizeof(secp256k1_frost_partial_sig)); - jpsig = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jpsigs, i); - size = (*penv)->GetArrayLength(penv, jpsig); - CHECKRESULT1(size != 32, "invalid partial signature size", free_partial_sigs(psigs, count)); - ptr = (*penv)->GetByteArrayElements(penv, jpsig, 0); - result = secp256k1_frost_partial_sig_parse(ctx, psigs[i], (unsigned char *)ptr); - (*penv)->ReleaseByteArrayElements(penv, jpsig, ptr, 0); - CHECKRESULT1(!result, "secp256k1_frost_partial_sig_parse failed", free_partial_sigs(psigs, count)); - } + for (i = 0; i < count; i++) + { + psigs[i] = calloc(1, sizeof(secp256k1_frost_partial_sig)); + jpsig = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jpsigs, i); + size = (*penv)->GetArrayLength(penv, jpsig); + CHECKRESULT1(size != 32, "invalid partial signature size", free_partial_sigs(psigs, count)); + ptr = (*penv)->GetByteArrayElements(penv, jpsig, 0); + result = secp256k1_frost_partial_sig_parse(ctx, psigs[i], (unsigned char *)ptr); + (*penv)->ReleaseByteArrayElements(penv, jpsig, ptr, 0); + CHECKRESULT1(!result, "secp256k1_frost_partial_sig_parse failed", free_partial_sigs(psigs, count)); + } - result = secp256k1_frost_partial_sig_agg( - ctx, - sig64, - &session, - (const secp256k1_frost_partial_sig *const *)psigs, - jn_sigs - ); - free_partial_sigs(psigs, count); - CHECKRESULT(!result, "secp256k1_frost_partial_sig_agg failed"); + result = secp256k1_frost_partial_sig_agg( + ctx, + sig64, + &session, + (const secp256k1_frost_partial_sig *const *)psigs, + jn_sigs + ); + free_partial_sigs(psigs, count); + CHECKRESULT(!result, "secp256k1_frost_partial_sig_agg failed"); - jpsig = (*penv)->NewByteArray(penv, 64); - copy_bytes_to_java(penv, jpsig, 64, sig64); - return jpsig; + jpsig = (*penv)->NewByteArray(penv, 64); + copy_bytes_to_java(penv, jpsig, 64, sig64); + return jpsig; } \ No newline at end of file