Merge bitcoin-core/secp256k1#2007: Add a self-check for miscompiled `memcmp` and remove `secp256k1_memcmp_var`
What changed, and why it matters
This commit removes a custom safe-comparison helper called secp256k1_memcmp_var and switches the codebase back to the standard C memcmp. It also adds a startup self-test that detects compilers affected by a known GCC bug that could miscompile memcmp when comparing arrays that contain zero bytes. The change is a cleanup and hardening measure: it relies on the compiler's memcmp being correct, but adds a guard so the library refuses to run on a compiler that mishandles it.
Ensure builds use a compiler not affected by GCC bug 95189, or build with -fno-builtin-memcmp if using an affected GCC version. Run the library's self-tests (they now include a memcmp miscompilation check) before deployment. Reviewers should verify that all replaced comparisons are safe to expose to standard memcmp semantics and that the self-test reliably fails on the affected compiler versions.
Security signals we found
Removal of a workaround for a known compiler bug (GCC 95189)
Addition of a runtime self-test to detect the same compiler bug
Wide replacement of custom comparison helper with standard memcmp across cryptographic code paths
Magic-number and public-key comparison paths now depend on correct memcmp behavior
No direct vulnerability fix; this is a defensive refactor and build-time guard
Evidence from the diff
The PR removes secp256k1_memcmp_var(), a variable-time byte-by-byte comparison routine that was introduced as a workaround for GCC bug 95189. That bug caused GCC 9.2-9.5, 10.1 and 10.2 to optimize memcmp so it stopped comparing at the first zero byte of a constant array under -O2/-O3. The patch replaces every secp256k1_memcmp_var call with memcmp, including magic-number checks, public-key comparisons, test assertions, and scalar/field verification. To compensate, it adds secp256k1_selftest_memcmp() in src/selftest.h, which compares “\0abc” against “\0\0\0\0” using a volatile result and returns failure if the buggy optimization makes the comparison stop early. secp256k1_selftest_passes() now requires both the memcmp self-test and the SHA-256 self-test to succeed. CONTRIBUTING.md is updated to drop the rule about using secp256k1_memcmp_var.
Changed components
src/util.hsrc/selftest.hsrc/secp256k1.csrc/group_impl.hsrc/hash_impl.hsrc/scalar_impl.hsrc/scratch_impl.hsrc/modules/ecdhsrc/modules/ellswiftsrc/modules/extrakeyssrc/modules/musigsrc/modules/recoverysrc/modules/schnorrsigsrc/modules/silentpaymentssrc/tests.cInspect captured patch +292 / −295
### CONTRIBUTING.md
@@ -48,7 +48,6 @@ In addition, libsecp256k1 tries to maintain the following coding conventions:
* The tests should cover all lines and branches of the library (see [Test coverage](#coverage)).
* Operations involving secret data should be tested for being constant time with respect to the secrets (see [src/ctime_tests.c](src/ctime_tests.c)).
* Local variables containing secret data should be cleared explicitly to try to delete secrets from memory.
-* Use `secp256k1_memcmp_var` instead of `memcmp` (see [#823](https://github.com/bitcoin-core/secp256k1/issues/823)).
* As a rule of thumb, the default values for configuration options should target standard desktop machines and align with Bitcoin Core's defaults, and the tests should mostly exercise the default configuration (see [#1549](https://github.com/bitcoin-core/secp256k1/issues/1549#issuecomment-2200559257)).
#### Style conventions
### src/group_impl.h
@@ -1070,7 +1070,7 @@ static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *g
SECP256K1_INLINE static void secp256k1_ge_impl_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data) {
static const unsigned char zeros[64] = { 0 };
- if (secp256k1_memcmp_var(data, zeros, sizeof(zeros)) == 0) {
+ if (memcmp(data, zeros, sizeof(zeros)) == 0) {
secp256k1_ge_set_infinity(ge);
} else {
secp256k1_ge_from_bytes(ge, data);
@@ -1182,7 +1182,7 @@ static void secp256k1_ge_serialize_ext33(unsigned char *out33, secp256k1_ge *ge)
SECP256K1_INLINE static int secp256k1_ge_impl_parse_ext33(secp256k1_ge *ge, const unsigned char *in33) {
unsigned char zeros[33] = { 0 };
- if (secp256k1_memcmp_var(in33, zeros, sizeof(zeros)) == 0) {
+ if (memcmp(in33, zeros, sizeof(zeros)) == 0) {
secp256k1_ge_set_infinity(ge);
return 1;
}
### src/hash_impl.h
@@ -199,7 +199,7 @@ static int secp256k1_sha256_smoke_test(const secp256k1_sha256_compression_functi
/* Compare against pre-computed accumulated digest */
secp256k1_sha256_finalize(&ctx, &sha_accum, out);
- return secp256k1_memcmp_var(accum_expected, out, 32) == 0;
+ return memcmp(accum_expected, out, 32) == 0;
}
static void secp256k1_hash_ctx_init(secp256k1_hash_ctx *hash_ctx) {
### src/modules/ecdh/tests_exhaustive_impl.h
@@ -31,7 +31,7 @@ static void test_exhaustive_ecdh(const secp256k1_context *ctx, const secp256k1_g
/* Calculate ECDH(i*G, j) and ECDH(j*G, i) using API function and verify that the results match. */
CHECK(secp256k1_ecdh(ctx, ecdh_result_ij, &pubkeys[i - 1], seckeys[j - 1], NULL, NULL));
CHECK(secp256k1_ecdh(ctx, ecdh_result_ji, &pubkeys[j - 1], seckeys[i - 1], NULL, NULL));
- CHECK(secp256k1_memcmp_var(ecdh_result_ij, ecdh_result_ji, 32) == 0);
+ CHECK(memcmp(ecdh_result_ij, ecdh_result_ji, 32) == 0);
/* Recalculate the expected ECDH result manually by invoking the default ECDH hash
* function on the precomputed group element (group[i * j]) coordinates, and verify
@@ -47,7 +47,7 @@ static void test_exhaustive_ecdh(const secp256k1_context *ctx, const secp256k1_g
secp256k1_fe_get_b32(x, &ecdh_ge_expected.x);
secp256k1_fe_get_b32(y, &ecdh_ge_expected.y);
CHECK(secp256k1_ecdh_hash_function_default(ecdh_result_expected, x, y, NULL));
- CHECK(secp256k1_memcmp_var(ecdh_result_ij, ecdh_result_expected, 32) == 0);
+ CHECK(memcmp(ecdh_result_ij, ecdh_result_expected, 32) == 0);
}
}
}
### src/modules/ecdh/tests_impl.h
@@ -77,7 +77,7 @@ static void test_ecdh_generator_basepoint(void) {
/* compute "explicitly" */
CHECK(secp256k1_ec_pubkey_serialize(CTX, point_ser, &point_ser_len, &point[1], SECP256K1_EC_UNCOMPRESSED) == 1);
/* compare */
- CHECK(secp256k1_memcmp_var(output_ecdh, point_ser, 65) == 0);
+ CHECK(memcmp(output_ecdh, point_ser, 65) == 0);
/* compute using ECDH function with default hash function */
CHECK(secp256k1_ecdh(CTX, output_ecdh, &point[0], s_b32, NULL, NULL) == 1);
@@ -87,7 +87,7 @@ static void test_ecdh_generator_basepoint(void) {
secp256k1_sha256_write(&CTX->hash_ctx, &sha, point_ser, point_ser_len);
secp256k1_sha256_finalize(&CTX->hash_ctx, &sha, output_ser);
/* compare */
- CHECK(secp256k1_memcmp_var(output_ecdh, output_ser, 32) == 0);
+ CHECK(memcmp(output_ecdh, output_ser, 32) == 0);
}
}
@@ -109,7 +109,7 @@ static void test_ecdh_ctx_sha256(void) {
CHECK(secp256k1_ecdh(ctx, out_custom, &pubkey, sk, NULL, NULL) == 1);
/* Outputs must differ if custom compression was used */
- CHECK(secp256k1_memcmp_var(out_default, out_custom, 32) != 0);
+ CHECK(memcmp(out_default, out_custom, 32) != 0);
CHECK(sha256_ecdh_called);
secp256k1_context_destroy(ctx);
@@ -164,11 +164,11 @@ static void test_result_basepoint(void) {
CHECK(secp256k1_ec_pubkey_create(CTX, &point, s) == 1);
CHECK(secp256k1_ecdh(CTX, out, &point, s_inv, NULL, NULL) == 1);
- CHECK(secp256k1_memcmp_var(out, out_base, 32) == 0);
+ CHECK(memcmp(out, out_base, 32) == 0);
CHECK(secp256k1_ec_pubkey_create(CTX, &point, s_inv) == 1);
CHECK(secp256k1_ecdh(CTX, out_inv, &point, s, NULL, NULL) == 1);
- CHECK(secp256k1_memcmp_var(out_inv, out_base, 32) == 0);
+ CHECK(memcmp(out_inv, out_base, 32) == 0);
}
}
@@ -201,7 +201,7 @@ static void test_ecdh_wycheproof(void) {
CHECK(secp256k1_ecdh(CTX, output_ecdh, &point, sk, ecdh_hash_function_test_xpassthru, NULL) == 1);
expected_shared_secret = &wycheproof_ecdh_shared_secrets[testvectors[t].shared_offset];
- CHECK(secp256k1_memcmp_var(output_ecdh, expected_shared_secret, testvectors[t].shared_len) == 0);
+ CHECK(memcmp(output_ecdh, expected_shared_secret, testvectors[t].shared_len) == 0);
}
}
### src/modules/ellswift/tests_impl.h
@@ -232,7 +232,7 @@ void ellswift_xdh_test_vectors_tests(void) {
secp256k1_ellswift_xdh_hash_function_bip324,
NULL);
CHECK(ret);
- CHECK(secp256k1_memcmp_var(shared_secret, test->shared_secret, 32) == 0);
+ CHECK(memcmp(shared_secret, test->shared_secret, 32) == 0);
}
}
@@ -372,33 +372,33 @@ void ellswift_xdh_correctness_tests(void) {
ret = secp256k1_ellswift_xdh(CTX, share32b, ell64a, ell64b, sec32b, 1, hash_function, data);
CHECK(ret);
/* And compare: */
- CHECK(secp256k1_memcmp_var(share32a, share32b, 32) == 0);
+ CHECK(memcmp(share32a, share32b, 32) == 0);
/* Verify that the shared secret doesn't match if other side's public key is incorrect. */
/* For A (using a bad public key for B): */
memcpy(ell64b_bad, ell64b, sizeof(ell64a_bad));
testrand_flip(ell64b_bad, sizeof(ell64b_bad));
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b_bad, sec32a, 0, hash_function, data);
CHECK(ret); /* Mismatching encodings don't get detected by secp256k1_ellswift_xdh. */
- CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
+ CHECK(memcmp(share32_bad, share32a, 32) != 0);
/* For B (using a bad public key for A): */
memcpy(ell64a_bad, ell64a, sizeof(ell64a_bad));
testrand_flip(ell64a_bad, sizeof(ell64a_bad));
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a_bad, ell64b, sec32b, 1, hash_function, data);
CHECK(ret);
- CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
+ CHECK(memcmp(share32_bad, share32b, 32) != 0);
/* Verify that the shared secret doesn't match if the private key is incorrect. */
/* For A: */
memcpy(sec32a_bad, sec32a, sizeof(sec32a_bad));
testrand_flip(sec32a_bad, sizeof(sec32a_bad));
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32a_bad, 0, hash_function, data);
- CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
+ CHECK(!ret || memcmp(share32_bad, share32a, 32) != 0);
/* For B: */
memcpy(sec32b_bad, sec32b, sizeof(sec32b_bad));
testrand_flip(sec32b_bad, sizeof(sec32b_bad));
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32b_bad, 1, hash_function, data);
- CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
+ CHECK(!ret || memcmp(share32_bad, share32b, 32) != 0);
if (hash_function != ellswift_xdh_hash_x32) {
/* Verify that the shared secret doesn't match when a different encoding of the same public key is used. */
@@ -409,25 +409,25 @@ void ellswift_xdh_correctness_tests(void) {
CHECK(ret);
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b_bad, sec32a, 0, hash_function, data);
CHECK(ret);
- CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
+ CHECK(memcmp(share32_bad, share32a, 32) != 0);
/* For B (changing A's public key): */
memcpy(auxrnd32a_bad, auxrnd32a, sizeof(auxrnd32a_bad));
testrand_flip(auxrnd32a_bad, sizeof(auxrnd32a_bad));
ret = secp256k1_ellswift_create(CTX, ell64a_bad, sec32a, auxrnd32a_bad);
CHECK(ret);
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a_bad, ell64b, sec32b, 1, hash_function, data);
CHECK(ret);
- CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
+ CHECK(memcmp(share32_bad, share32b, 32) != 0);
/* Verify that swapping sides changes the shared secret. */
/* For A (claiming to be B): */
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32a, 1, hash_function, data);
CHECK(ret);
- CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
+ CHECK(memcmp(share32_bad, share32a, 32) != 0);
/* For B (claiming to be A): */
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32b, 0, hash_function, data);
CHECK(ret);
- CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
+ CHECK(memcmp(share32_bad, share32b, 32) != 0);
}
}
}
@@ -460,7 +460,7 @@ void ellswift_xdh_ctx_sha256_tests(void) {
CHECK(secp256k1_ellswift_xdh(ctx, out_custom, keyA, keyB, skA, 0, hash_fn, data));
CHECK(sha256_ellswift_xdh_called);
/* Outputs must differ if custom compression was used */
- CHECK(secp256k1_memcmp_var(out_default, out_custom, 32) != 0);
+ CHECK(memcmp(out_default, out_custom, 32) != 0);
/* Restore defaults */
sha256_ellswift_xdh_called = 0;
### src/modules/extrakeys/main_impl.h
@@ -86,7 +86,7 @@ int secp256k1_xonly_pubkey_cmp(const secp256k1_context* ctx, const secp256k1_xon
memset(out[i], 0, sizeof(out[i]));
}
}
- return secp256k1_memcmp_var(out[0], out[1], sizeof(out[1]));
+ return memcmp(out[0], out[1], sizeof(out[1]));
}
/** Keeps a group element as is if it has an even Y and otherwise negates it.
@@ -156,7 +156,7 @@ int secp256k1_xonly_pubkey_tweak_add_check(const secp256k1_context* ctx, const u
secp256k1_fe_normalize_var(&pk.y);
secp256k1_fe_get_b32(pk_expected32, &pk.x);
- return secp256k1_memcmp_var(&pk_expected32, tweaked_pubkey32, 32) == 0
+ return memcmp(&pk_expected32, tweaked_pubkey32, 32) == 0
&& secp256k1_fe_is_odd(&pk.y) == tweaked_pk_parity;
}
### src/modules/extrakeys/tests_exhaustive_impl.h
@@ -38,13 +38,13 @@ static void test_exhaustive_extrakeys(const secp256k1_context *ctx, const secp25
/* Parse the xonly_pubkey back and verify it matches the previously serialized value. */
CHECK(secp256k1_xonly_pubkey_parse(ctx, &xonly_pubkey[i - 1], xonly_pubkey_bytes[i - 1]));
CHECK(secp256k1_xonly_pubkey_serialize(ctx, buf, &xonly_pubkey[i - 1]));
- CHECK(secp256k1_memcmp_var(xonly_pubkey_bytes[i - 1], buf, 32) == 0);
+ CHECK(memcmp(xonly_pubkey_bytes[i - 1], buf, 32) == 0);
/* Construct the xonly_pubkey from the pubkey, and verify it matches the same. */
CHECK(secp256k1_xonly_pubkey_from_pubkey(ctx, &xonly_pubkey[i - 1], &parity, &pubkey[i - 1]));
CHECK(parity == parities[i - 1]);
CHECK(secp256k1_xonly_pubkey_serialize(ctx, buf, &xonly_pubkey[i - 1]));
- CHECK(secp256k1_memcmp_var(xonly_pubkey_bytes[i - 1], buf, 32) == 0);
+ CHECK(memcmp(xonly_pubkey_bytes[i - 1], buf, 32) == 0);
/* Compare the xonly_pubkey bytes against the precomputed group. */
secp256k1_fe_set_b32_mod(&fe, xonly_pubkey_bytes[i - 1]);
@@ -57,7 +57,7 @@ static void test_exhaustive_extrakeys(const secp256k1_context *ctx, const secp25
/* Verify that the higher half is identical to the lower half mirrored. */
if (i > EXHAUSTIVE_TEST_ORDER / 2) {
- CHECK(secp256k1_memcmp_var(xonly_pubkey_bytes[i - 1], xonly_pubkey_bytes[EXHAUSTIVE_TEST_ORDER - i - 1], 32) == 0);
+ CHECK(memcmp(xonly_pubkey_bytes[i - 1], xonly_pubkey_bytes[EXHAUSTIVE_TEST_ORDER - i - 1], 32) == 0);
CHECK(parities[i - 1] == 1 - parities[EXHAUSTIVE_TEST_ORDER - i - 1]);
}
}
### src/modules/extrakeys/tests_impl.h
@@ -43,15 +43,15 @@ static void test_xonly_pubkey(void) {
sk[0] = 1;
CHECK(secp256k1_ec_pubkey_create(CTX, &pk, sk) == 1);
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &xonly_pk, &pk_parity, &pk) == 1);
- CHECK(secp256k1_memcmp_var(&pk, &xonly_pk, sizeof(pk)) == 0);
+ CHECK(memcmp(&pk, &xonly_pk, sizeof(pk)) == 0);
CHECK(pk_parity == 0);
/* Choose a secret key such that pubkey and xonly_pubkey are each others
* negation. */
sk[0] = 2;
CHECK(secp256k1_ec_pubkey_create(CTX, &pk, sk) == 1);
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &xonly_pk, &pk_parity, &pk) == 1);
- CHECK(secp256k1_memcmp_var(&xonly_pk, &pk, sizeof(xonly_pk)) != 0);
+ CHECK(memcmp(&xonly_pk, &pk, sizeof(xonly_pk)) != 0);
CHECK(pk_parity == 1);
CHECK(secp256k1_pubkey_load(CTX, &pk1, &pk) == 1);
CHECK(secp256k1_pubkey_load(CTX, &pk2, (secp256k1_pubkey *) &xonly_pk) == 1);
@@ -62,7 +62,7 @@ static void test_xonly_pubkey(void) {
/* Test xonly_pubkey_serialize and xonly_pubkey_parse */
CHECK_ILLEGAL(CTX, secp256k1_xonly_pubkey_serialize(CTX, NULL, &xonly_pk));
CHECK_ILLEGAL(CTX, secp256k1_xonly_pubkey_serialize(CTX, buf32, NULL));
- CHECK(secp256k1_memcmp_var(buf32, zeros64, 32) == 0);
+ CHECK(memcmp(buf32, zeros64, 32) == 0);
{
/* A pubkey filled with 0s will fail to serialize due to pubkey_load
* special casing. */
@@ -80,17 +80,17 @@ static void test_xonly_pubkey(void) {
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &xonly_pk, NULL, &pk) == 1);
CHECK(secp256k1_xonly_pubkey_serialize(CTX, buf32, &xonly_pk) == 1);
CHECK(secp256k1_xonly_pubkey_parse(CTX, &xonly_pk_tmp, buf32) == 1);
- CHECK(secp256k1_memcmp_var(&xonly_pk, &xonly_pk_tmp, sizeof(xonly_pk)) == 0);
+ CHECK(memcmp(&xonly_pk, &xonly_pk_tmp, sizeof(xonly_pk)) == 0);
/* Test parsing invalid field elements */
memset(&xonly_pk, 1, sizeof(xonly_pk));
/* Overflowing field element */
CHECK(secp256k1_xonly_pubkey_parse(CTX, &xonly_pk, ones32) == 0);
- CHECK(secp256k1_memcmp_var(&xonly_pk, zeros64, sizeof(xonly_pk)) == 0);
+ CHECK(memcmp(&xonly_pk, zeros64, sizeof(xonly_pk)) == 0);
memset(&xonly_pk, 1, sizeof(xonly_pk));
/* There's no point with x-coordinate 0 on secp256k1 */
CHECK(secp256k1_xonly_pubkey_parse(CTX, &xonly_pk, zeros64) == 0);
- CHECK(secp256k1_memcmp_var(&xonly_pk, zeros64, sizeof(xonly_pk)) == 0);
+ CHECK(memcmp(&xonly_pk, zeros64, sizeof(xonly_pk)) == 0);
/* If a random 32-byte string can not be parsed with ec_pubkey_parse
* (because interpreted as X coordinate it does not correspond to a point on
* the curve) then xonly_pubkey_parse should fail as well. */
@@ -101,7 +101,7 @@ static void test_xonly_pubkey(void) {
if (!secp256k1_ec_pubkey_parse(CTX, &pk, rand33, 33)) {
memset(&xonly_pk, 1, sizeof(xonly_pk));
CHECK(secp256k1_xonly_pubkey_parse(CTX, &xonly_pk, &rand33[1]) == 0);
- CHECK(secp256k1_memcmp_var(&xonly_pk, zeros64, sizeof(xonly_pk)) == 0);
+ CHECK(memcmp(&xonly_pk, zeros64, sizeof(xonly_pk)) == 0);
} else {
CHECK(secp256k1_xonly_pubkey_parse(CTX, &xonly_pk, &rand33[1]) == 1);
}
@@ -164,14 +164,14 @@ static void test_xonly_pubkey_tweak(void) {
CHECK_ILLEGAL(CTX, secp256k1_xonly_pubkey_tweak_add(CTX, NULL, &internal_xonly_pk, tweak));
CHECK_ILLEGAL(CTX, secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, NULL, tweak));
/* NULL internal_xonly_pk zeroes the output_pk */
- CHECK(secp256k1_memcmp_var(&output_pk, zeros64, sizeof(output_pk)) == 0);
+ CHECK(memcmp(&output_pk, zeros64, sizeof(output_pk)) == 0);
CHECK_ILLEGAL(CTX, secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, &internal_xonly_pk, NULL));
/* NULL tweak zeroes the output_pk */
- CHECK(secp256k1_memcmp_var(&output_pk, zeros64, sizeof(output_pk)) == 0);
+ CHECK(memcmp(&output_pk, zeros64, sizeof(output_pk)) == 0);
/* Invalid tweak zeroes the output_pk */
CHECK(secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, &internal_xonly_pk, overflows) == 0);
- CHECK(secp256k1_memcmp_var(&output_pk, zeros64, sizeof(output_pk)) == 0);
+ CHECK(memcmp(&output_pk, zeros64, sizeof(output_pk)) == 0);
/* A zero tweak is fine */
CHECK(secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, &internal_xonly_pk, zeros64) == 1);
@@ -186,14 +186,14 @@ static void test_xonly_pubkey_tweak(void) {
secp256k1_scalar_get_b32(tweak, &scalar_tweak);
CHECK((secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, &internal_xonly_pk, sk) == 0)
|| (secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, &internal_xonly_pk, tweak) == 0));
- CHECK(secp256k1_memcmp_var(&output_pk, zeros64, sizeof(output_pk)) == 0);
+ CHECK(memcmp(&output_pk, zeros64, sizeof(output_pk)) == 0);
}
/* Invalid pk with a valid tweak */
memset(&internal_xonly_pk, 0, sizeof(internal_xonly_pk));
testrand256(tweak);
CHECK_ILLEGAL(CTX, secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, &internal_xonly_pk, tweak));
- CHECK(secp256k1_memcmp_var(&output_pk, zeros64, sizeof(output_pk)) == 0);
+ CHECK(memcmp(&output_pk, zeros64, sizeof(output_pk)) == 0);
}
static void test_xonly_pubkey_tweak_check(void) {
@@ -243,7 +243,7 @@ static void test_xonly_pubkey_tweak_check(void) {
/* Overflowing tweak not allowed */
CHECK(secp256k1_xonly_pubkey_tweak_add_check(CTX, output_pk32, pk_parity, &internal_xonly_pk, overflows) == 0);
CHECK(secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, &internal_xonly_pk, overflows) == 0);
- CHECK(secp256k1_memcmp_var(&output_pk, zeros64, sizeof(output_pk)) == 0);
+ CHECK(memcmp(&output_pk, zeros64, sizeof(output_pk)) == 0);
}
/* Starts with an initial pubkey and recursively creates N_PUBKEYS - 1
@@ -295,40 +295,40 @@ static void test_keypair(void) {
/* Test keypair_create */
testrand256(sk);
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
- CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) != 0);
+ CHECK(memcmp(zeros96, &keypair, sizeof(keypair)) != 0);
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
- CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) != 0);
+ CHECK(memcmp(zeros96, &keypair, sizeof(keypair)) != 0);
CHECK_ILLEGAL(CTX, secp256k1_keypair_create(CTX, NULL, sk));
CHECK_ILLEGAL(CTX, secp256k1_keypair_create(CTX, &keypair, NULL));
- CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) == 0);
+ CHECK(memcmp(zeros96, &keypair, sizeof(keypair)) == 0);
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
CHECK_ILLEGAL(STATIC_CTX, secp256k1_keypair_create(STATIC_CTX, &keypair, sk));
- CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) == 0);
+ CHECK(memcmp(zeros96, &keypair, sizeof(keypair)) == 0);
/* Invalid secret key */
CHECK(secp256k1_keypair_create(CTX, &keypair, zeros96) == 0);
- CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) == 0);
+ CHECK(memcmp(zeros96, &keypair, sizeof(keypair)) == 0);
CHECK(secp256k1_keypair_create(CTX, &keypair, overflows) == 0);
- CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) == 0);
+ CHECK(memcmp(zeros96, &keypair, sizeof(keypair)) == 0);
/* Test keypair_pub */
testrand256(sk);
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
CHECK(secp256k1_keypair_pub(CTX, &pk, &keypair) == 1);
CHECK_ILLEGAL(CTX, secp256k1_keypair_pub(CTX, NULL, &keypair));
CHECK_ILLEGAL(CTX, secp256k1_keypair_pub(CTX, &pk, NULL));
- CHECK(secp256k1_memcmp_var(zeros96, &pk, sizeof(pk)) == 0);
+ CHECK(memcmp(zeros96, &pk, sizeof(pk)) == 0);
/* Using an invalid keypair is fine for keypair_pub */
memset(&keypair, 0, sizeof(keypair));
CHECK(secp256k1_keypair_pub(CTX, &pk, &keypair) == 1);
- CHECK(secp256k1_memcmp_var(zeros96, &pk, sizeof(pk)) == 0);
+ CHECK(memcmp(zeros96, &pk, sizeof(pk)) == 0);
/* keypair holds the same pubkey as pubkey_create */
CHECK(secp256k1_ec_pubkey_create(CTX, &pk, sk) == 1);
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
CHECK(secp256k1_keypair_pub(CTX, &pk_tmp, &keypair) == 1);
- CHECK(secp256k1_memcmp_var(&pk, &pk_tmp, sizeof(pk)) == 0);
+ CHECK(memcmp(&pk, &pk_tmp, sizeof(pk)) == 0);
/** Test keypair_xonly_pub **/
testrand256(sk);
@@ -337,20 +337,20 @@ static void test_keypair(void) {
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_pub(CTX, NULL, &pk_parity, &keypair));
CHECK(secp256k1_keypair_xonly_pub(CTX, &xonly_pk, NULL, &keypair) == 1);
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_pub(CTX, &xonly_pk, &pk_parity, NULL));
- CHECK(secp256k1_memcmp_var(zeros96, &xonly_pk, sizeof(xonly_pk)) == 0);
+ CHECK(memcmp(zeros96, &xonly_pk, sizeof(xonly_pk)) == 0);
/* Using an invalid keypair will set the xonly_pk to 0 (first reset
* xonly_pk). */
CHECK(secp256k1_keypair_xonly_pub(CTX, &xonly_pk, &pk_parity, &keypair) == 1);
memset(&keypair, 0, sizeof(keypair));
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_pub(CTX, &xonly_pk, &pk_parity, &keypair));
- CHECK(secp256k1_memcmp_var(zeros96, &xonly_pk, sizeof(xonly_pk)) == 0);
+ CHECK(memcmp(zeros96, &xonly_pk, sizeof(xonly_pk)) == 0);
/** keypair holds the same xonly pubkey as pubkey_create **/
CHECK(secp256k1_ec_pubkey_create(CTX, &pk, sk) == 1);
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &xonly_pk, &pk_parity, &pk) == 1);
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
CHECK(secp256k1_keypair_xonly_pub(CTX, &xonly_pk_tmp, &pk_parity_tmp, &keypair) == 1);
- CHECK(secp256k1_memcmp_var(&xonly_pk, &xonly_pk_tmp, sizeof(pk)) == 0);
+ CHECK(memcmp(&xonly_pk, &xonly_pk_tmp, sizeof(pk)) == 0);
CHECK(pk_parity == pk_parity_tmp);
/* Test keypair_seckey */
@@ -359,18 +359,18 @@ static void test_keypair(void) {
CHECK(secp256k1_keypair_sec(CTX, sk_tmp, &keypair) == 1);
CHECK_ILLEGAL(CTX, secp256k1_keypair_sec(CTX, NULL, &keypair));
CHECK_ILLEGAL(CTX, secp256k1_keypair_sec(CTX, sk_tmp, NULL));
- CHECK(secp256k1_memcmp_var(zeros96, sk_tmp, sizeof(sk_tmp)) == 0);
+ CHECK(memcmp(zeros96, sk_tmp, sizeof(sk_tmp)) == 0);
/* keypair returns the same seckey it got */
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
CHECK(secp256k1_keypair_sec(CTX, sk_tmp, &keypair) == 1);
- CHECK(secp256k1_memcmp_var(sk, sk_tmp, sizeof(sk_tmp)) == 0);
+ CHECK(memcmp(sk, sk_tmp, sizeof(sk_tmp)) == 0);
/* Using an invalid keypair is fine for keypair_seckey */
memset(&keypair, 0, sizeof(keypair));
CHECK(secp256k1_keypair_sec(CTX, sk_tmp, &keypair) == 1);
- CHECK(secp256k1_memcmp_var(zeros96, sk_tmp, sizeof(sk_tmp)) == 0);
+ CHECK(memcmp(zeros96, sk_tmp, sizeof(sk_tmp)) == 0);
}
static void test_keypair_add(void) {
@@ -393,12 +393,12 @@ static void test_keypair_add(void) {
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_tweak_add(CTX, NULL, tweak));
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_tweak_add(CTX, &keypair, NULL));
/* This does not set the keypair to zeroes */
- CHECK(secp256k1_memcmp_var(&keypair, zeros96, sizeof(keypair)) != 0);
+ CHECK(memcmp(&keypair, zeros96, sizeof(keypair)) != 0);
/* Invalid tweak zeroes the keypair */
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
CHECK(secp256k1_keypair_xonly_tweak_add(CTX, &keypair, overflows) == 0);
- CHECK(secp256k1_memcmp_var(&keypair, zeros96, sizeof(keypair)) == 0);
+ CHECK(memcmp(&keypair, zeros96, sizeof(keypair)) == 0);
/* A zero tweak is fine */
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
@@ -418,15 +418,15 @@ static void test_keypair_add(void) {
secp256k1_scalar_get_b32(tweak, &scalar_tweak);
CHECK((secp256k1_keypair_xonly_tweak_add(CTX, &keypair, sk) == 0)
|| (secp256k1_keypair_xonly_tweak_add(CTX, &keypair_tmp, tweak) == 0));
- CHECK(secp256k1_memcmp_var(&keypair, zeros96, sizeof(keypair)) == 0
- || secp256k1_memcmp_var(&keypair_tmp, zeros96, sizeof(keypair_tmp)) == 0);
+ CHECK(memcmp(&keypair, zeros96, sizeof(keypair)) == 0
+ || memcmp(&keypair_tmp, zeros96, sizeof(keypair_tmp)) == 0);
}
/* Invalid keypair with a valid tweak */
memset(&keypair, 0, sizeof(keypair));
testrand256(tweak);
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_tweak_add(CTX, &keypair, tweak));
- CHECK(secp256k1_memcmp_var(&keypair, zeros96, sizeof(keypair)) == 0);
+ CHECK(memcmp(&keypair, zeros96, sizeof(keypair)) == 0);
/* Only seckey part of keypair invalid */
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
memset(&keypair, 0, 32);
@@ -459,12 +459,12 @@ static void test_keypair_add(void) {
/* Check that the resulting pubkey matches xonly_pubkey_tweak_add */
CHECK(secp256k1_keypair_pub(CTX, &output_pk_xy, &keypair) == 1);
CHECK(secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk_expected, &internal_pk, tweak) == 1);
- CHECK(secp256k1_memcmp_var(&output_pk_xy, &output_pk_expected, sizeof(output_pk_xy)) == 0);
+ CHECK(memcmp(&output_pk_xy, &output_pk_expected, sizeof(output_pk_xy)) == 0);
/* Check that the secret key in the keypair is tweaked correctly */
CHECK(secp256k1_keypair_sec(CTX, sk32, &keypair) == 1);
CHECK(secp256k1_ec_pubkey_create(CTX, &output_pk_expected, sk32) == 1);
- CHECK(secp256k1_memcmp_var(&output_pk_xy, &output_pk_expected, sizeof(output_pk_xy)) == 0);
+ CHECK(memcmp(&output_pk_xy, &output_pk_expected, sizeof(output_pk_xy)) == 0);
}
}
### src/modules/musig/keyagg_impl.h
@@ -45,7 +45,7 @@ static void secp256k1_keyagg_cache_save(secp256k1_musig_keyagg_cache *cache, con
static int secp256k1_keyagg_cache_load(const secp256k1_context* ctx, secp256k1_keyagg_cache_internal *cache_i, const secp256k1_musig_keyagg_cache *cache) {
const unsigned char *ptr = cache->data;
- ARG_CHECK(secp256k1_memcmp_var(ptr, secp256k1_musig_keyagg_cache_magic, 4) == 0);
+ ARG_CHECK(memcmp(ptr, secp256k1_musig_keyagg_cache_magic, 4) == 0);
ptr += 4;
secp256k1_ge_from_bytes(&cache_i->pk, ptr);
ptr += 64;
@@ -174,7 +174,7 @@ int secp256k1_musig_pubkey_agg(const secp256k1_context* ctx, secp256k1_xonly_pub
secp256k1_ge_set_infinity(&ecmult_data.second_pk);
for (i = 1; i < n_pubkeys; i++) {
- if (secp256k1_memcmp_var(pubkeys[0], pubkeys[i], sizeof(*pubkeys[0])) != 0) {
+ if (memcmp(pubkeys[0], pubkeys[i], sizeof(*pubkeys[0])) != 0) {
secp256k1_ge pk;
if (!secp256k1_pubkey_load(ctx, &pk, pubkeys[i])) {
return 0;
### src/modules/musig/session_impl.h
@@ -30,7 +30,7 @@ static void secp256k1_musig_secnonce_save(secp256k1_musig_secnonce *secnonce, co
static int secp256k1_musig_secnonce_load(const secp256k1_context* ctx, secp256k1_scalar *k, secp256k1_ge *pk, const secp256k1_musig_secnonce *secnonce) {
int is_zero;
- ARG_CHECK(secp256k1_memcmp_var(&secnonce->data[0], secp256k1_musig_secnonce_magic, 4) == 0);
+ ARG_CHECK(memcmp(&secnonce->data[0], secp256k1_musig_secnonce_magic, 4) == 0);
/* We make very sure that the nonce isn't invalidated by checking the values
* in addition to the magic. */
is_zero = secp256k1_is_zero_array(&secnonce->data[4], 2 * 32);
@@ -72,7 +72,7 @@ static void secp256k1_musig_pubnonce_save(secp256k1_musig_pubnonce* nonce, const
static int secp256k1_musig_pubnonce_load(const secp256k1_context* ctx, secp256k1_ge* ges, const secp256k1_musig_pubnonce* nonce) {
int i;
- ARG_CHECK(secp256k1_memcmp_var(&nonce->data[0], secp256k1_musig_pubnonce_magic, 4) == 0);
+ ARG_CHECK(memcmp(&nonce->data[0], secp256k1_musig_pubnonce_magic, 4) == 0);
for (i = 0; i < 2; i++) {
secp256k1_ge_from_bytes(&ges[i], nonce->data + 4 + 64*i);
}
@@ -92,7 +92,7 @@ static void secp256k1_musig_aggnonce_save(secp256k1_musig_aggnonce* nonce, const
static int secp256k1_musig_aggnonce_load(const secp256k1_context* ctx, secp256k1_ge* ges, const secp256k1_musig_aggnonce* nonce) {
int i;
- ARG_CHECK(secp256k1_memcmp_var(&nonce->data[0], secp256k1_musig_aggnonce_magic, 4) == 0);
+ ARG_CHECK(memcmp(&nonce->data[0], secp256k1_musig_aggnonce_magic, 4) == 0);
for (i = 0; i < 2; i++) {
secp256k1_ge_from_bytes_ext(&ges[i], &nonce->data[4 + 64*i]);
}
@@ -128,7 +128,7 @@ static void secp256k1_musig_session_save(secp256k1_musig_session *session, const
static int secp256k1_musig_session_load(const secp256k1_context* ctx, secp256k1_musig_session_internal *session_i, const secp256k1_musig_session *session) {
const unsigned char *ptr = session->data;
- ARG_CHECK(secp256k1_memcmp_var(ptr, secp256k1_musig_session_cache_magic, 4) == 0);
+ ARG_CHECK(memcmp(ptr, secp256k1_musig_session_cache_magic, 4) == 0);
ptr += 4;
session_i->fin_nonce_parity = *ptr;
ptr += 1;
@@ -152,7 +152,7 @@ static void secp256k1_musig_partial_sig_save(secp256k1_musig_partial_sig* sig, s
static int secp256k1_musig_partial_sig_load(const secp256k1_context* ctx, secp256k1_scalar *s, const secp256k1_musig_partial_sig* sig) {
int overflow;
- ARG_CHECK(secp256k1_memcmp_var(&sig->data[0], secp256k1_musig_partial_sig_magic, 4) == 0);
+ ARG_CHECK(memcmp(&sig->data[0], secp256k1_musig_partial_sig_magic, 4) == 0);
secp256k1_scalar_set_b32(s, &sig->data[4], &overflow);
/* Parsed signatures can not overflow */
VERIFY_CHECK(!overflow);
@@ -256,7 +256,7 @@ int secp256k1_musig_partial_sig_serialize(const secp256k1_context* ctx, unsigned
VERIFY_CHECK(ctx != NULL);
ARG_CHECK(out32 != NULL);
ARG_CHECK(sig != NULL);
- ARG_CHECK(secp256k1_memcmp_var(&sig->data[0], secp256k1_musig_partial_sig_magic, 4) == 0);
+ ARG_CHECK(memcmp(&sig->data[0], secp256k1_musig_partial_sig_magic, 4) == 0);
memcpy(out32, &sig->data[4], 32);
return 1;
### src/modules/musig/tests_impl.h
@@ -113,7 +113,7 @@ static void pubnonce_summing_to_inf(secp256k1_musig_pubnonce *pubnonce) {
int memcmp_and_randomize(unsigned char *value, const unsigned char *expected, size_t len) {
int ret;
size_t i;
- ret = secp256k1_memcmp_var(value, expected, len);
+ ret = memcmp(value, expected, len);
for (i = 0; i < len; i++) {
value[i] = testrand_bits(8);
}
@@ -226,7 +226,7 @@ static void musig_api_tests(void) {
CHECK(secp256k1_musig_pubkey_get(CTX, &full_agg_pk, &keyagg_cache) == 1);
CHECK_ILLEGAL(CTX, secp256k1_musig_pubkey_get(CTX, NULL, &keyagg_cache));
CHECK_ILLEGAL(CTX, secp256k1_musig_pubkey_get(CTX, &full_agg_pk, NULL));
- CHECK(secp256k1_memcmp_var(&full_agg_pk, zeros132, sizeof(full_agg_pk)) == 0);
+ CHECK(memcmp(&full_agg_pk, zeros132, sizeof(full_agg_pk)) == 0);
/** Tweaking **/
{
@@ -353,7 +353,7 @@ static void musig_api_tests(void) {
secp256k1_musig_pubnonce tmp;
CHECK(secp256k1_musig_pubnonce_serialize(CTX, pubnonce_ser, &pubnonce[0]) == 1);
CHECK(secp256k1_musig_pubnonce_parse(CTX, &tmp, pubnonce_ser) == 1);
- CHECK(secp256k1_memcmp_var(&tmp, &pubnonce[0], sizeof(tmp)) == 0);
+ CHECK(memcmp(&tmp, &pubnonce[0], sizeof(tmp)) == 0);
}
/** Receive nonces and aggregate **/
@@ -400,7 +400,7 @@ static void musig_api_tests(void) {
secp256k1_musig_aggnonce tmp;
CHECK(secp256k1_musig_aggnonce_serialize(CTX, aggnonce_ser, &aggnonce) == 1);
CHECK(secp256k1_musig_aggnonce_parse(CTX, &tmp, aggnonce_ser) == 1);
- CHECK(secp256k1_memcmp_var(&tmp, &aggnonce, sizeof(tmp)) == 0);
+ CHECK(memcmp(&tmp, &aggnonce, sizeof(tmp)) == 0);
}
/** Process nonces **/
@@ -417,7 +417,7 @@ static void musig_api_tests(void) {
memcpy(&secnonce_tmp, &secnonce[0], sizeof(secnonce_tmp));
CHECK(secp256k1_musig_partial_sign(CTX, &partial_sig[0], &secnonce_tmp, &keypair[0], &keyagg_cache, &session) == 1);
/* The secnonce is set to 0 and subsequent signing attempts fail */
- CHECK(secp256k1_memcmp_var(&secnonce_tmp, zeros132, sizeof(secnonce_tmp)) == 0);
+ CHECK(memcmp(&secnonce_tmp, zeros132, sizeof(secnonce_tmp)) == 0);
CHECK_ILLEGAL(CTX, secp256k1_musig_partial_sign(CTX, &partial_sig[0], &secnonce_tmp, &keypair[0], &keyagg_cache, &session));
memcpy(&secnonce_tmp, &secnonce[0], sizeof(secnonce_tmp));
CHECK_ILLEGAL(CTX, secp256k1_musig_partial_sign(CTX, NULL, &secnonce_tmp, &keypair[0], &keyagg_cache, &session));
@@ -458,7 +458,7 @@ static void musig_api_tests(void) {
/* Check that parsing failure results in an invalid sig */
secp256k1_musig_partial_sig tmp;
CHECK(secp256k1_musig_partial_sig_parse(CTX, &tmp, max64) == 0);
- CHECK(secp256k1_memcmp_var(&tmp, zeros132, sizeof(partial_sig[0])) == 0);
+ CHECK(memcmp(&tmp, zeros132, sizeof(partial_sig[0])) == 0);
}
CHECK_ILLEGAL(CTX, secp256k1_musig_partial_sig_parse(CTX, &partial_sig[0], NULL));
@@ -467,7 +467,7 @@ static void musig_api_tests(void) {
secp256k1_musig_partial_sig tmp;
CHECK(secp256k1_musig_partial_sig_serialize(CTX, buf, &partial_sig[0]) == 1);
CHECK(secp256k1_musig_partial_sig_parse(CTX, &tmp, buf) == 1);
- CHECK(secp256k1_memcmp_var(&tmp, &partial_sig[0], sizeof(tmp)) == 0);
+ CHECK(memcmp(&tmp, &partial_sig[0], sizeof(tmp)) == 0);
}
/** Partial signature verification */
@@ -667,24 +667,24 @@ static void musig_ctx_sha256(void) {
ctx->hash_ctx.fn_sha256_compression = sha256_musig_pubkey_agg;
CHECK(secp256k1_musig_pubkey_agg(ctx, NULL, &keyagg_cache_custom, pks, 1) == 1);
CHECK(sha256_musig_pubkey_agg_called);
- CHECK(secp256k1_memcmp_var(keyagg_cache.data, keyagg_cache_custom.data, sizeof(keyagg_cache.data)) != 0);
+ CHECK(memcmp(keyagg_cache.data, keyagg_cache_custom.data, sizeof(keyagg_cache.data)) != 0);
/* Restore secrand (nonce_gen zeroed it). */
secrand[0] = 1;
ctx->hash_ctx.fn_sha256_compression = sha256_musig_nonce_gen;
CHECK(secp256k1_musig_nonce_gen(ctx, &secnonce_unused, &pubnonce_custom, secrand, sk, &pk, NULL, NULL, NULL) == 1);
CHECK(sha256_musig_nonce_gen_called);
- CHECK(secp256k1_memcmp_var(pubnonce.data, pubnonce_custom.data, sizeof(pubnonce.data)) != 0);
+ CHECK(memcmp(pubnonce.data, pubnonce_custom.data, sizeof(pubnonce.data)) != 0);
ctx->hash_ctx.fn_sha256_compression = sha256_musig_nonce_process;
CHECK(secp256k1_musig_nonce_process(ctx, &session_custom, &aggnonce, msg32, &keyagg_cache) == 1);
CHECK(sha256_musig_nonce_process_called);
- CHECK(secp256k1_memcmp_var(session.data, session_custom.data, sizeof(session.data)) != 0);
+ CHECK(memcmp(session.data, session_custom.data, sizeof(session.data)) != 0);
ctx->hash_ctx.fn_sha256_compression = sha256_musig_partial_sign;
CHECK(secp256k1_musig_partial_sign(ctx, &partial_sig_custom, &secnonce_tmp, &keypair, &keyagg_cache, &session) == 1);
CHECK(sha256_musig_partial_sign_called);
- CHECK(secp256k1_memcmp_var(partial_sig.data, partial_sig_custom.data, sizeof(partial_sig.data)) != 0);
+ CHECK(memcmp(partial_sig.data, partial_sig_custom.data, sizeof(partial_sig.data)) != 0);
/* The default partial sig no longer verifies once the override changes the KeyAgg coefficient. */
ctx->hash_ctx.fn_sha256_compression = sha256_musig_partial_sig_verify;
@@ -694,7 +694,7 @@ static void musig_ctx_sha256(void) {
ctx->hash_ctx.fn_sha256_compression = sha256_musig_nonce_gen_counter;
CHECK(secp256k1_musig_nonce_gen_counter(ctx, &secnonce_unused, &pubnonce_counter_custom, 0, &keypair, NULL, NULL, NULL) == 1);
CHECK(sha256_musig_nonce_gen_counter_called);
- CHECK(secp256k1_memcmp_var(pubnonce_counter.data, pubnonce_counter_custom.data, sizeof(pubnonce_counter.data)) != 0);
+ CHECK(memcmp(pubnonce_counter.data, pubnonce_counter_custom.data, sizeof(pubnonce_counter.data)) != 0);
secp256k1_context_destroy(ctx);
}
@@ -790,7 +790,7 @@ static void musig_tweak_test_internal(void) {
} else {
secp256k1_pubkey tmp_key = P[i-1];
CHECK(secp256k1_ec_pubkey_tweak_add(CTX, &tmp_key, tweak));
- CHECK(secp256k1_memcmp_var(&tmp_key, &P[i], sizeof(tmp_key)) == 0);
+ CHECK(memcmp(&tmp_key, &P[i], sizeof(tmp_key)) == 0);
}
/* Test signing for P[i] */
musig_tweak_test_helper(&P_xonly[i], sk[0], sk[1], &keyagg_cache);
@@ -869,7 +869,7 @@ static void musig_test_vectors_keyagg(void) {
unsigned char agg_pk[32];
CHECK(musig_vectors_keyagg_and_tweak(&error, &keyagg_cache, agg_pk, vector->pubkeys, vector->tweaks, c->key_indices_len, c->key_indices, 0, NULL, NULL));
- CHECK(secp256k1_memcmp_var(agg_pk, c->expected, sizeof(agg_pk)) == 0);
+ CHECK(memcmp(agg_pk, c->expected, sizeof(agg_pk)) == 0);
}
for (i = 0; i < ARRAY_SIZE(vector->error_case); i++) {
@@ -922,15 +922,15 @@ static void musig_test_vectors_noncegen(void) {
CHECK(secp256k1_ec_pubkey_parse(CTX, &pk, c->pk, sizeof(c->pk)));
CHECK(secp256k1_musig_nonce_gen(CTX, &secnonce, &pubnonce, session_secrand32, sk, &pk, msg, keyagg_cache_ptr, extra_in) == 1);
- CHECK(secp256k1_memcmp_var(&secnonce.data[4], c->expected_secnonce, 2*32) == 0);
+ CHECK(memcmp(&secnonce.data[4], c->expected_secnonce, 2*32) == 0);
/* The last element of the secnonce is the public key (uncompressed in
* secp256k1_musig_secnonce, compressed in the test vector secnonce). */
- CHECK(secp256k1_memcmp_var(&secnonce.data[4+2*32], &pk, sizeof(pk)) == 0);
- CHECK(secp256k1_memcmp_var(&c->expected_secnonce[2*32], c->pk, sizeof(c->pk)) == 0);
+ CHECK(memcmp(&secnonce.data[4+2*32], &pk, sizeof(pk)) == 0);
+ CHECK(memcmp(&c->expected_secnonce[2*32], c->pk, sizeof(c->pk)) == 0);
CHECK(secp256k1_musig_pubnonce_serialize(CTX, pubnonce66, &pubnonce) == 1);
CHECK(sizeof(c->expected_pubnonce) == sizeof(pubnonce66));
- CHECK(secp256k1_memcmp_var(pubnonce66, c->expected_pubnonce, sizeof(pubnonce66)) == 0);
+ CHECK(memcmp(pubnonce66, c->expected_pubnonce, sizeof(pubnonce66)) == 0);
}
}
@@ -953,7 +953,7 @@ static void musig_test_vectors_nonceagg(void) {
}
CHECK(secp256k1_musig_nonce_agg(CTX, &aggnonce, pubnonce_ptr, 2));
CHECK(secp256k1_musig_aggnonce_serialize(CTX, aggnonce66, &aggnonce));
- CHECK(secp256k1_memcmp_var(aggnonce66, c->expected, sizeof(aggnonce66)) == 0);
+ CHECK(memcmp(aggnonce66, c->expected, sizeof(aggnonce66)) == 0);
}
for (i = 0; i < ARRAY_SIZE(vector->error_case); i++) {
const struct musig_nonce_agg_test_case *c = &vector->error_case[i];
@@ -1002,7 +1002,7 @@ static void musig_test_vectors_signverify(void) {
musig_test_set_secnonce(&secnonce, vector->secnonces[0], &pubkey);
CHECK(secp256k1_musig_partial_sign(CTX, &partial_sig, &secnonce, &keypair, &keyagg_cache, &session));
CHECK(secp256k1_musig_partial_sig_serialize(CTX, partial_sig32, &partial_sig));
- CHECK(secp256k1_memcmp_var(partial_sig32, c->expected, sizeof(partial_sig32)) == 0);
+ CHECK(memcmp(partial_sig32, c->expected, sizeof(partial_sig32)) == 0);
CHECK(secp256k1_musig_pubnonce_parse(CTX, &pubnonce, vector->pubnonces[0]));
CHECK(secp256k1_musig_partial_sig_verify(CTX, &partial_sig, &pubnonce, &pubkey, &keyagg_cache, &session));
@@ -1127,7 +1127,7 @@ static void musig_test_vectors_tweak(void) {
CHECK(secp256k1_musig_partial_sign(CTX, &partial_sig, &secnonce, &keypair, &keyagg_cache, &session));
CHECK(secp256k1_musig_partial_sig_serialize(CTX, partial_sig32, &partial_sig));
- CHECK(secp256k1_memcmp_var(partial_sig32, c->expected, sizeof(partial_sig32)) == 0);
+ CHECK(memcmp(partial_sig32, c->expected, sizeof(partial_sig32)) == 0);
CHECK(secp256k1_musig_pubnonce_parse(CTX, &pubnonce, vector->pubnonces[c->nonce_indices[c->signer_index]]));
CHECK(secp256k1_musig_partial_sig_verify(CTX, &partial_sig, &pubnonce, &pubkey, &keyagg_cache, &session));
@@ -1166,7 +1166,7 @@ static void musig_test_vectors_sigagg(void) {
}
CHECK(secp256k1_musig_partial_sig_agg(CTX, final_sig, &session, partial_sig_ptr, c->psig_indices_len) == 1);
- CHECK(secp256k1_memcmp_var(final_sig, c->expected, sizeof(final_sig)) == 0);
+ CHECK(memcmp(final_sig, c->expected, sizeof(final_sig)) == 0);
CHECK(secp256k1_xonly_pubkey_parse(CTX, &agg_pk, agg_pk32));
CHECK(secp256k1_schnorrsig_verify(CTX, final_sig, vector->msg, sizeof(vector->msg), &agg_pk) == 1);
@@ -1222,11 +1222,11 @@ static void musig_test_static_nonce_gen_counter(void) {
CHECK(secp256k1_keypair_pub(CTX, &pk, &keypair));
CHECK(secp256k1_musig_nonce_gen_counter(CTX, &secnonce, &pubnonce, nonrepeating_cnt, &keypair, NULL, NULL, NULL) == 1);
- CHECK(secp256k1_memcmp_var(&secnonce.data[4], expected_secnonce, 2*32) == 0);
- CHECK(secp256k1_memcmp_var(&secnonce.data[4+2*32], &pk, sizeof(pk)) == 0);
+ CHECK(memcmp(&secnonce.data[4], expected_secnonce, 2*32) == 0);
+ CHECK(memcmp(&secnonce.data[4+2*32], &pk, sizeof(pk)) == 0);
CHECK(secp256k1_musig_pubnonce_serialize(CTX, pubnonce66, &pubnonce) == 1);
- CHECK(secp256k1_memcmp_var(pubnonce66, expected_pubnonce, sizeof(pubnonce66)) == 0);
+ CHECK(memcmp(pubnonce66, expected_pubnonce, sizeof(pubnonce66)) == 0);
}
/* Checks that nonce_gen_counter matches nonce_gen */
@@ -1248,7 +1248,7 @@ static void musig_nonce_gen_counter_test(void) {
secp256k1_write_be64(session_secrand32, nonrepeating_cnt);
CHECK(secp256k1_musig_nonce_gen_counter(CTX, &secnonce, &pubnonce[0], nonrepeating_cnt, &keypair, NULL, NULL, NULL) == 1);
CHECK(secp256k1_musig_nonce_gen(CTX, &secnonce, &pubnonce[1], session_secrand32, sk, &pk, NULL, NULL, NULL) == 1);
- CHECK(secp256k1_memcmp_var(&pubnonce[0], &pubnonce[1], sizeof(pubnonce[0])) == 0);
+ CHECK(memcmp(&pubnonce[0], &pubnonce[1], sizeof(pubnonce[0])) == 0);
}
}
### src/modules/recovery/tests_impl.h
@@ -131,7 +131,7 @@ static void test_ecdsa_recovery_end_to_end_internal(void) {
CHECK(secp256k1_ecdsa_sign_recoverable(CTX, &rsignature[3], message, privkey, NULL, extra) == 1);
CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(CTX, sig, &recid, &rsignature[4]) == 1);
CHECK(secp256k1_ecdsa_recoverable_signature_convert(CTX, &signature[4], &rsignature[4]) == 1);
- CHECK(secp256k1_memcmp_var(&signature[4], &signature[0], 64) == 0);
+ CHECK(memcmp(&signature[4], &signature[0], 64) == 0);
CHECK(secp256k1_ecdsa_verify(CTX, &signature[4], message, &pubkey) == 1);
memset(&rsignature[4], 0, sizeof(rsignature[4]));
CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(CTX, &rsignature[4], sig, recid) == 1);
@@ -140,7 +140,7 @@ static void test_ecdsa_recovery_end_to_end_internal(void) {
/* Parse compact (with recovery id) and recover. */
CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(CTX, &rsignature[4], sig, recid) == 1);
CHECK(secp256k1_ecdsa_recover(CTX, &recpubkey, &rsignature[4], message) == 1);
- CHECK(secp256k1_memcmp_var(&pubkey, &recpubkey, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, &recpubkey, sizeof(pubkey)) == 0);
/* Serialize/destroy/parse signature and verify again. */
CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(CTX, sig, &recid, &rsignature[4]) == 1);
sig[testrand_bits(6)] += 1 + testrand_int(255);
@@ -149,7 +149,7 @@ static void test_ecdsa_recovery_end_to_end_internal(void) {
CHECK(secp256k1_ecdsa_verify(CTX, &signature[4], message, &pubkey) == 0);
/* Recover again */
CHECK(secp256k1_ecdsa_recover(CTX, &recpubkey, &rsignature[4], message) == 0 ||
- secp256k1_memcmp_var(&pubkey, &recpubkey, sizeof(pubkey)) != 0);
+ memcmp(&pubkey, &recpubkey, sizeof(pubkey)) != 0);
}
/* Tests several edge cases. */
### src/modules/schnorrsig/main_impl.h
@@ -70,7 +70,7 @@ static int nonce_function_bip340_impl(const secp256k1_hash_ctx *hash_ctx, unsign
* algorithms. If this nonce function is used in BIP-340 signing as defined
* in the spec, an optimized tagging implementation is used. */
if (algolen == sizeof(bip340_algo)
- && secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) {
+ && memcmp(algo, bip340_algo, algolen) == 0) {
secp256k1_nonce_function_bip340_sha256_tagged(&sha);
} else {
secp256k1_sha256_initialize_tagged(hash_ctx, &sha, algo, algolen);
@@ -196,7 +196,7 @@ int secp256k1_schnorrsig_sign_custom(const secp256k1_context* ctx, unsigned char
VERIFY_CHECK(ctx != NULL);
if (extraparams != NULL) {
- ARG_CHECK(secp256k1_memcmp_var(extraparams->magic,
+ ARG_CHECK(memcmp(extraparams->magic,
schnorrsig_extraparams_magic,
sizeof(extraparams->magic)) == 0);
noncefp = extraparams->noncefp;
### src/modules/schnorrsig/tests_exhaustive_impl.h
@@ -171,9 +171,9 @@ static void test_exhaustive_schnorrsig_sign(const secp256k1_context *ctx, unsign
/* Invoke the real function to construct a signature. */
CHECK(secp256k1_schnorrsig_sign_custom(ctx, sig64, msg32, sizeof(msg32), &keypairs[d - 1], &extraparams));
/* The first 32 bytes must match the xonly pubkey for the specified k. */
- CHECK(secp256k1_memcmp_var(sig64, xonly_pubkey_bytes[k - 1], 32) == 0);
+ CHECK(memcmp(sig64, xonly_pubkey_bytes[k - 1], 32) == 0);
/* The last 32 bytes must match the expected s value. */
- CHECK(secp256k1_memcmp_var(sig64 + 32, expected_s_bytes, 32) == 0);
+ CHECK(memcmp(sig64 + 32, expected_s_bytes, 32) == 0);
/* Don't retry other messages that result in the same challenge. */
e_done[e] = 1;
++e_count_done;
### src/modules/schnorrsig/tests_impl.h
@@ -18,7 +18,7 @@ static void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, s
CHECK(nonce_function_bip340(nonces[0], args[0], msglen, args[1], args[2], args[3], algolen, args[4]) == 1);
testrand_flip(args[n_flip], n_bytes);
CHECK(nonce_function_bip340(nonces[1], args[0], msglen, args[1], args[2], args[3], algolen, args[4]) == 1);
- CHECK(secp256k1_memcmp_var(nonces[0], nonces[1], 32) != 0);
+ CHECK(memcmp(nonces[0], nonces[1], 32) != 0);
}
static void run_nonce_function_bip340_tests(void) {
@@ -90,20 +90,20 @@ static void run_nonce_function_bip340_tests(void) {
/* Different msglen gives different nonce */
CHECK(nonce_function_bip340(nonce2, msg, msglen_tmp, key, pk, algo, algolen, NULL) == 1);
- CHECK(secp256k1_memcmp_var(nonce, nonce2, 32) != 0);
+ CHECK(memcmp(nonce, nonce2, 32) != 0);
/* Different algolen gives different nonce */
offset = testrand_int(algolen - 1);
algolen_tmp = (algolen + offset) % algolen;
CHECK(nonce_function_bip340(nonce2, msg, msglen, key, pk, algo, algolen_tmp, NULL) == 1);
- CHECK(secp256k1_memcmp_var(nonce, nonce2, 32) != 0);
+ CHECK(memcmp(nonce, nonce2, 32) != 0);
}
/* NULL aux_rand argument is allowed, and identical to passing all zero aux_rand. */
memset(aux_rand, 0, 32);
CHECK(nonce_function_bip340(nonce_z, msg, msglen, key, pk, algo, algolen, &aux_rand) == 1);
CHECK(nonce_function_bip340(nonce, msg, msglen, key, pk, algo, algolen, NULL) == 1);
- CHECK(secp256k1_memcmp_var(nonce_z, nonce, 32) == 0);
+ CHECK(memcmp(nonce_z, nonce, 32) == 0);
}
static void test_schnorrsig_api(void) {
@@ -183,16 +183,16 @@ static void test_schnorrsig_bip_vectors_check_signing(const unsigned char *sk, c
CHECK(secp256k1_keypair_create(CTX, &keypair, sk));
CHECK(secp256k1_schnorrsig_sign_custom(CTX, sig, msg, msglen, &keypair, &extraparams));
- CHECK(secp256k1_memcmp_var(sig, expected_sig, 64) == 0);
+ CHECK(memcmp(sig, expected_sig, 64) == 0);
if (msglen == 32) {
memset(sig, 0, 64);
CHECK(secp256k1_schnorrsig_sign32(CTX, sig, msg, &keypair, aux_rand));
- CHECK(secp256k1_memcmp_var(sig, expected_sig, 64) == 0);
+ CHECK(memcmp(sig, expected_sig, 64) == 0);
}
CHECK(secp256k1_xonly_pubkey_parse(CTX, &pk_expected, pk_serialized));
CHECK(secp256k1_keypair_xonly_pub(CTX, &pk, NULL, &keypair));
- CHECK(secp256k1_memcmp_var(&pk, &pk_expected, sizeof(pk)) == 0);
+ CHECK(memcmp(&pk, &pk_expected, sizeof(pk)) == 0);
CHECK(secp256k1_schnorrsig_verify(CTX, sig, msg, msglen, &pk));
}
@@ -830,11 +830,11 @@ static void test_schnorrsig_sign_internal(void) {
memset(sig, 1, sizeof(sig));
extraparams.noncefp = nonce_function_failing;
CHECK(secp256k1_schnorrsig_sign_custom(CTX, sig, msg, sizeof(msg), &keypair, &extraparams) == 0);
- CHECK(secp256k1_memcmp_var(sig, zeros64, sizeof(sig)) == 0);
+ CHECK(memcmp(sig, zeros64, sizeof(sig)) == 0);
memset(&sig, 1, sizeof(sig));
extraparams.noncefp = nonce_function_0;
CHECK(secp256k1_schnorrsig_sign_custom(CTX, sig, msg, sizeof(msg), &keypair, &extraparams) == 0);
- CHECK(secp256k1_memcmp_var(sig, zeros64, sizeof(sig)) == 0);
+ CHECK(memcmp(sig, zeros64, sizeof(sig)) == 0);
memset(&sig, 1, sizeof(sig));
extraparams.noncefp = nonce_function_overflowing;
CHECK(secp256k1_schnorrsig_sign_custom(CTX, sig, msg, sizeof(msg), &keypair, &extraparams) == 1);
@@ -846,7 +846,7 @@ static void test_schnorrsig_sign_internal(void) {
extraparams.ndata = aux_rand;
CHECK(secp256k1_schnorrsig_sign_custom(CTX, sig, msg, sizeof(msg), &keypair, &extraparams) == 1);
CHECK(secp256k1_schnorrsig_sign32(CTX, sig2, msg, &keypair, extraparams.ndata) == 1);
- CHECK(secp256k1_memcmp_var(sig, sig2, sizeof(sig)) == 0);
+ CHECK(memcmp(sig, sig2, sizeof(sig)) == 0);
}
DEFINE_SHA256_TRANSFORM_PROBE(sha256_schnorrsig)
@@ -867,7 +867,7 @@ static void test_schnorrsig_ctx_sha256(void) {
CHECK(secp256k1_schnorrsig_sign32(ctx, out_custom, msg32, &keypair, NULL));
CHECK(sha256_schnorrsig_called);
/* Outputs must differ if custom compression was used */
- CHECK(secp256k1_memcmp_var(out_default, out_custom, 64) != 0);
+ CHECK(memcmp(out_default, out_custom, 64) != 0);
secp256k1_context_destroy(ctx);
}
### src/modules/silentpayments/bench_impl.h
@@ -39,7 +39,7 @@ typedef struct {
const unsigned char* label_lookup(const unsigned char* key, const void* cache_ptr) {
bench_silentpayments_data *data = (bench_silentpayments_data*)cache_ptr;
- if (secp256k1_memcmp_var(key, data->label, 33) == 0) {
+ if (memcmp(key, data->label, 33) == 0) {
return data->label_tweak;
}
return NULL;
### src/modules/silentpayments/main_impl.h
@@ -347,7 +347,7 @@ static void secp256k1_silentpayments_label_save(secp256k1_silentpayments_label*
/* Loads a group element from a label. Returns 1 unless the label wasn't properly initialized. */
static int secp256k1_silentpayments_label_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_silentpayments_label* label) {
- ARG_CHECK(secp256k1_memcmp_var(&label->data[0], secp256k1_silentpayments_label_magic, 4) == 0);
+ ARG_CHECK(memcmp(&label->data[0], secp256k1_silentpayments_label_magic, 4) == 0);
secp256k1_ge_from_bytes(ge, label->data + 4);
return 1;
}
@@ -627,7 +627,7 @@ int secp256k1_silentpayments_recipient_scan_outputs(
}
ARG_CHECK(scan_key32 != NULL);
ARG_CHECK(prevouts_summary != NULL);
- ARG_CHECK(secp256k1_memcmp_var(&prevouts_summary->data[0], secp256k1_silentpayments_prevouts_summary_magic, 4) == 0);
+ ARG_CHECK(memcmp(&prevouts_summary->data[0], secp256k1_silentpayments_prevouts_summary_magic, 4) == 0);
ARG_CHECK(unlabeled_spend_pubkey != NULL);
/* Passing a context without a lookup function is non-sensical */
if (label_context != NULL) {
### src/modules/silentpayments/tests_impl.h
@@ -113,7 +113,7 @@ const unsigned char* label_lookup(const unsigned char* key, const void* cache_pt
}
cache = (const struct labels_cache*)cache_ptr;
for (i = 0; i < cache->entries_used; i++) {
- if (secp256k1_memcmp_var(cache->entries[i].label, key, 33) == 0) {
+ if (memcmp(cache->entries[i].label, key, 33) == 0) {
return cache->entries[i].label_tweak;
}
}
@@ -172,7 +172,7 @@ static void test_recipient_sort_helper(unsigned char (*sp_addresses[3])[2][33],
CHECK(ret == 1);
for (i = 0; i < 3; i++) {
CHECK(secp256k1_xonly_pubkey_serialize(CTX, xonly_ser, &generated_outputs[i]) == 1);
- CHECK(secp256k1_memcmp_var(xonly_ser, (*sp_outputs[i]), 32) == 0);
+ CHECK(memcmp(xonly_ser, (*sp_outputs[i]), 32) == 0);
}
}
@@ -473,7 +473,7 @@ static void test_label_api(void) {
CHECK(secp256k1_silentpayments_recipient_label_serialize(CTX, label_ser, &l));
CHECK(secp256k1_silentpayments_recipient_label_parse(CTX, &parsed_label, label_ser));
CHECK(secp256k1_silentpayments_recipient_label_serialize(CTX, parsed_label_ser, &parsed_label));
- CHECK(secp256k1_memcmp_var(label_ser, parsed_label_ser, 33) == 0);
+ CHECK(memcmp(label_ser, parsed_label_ser, 33) == 0);
CHECK(secp256k1_silentpayments_recipient_label_parse(CTX, &parsed_label, invalid_label_ser) == 0);
CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_label_serialize(CTX, parsed_label_ser, &parsed_label));
@@ -615,12 +615,12 @@ static void test_recipient_api(void) {
found = 0;
CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &found, output_xonly_ptrs, 1, ALICE_SECKEY, &ps, &spk, &label_lookup, &labels_cache));
CHECK(found == 1);
- CHECK(secp256k1_memcmp_var(fp[0]->tweak, zero, 32) == 0);
+ CHECK(memcmp(fp[0]->tweak, zero, 32) == 0);
CHECK(fp[0]->found_with_label == 1);
{
unsigned char found_label_ser[33];
CHECK(secp256k1_silentpayments_recipient_label_serialize(CTX, found_label_ser, &fp[0]->label));
- CHECK(secp256k1_memcmp_var(found_label_ser, labels_cache.entries[0].label, 33) == 0);
+ CHECK(memcmp(found_label_ser, labels_cache.entries[0].label, 33) == 0);
}
/* check that a label tweak overflowing the group order is rejected */
memcpy(labels_cache.entries[0].label_tweak, secp256k1_group_order_bytes, 32);
@@ -730,7 +730,7 @@ static void test_recipient_scan_label_precedes_direct_match(void) {
CHECK(secp256k1_xonly_pubkey_cmp(CTX, &found_output[0].output, &labeled_output) == 0);
CHECK(found_output[0].found_with_label);
CHECK(secp256k1_silentpayments_recipient_label_serialize(CTX, found_label, &found_output[0].label));
- CHECK(secp256k1_memcmp_var(found_label, cache.entries[0].label, sizeof(found_label)) == 0);
+ CHECK(memcmp(found_label, cache.entries[0].label, sizeof(found_label)) == 0);
}
static void test_recipient_scan_label_batch_index(void) {
@@ -843,7 +843,7 @@ void run_silentpayments_test_vector_send(const struct bip352_test_vector *test)
CHECK(secp256k1_xonly_pubkey_serialize(CTX, created_output, &generated_outputs[j]));
/* Loop over both lists to ensure tests don't fail due to different orderings of outputs */
for (k = 0; k < test->num_recipient_outputs; k++) {
- if (secp256k1_memcmp_var(created_output, test->recipient_outputs[i][k], 32) == 0) {
+ if (memcmp(created_output, test->recipient_outputs[i][k], 32) == 0) {
n_matches++;
break;
}
@@ -960,10 +960,10 @@ void run_silentpayments_test_vector_receive(const struct bip352_test_vector *tes
int match = 0;
CHECK(secp256k1_xonly_pubkey_serialize(CTX, found_output, &found_outputs[i]->output));
for (j = 0; j < subtest->num_found_output_pubkeys; j++) {
- if (secp256k1_memcmp_var(&found_output, subtest->found_output_pubkeys[j], 32) == 0) {
- CHECK(secp256k1_memcmp_var(found_outputs[i]->tweak, subtest->found_seckey_tweaks[j], 32) == 0);
+ if (memcmp(&found_output, subtest->found_output_pubkeys[j], 32) == 0) {
+ CHECK(memcmp(found_outputs[i]->tweak, subtest->found_seckey_tweaks[j], 32) == 0);
#ifdef ENABLE_MODULE_SCHNORRSIG
- CHECK(secp256k1_memcmp_var(found_signatures[i], subtest->found_signatures[j], 64) == 0);
+ CHECK(memcmp(found_signatures[i], subtest->found_signatures[j], 64) == 0);
#endif
match = 1;
break;
### src/scalar_impl.h
@@ -308,12 +308,12 @@ static void secp256k1_scalar_split_lambda_verify(const secp256k1_scalar *r1, con
secp256k1_scalar_negate(&s, r1);
secp256k1_scalar_get_b32(buf1, r1);
secp256k1_scalar_get_b32(buf2, &s);
- VERIFY_CHECK(secp256k1_memcmp_var(buf1, k1_bound, 32) < 0 || secp256k1_memcmp_var(buf2, k1_bound, 32) < 0);
+ VERIFY_CHECK(memcmp(buf1, k1_bound, 32) < 0 || memcmp(buf2, k1_bound, 32) < 0);
secp256k1_scalar_negate(&s, r2);
secp256k1_scalar_get_b32(buf1, r2);
secp256k1_scalar_get_b32(buf2, &s);
- VERIFY_CHECK(secp256k1_memcmp_var(buf1, k2_bound, 32) < 0 || secp256k1_memcmp_var(buf2, k2_bound, 32) < 0);
+ VERIFY_CHECK(memcmp(buf1, k2_bound, 32) < 0 || memcmp(buf2, k2_bound, 32) < 0);
}
#endif /* VERIFY */
#endif /* !defined(EXHAUSTIVE_TEST_ORDER) */
### src/scratch_impl.h
@@ -31,7 +31,7 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch) {
if (scratch != NULL) {
- if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
+ if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return;
}
@@ -42,15 +42,15 @@ static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback,
}
static size_t secp256k1_scratch_checkpoint(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch) {
- if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
+ if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return 0;
}
return scratch->alloc_size;
}
static void secp256k1_scratch_apply_checkpoint(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t checkpoint) {
- if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
+ if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return;
}
@@ -62,7 +62,7 @@ static void secp256k1_scratch_apply_checkpoint(const secp256k1_callback* error_c
}
static size_t secp256k1_scratch_max_allocation(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch, size_t objects) {
- if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
+ if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return 0;
}
@@ -87,7 +87,7 @@ static void *secp256k1_scratch_alloc(const secp256k1_callback* error_callback, s
}
size = rounded_size;
- if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
+ if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return NULL;
}
### src/secp256k1.c
@@ -334,7 +334,7 @@ int secp256k1_ec_pubkey_cmp(const secp256k1_context* ctx, const secp256k1_pubkey
memset(out[i], 0, sizeof(out[i]));
}
}
- return secp256k1_memcmp_var(out[0], out[1], sizeof(out[0]));
+ return memcmp(out[0], out[1], sizeof(out[0]));
}
static int secp256k1_ec_pubkey_sort_cmp(const void* pk1, const void* pk2, void *ctx) {
### src/selftest.h
@@ -11,6 +11,21 @@
#include <string.h>
+/* Check that the compiler does not miscompile memcmp.
+ *
+ * GCC 9.2 to 9.5, 10.1 and 10.2 may stop comparing at the first zero
+ * byte of a constant array when optimizing with -O2 or -O3, see
+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189 (fixed in GCC 10.3).
+ * On affected compilers, building with -fno-builtin-memcmp works around
+ * the bug.
+ */
+static int secp256k1_selftest_memcmp(void) {
+ char a[] = "\0abc";
+ /* volatile is required to trigger the bug. */
+ volatile int res = memcmp(a, "\0\0\0\0", 4);
+ return res != 0;
+}
+
static int secp256k1_selftest_sha256(secp256k1_sha256_compression_function fn_compression) {
secp256k1_hash_ctx hash_ctx;
static const char *input63 = "For this sample, this 63-byte string will be used as input data";
@@ -24,7 +39,7 @@ static int secp256k1_selftest_sha256(secp256k1_sha256_compression_function fn_co
hash_ctx.fn_sha256_compression = fn_compression;
secp256k1_sha256_write(&hash_ctx, &hasher, (const unsigned char*)input63, 63);
secp256k1_sha256_finalize(&hash_ctx, &hasher, out);
- if (secp256k1_memcmp_var(out, output32, 32) != 0) {
+ if (memcmp(out, output32, 32) != 0) {
return 0;
}
if (fn_compression != secp256k1_sha256_transform && !secp256k1_sha256_smoke_test(fn_compression)) {
@@ -34,8 +49,9 @@ static int secp256k1_selftest_sha256(secp256k1_sha256_compression_function fn_co
}
static int secp256k1_selftest_passes(void) {
- /* Use default sha256 compression */
- return secp256k1_selftest_sha256(secp256k1_sha256_transform);
+ return secp256k1_selftest_memcmp()
+ /* Use default sha256 compression */
+ && secp256k1_selftest_sha256(secp256k1_sha256_transform);
}
#endif /* SECP256K1_SELFTEST_H */
### src/tests.c
@@ -119,7 +119,7 @@ static void run_xoshiro256pp_tests(void) {
for (i = 0; i < 17; i++) {
testrand256(buf32);
}
- CHECK(secp256k1_memcmp_var(buf32, buf32_expected, sizeof(buf32)) == 0);
+ CHECK(memcmp(buf32, buf32_expected, sizeof(buf32)) == 0);
}
}
@@ -591,7 +591,7 @@ static void run_sha256_multi_block_compression_tests(void) {
secp256k1_sha256_write(&hash_ctx, &sha256_two, data32, 32);
secp256k1_sha256_finalize(&hash_ctx, &sha256_two, out_two);
- CHECK(secp256k1_memcmp_var(out_one, out_two, 32) == 0);
+ CHECK(memcmp(out_one, out_two, 32) == 0);
}
{ /* 2) Writing one 80-byte block vs two 40-byte blocks */
@@ -612,7 +612,7 @@ static void run_sha256_multi_block_compression_tests(void) {
secp256k1_sha256_write(&hash_ctx, &sha256_two, data40, 40);
secp256k1_sha256_finalize(&hash_ctx, &sha256_two, out_two);
- CHECK(secp256k1_memcmp_var(out_one, out_two, 32) == 0);
+ CHECK(memcmp(out_one, out_two, 32) == 0);
}
{ /* 3) Writing multiple consecutive full blocks in one write (128 bytes) */
@@ -632,7 +632,7 @@ static void run_sha256_multi_block_compression_tests(void) {
secp256k1_sha256_write(&hash_ctx, &sha256_two, data + 64, 64);
secp256k1_sha256_finalize(&hash_ctx, &sha256_two, out_two);
- CHECK(secp256k1_memcmp_var(out_one, out_two, 32) == 0);
+ CHECK(memcmp(out_one, out_two, 32) == 0);
}
{ /* 4) Mixed small + large writes in sequence */
@@ -654,7 +654,7 @@ static void run_sha256_multi_block_compression_tests(void) {
secp256k1_sha256_write(&hash_ctx, &sha256_two, data + 138, 12);
secp256k1_sha256_finalize(&hash_ctx, &sha256_two, out_two);
- CHECK(secp256k1_memcmp_var(out_one, out_two, 32) == 0);
+ CHECK(memcmp(out_one, out_two, 32) == 0);
}
}
@@ -720,7 +720,7 @@ static void run_sha256_known_output_tests(void) {
j--;
}
secp256k1_sha256_finalize(hash_ctx, &hasher, out);
- CHECK(secp256k1_memcmp_var(out, outputs[i], 32) == 0);
+ CHECK(memcmp(out, outputs[i], 32) == 0);
/* 2. Run: split the input bytestrings randomly before writing */
if (strlen(inputs[i]) > 0) {
int split = testrand_int(strlen(inputs[i]));
@@ -732,7 +732,7 @@ static void run_sha256_known_output_tests(void) {
j--;
}
secp256k1_sha256_finalize(hash_ctx, &hasher, out);
- CHECK(secp256k1_memcmp_var(out, outputs[i], 32) == 0);
+ CHECK(memcmp(out, outputs[i], 32) == 0);
}
}
}
@@ -836,7 +836,7 @@ static void run_sha256_counter_tests(void) {
secp256k1_sha256 hasher = midstates[i];
secp256k1_sha256_write(hash_ctx, &hasher, (const unsigned char*)input, strlen(input));
secp256k1_sha256_finalize(hash_ctx, &hasher, out);
- CHECK(secp256k1_memcmp_var(out, outputs[i], 32) == 0);
+ CHECK(memcmp(out, outputs[i], 32) == 0);
}
}
@@ -848,7 +848,7 @@ static void test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_sha256
CHECK((sha1->bytes & 0x3F) == 0);
CHECK(sha1->bytes == sha2->bytes);
- CHECK(secp256k1_memcmp_var(sha1->s, sha2->s, sizeof(sha1->s)) == 0);
+ CHECK(memcmp(sha1->s, sha2->s, sizeof(sha1->s)) == 0);
}
/* Convenience function for using test_sha256_eq to verify the correctness of a
* tagged hash midstate. This function is used by some module tests. */
@@ -891,14 +891,14 @@ static void run_hmac_sha256_tests(void) {
secp256k1_hmac_sha256_initialize(hash_ctx, &hasher, (const unsigned char*)(keys[i]), strlen(keys[i]));
secp256k1_hmac_sha256_write(hash_ctx, &hasher, (const unsigned char*)(inputs[i]), strlen(inputs[i]));
secp256k1_hmac_sha256_finalize(hash_ctx, &hasher, out);
- CHECK(secp256k1_memcmp_var(out, outputs[i], 32) == 0);
+ CHECK(memcmp(out, outputs[i], 32) == 0);
if (strlen(inputs[i]) > 0) {
int split = testrand_int(strlen(inputs[i]));
secp256k1_hmac_sha256_initialize(hash_ctx, &hasher, (const unsigned char*)(keys[i]), strlen(keys[i]));
secp256k1_hmac_sha256_write(hash_ctx, &hasher, (const unsigned char*)(inputs[i]), split);
secp256k1_hmac_sha256_write(hash_ctx, &hasher, (const unsigned char*)(inputs[i] + split), strlen(inputs[i]) - split);
secp256k1_hmac_sha256_finalize(hash_ctx, &hasher, out);
- CHECK(secp256k1_memcmp_var(out, outputs[i], 32) == 0);
+ CHECK(memcmp(out, outputs[i], 32) == 0);
}
}
}
@@ -926,21 +926,21 @@ static void run_rfc6979_hmac_sha256_tests(void) {
secp256k1_rfc6979_hmac_sha256_initialize(hash_ctx, &rng, key1, 64);
for (i = 0; i < 3; i++) {
secp256k1_rfc6979_hmac_sha256_generate(hash_ctx, &rng, out, 32);
- CHECK(secp256k1_memcmp_var(out, out1[i], 32) == 0);
+ CHECK(memcmp(out, out1[i], 32) == 0);
}
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
secp256k1_rfc6979_hmac_sha256_initialize(hash_ctx, &rng, key1, 65);
for (i = 0; i < 3; i++) {
secp256k1_rfc6979_hmac_sha256_generate(hash_ctx, &rng, out, 32);
- CHECK(secp256k1_memcmp_var(out, out1[i], 32) != 0);
+ CHECK(memcmp(out, out1[i], 32) != 0);
}
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
secp256k1_rfc6979_hmac_sha256_initialize(hash_ctx, &rng, key2, 64);
for (i = 0; i < 3; i++) {
secp256k1_rfc6979_hmac_sha256_generate(hash_ctx, &rng, out, 32);
- CHECK(secp256k1_memcmp_var(out, out2[i], 32) == 0);
+ CHECK(memcmp(out, out2[i], 32) == 0);
}
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
}
@@ -966,7 +966,7 @@ static void run_tagged_sha256_tests(void) {
memcpy(tag, "tag", 3);
memcpy(msg, "msg", 3);
CHECK(secp256k1_tagged_sha256(CTX, hash32, tag, 3, msg, 3) == 1);
- CHECK(secp256k1_memcmp_var(hash32, hash_expected, sizeof(hash32)) == 0);
+ CHECK(memcmp(hash32, hash_expected, sizeof(hash32)) == 0);
}
static void run_sha256_initialize_midstate_tests(void) {
@@ -2072,34 +2072,34 @@ static void run_int128_test_case(void) {
mulmod256(ruwr, rub, ruc, NULL);
secp256k1_u128_mul(&uwz, ub, uc);
load256u128(ruwz, &uwz);
- CHECK(secp256k1_memcmp_var(ruwr, ruwz, 16) == 0);
+ CHECK(memcmp(ruwr, ruwz, 16) == 0);
/* test secp256k1_u128_accum_mul */
mulmod256(ruwr, rub, ruc, NULL);
add256(ruwr, ruwr, ruwa);
uwz = uwa;
secp256k1_u128_accum_mul(&uwz, ub, uc);
load256u128(ruwz, &uwz);
- CHECK(secp256k1_memcmp_var(ruwr, ruwz, 16) == 0);
+ CHECK(memcmp(ruwr, ruwz, 16) == 0);
/* test secp256k1_u128_accum_u64 */
add256(ruwr, rub, ruwa);
uwz = uwa;
secp256k1_u128_accum_u64(&uwz, ub);
load256u128(ruwz, &uwz);
- CHECK(secp256k1_memcmp_var(ruwr, ruwz, 16) == 0);
+ CHECK(memcmp(ruwr, ruwz, 16) == 0);
/* test secp256k1_u128_rshift */
rshift256(ruwr, ruwa, uc % 128, 0);
uwz = uwa;
secp256k1_u128_rshift(&uwz, uc % 128);
load256u128(ruwz, &uwz);
- CHECK(secp256k1_memcmp_var(ruwr, ruwz, 16) == 0);
+ CHECK(memcmp(ruwr, ruwz, 16) == 0);
/* test secp256k1_u128_to_u64 */
CHECK(secp256k1_u128_to_u64(&uwa) == v[0]);
/* test secp256k1_u128_hi_u64 */
CHECK(secp256k1_u128_hi_u64(&uwa) == v[1]);
/* test secp256k1_u128_from_u64 */
secp256k1_u128_from_u64(&uwz, ub);
load256u128(ruwz, &uwz);
- CHECK(secp256k1_memcmp_var(rub, ruwz, 16) == 0);
+ CHECK(memcmp(rub, ruwz, 16) == 0);
/* test secp256k1_u128_check_bits */
{
int uwa_bits = 0;
@@ -2115,15 +2115,15 @@ static void run_int128_test_case(void) {
mulmod256(rswr, rsb, rsc, NULL);
secp256k1_i128_mul(&swz, sb, sc);
load256i128(rswz, &swz);
- CHECK(secp256k1_memcmp_var(rswr, rswz, 16) == 0);
+ CHECK(memcmp(rswr, rswz, 16) == 0);
/* test secp256k1_i128_accum_mul */
mulmod256(rswr, rsb, rsc, NULL);
add256(rswr, rswr, rswa);
if (int256is127(rswr)) {
swz = swa;
secp256k1_i128_accum_mul(&swz, sb, sc);
load256i128(rswz, &swz);
- CHECK(secp256k1_memcmp_var(rswr, rswz, 16) == 0);
+ CHECK(memcmp(rswr, rswz, 16) == 0);
}
/* test secp256k1_i128_det */
{
@@ -2137,20 +2137,20 @@ static void run_int128_test_case(void) {
add256(rswr, rswr, rst);
secp256k1_i128_det(&swz, sb, sc, sd, se);
load256i128(rswz, &swz);
- CHECK(secp256k1_memcmp_var(rswr, rswz, 16) == 0);
+ CHECK(memcmp(rswr, rswz, 16) == 0);
}
/* test secp256k1_i128_rshift */
rshift256(rswr, rswa, uc % 127, 1);
swz = swa;
secp256k1_i128_rshift(&swz, uc % 127);
load256i128(rswz, &swz);
- CHECK(secp256k1_memcmp_var(rswr, rswz, 16) == 0);
+ CHECK(memcmp(rswr, rswz, 16) == 0);
/* test secp256k1_i128_to_u64 */
CHECK(secp256k1_i128_to_u64(&swa) == v[0]);
/* test secp256k1_i128_from_i64 */
secp256k1_i128_from_i64(&swz, sb);
load256i128(rswz, &swz);
- CHECK(secp256k1_memcmp_var(rsb, rswz, 16) == 0);
+ CHECK(memcmp(rsb, rswz, 16) == 0);
/* test secp256k1_i128_to_i64 */
CHECK(secp256k1_i128_to_i64(&swz) == sb);
/* test secp256k1_i128_eq_var */
@@ -2477,7 +2477,7 @@ static void test_scalar_check_overflow(void) {
memset(b32, 0xFF, 16);
}
- expected_overflow = (secp256k1_memcmp_var(b32, secp256k1_group_order_bytes, 32) >= 0);
+ expected_overflow = (memcmp(b32, secp256k1_group_order_bytes, 32) >= 0);
secp256k1_scalar_set_b32(&s, b32, &overflow);
CHECK(overflow == expected_overflow);
@@ -3178,9 +3178,9 @@ static void run_field_convert(void) {
CHECK(secp256k1_fe_equal(&fe, &fe2));
/* Check conversion from fe. */
secp256k1_fe_get_b32(b322, &fe);
- CHECK(secp256k1_memcmp_var(b322, b32, 32) == 0);
+ CHECK(memcmp(b322, b32, 32) == 0);
secp256k1_fe_to_storage(&fes2, &fe);
- CHECK(secp256k1_memcmp_var(&fes2, &fes, sizeof(fes)) == 0);
+ CHECK(memcmp(&fes2, &fes, sizeof(fes)) == 0);
}
static void run_field_be32_overflow(void) {
@@ -3200,7 +3200,7 @@ static void run_field_be32_overflow(void) {
secp256k1_fe_normalize(&fe);
CHECK(secp256k1_fe_is_zero(&fe) == 1);
secp256k1_fe_get_b32(out, &fe);
- CHECK(secp256k1_memcmp_var(out, zero, 32) == 0);
+ CHECK(memcmp(out, zero, 32) == 0);
}
{
static const unsigned char one_overflow[32] = {
@@ -3222,7 +3222,7 @@ static void run_field_be32_overflow(void) {
secp256k1_fe_normalize(&fe);
CHECK(secp256k1_fe_cmp_var(&fe, &secp256k1_fe_one) == 0);
secp256k1_fe_get_b32(out, &fe);
- CHECK(secp256k1_memcmp_var(out, one, 32) == 0);
+ CHECK(memcmp(out, one, 32) == 0);
}
{
static const unsigned char ff_overflow[32] = {
@@ -3245,15 +3245,15 @@ static void run_field_be32_overflow(void) {
secp256k1_fe_normalize(&fe);
CHECK(secp256k1_fe_cmp_var(&fe, &fe_ff) == 0);
secp256k1_fe_get_b32(out, &fe);
- CHECK(secp256k1_memcmp_var(out, ff, 32) == 0);
+ CHECK(memcmp(out, ff, 32) == 0);
}
}
/* Returns true if two field elements have the same representation. */
static int fe_identical(const secp256k1_fe *a, const secp256k1_fe *b) {
int ret = 1;
/* Compare the struct member that holds the limbs. */
- ret &= (secp256k1_memcmp_var(a->n, b->n, sizeof(a->n)) == 0);
+ ret &= (memcmp(a->n, b->n, sizeof(a->n)) == 0);
return ret;
}
@@ -3378,9 +3378,9 @@ static void run_field_misc(void) {
secp256k1_fe_to_storage(&zs, &z);
secp256k1_fe_storage_cmov(&zs, &xs, 0);
secp256k1_fe_storage_cmov(&zs, &zs, 1);
- CHECK(secp256k1_memcmp_var(&xs, &zs, sizeof(xs)) != 0);
+ CHECK(memcmp(&xs, &zs, sizeof(xs)) != 0);
secp256k1_fe_storage_cmov(&ys, &xs, 1);
- CHECK(secp256k1_memcmp_var(&xs, &ys, sizeof(xs)) == 0);
+ CHECK(memcmp(&xs, &ys, sizeof(xs)) == 0);
secp256k1_fe_from_storage(&x, &xs);
secp256k1_fe_from_storage(&y, &ys);
secp256k1_fe_from_storage(&z, &zs);
@@ -3453,7 +3453,7 @@ static void test_fe_mul(const secp256k1_fe* a, const secp256k1_fe* b, int use_sq
/* Compute T = A * B in LE 16x uint16_t format. */
mulmod256(t16, a16, b16, m16);
/* Compare */
- CHECK(secp256k1_memcmp_var(t16, c16, 32) == 0);
+ CHECK(memcmp(t16, c16, 32) == 0);
}
static void run_fe_mul(void) {
@@ -3880,13 +3880,13 @@ static void test_heap_swap(void) {
memset(e + 200, 99, 200);
memset(e + 400, 21, 200);
secp256k1_heap_swap(a, 0, 2, 200);
- CHECK(secp256k1_memcmp_var(a, e, sizeof(a)) == 0);
+ CHECK(memcmp(a, e, sizeof(a)) == 0);
}
static void test_hsort_is_sorted(unsigned char *elements, size_t n, size_t len) {
size_t i;
for (i = 1; i < n; i++) {
- CHECK(secp256k1_memcmp_var(&elements[(i-1) * len], &elements[i * len], len) <= 0);
+ CHECK(memcmp(&elements[(i-1) * len], &elements[i * len], len) <= 0);
}
}
@@ -3899,7 +3899,7 @@ struct test_hsort_cmp_data {
static int test_hsort_cmp(const void *ele1, const void *ele2, void *data) {
struct test_hsort_cmp_data *d = (struct test_hsort_cmp_data *) data;
d->counter += 1;
- return secp256k1_memcmp_var((unsigned char *)ele1, (unsigned char *)ele2, d->element_len);
+ return memcmp((unsigned char *)ele1, (unsigned char *)ele2, d->element_len);
}
#define NUM 65
@@ -4413,7 +4413,7 @@ static void test_ec_combine(void) {
secp256k1_ecmult_gen_ge(&CTX->ecmult_gen_ctx, &Q, &sum);
secp256k1_pubkey_save(&sd, &Q);
CHECK(secp256k1_ec_pubkey_combine(CTX, &sd2, d, i) == 1);
- CHECK(secp256k1_memcmp_var(&sd, &sd2, sizeof(sd)) == 0);
+ CHECK(memcmp(&sd, &sd2, sizeof(sd)) == 0);
}
}
@@ -4532,7 +4532,7 @@ static void run_ecmult_pre_g(void) {
/* Check the first entry from the pre_g table. */
secp256k1_ge_to_storage(&gs, &secp256k1_ge_const_g);
- CHECK(secp256k1_memcmp_var(&gs, &secp256k1_pre_g[0], sizeof(gs)) == 0);
+ CHECK(memcmp(&gs, &secp256k1_pre_g[0], sizeof(gs)) == 0);
/* Check the first entry from the pre_g_128 table. */
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
@@ -4541,7 +4541,7 @@ static void run_ecmult_pre_g(void) {
}
secp256k1_ge_set_gej(&g, &gj);
secp256k1_ge_to_storage(&gs, &g);
- CHECK(secp256k1_memcmp_var(&gs, &secp256k1_pre_g_128[0], sizeof(gs)) == 0);
+ CHECK(memcmp(&gs, &secp256k1_pre_g_128[0], sizeof(gs)) == 0);
}
static void run_ecmult_chain(void) {
@@ -5790,7 +5790,7 @@ static void test_ecmult_constants_2bit(void) {
}
}
secp256k1_sha256_finalize(&CTX->hash_ctx, &acc, b32);
- CHECK(secp256k1_memcmp_var(b32, expected32, 32) == 0);
+ CHECK(memcmp(b32, expected32, 32) == 0);
secp256k1_scratch_space_destroy(CTX, scratch);
}
@@ -5834,7 +5834,7 @@ static void test_ecmult_constants_sha(uint32_t prefix, size_t iter, const unsign
test_ecmult_accumulate(&acc, &x, scratch);
}
secp256k1_sha256_finalize(hash_ctx, &acc, b32);
- CHECK(secp256k1_memcmp_var(b32, expected32, 32) == 0);
+ CHECK(memcmp(b32, expected32, 32) == 0);
secp256k1_scratch_space_destroy(CTX, scratch);
}
@@ -5978,9 +5978,9 @@ static void test_scalar_split(const secp256k1_scalar* full) {
}
secp256k1_scalar_get_b32(tmp, &s1);
- CHECK(secp256k1_memcmp_var(zero, tmp, 16) == 0);
+ CHECK(memcmp(zero, tmp, 16) == 0);
secp256k1_scalar_get_b32(tmp, &slam);
- CHECK(secp256k1_memcmp_var(zero, tmp, 16) == 0);
+ CHECK(memcmp(zero, tmp, 16) == 0);
}
@@ -6044,7 +6044,7 @@ static void ec_pubkey_parse_pointtest(const unsigned char *input, int xvalid, in
CHECK(secp256k1_ec_pubkey_serialize(CTX, pubkeyo, &outl, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
SECP256K1_CHECKMEM_CHECK(pubkeyo, outl);
CHECK(outl == 33);
- CHECK(secp256k1_memcmp_var(&pubkeyo[1], &pubkeyc[1], 32) == 0);
+ CHECK(memcmp(&pubkeyo[1], &pubkeyc[1], 32) == 0);
CHECK((pubkeyclen != 33) || (pubkeyo[0] == pubkeyc[0]));
if (ypass) {
/* This test isn't always done because we decode with alternative signs, so the y won't match. */
@@ -6060,7 +6060,7 @@ static void ec_pubkey_parse_pointtest(const unsigned char *input, int xvalid, in
SECP256K1_CHECKMEM_CHECK(pubkeyo, outl);
CHECK(outl == 65);
CHECK(pubkeyo[0] == 4);
- CHECK(secp256k1_memcmp_var(&pubkeyo[1], input, 64) == 0);
+ CHECK(memcmp(&pubkeyo[1], input, 64) == 0);
}
} else {
/* These cases must fail to parse. */
@@ -6392,7 +6392,7 @@ static void run_ec_pubkey_parse_test(void) {
CHECK(secp256k1_ec_pubkey_serialize(CTX, sout, &len, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
SECP256K1_CHECKMEM_CHECK(sout, 33);
CHECK(len == 33);
- CHECK(secp256k1_memcmp_var(sout, pubkeyc_comp, 33) == 0);
+ CHECK(memcmp(sout, pubkeyc_comp, 33) == 0);
/* Try a bunch of prefabbed points with all possible encodings. */
for (i = 0; i < SECP256K1_EC_PARSE_TEST_NVALID; i++) {
@@ -6423,31 +6423,31 @@ static void run_eckey_edge_case_test(void) {
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_create(CTX, &pubkey, orderc) == 0);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
/* Maximum value is too large, reject. */
memset(ctmp, 255, 32);
CHECK(secp256k1_ec_seckey_verify(CTX, ctmp) == 0);
memset(&pubkey, 1, sizeof(pubkey));
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_create(CTX, &pubkey, ctmp) == 0);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
/* Zero is too small, reject. */
memset(ctmp, 0, 32);
CHECK(secp256k1_ec_seckey_verify(CTX, ctmp) == 0);
memset(&pubkey, 1, sizeof(pubkey));
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_create(CTX, &pubkey, ctmp) == 0);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
/* One must be accepted. */
ctmp[31] = 0x01;
CHECK(secp256k1_ec_seckey_verify(CTX, ctmp) == 1);
memset(&pubkey, 0, sizeof(pubkey));
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_create(CTX, &pubkey, ctmp) == 1);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
pubkey_one = pubkey;
/* Group order + 1 is too large, reject. */
memcpy(ctmp, orderc, 32);
@@ -6457,28 +6457,28 @@ static void run_eckey_edge_case_test(void) {
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_create(CTX, &pubkey, ctmp) == 0);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
/* -1 must be accepted. */
ctmp[31] = 0x40;
CHECK(secp256k1_ec_seckey_verify(CTX, ctmp) == 1);
memset(&pubkey, 0, sizeof(pubkey));
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_create(CTX, &pubkey, ctmp) == 1);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
pubkey_negone = pubkey;
/* Tweak of zero leaves the value unchanged. */
memset(ctmp2, 0, 32);
CHECK(secp256k1_ec_seckey_tweak_add(CTX, ctmp, ctmp2) == 1);
- CHECK(secp256k1_memcmp_var(orderc, ctmp, 31) == 0 && ctmp[31] == 0x40);
+ CHECK(memcmp(orderc, ctmp, 31) == 0 && ctmp[31] == 0x40);
memcpy(&pubkey2, &pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_tweak_add(CTX, &pubkey, ctmp2) == 1);
- CHECK(secp256k1_memcmp_var(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
/* Multiply tweak of zero zeroizes the output. */
CHECK(secp256k1_ec_seckey_tweak_mul(CTX, ctmp, ctmp2) == 0);
- CHECK(secp256k1_memcmp_var(zeros, ctmp, 32) == 0);
+ CHECK(memcmp(zeros, ctmp, 32) == 0);
CHECK(secp256k1_ec_pubkey_tweak_mul(CTX, &pubkey, ctmp2) == 0);
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
/* If seckey_tweak_add or seckey_tweak_mul are called with an overflowing
seckey, the seckey is zeroized. */
@@ -6488,29 +6488,29 @@ static void run_eckey_edge_case_test(void) {
CHECK(secp256k1_ec_seckey_verify(CTX, ctmp2) == 1);
CHECK(secp256k1_ec_seckey_verify(CTX, ctmp) == 0);
CHECK(secp256k1_ec_seckey_tweak_add(CTX, ctmp, ctmp2) == 0);
- CHECK(secp256k1_memcmp_var(zeros, ctmp, 32) == 0);
+ CHECK(memcmp(zeros, ctmp, 32) == 0);
memcpy(ctmp, orderc, 32);
CHECK(secp256k1_ec_seckey_tweak_mul(CTX, ctmp, ctmp2) == 0);
- CHECK(secp256k1_memcmp_var(zeros, ctmp, 32) == 0);
+ CHECK(memcmp(zeros, ctmp, 32) == 0);
/* If seckey_tweak_add or seckey_tweak_mul are called with an overflowing
tweak, the seckey is zeroized. */
memcpy(ctmp, orderc, 32);
ctmp[31] = 0x40;
CHECK(secp256k1_ec_seckey_tweak_add(CTX, ctmp, orderc) == 0);
- CHECK(secp256k1_memcmp_var(zeros, ctmp, 32) == 0);
+ CHECK(memcmp(zeros, ctmp, 32) == 0);
memcpy(ctmp, orderc, 32);
ctmp[31] = 0x40;
CHECK(secp256k1_ec_seckey_tweak_mul(CTX, ctmp, orderc) == 0);
- CHECK(secp256k1_memcmp_var(zeros, ctmp, 32) == 0);
+ CHECK(memcmp(zeros, ctmp, 32) == 0);
memcpy(ctmp, orderc, 32);
ctmp[31] = 0x40;
/* If pubkey_tweak_add or pubkey_tweak_mul are called with an overflowing
tweak, the pubkey is zeroized. */
CHECK(secp256k1_ec_pubkey_tweak_add(CTX, &pubkey, orderc) == 0);
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_tweak_mul(CTX, &pubkey, orderc) == 0);
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
/* If the resulting key in secp256k1_ec_seckey_tweak_add and
* secp256k1_ec_pubkey_tweak_add is 0 the functions fail and in the latter
@@ -6520,33 +6520,33 @@ static void run_eckey_edge_case_test(void) {
memset(ctmp2, 0, 32);
ctmp2[31] = 1;
CHECK(secp256k1_ec_seckey_tweak_add(CTX, ctmp2, ctmp) == 0);
- CHECK(secp256k1_memcmp_var(zeros, ctmp2, 32) == 0);
+ CHECK(memcmp(zeros, ctmp2, 32) == 0);
ctmp2[31] = 1;
CHECK(secp256k1_ec_pubkey_tweak_add(CTX, &pubkey, ctmp2) == 0);
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
/* Tweak computation wraps and results in a key of 1. */
ctmp2[31] = 2;
CHECK(secp256k1_ec_seckey_tweak_add(CTX, ctmp2, ctmp) == 1);
- CHECK(secp256k1_memcmp_var(ctmp2, zeros, 31) == 0 && ctmp2[31] == 1);
+ CHECK(memcmp(ctmp2, zeros, 31) == 0 && ctmp2[31] == 1);
ctmp2[31] = 2;
CHECK(secp256k1_ec_pubkey_tweak_add(CTX, &pubkey, ctmp2) == 1);
ctmp2[31] = 1;
CHECK(secp256k1_ec_pubkey_create(CTX, &pubkey2, ctmp2) == 1);
- CHECK(secp256k1_memcmp_var(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
/* Tweak mul * 2 = 1+1. */
CHECK(secp256k1_ec_pubkey_tweak_add(CTX, &pubkey, ctmp2) == 1);
ctmp2[31] = 2;
CHECK(secp256k1_ec_pubkey_tweak_mul(CTX, &pubkey2, ctmp2) == 1);
- CHECK(secp256k1_memcmp_var(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
/* Zeroize pubkey on parse error. */
memset(&pubkey, 0, 32);
CHECK_ILLEGAL(CTX, secp256k1_ec_pubkey_tweak_add(CTX, &pubkey, ctmp2));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
memset(&pubkey2, 0, 32);
CHECK_ILLEGAL(CTX, secp256k1_ec_pubkey_tweak_mul(CTX, &pubkey2, ctmp2));
- CHECK(secp256k1_memcmp_var(&pubkey2, zeros, sizeof(pubkey2)) == 0);
+ CHECK(memcmp(&pubkey2, zeros, sizeof(pubkey2)) == 0);
/* Plain argument errors. */
CHECK(secp256k1_ec_seckey_verify(CTX, ctmp) == 1);
CHECK_ILLEGAL(CTX, secp256k1_ec_seckey_verify(CTX, NULL));
@@ -6568,7 +6568,7 @@ static void run_eckey_edge_case_test(void) {
CHECK_ILLEGAL(CTX, secp256k1_ec_pubkey_create(CTX, NULL, ctmp));
memset(&pubkey, 1, sizeof(pubkey));
CHECK_ILLEGAL(CTX, secp256k1_ec_pubkey_create(CTX, &pubkey, NULL));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
/* secp256k1_ec_pubkey_combine tests. */
pubkeys[0] = &pubkey_one;
SECP256K1_CHECKMEM_UNDEFINE(&pubkeys[0], sizeof(secp256k1_pubkey *));
@@ -6578,39 +6578,39 @@ static void run_eckey_edge_case_test(void) {
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(secp256k1_pubkey));
CHECK_ILLEGAL(CTX, secp256k1_ec_pubkey_combine(CTX, &pubkey, pubkeys, 0));
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(secp256k1_pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
CHECK_ILLEGAL(CTX, secp256k1_ec_pubkey_combine(CTX, NULL, pubkeys, 1));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
memset(&pubkey, 255, sizeof(secp256k1_pubkey));
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(secp256k1_pubkey));
CHECK_ILLEGAL(CTX, secp256k1_ec_pubkey_combine(CTX, &pubkey, NULL, 1));
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(secp256k1_pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
pubkeys[0] = &pubkey_negone;
memset(&pubkey, 255, sizeof(secp256k1_pubkey));
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(secp256k1_pubkey));
CHECK(secp256k1_ec_pubkey_combine(CTX, &pubkey, pubkeys, 1) == 1);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(secp256k1_pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
len = 33;
CHECK(secp256k1_ec_pubkey_serialize(CTX, ctmp, &len, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
CHECK(secp256k1_ec_pubkey_serialize(CTX, ctmp2, &len, &pubkey_negone, SECP256K1_EC_COMPRESSED) == 1);
- CHECK(secp256k1_memcmp_var(ctmp, ctmp2, 33) == 0);
+ CHECK(memcmp(ctmp, ctmp2, 33) == 0);
/* Result is infinity. */
pubkeys[0] = &pubkey_one;
pubkeys[1] = &pubkey_negone;
memset(&pubkey, 255, sizeof(secp256k1_pubkey));
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(secp256k1_pubkey));
CHECK(secp256k1_ec_pubkey_combine(CTX, &pubkey, pubkeys, 2) == 0);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(secp256k1_pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
/* Passes through infinity but comes out one. */
pubkeys[2] = &pubkey_one;
memset(&pubkey, 255, sizeof(secp256k1_pubkey));
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(secp256k1_pubkey));
CHECK(secp256k1_ec_pubkey_combine(CTX, &pubkey, pubkeys, 3) == 1);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(secp256k1_pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
/* check that NULL in array of pubkey pointers is not allowed */
for (i = 0; i < 3; i++) {
const secp256k1_pubkey *original_ptr = pubkeys[i];
@@ -6622,14 +6622,14 @@ static void run_eckey_edge_case_test(void) {
len = 33;
CHECK(secp256k1_ec_pubkey_serialize(CTX, ctmp, &len, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
CHECK(secp256k1_ec_pubkey_serialize(CTX, ctmp2, &len, &pubkey_one, SECP256K1_EC_COMPRESSED) == 1);
- CHECK(secp256k1_memcmp_var(ctmp, ctmp2, 33) == 0);
+ CHECK(memcmp(ctmp, ctmp2, 33) == 0);
/* Adds to two. */
pubkeys[1] = &pubkey_one;
memset(&pubkey, 255, sizeof(secp256k1_pubkey));
SECP256K1_CHECKMEM_UNDEFINE(&pubkey, sizeof(secp256k1_pubkey));
CHECK(secp256k1_ec_pubkey_combine(CTX, &pubkey, pubkeys, 2) == 1);
SECP256K1_CHECKMEM_CHECK(&pubkey, sizeof(secp256k1_pubkey));
- CHECK(secp256k1_memcmp_var(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
+ CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
}
static void run_eckey_negate_test(void) {
@@ -6641,16 +6641,16 @@ static void run_eckey_negate_test(void) {
/* Verify negation changes the key and changes it back */
CHECK(secp256k1_ec_seckey_negate(CTX, seckey) == 1);
- CHECK(secp256k1_memcmp_var(seckey, seckey_tmp, 32) != 0);
+ CHECK(memcmp(seckey, seckey_tmp, 32) != 0);
CHECK(secp256k1_ec_seckey_negate(CTX, seckey) == 1);
- CHECK(secp256k1_memcmp_var(seckey, seckey_tmp, 32) == 0);
+ CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
/* Negating all 0s fails */
memset(seckey, 0, 32);
memset(seckey_tmp, 0, 32);
CHECK(secp256k1_ec_seckey_negate(CTX, seckey) == 0);
/* Check that seckey is not modified */
- CHECK(secp256k1_memcmp_var(seckey, seckey_tmp, 32) == 0);
+ CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
/* Negating an overflowing seckey fails and the seckey is zeroed. In this
* test, the seckey has 16 random bytes to ensure that ec_seckey_negate
@@ -6659,7 +6659,7 @@ static void run_eckey_negate_test(void) {
memset(seckey, 0xFF, 16);
memset(seckey_tmp, 0, 32);
CHECK(secp256k1_ec_seckey_negate(CTX, seckey) == 0);
- CHECK(secp256k1_memcmp_var(seckey, seckey_tmp, 32) == 0);
+ CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
}
static void random_sign(secp256k1_scalar *sigr, secp256k1_scalar *sigs, const secp256k1_scalar *key, const secp256k1_scalar *msg, int *recid) {
@@ -6744,7 +6744,7 @@ static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char
static int is_empty_signature(const secp256k1_ecdsa_signature *sig) {
static const unsigned char res[sizeof(secp256k1_ecdsa_signature)] = {0};
- return secp256k1_memcmp_var(sig, res, sizeof(secp256k1_ecdsa_signature)) == 0;
+ return memcmp(sig, res, sizeof(secp256k1_ecdsa_signature)) == 0;
}
static void test_ecdsa_end_to_end(void) {
@@ -6784,14 +6784,14 @@ static void test_ecdsa_end_to_end(void) {
/* Verify negation changes the key and changes it back */
memcpy(&pubkey_tmp, &pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_negate(CTX, &pubkey_tmp) == 1);
- CHECK(secp256k1_memcmp_var(&pubkey_tmp, &pubkey, sizeof(pubkey)) != 0);
+ CHECK(memcmp(&pubkey_tmp, &pubkey, sizeof(pubkey)) != 0);
CHECK(secp256k1_ec_pubkey_negate(CTX, &pubkey_tmp) == 1);
- CHECK(secp256k1_memcmp_var(&pubkey_tmp, &pubkey, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey_tmp, &pubkey, sizeof(pubkey)) == 0);
/* Verify private key import and export. */
CHECK(ec_privkey_export_der(CTX, seckey, &seckeylen, privkey, testrand_bits(1) == 1));
CHECK(ec_privkey_import_der(CTX, privkey2, seckey, seckeylen) == 1);
- CHECK(secp256k1_memcmp_var(privkey, privkey2, 32) == 0);
+ CHECK(memcmp(privkey, privkey2, 32) == 0);
/* Optionally tweak the keys using addition. */
if (testrand_int(3) == 0) {
@@ -6807,7 +6807,7 @@ static void test_ecdsa_end_to_end(void) {
return;
}
CHECK(secp256k1_ec_pubkey_create(CTX, &pubkey2, privkey) == 1);
- CHECK(secp256k1_memcmp_var(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
}
/* Optionally tweak the keys using multiplication. */
@@ -6824,7 +6824,7 @@ static void test_ecdsa_end_to_end(void) {
return;
}
CHECK(secp256k1_ec_pubkey_create(CTX, &pubkey2, privkey) == 1);
- CHECK(secp256k1_memcmp_var(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
+ CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
}
/* Sign. */
@@ -6836,13 +6836,13 @@ static void test_ecdsa_end_to_end(void) {
extra[31] = 0;
extra[0] = 1;
CHECK(secp256k1_ecdsa_sign(CTX, &signature[3], message, privkey, NULL, extra) == 1);
- CHECK(secp256k1_memcmp_var(&signature[0], &signature[4], sizeof(signature[0])) == 0);
- CHECK(secp256k1_memcmp_var(&signature[0], &signature[1], sizeof(signature[0])) != 0);
- CHECK(secp256k1_memcmp_var(&signature[0], &signature[2], sizeof(signature[0])) != 0);
- CHECK(secp256k1_memcmp_var(&signature[0], &signature[3], sizeof(signature[0])) != 0);
- CHECK(secp256k1_memcmp_var(&signature[1], &signature[2], sizeof(signature[0])) != 0);
- CHECK(secp256k1_memcmp_var(&signature[1], &signature[3], sizeof(signature[0])) != 0);
- CHECK(secp256k1_memcmp_var(&signature[2], &signature[3], sizeof(signature[0])) != 0);
+ CHECK(memcmp(&signature[0], &signature[4], sizeof(signature[0])) == 0);
+ CHECK(memcmp(&signature[0], &signature[1], sizeof(signature[0])) != 0);
+ CHECK(memcmp(&signature[0], &signature[2], sizeof(signature[0])) != 0);
+ CHECK(memcmp(&signature[0], &signature[3], sizeof(signature[0])) != 0);
+ CHECK(memcmp(&signature[1], &signature[2], sizeof(signature[0])) != 0);
+ CHECK(memcmp(&signature[1], &signature[3], sizeof(signature[0])) != 0);
+ CHECK(memcmp(&signature[2], &signature[3], sizeof(signature[0])) != 0);
/* Verify. */
CHECK(secp256k1_ecdsa_verify(CTX, &signature[0], message, &pubkey) == 1);
CHECK(secp256k1_ecdsa_verify(CTX, &signature[1], message, &pubkey) == 1);
@@ -6863,7 +6863,7 @@ static void test_ecdsa_end_to_end(void) {
secp256k1_ecdsa_signature_save(&signature[5], &r, &s);
CHECK(!secp256k1_ecdsa_signature_normalize(CTX, NULL, &signature[5]));
CHECK(secp256k1_ecdsa_verify(CTX, &signature[5], message, &pubkey) == 1);
- CHECK(secp256k1_memcmp_var(&signature[5], &signature[0], 64) == 0);
+ CHECK(memcmp(&signature[5], &signature[0], 64) == 0);
/* Serialize/parse DER and verify again */
CHECK(secp256k1_ecdsa_signature_serialize_der(CTX, sig, &siglen, &signature[0]) == 1);
@@ -6877,7 +6877,7 @@ static void test_ecdsa_end_to_end(void) {
size_t siglen2 = siglen;
CHECK(secp256k1_ecdsa_signature_serialize_der(CTX, sig2, &siglen2, &signature[0]) == 1);
CHECK(siglen2 == siglen);
- CHECK(secp256k1_memcmp_var(sig2, sig, siglen) == 0);
+ CHECK(memcmp(sig2, sig, siglen) == 0);
siglen2 = siglen - 1;
CHECK(secp256k1_ecdsa_signature_serialize_der(CTX, sig2, &siglen2, &signature[0]) == 0);
CHECK(siglen2 == siglen);
@@ -6920,7 +6920,7 @@ static void test_random_pubkeys(void) {
} else {
secp256k1_ge_serialize65(&elem, out);
}
- CHECK(secp256k1_memcmp_var(&in[1], &out[1], len-1) == 0);
+ CHECK(memcmp(&in[1], &out[1], len-1) == 0);
/* ... except for the type of hybrid inputs. */
if ((in[0] != 6) && (in[0] != 7)) {
CHECK(in[0] == out[0]);
@@ -6942,7 +6942,7 @@ static void test_random_pubkeys(void) {
if (res) {
CHECK(secp256k1_ge_eq_var(&elem, &elem2));
secp256k1_ge_serialize65(&elem, out);
- CHECK(secp256k1_memcmp_var(&in[1], &out[1], 64) == 0);
+ CHECK(memcmp(&in[1], &out[1], 64) == 0);
}
}
}
@@ -7001,7 +7001,7 @@ static void test_sort_helper(secp256k1_pubkey *pk, size_t *pk_order, size_t n_pk
}
CHECK(secp256k1_ec_pubkey_sort(CTX, pk_test, n_pk) == 1);
for (i = 0; i < n_pk; i++) {
- CHECK(secp256k1_memcmp_var(pk_test[i], &pk[i], sizeof(*pk_test[i])) == 0);
+ CHECK(memcmp(pk_test[i], &pk[i], sizeof(*pk_test[i])) == 0);
}
}
@@ -7136,7 +7136,7 @@ static void test_sort_vectors(void) {
}
CHECK(secp256k1_ec_pubkey_sort(CTX, pks_ptr, N_PUBKEYS) == 1);
for (i = 0; i < N_PUBKEYS; i++) {
- CHECK(secp256k1_memcmp_var(pks_ptr[i], sorted[i], sizeof(secp256k1_pubkey)) == 0);
+ CHECK(memcmp(pks_ptr[i], sorted[i], sizeof(secp256k1_pubkey)) == 0);
}
}
@@ -7181,21 +7181,21 @@ static int test_ecdsa_der_parse(const unsigned char *sig, size_t siglen, int cer
parsed_der = secp256k1_ecdsa_signature_parse_der(CTX, &sig_der, sig, siglen);
if (parsed_der) {
ret |= (!secp256k1_ecdsa_signature_serialize_compact(CTX, compact_der, &sig_der)) << 0;
- valid_der = (secp256k1_memcmp_var(compact_der, zeroes, 32) != 0) && (secp256k1_memcmp_var(compact_der + 32, zeroes, 32) != 0);
+ valid_der = (memcmp(compact_der, zeroes, 32) != 0) && (memcmp(compact_der + 32, zeroes, 32) != 0);
}
if (valid_der) {
ret |= (!secp256k1_ecdsa_signature_serialize_der(CTX, roundtrip_der, &len_der, &sig_der)) << 1;
- roundtrips_der = (len_der == siglen) && secp256k1_memcmp_var(roundtrip_der, sig, siglen) == 0;
+ roundtrips_der = (len_der == siglen) && memcmp(roundtrip_der, sig, siglen) == 0;
}
parsed_der_lax = ecdsa_signature_parse_der_lax(CTX, &sig_der_lax, sig, siglen);
if (parsed_der_lax) {
ret |= (!secp256k1_ecdsa_signature_serialize_compact(CTX, compact_der_lax, &sig_der_lax)) << 10;
- valid_der_lax = (secp256k1_memcmp_var(compact_der_lax, zeroes, 32) != 0) && (secp256k1_memcmp_var(compact_der_lax + 32, zeroes, 32) != 0);
+ valid_der_lax = (memcmp(compact_der_lax, zeroes, 32) != 0) && (memcmp(compact_der_lax + 32, zeroes, 32) != 0);
}
if (valid_der_lax) {
ret |= (!secp256k1_ecdsa_signature_serialize_der(CTX, roundtrip_der_lax, &len_der_lax, &sig_der_lax)) << 11;
- roundtrips_der_lax = (len_der_lax == siglen) && secp256k1_memcmp_var(roundtrip_der_lax, sig, siglen) == 0;
+ roundtrips_der_lax = (len_der_lax == siglen) && memcmp(roundtrip_der_lax, sig, siglen) == 0;
}
if (certainly_der) {
@@ -7211,7 +7211,7 @@ static int test_ecdsa_der_parse(const unsigned char *sig, size_t siglen, int cer
if (valid_der) {
ret |= (!roundtrips_der_lax) << 12;
ret |= (len_der != len_der_lax) << 13;
- ret |= ((len_der != len_der_lax) || (secp256k1_memcmp_var(roundtrip_der_lax, roundtrip_der, len_der) != 0)) << 14;
+ ret |= ((len_der != len_der_lax) || (memcmp(roundtrip_der_lax, roundtrip_der, len_der) != 0)) << 14;
}
ret |= (roundtrips_der != roundtrips_der_lax) << 15;
if (parsed_der) {
@@ -7478,7 +7478,7 @@ static void der_long_form_check(const secp256k1_ecdsa_signature *sig) {
static const unsigned char zeroes[62] = {0};
unsigned char compact[64];
CHECK(secp256k1_ecdsa_signature_serialize_compact(CTX, compact, sig) == 1);
- CHECK(secp256k1_memcmp_var(compact, zeroes, 62) == 0);
+ CHECK(memcmp(compact, zeroes, 62) == 0);
CHECK(compact[62] == 0x01);
CHECK(compact[63] == 0x23);
}
@@ -7842,11 +7842,11 @@ static void run_ecdsa_edge_cases(void) {
CHECK(!is_empty_signature(&sig));
CHECK(secp256k1_ecdsa_sign(CTX, &sig2, msg, key, nonce_function_rfc6979, extra) == 1);
CHECK(!is_empty_signature(&sig2));
- CHECK(secp256k1_memcmp_var(&sig, &sig2, sizeof(sig)) == 0);
+ CHECK(memcmp(&sig, &sig2, sizeof(sig)) == 0);
/* The default nonce function is deterministic. */
CHECK(secp256k1_ecdsa_sign(CTX, &sig2, msg, key, NULL, extra) == 1);
CHECK(!is_empty_signature(&sig2));
- CHECK(secp256k1_memcmp_var(&sig, &sig2, sizeof(sig)) == 0);
+ CHECK(memcmp(&sig, &sig2, sizeof(sig)) == 0);
/* The default nonce function changes output with different messages. */
for(i = 0; i < 256; i++) {
int j;
@@ -7893,12 +7893,12 @@ static void run_ecdsa_edge_cases(void) {
SECP256K1_CHECKMEM_CHECK(nonce3,32);
CHECK(nonce_function_rfc6979(nonce4, zeros, zeros, zeros, (void *)zeros, 0) == 1);
SECP256K1_CHECKMEM_CHECK(nonce4,32);
- CHECK(secp256k1_memcmp_var(nonce, nonce2, 32) != 0);
- CHECK(secp256k1_memcmp_var(nonce, nonce3, 32) != 0);
- CHECK(secp256k1_memcmp_var(nonce, nonce4, 32) != 0);
- CHECK(secp256k1_memcmp_var(nonce2, nonce3, 32) != 0);
- CHECK(secp256k1_memcmp_var(nonce2, nonce4, 32) != 0);
- CHECK(secp256k1_memcmp_var(nonce3, nonce4, 32) != 0);
+ CHECK(memcmp(nonce, nonce2, 32) != 0);
+ CHECK(memcmp(nonce, nonce3, 32) != 0);
+ CHECK(memcmp(nonce, nonce4, 32) != 0);
+ CHECK(memcmp(nonce2, nonce3, 32) != 0);
+ CHECK(memcmp(nonce2, nonce4, 32) != 0);
+ CHECK(memcmp(nonce3, nonce4, 32) != 0);
}
@@ -7929,7 +7929,7 @@ static void ecdsa_ctx_sha256(void) {
CHECK(secp256k1_ecdsa_sign(ctx, &out_custom, msg32, sk, NULL, NULL));
CHECK(sha256_ecdsa_called);
/* Outputs must differ if custom compression was used */
- CHECK(secp256k1_memcmp_var(out_default.data, out_custom.data, 64) != 0);
+ CHECK(memcmp(out_default.data, out_custom.data, 64) != 0);
secp256k1_context_destroy(ctx);
}
@@ -8008,12 +8008,12 @@ static void run_secp256k1_memczero_test(void) {
/* secp256k1_memczero(..., ..., 0) is a noop. */
memcpy(buf2, buf1, sizeof(buf1));
secp256k1_memczero(buf1, sizeof(buf1), 0);
- CHECK(secp256k1_memcmp_var(buf1, buf2, sizeof(buf1)) == 0);
+ CHECK(memcmp(buf1, buf2, sizeof(buf1)) == 0);
/* secp256k1_memczero(..., ..., 1) zeros the buffer. */
memset(buf2, 0, sizeof(buf2));
secp256k1_memczero(buf1, sizeof(buf1) , 1);
- CHECK(secp256k1_memcmp_var(buf1, buf2, sizeof(buf1)) == 0);
+ CHECK(memcmp(buf1, buf2, sizeof(buf1)) == 0);
}
@@ -8036,7 +8036,7 @@ static void run_secp256k1_byteorder_tests(void) {
uint32_t x_;
secp256k1_write_be32(buf, x);
- CHECK(secp256k1_memcmp_var(buf, x_be, sizeof(buf)) == 0);
+ CHECK(memcmp(buf, x_be, sizeof(buf)) == 0);
x_ = secp256k1_read_be32(buf);
CHECK(x == x_);
@@ -8049,7 +8049,7 @@ static void run_secp256k1_byteorder_tests(void) {
uint64_t x_;
secp256k1_write_be64(buf, x);
- CHECK(secp256k1_memcmp_var(buf, x_be, sizeof(buf)) == 0);
+ CHECK(memcmp(buf, x_be, sizeof(buf)) == 0);
x_ = secp256k1_read_be64(buf);
CHECK(x == x_);
@@ -8122,23 +8122,23 @@ static void fe_storage_cmov_test(void) {
secp256k1_fe_storage a = zero;
secp256k1_fe_storage_cmov(&r, &a, 0);
- CHECK(secp256k1_memcmp_var(&r, &max, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &max, sizeof(r)) == 0);
r = zero; a = max;
secp256k1_fe_storage_cmov(&r, &a, 1);
- CHECK(secp256k1_memcmp_var(&r, &max, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &max, sizeof(r)) == 0);
a = zero;
secp256k1_fe_storage_cmov(&r, &a, 1);
- CHECK(secp256k1_memcmp_var(&r, &zero, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &zero, sizeof(r)) == 0);
a = one;
secp256k1_fe_storage_cmov(&r, &a, 1);
- CHECK(secp256k1_memcmp_var(&r, &one, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &one, sizeof(r)) == 0);
r = one; a = zero;
secp256k1_fe_storage_cmov(&r, &a, 0);
- CHECK(secp256k1_memcmp_var(&r, &one, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &one, sizeof(r)) == 0);
}
static void scalar_cmov_test(void) {
@@ -8150,23 +8150,23 @@ static void scalar_cmov_test(void) {
secp256k1_scalar a = secp256k1_scalar_zero;
secp256k1_scalar_cmov(&r, &a, 0);
- CHECK(secp256k1_memcmp_var(&r, &max, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &max, sizeof(r)) == 0);
r = secp256k1_scalar_zero; a = max;
secp256k1_scalar_cmov(&r, &a, 1);
- CHECK(secp256k1_memcmp_var(&r, &max, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &max, sizeof(r)) == 0);
a = secp256k1_scalar_zero;
secp256k1_scalar_cmov(&r, &a, 1);
- CHECK(secp256k1_memcmp_var(&r, &secp256k1_scalar_zero, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &secp256k1_scalar_zero, sizeof(r)) == 0);
a = secp256k1_scalar_one;
secp256k1_scalar_cmov(&r, &a, 1);
- CHECK(secp256k1_memcmp_var(&r, &secp256k1_scalar_one, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &secp256k1_scalar_one, sizeof(r)) == 0);
r = secp256k1_scalar_one; a = secp256k1_scalar_zero;
secp256k1_scalar_cmov(&r, &a, 0);
- CHECK(secp256k1_memcmp_var(&r, &secp256k1_scalar_one, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &secp256k1_scalar_one, sizeof(r)) == 0);
}
static void ge_storage_cmov_test(void) {
@@ -8182,23 +8182,23 @@ static void ge_storage_cmov_test(void) {
secp256k1_ge_storage a = zero;
secp256k1_ge_storage_cmov(&r, &a, 0);
- CHECK(secp256k1_memcmp_var(&r, &max, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &max, sizeof(r)) == 0);
r = zero; a = max;
secp256k1_ge_storage_cmov(&r, &a, 1);
- CHECK(secp256k1_memcmp_var(&r, &max, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &max, sizeof(r)) == 0);
a = zero;
secp256k1_ge_storage_cmov(&r, &a, 1);
- CHECK(secp256k1_memcmp_var(&r, &zero, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &zero, sizeof(r)) == 0);
a = one;
secp256k1_ge_storage_cmov(&r, &a, 1);
- CHECK(secp256k1_memcmp_var(&r, &one, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &one, sizeof(r)) == 0);
r = one; a = zero;
secp256k1_ge_storage_cmov(&r, &a, 0);
- CHECK(secp256k1_memcmp_var(&r, &one, sizeof(r)) == 0);
+ CHECK(memcmp(&r, &one, sizeof(r)) == 0);
}
static void run_cmov_tests(void) {
### src/util.h
@@ -275,24 +275,6 @@ static SECP256K1_INLINE void secp256k1_memclear_explicit(void *ptr, size_t len)
#endif
}
-/** Semantics like memcmp. Variable-time.
- *
- * We use this to avoid possible compiler bugs with memcmp, e.g.
- * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189
- */
-static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n) {
- const unsigned char *p1 = s1, *p2 = s2;
- size_t i;
-
- for (i = 0; i < n; i++) {
- int diff = p1[i] - p2[i];
- if (diff != 0) {
- return diff;
- }
- }
- return 0;
-}
-
/* Return 1 if all elements of array s are 0 and otherwise return 0.
* Constant-time. */
static SECP256K1_INLINE int secp256k1_is_zero_array(const unsigned char *s, size_t len) {Why this scored 47/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.