More logic for the JNI stuff

This commit is contained in:
kngako 2024-08-19 22:14:27 +02:00
parent b1e96f329b
commit 6e578d43d7
3 changed files with 24 additions and 34 deletions

View File

@ -270,9 +270,9 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
/*
* Class: fr_acinq_secp256k1_Secp256k1CFunctions
* Method: secp256k1_frost_shares_gen
* Signature: (J[B[BII[[B)[[[B
* Signature: (J[B[BII[[B)[B
*/
JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1shares_1gen
JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1shares_1gen
(JNIEnv *, jclass, jlong, jbyteArray, jbyteArray, jint, jint, jobjectArray);
/*

View File

@ -1363,9 +1363,9 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
/*
* Class: fr_acinq_secp256k1_Secp256k1CFunctions
* Method: secp256k1_frost_shares_gen
* Signature: (J[B[BII[[B)[[[B
* Signature: (J[B[BII[[B)[B
*/
JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1shares_1gen
JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1frost_1shares_1gen
(JNIEnv *penv, jclass clazz, jlong jctx, jbyteArray jpok64, jbyteArray jseed32, jint jthreshold, jint jn_participants, jobjectArray jids33)
{
secp256k1_context *ctx = (secp256k1_context *)jctx;
@ -1403,9 +1403,6 @@ JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp2
CHECKRESULT((*penv)->GetArrayLength(penv, jpok64) != 64, "pok64 length must be 64 bytes");
// shares = calloc(jn_participants, sizeof(secp256k1_frost_share*));
// vss_commitment = calloc(jthreshold, sizeof(secp256k1_pubkey*));
for (i = 0; i < jn_participants; i++)
{
jbyteArray jid33 = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jids33, i);
@ -1414,6 +1411,13 @@ JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp2
copy_bytes_from_java(penv, jid33, 33, ids33[i]);
}
int sharesLength = jn_participants * 32;
int vssCommitmentLength = jthreshold * 65;
jbyteArray jShareGenOutput;
jbyte *share_gen_output_ptr = NULL;
unsigned char shareGenOutput[sharesLength + vssCommitmentLength];
int result = 0;
pok64 = (*penv)->GetByteArrayElements(penv, jpok64, 0);
@ -1430,40 +1434,26 @@ JNIEXPORT jobjectArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp2
(*penv)->ReleaseByteArrayElements(penv, jpok64, pok64, 0);
CHECKRESULT(!result, "secp256k1_frost_shares_gen failed");
jobjectArray output = (*penv)->NewObjectArray(penv, 2, jobjectArray, NULL);
jobjectArray jshares = (*penv)->NewObjectArray(penv, jn_participants, jbyteArray, NULL);
// Copy shares into jshares
unsigned char out32[32];
for (i = 0; i < jn_participants; i++)
{
result = secp256k1_frost_share_serialize(ctx, out32, &shares[i]);
result = secp256k1_frost_share_serialize(ctx, shareGenOutput + (32 * i), &shares[i]);
CHECKRESULT(!result, "secp256k1_frost_share_serialize failed");
jbyteArray jshare = (*penv)->NewByteArray(penv, 32);
copy_bytes_to_java(penv, jshare, 32, out32);
jshares[i] = jshare;
}
output[0] = jshares;
jobjectArray jvss_commitment = (*penv)->NewObjectArray(penv, jthreshold, jbyteArray, NULL);
// Copy vss_commitment into jvss_commitment
for (i = 0; i < jn_participants; i++)
{
// need share object...
result = secp256k1_xonly_pubkey_serialize(ctx, out32, vss_commitment[i]);
CHECKRESULT(!result, "secp256k1_xonly_pubkey_serialize failed");
jbyteArray jpubkey = (*penv)->NewByteArray(penv, 32);
copy_bytes_to_java(penv, jpubkey, 32, out32);
jvss_commitment[i] = jpubkey;
size = 65;
secp256k1_ec_pubkey_serialize(ctx, shareGenOutput + sharesLength + (65*i), &size, &vss_commitment[i], SECP256K1_EC_UNCOMPRESSED);
CHECKRESULT(!result, "secp256k1_ec_pubkey_serialize failed");
}
output[1] = jvss_commitment;
return output;
jbyteArray jresult = (*penv)->NewByteArray(penv, sizeof(shareGenOutput));
share_gen_output_ptr = (*penv)->GetByteArrayElements(penv, jresult, 0);
memcpy(share_gen_output_ptr, shareGenOutput, sizeof(shareGenOutput));
(*penv)->ReleaseByteArrayElements(penv, jresult, share_gen_output_ptr, 0);
return jresult;
}
void free_shares(secp256k1_frost_share **shares, size_t count)
@ -1622,11 +1612,11 @@ JNIEXPORT jint JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1fr
size = (*penv)->GetArrayLength(penv, jshare);
CHECKRESULT1(size != sizeof(secp256k1_frost_share), "invalid share size", free(&share));
CHECKRESULT(size != sizeof(secp256k1_frost_share), "invalid share size");
in32 = (*penv)->GetByteArrayElements(penv, jshare, 0);
result = secp256k1_frost_share_parse(ctx, &share, (unsigned char *)in32);
(*penv)->ReleaseByteArrayElements(penv, jshare, in32, 0);
CHECKRESULT1(!result, "secp256k1_frost_share_parse failed", free(&share));
CHECKRESULT(!result, "secp256k1_frost_share_parse failed");
id33 = (*penv)->GetByteArrayElements(penv, jid33, 0);

View File

@ -134,7 +134,7 @@ public class Secp256k1CFunctions {
* [1] vss_commitment: pointer to the VSS commitment
* [2] pok64: pointer to the proof of knowledge
*/
public static native byte[][][] secp256k1_frost_shares_gen(long ctx, byte[] pok64, byte[] seed32, int threshold, int total_signers, byte[][] ids33);
public static native byte[] secp256k1_frost_shares_gen(long ctx, byte[] pok64, byte[] seed32, int threshold, int total_signers, byte[][] ids33);
/**
* Aggregates shares