diff --git a/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c b/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c index 44e7257..b25a8f7 100644 --- a/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c +++ b/jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c @@ -1372,12 +1372,16 @@ JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp2 if (jctx == 0) return NULL; - if (jseed32 != NULL) + if (jseed32 == NULL) return NULL; - if (jids33 != NULL) + if (jids33 == NULL) return NULL; + CHECKRESULT(jthreshold <= 0, "threshold can't be 0"); + CHECKRESULT(jn_participants <= 0, "n_participants can't be 0"); + CHECKRESULT(jthreshold > jn_participants, "threshold can't be greater then n_participants"); + size = (*penv)->GetArrayLength(penv, jseed32); CHECKRESULT(size != 32, "invalid seed32 size"); copy_bytes_from_java(penv, jseed32, size, seed32); @@ -1461,6 +1465,19 @@ JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp2 if (jctx == 0) return NULL; + if (jshares == NULL) + return NULL; + + if (jvss_commitments == NULL) + return NULL; + + if (jid33 == NULL) + return NULL; + + CHECKRESULT(jthreshold <= 0, "threshold can't be 0"); + CHECKRESULT(jtotalShareCount <= 0, "totalShareCount can't be 0"); + CHECKRESULT(jthreshold > jtotalShareCount, "threshold can't be greater then totalShareCount"); + count = (*penv)->GetArrayLength(penv, jnonces); CHECKRESULT(count != jtotalShareCount, "jshares count should be total share count."); shares = calloc(count, sizeof(secp256k1_frost_share*)); @@ -1551,6 +1568,17 @@ JNIEXPORT jint JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1fr if (jctx == 0) return NULL; + if (jid33 == NULL) + return NULL; + + if (jshare == NULL) + return NULL; + + if (jvss_commitment == NULL) + return NULL; + + CHECKRESULT(jthreshold <= 0, "threshold can't be 0"); + 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)); @@ -1604,6 +1632,16 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 if (jctx == 0) return NULL; + if (jid33 == NULL) + return NULL; + + if (jvss_commitments == NULL) + return NULL; + + CHECKRESULT(jthreshold <= 0, "threshold can't be 0"); + CHECKRESULT(jtotalSignersCount <= 0, "totalSignersCount can't be 0"); + CHECKRESULT(jthreshold > jtotalSignersCount, "totalSignersCount can't be greater then n_participants"); + id33 = (*penv)->GetByteArrayElements(penv, jid33, 0); count = (*penv)->GetArrayLength(penv, jvss_commitments); @@ -1658,6 +1696,9 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 if (jctx == 0) return NULL; + if (jpublicKey == NULL) + 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); @@ -1756,6 +1797,8 @@ JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp2 return NULL; if (jtweak_cache == NULL) return NULL; + if (jtweak32 == NULL) + return NULL; size = (*penv)->GetArrayLength(penv, jtweak_cache); CHECKRESULT(size != sizeof(secp256k1_frost_tweak_cache), "invalid tweak_cache size"); @@ -1818,8 +1861,9 @@ JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp2 if (jctx == 0) return NULL; - if (jsession_id32 == 0) + if (jsession_id32 == NULL) 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); @@ -1914,8 +1958,26 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 if (jctx == 0) return NULL; + if (jpubnonces == NULL) + return NULL; + + if (jmsg32 == NULL) + return NULL; + + if (jpubkey == NULL) + return NULL; + + if (jmy_id33 == NULL) + return NULL; + + if (jids33 == NULL) + return NULL; + + CHECKRESULT(n_pubnonces <= 0, "n_pubnonces can't be 0"); + count = (*penv)->GetArrayLength(penv, jpubnonces); CHECKRESULT(count <= 0, "public nonces count cannot be 0"); + CHECKRESULT(n_pubnonces != 0, "n_pubnonces doesn't match public nonces count"); pubnonces = calloc(count, sizeof(secp256k1_frost_pubnonce *)); @@ -1931,12 +1993,9 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 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); - } + 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); @@ -1945,23 +2004,23 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 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, 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); + if (jtweak_cache != NULL) + { + 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) { @@ -1970,6 +2029,7 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 (*penv)->ReleaseByteArrayElements(penv, jadaptor, pub, 0); CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed"); } + int result = secp256k1_frost_nonce_process( ctx, &session, @@ -1979,10 +2039,9 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 &public_key, id33, ids33, - &tweak_cache, - jadaptor == NULL ? NULL : adaptor + jtweak_cache == NULL ? NULL : &tweak_cache, + jadaptor == NULL ? NULL : &adaptor ); - CHECKRESULT(!result, "secp256k1_frost_nonce_process failed"); size = sizeof(secp256k1_frost_session); @@ -2020,24 +2079,35 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 if (jctx == 0) return NULL; + if (jsecnonce == NULL) + return NULL; + + if (jagg_share == NULL) + return NULL; + + if (jsession == NULL) + return NULL; + copy_bytes_from_java(penv, jsecnonce, fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SECRET_NONCE_SIZE, secnonce.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)); - } + 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)); 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); - 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 (jtweak_cache != NULL) + { + 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_sign( ctx, @@ -2045,7 +2115,7 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 &secnonce, &agg_share, &session, - &tweak_cache + jtweak_cache == NULL ? NULL : &tweak_cache ); CHECKRESULT(!result, "secp256k1_frost_partial_sign failed"); @@ -2083,6 +2153,18 @@ JNIEXPORT jint JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1fr if (jctx == 0) return NULL; + if (jpartial_sig == NULL) + return NULL; + + if (jpubnonce == NULL) + return NULL; + + if (jpubshare == NULL) + return NULL; + + if (jsession == NULL) + return NULL; + ptr = (*penv)->GetByteArrayElements(penv, jpartial_sig, 0); result = secp256k1_frost_partial_sig_parse(ctx, &partial_sig, ptr); (*penv)->ReleaseByteArrayElements(penv, jpartial_sig, ptr, 0); @@ -2106,9 +2188,13 @@ JNIEXPORT jint JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1fr 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); + if (jtweak_cache != NULL) + { + 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, @@ -2156,9 +2242,15 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256 if (jctx == 0) return NULL; + if (jsession == NULL) return NULL; + if (jpartial_sigs == NULL) + return NULL; + + CHECKRESULT(jn_sigs <= 0, "n_sigs can't be 0"); + size = (*penv)->GetArrayLength(penv, jsession); CHECKRESULT(size != sizeof(secp256k1_frost_session), "invalid session size"); copy_bytes_from_java(penv, jsession, size, session.data);