crypto_tr_lift_x: reject x-coordinates not smaller than p
What changed, and why it matters
This commit tightens a cryptographic check in Ledger's Bitcoin app. A function that converts an x-coordinate into a point on the secp256k1 curve now rejects any x value that is equal to or larger than the curve's prime field size. Before, such values could be silently reduced modulo the prime, potentially producing a valid point that BIP-327 says must be rejected. The change improves standards conformance and removes a subtle way to feed the function an out-of-range input.
Treat as a low-to-moderate security hardening patch. Review any code paths that call crypto_tr_lift_x to ensure returned -1 is handled correctly and does not cause unexpected state. Include this commit in the next firmware/app release because it closes a BIP-327 non-conformance issue that could affect MuSig2 key aggregation behavior.
Security signals we found
BIP-327 conformance fix for lift_x input validation
Rejection of x-coordinates >= secp256k1 field prime
Prevents silent modular reduction of invalid x inputs
Adds unit test for x == p and x == p + 1 cases
Evidence from the diff
crypto_tr_lift_x in src/crypto.c now returns -1 if the supplied 32-byte x is >= secp256k1_p (the field prime). Previously it only checked whether the resulting y^2 had a square root (i.e., whether x maps to a curve point). Because downstream field arithmetic is performed modulo p, an input of p+1 would behave like x=1, which is on the curve. BIP-327 (MuSig) explicitly requires rejecting x-coordinates not smaller than p, so this patch adds the range check before any computation. Header comments and unit tests are updated accordingly.
Changed components
src/crypto.c: crypto_tr_lift_xsrc/crypto.h: crypto_tr_lift_x documentationunit-tests/test_crypto.c: new test_crypto_tr_lift_x_not_in_field testInspect captured patch +33 / −2
### src/crypto.c
@@ -481,6 +481,11 @@ void crypto_tr_tapleaf_hash_init(cx_sha256_t *hash_context) {
}
int crypto_tr_lift_x(const uint8_t x[static 32], uint8_t out[static 65]) {
+ // fail if x >= p
+ if (memcmp(x, secp256k1_p, 32) >= 0) {
+ return -1;
+ }
+
// save memory by reusing output buffer for intermediate results
uint8_t *y = out + 1 + 32;
// we use the memory for the x-coordinate of the output as a temporary variable
### src/crypto.h
@@ -382,8 +382,9 @@ void crypto_tr_tagged_hash_init(cx_sha256_t *hash_context, const uint8_t *tag, u
* @param[out] out
* Pointer to an array that will received the output as an uncompressed 65-bytes pubkey.
*
- * @return 0 on success, -1 if x is not the x-coordinate of a point on the curve. On failure, the
- * content of `out` is unspecified and must not be used.
+ * @return 0 on success, -1 if x is not smaller than the field size, or if it is not the
+ * x-coordinate of a point on the curve. On failure, the content of `out` is unspecified and must
+ * not be used.
*/
int crypto_tr_lift_x(const uint8_t x[static 32], uint8_t out[static 65]);
### unit-tests/test_crypto.c
@@ -763,6 +763,30 @@ static void test_crypto_tr_lift_x_not_on_curve(void **state) {
(void) x_invalid;
}
+static void test_crypto_tr_lift_x_not_in_field(void **state) {
+ (void) state;
+ /* x must be strictly smaller than the field size p. x = p + 1 is
+ * congruent to 1, which is the x-coordinate of a point on the curve
+ * (1^3 + 7 = 8 is a quadratic residue mod p), so this checks that the
+ * input is not silently reduced modulo p. */
+ static const uint8_t p[32] = {
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2f,
+ };
+ uint8_t x[32], out[65];
+
+ uint8_t one[32] = {0};
+ one[31] = 1;
+ assert_int_equal(crypto_tr_lift_x(one, out), 0); // x = 1 is valid
+
+ memcpy(x, p, 32); // x == p
+ assert_int_equal(crypto_tr_lift_x(x, out), -1);
+
+ x[31] = 0x30; // x == p + 1
+ assert_int_equal(crypto_tr_lift_x(x, out), -1);
+}
+
static void test_crypto_tr_tagged_hash_single_data(void **state) {
(void) state;
/* tagged_hash("TapTweak", G_x) */
@@ -983,6 +1007,7 @@ int main(void) {
cmocka_unit_test(test_crypto_tr_tapleaf_hash_init),
cmocka_unit_test(test_crypto_tr_lift_x_secp256k1_generator),
cmocka_unit_test(test_crypto_tr_lift_x_not_on_curve),
+ cmocka_unit_test(test_crypto_tr_lift_x_not_in_field),
cmocka_unit_test(test_crypto_tr_tagged_hash_single_data),
cmocka_unit_test(test_crypto_tr_tagged_hash_two_parts),
cmocka_unit_test(test_crypto_tr_combine_taptree_hashes_sorted),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.