Skip to content

Commit 37fd684

Browse files
committed
Use static data instead of dynamically allocated memory
1 parent 0d25374 commit 37fd684

File tree

4 files changed

+28
-80
lines changed

4 files changed

+28
-80
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ buildscript {
2323

2424
allprojects {
2525
group = "fr.acinq.secp256k1"
26-
version = "0.21.0"
26+
version = "0.21.1-MEMTEST-SNAPSHOT"
2727

2828
repositories {
2929
google()

jni/c/src/fr_acinq_secp256k1_Secp256k1CFunctions.c

Lines changed: 25 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -475,18 +475,6 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
475475
return jpubkey;
476476
}
477477

478-
static void free_pubkeys(secp256k1_pubkey **pubkeys, size_t count)
479-
{
480-
size_t i;
481-
if (pubkeys == NULL) return;
482-
for (i = 0; i < count; i++)
483-
{
484-
if (pubkeys[i] != NULL)
485-
free(pubkeys[i]);
486-
}
487-
free(pubkeys);
488-
}
489-
490478
/*
491479
* Class: fr_acinq_bitcoin_Secp256k1Bindings
492480
* Method: secp256k1_ec_pubkey_combine
@@ -496,7 +484,8 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
496484
{
497485
const secp256k1_context *ctx = (const secp256k1_context *)jctx;
498486
jbyte pub[65];
499-
secp256k1_pubkey **pubkeys;
487+
static secp256k1_pubkey pubkeys[100];
488+
secp256k1_pubkey* publkeys_ptr[100];
500489
secp256k1_pubkey combined;
501490
jbyteArray jpubkey;
502491
size_t size, count;
@@ -508,24 +497,20 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
508497
if (jpubkeys == NULL)
509498
return NULL;
510499

500+
for (i = 0; i < 100; i++) publkeys_ptr[i] = &pubkeys[i];
511501
count = (*penv)->GetArrayLength(penv, jpubkeys);
512502
CHECKRESULT(count < 1, "pubkey array cannot be empty")
513-
pubkeys = calloc(count, sizeof(secp256k1_pubkey *));
514-
CHECKRESULT(pubkeys == NULL, "memory allocation failed");
515503

516504
for (i = 0; i < count; i++)
517505
{
518-
pubkeys[i] = calloc(1, sizeof(secp256k1_pubkey));
519-
CHECKRESULT1(pubkeys[i] == NULL, "memory allocation failed", free_pubkeys(pubkeys, i));
520506
jpubkey = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jpubkeys, i);
521507
size = (*penv)->GetArrayLength(penv, jpubkey);
522-
CHECKRESULT1((size != 33) && (size != 65), "invalid public key size", free_pubkeys(pubkeys, i));
508+
CHECKRESULT((size != 33) && (size != 65), "invalid public key size");
523509
(*penv)->GetByteArrayRegion(penv, jpubkey, 0, size, pub);
524-
result = secp256k1_ec_pubkey_parse(ctx, pubkeys[i], (unsigned char *)pub, size);
525-
CHECKRESULT1(!result, "secp256k1_ec_pubkey_parse failed", free_pubkeys(pubkeys, i));
510+
result = secp256k1_ec_pubkey_parse(ctx, &pubkeys[i], (unsigned char *)pub, size);
511+
CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed");
526512
}
527513
result = secp256k1_ec_pubkey_combine(ctx, &combined, (const secp256k1_pubkey *const *)pubkeys, count);
528-
free_pubkeys(pubkeys, count);
529514
CHECKRESULT(!result, "secp256k1_ec_pubkey_combine failed");
530515

531516
size = 65;
@@ -869,18 +854,6 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
869854
return jnonce;
870855
}
871856

872-
static void free_nonces(secp256k1_musig_pubnonce **nonces, size_t count)
873-
{
874-
size_t i;
875-
if (nonces == NULL) return;
876-
for (i = 0; i < count; i++)
877-
{
878-
if (nonces[i] != NULL)
879-
free(nonces[i]);
880-
}
881-
free(nonces);
882-
}
883-
884857
/*
885858
* Class: fr_acinq_secp256k1_Secp256k1CFunctions
886859
* Method: secp256k1_musig_nonce_agg
@@ -890,7 +863,8 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
890863
{
891864
const secp256k1_context *ctx = (const secp256k1_context *)jctx;
892865
jbyte in66[fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE];
893-
secp256k1_musig_pubnonce **pubnonces;
866+
static secp256k1_musig_pubnonce pubnonces[100];
867+
secp256k1_musig_pubnonce* pubnonces_ptr[100];
894868
secp256k1_musig_aggnonce combined;
895869
jbyteArray jnonce;
896870
size_t size, count;
@@ -899,31 +873,21 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
899873

900874
if (jctx == 0) return NULL;
901875
if (jnonces == NULL) return NULL;
876+
for (i = 0; i < 100; i++) pubnonces_ptr[i] = &pubnonces[i];
902877

903878
count = (*penv)->GetArrayLength(penv, jnonces);
904879
CHECKRESULT(count == 0, "public nonces count cannot be 0");
905-
for (i = 0; i < count; i++)
906-
{
907-
jnonce = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jnonces, i);
908-
size = (*penv)->GetArrayLength(penv, jnonce);
909-
CHECKRESULT(size != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE, "invalid public nonce size");
910-
}
911-
912-
pubnonces = calloc(count, sizeof(secp256k1_musig_pubnonce *));
913-
CHECKRESULT(pubnonces == NULL, "memory allocation error");
914880

915881
for (i = 0; i < count; i++)
916882
{
917-
pubnonces[i] = calloc(1, sizeof(secp256k1_musig_pubnonce));
918-
CHECKRESULT1(pubnonces[i] == NULL, "memory allocation error", free_nonces(pubnonces, i));
919883
jnonce = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jnonces, i);
920884
size = fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE;
885+
CHECKRESULT(size != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_PUBLIC_NONCE_SIZE, "invalid public nonce size");
921886
(*penv)->GetByteArrayRegion(penv, jnonce, 0, size, in66);
922-
result = secp256k1_musig_pubnonce_parse(ctx, pubnonces[i], (unsigned char *)in66);
923-
CHECKRESULT1(!result, "secp256k1_musig_pubnonce_parse failed", free_nonces(pubnonces, i));
887+
result = secp256k1_musig_pubnonce_parse(ctx, &pubnonces[i], (unsigned char *)in66);
888+
CHECKRESULT(!result, "secp256k1_musig_pubnonce_parse failed");
924889
}
925890
result = secp256k1_musig_nonce_agg(ctx, &combined, (const secp256k1_musig_pubnonce *const *)pubnonces, count);
926-
free_nonces(pubnonces, count);
927891
CHECKRESULT(!result, "secp256k1_musig_nonce_agg failed");
928892

929893
result = secp256k1_musig_aggnonce_serialize(ctx, (unsigned char *)in66, &combined);
@@ -942,14 +906,16 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
942906
{
943907
const secp256k1_context *ctx = (const secp256k1_context *)jctx;
944908
jbyte pub[65];
945-
secp256k1_pubkey **pubkeys;
909+
static secp256k1_pubkey pubkeys[100];
910+
secp256k1_pubkey* pubkeys_ptr[100];
946911
secp256k1_xonly_pubkey combined;
947912
secp256k1_musig_keyagg_cache keyaggcache;
948913
jbyteArray jpubkey;
949914
size_t size, count;
950915
size_t i;
951916
int result = 0;
952917

918+
for (i = 0; i < 100; i++) pubkeys_ptr[i] = &(pubkeys[i]);
953919
if (jctx == 0) return NULL;
954920
if (jpubkeys == NULL) return NULL;
955921
count = (*penv)->GetArrayLength(penv, jpubkeys);
@@ -968,21 +934,17 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
968934
(*penv)->GetByteArrayRegion(penv, jkeyaggcache, 0, size, keyaggcache.data);
969935
}
970936

971-
pubkeys = calloc(count, sizeof(secp256k1_pubkey *));
972937
CHECKRESULT(pubkeys == NULL, "memory allocation error");
973938

974939
for (i = 0; i < count; i++)
975940
{
976-
pubkeys[i] = calloc(1, sizeof(secp256k1_pubkey));
977-
CHECKRESULT1(pubkeys[i] == NULL, "memory allocation error", free_pubkeys(pubkeys, i));
978941
jpubkey = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jpubkeys, i);
979942
size = (*penv)->GetArrayLength(penv, jpubkey);
980943
(*penv)->GetByteArrayRegion(penv, jpubkey, 0, size, pub);
981-
result = secp256k1_ec_pubkey_parse(ctx, pubkeys[i], (unsigned char *)pub, size);
982-
CHECKRESULT1(!result, "secp256k1_ec_pubkey_parse failed", free_pubkeys(pubkeys, i));
944+
result = secp256k1_ec_pubkey_parse(ctx, &pubkeys[i], (unsigned char *)pub, size);
945+
CHECKRESULT(!result, "secp256k1_ec_pubkey_parse failed");
983946
}
984947
result = secp256k1_musig_pubkey_agg(ctx, &combined, jkeyaggcache == NULL ? NULL : &keyaggcache, (const secp256k1_pubkey *const *)pubkeys, count);
985-
free_pubkeys(pubkeys, count);
986948
CHECKRESULT(!result, "secp256k1_musig_pubkey_agg failed");
987949
result = secp256k1_xonly_pubkey_serialize(ctx, (unsigned char *)pub, &combined);
988950
CHECKRESULT(!result, "secp256k1_xonly_pubkey_serialize failed");
@@ -1222,18 +1184,6 @@ JNIEXPORT jint JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256k1_1mu
12221184
return result;
12231185
}
12241186

1225-
static void free_partial_sigs(secp256k1_musig_partial_sig **psigs, size_t count)
1226-
{
1227-
size_t i;
1228-
if (psigs == NULL) return;
1229-
for (i = 0; i < count; i++)
1230-
{
1231-
if (psigs[i] != NULL)
1232-
free(psigs[i]);
1233-
}
1234-
free(psigs);
1235-
}
1236-
12371187
/*
12381188
* Class: fr_acinq_secp256k1_Secp256k1CFunctions
12391189
* Method: secp256k1_musig_partial_sig_agg
@@ -1243,13 +1193,16 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
12431193
{
12441194
const secp256k1_context *ctx = (const secp256k1_context *)jctx;
12451195
secp256k1_musig_session session;
1246-
secp256k1_musig_partial_sig **psigs;
1196+
static secp256k1_musig_partial_sig psigs[100];
1197+
secp256k1_musig_partial_sig* psigs_ptr[100];
1198+
12471199
unsigned char sig64[64];
12481200
jbyteArray jpsig;
12491201
size_t size, count;
12501202
size_t i;
12511203
int result = 0;
12521204

1205+
for (i = 0; i < 100; i++) psigs_ptr[i] = &(psigs[i]);
12531206
if (jctx == 0) return NULL;
12541207
if (jsession == NULL) return NULL;
12551208
CHECKRESULT((*penv)->GetArrayLength(penv, jsession) != fr_acinq_secp256k1_Secp256k1CFunctions_SECP256K1_MUSIG_SESSION_SIZE, "invalid session size");
@@ -1258,23 +1211,18 @@ JNIEXPORT jbyteArray JNICALL Java_fr_acinq_secp256k1_Secp256k1CFunctions_secp256
12581211
if (jpsigs == NULL) return NULL;
12591212
count = (*penv)->GetArrayLength(penv, jpsigs);
12601213
CHECKRESULT(count == 0, "partial sigs count cannot be 0");
1261-
1262-
psigs = calloc(count, sizeof(secp256k1_musig_partial_sig *));
1263-
CHECKRESULT(psigs == NULL, "memory allocation error");
1214+
CHECKRESULT(count > 100, "partial sigs count cannot greater than 100");
12641215

12651216
for (i = 0; i < count; i++)
12661217
{
1267-
psigs[i] = calloc(1, sizeof(secp256k1_musig_partial_sig));
1268-
CHECKRESULT1(psigs[i] == NULL, "memory allocation error", free_partial_sigs(psigs, i));
12691218
jpsig = (jbyteArray)(*penv)->GetObjectArrayElement(penv, jpsigs, i);
12701219
size = (*penv)->GetArrayLength(penv, jpsig);
1271-
CHECKRESULT1(size != 32, "invalid partial signature size", free_partial_sigs(psigs, i));
1220+
CHECKRESULT(size != 32, "invalid partial signature size");
12721221
(*penv)->GetByteArrayRegion(penv, jpsig, 0, 32, (jbyte*)sig64);
1273-
result = secp256k1_musig_partial_sig_parse(ctx, psigs[i], sig64);
1274-
CHECKRESULT1(!result, "secp256k1_musig_partial_sig_parse failed", free_partial_sigs(psigs, i));
1222+
result = secp256k1_musig_partial_sig_parse(ctx, &(psigs[i]), sig64);
1223+
CHECKRESULT(!result, "secp256k1_musig_partial_sig_parse failed");
12751224
}
12761225
result = secp256k1_musig_partial_sig_agg(ctx, sig64, &session, (const secp256k1_musig_partial_sig *const *)psigs, count);
1277-
free_partial_sigs(psigs, count);
12781226
CHECKRESULT(!result, "secp256k1_musig_pubkey_agg failed");
12791227

12801228
jpsig = (*penv)->NewByteArray(penv, 64);

jni/jvm/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ JNI_HEADERS=$TARGET
1414

1515
if [ "$TARGET" == "linux" ]; then
1616
OUTFILE=libsecp256k1-jni.so
17-
CC_OPTS="-fPIC"
17+
CC_OPTS="-fPIC -O0 -g"
1818
elif [ "$TARGET" == "darwin" ]; then
1919
OUTFILE=libsecp256k1-jni.dylib
2020
CC_OPTS="-arch arm64 -arch x86_64"

native/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ else
2828
fi
2929

3030
./autogen.sh
31-
CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" ./configure $CONF_OPTS --enable-experimental --enable-module_ecdh --enable-module-recovery --enable-module-schnorrsig --enable-module-musig --enable-benchmark=no --enable-shared=no --enable-exhaustive-tests=no --enable-tests=no
31+
CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" ./configure $CONF_OPTS --enable-valgrind --enable-experimental --enable-module_ecdh --enable-module-recovery --enable-module-schnorrsig --enable-module-musig --enable-benchmark=no --enable-shared=no --enable-exhaustive-tests=no --enable-tests=no
3232
make clean
3333
make
3434

0 commit comments

Comments
 (0)