test: introduce group order byte-array constant for deduplication
What changed, and why it matters
This commit is a simple cleanup in the test code. It replaces several copies of the same 32-byte secp256k1 curve group-order constant with a single shared constant. There is no change to the actual cryptographic library or to how tests behave; it only removes duplicated code.
No security action needed; this is a non-functional test refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces secp256k1_group_order_bytes in src/testutil.h and uses it in src/modules/ecdh/tests_impl.h and src/tests.c instead of locally-defined byte arrays. The values are identical, and the test logic is unchanged. One site now initializes the array to zeros and then memcpys the constant, which is functionally equivalent to the previous compile-time initialization.
Changed components
src/testutil.hsrc/modules/ecdh/tests_impl.hsrc/tests.cInspect captured patch +13 / −25
diff --git a/src/modules/ecdh/tests_impl.h b/src/modules/ecdh/tests_impl.h
index 6888f18..8265131 100644
--- a/src/modules/ecdh/tests_impl.h
+++ b/src/modules/ecdh/tests_impl.h
@@ -90,12 +90,7 @@ static void test_ecdh_generator_basepoint(void) {
static void test_bad_scalar(void) {
unsigned char s_zero[32] = { 0 };
- unsigned char s_overflow[32] = {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
- 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
- 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41
- };
+ unsigned char s_overflow[32] = { 0 };
unsigned char s_rand[32] = { 0 };
unsigned char output[32];
secp256k1_scalar rand;
@@ -107,6 +102,7 @@ static void test_bad_scalar(void) {
CHECK(secp256k1_ec_pubkey_create(CTX, &point, s_rand) == 1);
/* Try to multiply it by bad values */
+ memcpy(s_overflow, secp256k1_group_order_bytes, 32);
CHECK(secp256k1_ecdh(CTX, output, &point, s_zero, NULL, NULL) == 0);
CHECK(secp256k1_ecdh(CTX, output, &point, s_overflow, NULL, NULL) == 0);
/* ...and a good one */
diff --git a/src/tests.c b/src/tests.c
index 28bec59..cb3b3c4 100644
--- a/src/tests.c
+++ b/src/tests.c
@@ -6036,12 +6036,7 @@ static void run_ec_pubkey_parse_test(void) {
}
static void run_eckey_edge_case_test(void) {
- const unsigned char orderc[32] = {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
- 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
- 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41
- };
+ const unsigned char *orderc = secp256k1_group_order_bytes;
const unsigned char zeros[sizeof(secp256k1_pubkey)] = {0x00};
unsigned char ctmp[33];
unsigned char ctmp2[33];
@@ -6355,13 +6350,7 @@ static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char
return 1;
}
if (counter < 5) {
- static const unsigned char order[] = {
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
- 0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
- 0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
- };
- memcpy(nonce32, order, 32);
+ memcpy(nonce32, secp256k1_group_order_bytes, 32);
if (counter == 4) {
nonce32[31]++;
}
@@ -7379,12 +7368,7 @@ static void test_ecdsa_edge_cases(void) {
/* Privkey export where pubkey is the point at infinity. */
{
unsigned char privkey[300];
- unsigned char seckey[32] = {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
- 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
- 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41,
- };
+ const unsigned char *seckey = secp256k1_group_order_bytes;
size_t outlen = 300;
CHECK(!ec_privkey_export_der(CTX, privkey, &outlen, seckey, 0));
outlen = 300;
diff --git a/src/testutil.h b/src/testutil.h
index 64b3bb4..480f6a1 100644
--- a/src/testutil.h
+++ b/src/testutil.h
@@ -11,6 +11,14 @@
#include "testrand.h"
#include "util.h"
+/* group order of the secp256k1 curve in 32-byte big endian representation */
+static const unsigned char secp256k1_group_order_bytes[32] = {
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
+ 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
+ 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41
+};
+
static void testutil_random_fe(secp256k1_fe *x) {
unsigned char bin[32];
do {
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.