What changed, and why it matters
This commit adds new, unused helper code for BIP-322 generic Bitcoin message signing. It only implements low-level building blocks (message hashing and a virtual transaction serializer) and includes unit tests that check the results against official BIP-322 test vectors. The code is not wired into any user-facing signing flow, so it cannot by itself change app behavior or expose user funds.
No security action required. Treat as normal feature development. When these primitives are later integrated into a signing flow, review that integration for proper user confirmation, script validation, and signature handling.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change introduces src/common/bip322.c/h with three primitives: bip322_message_hash_init (BIP-340 tagged hash init), bip322_serialize_to_spend (serializes the BIP-322 to_spend virtual transaction), and bip322_compute_to_spend_txid (double-SHA256 of that serialization). It also adds unit tests verifying the message hash, to_spend serialization, and txid against BIP-322 reference vectors for empty and “Hello World” messages with a P2WPKH challenge script. The serializer rejects challenge scripts longer than MAX_PREVOUT_SCRIPTPUBKEY_LEN and uses a compile-time static assert to ensure the length fits in a single-byte compact size. The commit message explicitly states these primitives are “Not used yet.”
Changed components
src/common/bip322.csrc/common/bip322.hunit-tests/CMakeLists.txtunit-tests/test_bip322.cInspect captured patch +306 / −0
### src/common/bip322.c
@@ -0,0 +1,89 @@
+/*****************************************************************************
+ * Ledger App Bitcoin.
+ * (c) 2026 Ledger SAS.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *****************************************************************************/
+
+#include <string.h>
+
+#include "bip322.h"
+
+/* SDK headers */
+#include "write.h"
+
+/* Local headers */
+#include "crypto.h"
+
+// The BIP-340 tag used for the message hash, as defined by BIP-322.
+static const uint8_t BIP0322_MSG_TAG[] = {'B', 'I', 'P', '0', '3', '2', '2', '-', 's', 'i', 'g',
+ 'n', 'e', 'd', '-', 'm', 'e', 's', 's', 'a', 'g', 'e'};
+
+void bip322_message_hash_init(cx_sha256_t *hash_context) {
+ crypto_tr_tagged_hash_init(hash_context, BIP0322_MSG_TAG, sizeof(BIP0322_MSG_TAG));
+}
+
+int bip322_serialize_to_spend(const uint8_t message_hash[static 32],
+ const uint8_t *challenge_script,
+ size_t challenge_script_len,
+ uint8_t out[static BIP322_TO_SPEND_MAX_LEN]) {
+ if (challenge_script_len > MAX_PREVOUT_SCRIPTPUBKEY_LEN) {
+ return -1;
+ }
+
+ size_t offset = 0;
+ write_u32_le(out, offset, 0); // nVersion = 0
+ offset += 4;
+ out[offset++] = 0x01; // 1 input
+ memset(out + offset, 0, 32); // prevout hash = null
+ offset += 32;
+ write_u32_le(out, offset, 0xFFFFFFFF); // prevout index
+ offset += 4;
+ out[offset++] = 34; // scriptSig length
+ out[offset++] = 0x00; // OP_0
+ out[offset++] = 0x20; // push of 32 bytes
+ memcpy(out + offset, message_hash, 32);
+ offset += 32;
+ write_u32_le(out, offset, 0); // nSequence = 0
+ offset += 4;
+ out[offset++] = 0x01; // 1 output
+ memset(out + offset, 0, 8); // value = 0
+ offset += 8;
+ // the compact size of the script length is a single byte for lengths below 0xFD
+ _Static_assert(MAX_PREVOUT_SCRIPTPUBKEY_LEN < 0xFD,
+ "the challenge script length must be serializable as a single byte");
+ out[offset++] = (uint8_t) challenge_script_len;
+ memcpy(out + offset, challenge_script, challenge_script_len);
+ offset += challenge_script_len;
+ write_u32_le(out, offset, 0); // nLockTime = 0
+ offset += 4;
+
+ return (int) offset;
+}
+
+int bip322_compute_to_spend_txid(const uint8_t message_hash[static 32],
+ const uint8_t *challenge_script,
+ size_t challenge_script_len,
+ uint8_t out_txid[static 32]) {
+ uint8_t to_spend[BIP322_TO_SPEND_MAX_LEN];
+
+ int len =
+ bip322_serialize_to_spend(message_hash, challenge_script, challenge_script_len, to_spend);
+ if (len < 0) {
+ return -1;
+ }
+
+ cx_hash_sha256(to_spend, len, out_txid, 32);
+ cx_hash_sha256(out_txid, 32, out_txid, 32);
+ return 0;
+}
### src/common/bip322.h
@@ -0,0 +1,71 @@
+/*****************************************************************************
+ * Ledger App Bitcoin.
+ * (c) 2026 Ledger SAS.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *****************************************************************************/
+
+#pragma once
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "cx.h"
+
+#include "constants.h"
+
+// BIP-322 generic signed messages: the parts of the specification that do not depend on how a
+// signing request reaches the app.
+//
+// A BIP-322 signature for a message and a message_challenge (the scriptPubKey whose control is
+// proven) is a signature of the "to_sign" virtual transaction, whose first input spends output 0
+// of the "to_spend" virtual transaction. to_spend commits to the message hash and to the
+// message_challenge; its own input references the null outpoint, so neither transaction can
+// ever be broadcast.
+
+/**
+ * Initializes hash_context for the BIP-322 message hash: the BIP-340 tagged hash of the message
+ * with the tag "BIP0322-signed-message". The message is then added with crypto_hash_update(),
+ * and the hash obtained with crypto_hash_digest().
+ */
+void bip322_message_hash_init(cx_sha256_t *hash_context);
+
+// Maximum length of the serialization of a BIP-322 to_spend transaction:
+// 4 (version) + 1 (input count) + 32 (prevout hash) + 4 (prevout index) + 1 (scriptSig length)
+// + 34 (scriptSig) + 4 (sequence) + 1 (output count) + 8 (value) + 1 (script length)
+// + MAX_PREVOUT_SCRIPTPUBKEY_LEN (challenge script) + 4 (locktime)
+#define BIP322_TO_SPEND_MAX_LEN (94 + MAX_PREVOUT_SCRIPTPUBKEY_LEN)
+
+/**
+ * Serializes the BIP-322 to_spend transaction for the given message hash and challenge
+ * scriptPubKey into out (which must be at least BIP322_TO_SPEND_MAX_LEN bytes long).
+ *
+ * Returns the length of the serialization, or -1 if challenge_script_len is larger than
+ * MAX_PREVOUT_SCRIPTPUBKEY_LEN.
+ */
+int bip322_serialize_to_spend(const uint8_t message_hash[static 32],
+ const uint8_t *challenge_script,
+ size_t challenge_script_len,
+ uint8_t out[static BIP322_TO_SPEND_MAX_LEN]);
+
+/**
+ * Computes the txid (in the byte order used inside transaction serializations, matching
+ * PSBT_IN_PREVIOUS_TXID) of the BIP-322 to_spend transaction for the given message hash and
+ * challenge scriptPubKey.
+ *
+ * Returns 0 on success, -1 on failure.
+ */
+int bip322_compute_to_spend_txid(const uint8_t message_hash[static 32],
+ const uint8_t *challenge_script,
+ size_t challenge_script_len,
+ uint8_t out_txid[static 32]);
### unit-tests/CMakeLists.txt
@@ -332,6 +332,13 @@ if(SPECULOS AND SPECULOS_SRC)
target_link_libraries(test_script PRIVATE cmocka app_crypto)
add_test(test_script test_script)
+ # test_bip322 exercises the BIP-322 primitives of src/common/bip322.c (message hash, to_spend
+ # serialization and txid) against the vectors of the BIP.
+ add_executable(test_bip322 test_bip322.c ../src/common/bip322.c)
+ app_apply_real_sdk_config(test_bip322)
+ target_link_libraries(test_bip322 PRIVATE cmocka app_crypto write)
+ add_test(test_bip322 test_bip322)
+
# test_sighash exercises classify_sighash (src/common/sighash.h), which only
# depends on constants.h (and therefore the real SDK headers). No app object
# code or syscalls are needed, so it just links cmocka.
### unit-tests/test_bip322.c
@@ -0,0 +1,139 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include "common/bip322.h"
+#include "crypto.h"
+
+// ---------------------------------------------------------------------------
+// Test vectors
+// ---------------------------------------------------------------------------
+
+// scriptPubKey of bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l, the address used by the
+// test vectors of BIP-322
+static const uint8_t CHALLENGE_P2WPKH[] = {0x00, 0x14, 0x2b, 0x05, 0xd5, 0x64, 0xe6, 0xa7,
+ 0xa3, 0x3c, 0x08, 0x7f, 0x16, 0xe0, 0xf7, 0x30,
+ 0xd1, 0x44, 0x01, 0x23, 0x79, 0x9d};
+
+// BIP0322-signed-message tagged hash of ""
+static const uint8_t MSG_HASH_EMPTY[32] = {
+ 0xc9, 0x0c, 0x26, 0x9c, 0x4f, 0x8f, 0xcb, 0xe6, 0x88, 0x0f, 0x72, 0xa7, 0x21, 0xdd, 0xfb, 0xf1,
+ 0x91, 0x42, 0x68, 0xa7, 0x94, 0xcb, 0xb2, 0x1c, 0xfa, 0xfe, 0xe1, 0x37, 0x70, 0xae, 0x19, 0xf1};
+
+// BIP0322-signed-message tagged hash of "Hello World"
+static const uint8_t MSG_HASH_HELLO_WORLD[32] = {
+ 0xf0, 0xeb, 0x03, 0xb1, 0xa7, 0x5a, 0xc6, 0xd9, 0x84, 0x7f, 0x55, 0xc6, 0x24, 0xa9, 0x91, 0x69,
+ 0xb5, 0xdc, 0xcb, 0xa2, 0xa3, 0x1f, 0x5b, 0x23, 0xbe, 0xa7, 0x7b, 0xa2, 0x70, 0xde, 0x0a, 0x7a};
+
+// Expected serialization of the to_spend transaction for "Hello World" and CHALLENGE_P2WPKH.
+// The to_spend transactions (and their txids below) for "" and "Hello World" are given in the
+// test vectors of BIP-322.
+static const uint8_t TO_SPEND_HELLO_WORLD[] = {
+ // nVersion = 0
+ 0x00, 0x00, 0x00, 0x00,
+ // 1 input: null prevout hash, index 0xffffffff
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0xff, 0xff, 0xff, 0xff,
+ // scriptSig: OP_0 PUSH32(message_hash)
+ 0x22, 0x00, 0x20, 0xf0, 0xeb, 0x03, 0xb1, 0xa7, 0x5a, 0xc6, 0xd9, 0x84, 0x7f, 0x55, 0xc6, 0x24,
+ 0xa9, 0x91, 0x69, 0xb5, 0xdc, 0xcb, 0xa2, 0xa3, 0x1f, 0x5b, 0x23, 0xbe, 0xa7, 0x7b, 0xa2, 0x70,
+ 0xde, 0x0a, 0x7a,
+ // nSequence = 0
+ 0x00, 0x00, 0x00, 0x00,
+ // 1 output: value 0, the challenge scriptPubKey
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x14, 0x2b, 0x05, 0xd5, 0x64,
+ 0xe6, 0xa7, 0xa3, 0x3c, 0x08, 0x7f, 0x16, 0xe0, 0xf7, 0x30, 0xd1, 0x44, 0x01, 0x23, 0x79, 0x9d,
+ // nLockTime = 0
+ 0x00, 0x00, 0x00, 0x00};
+
+// txid of to_spend for "" (in the byte order used inside transactions, i.e. the reverse of the
+// displayed c5680aa69bb8d860bf82d4e9cd3504b55dde018de765a91bb566283c545a99a7)
+static const uint8_t TO_SPEND_TXID_EMPTY[32] = {
+ 0xa7, 0x99, 0x5a, 0x54, 0x3c, 0x28, 0x66, 0xb5, 0x1b, 0xa9, 0x65, 0xe7, 0x8d, 0x01, 0xde, 0x5d,
+ 0xb5, 0x04, 0x35, 0xcd, 0xe9, 0xd4, 0x82, 0xbf, 0x60, 0xd8, 0xb8, 0x9b, 0xa6, 0x0a, 0x68, 0xc5};
+
+// txid of to_spend for "Hello World" (reverse of the displayed
+// b79d196740ad5217771c1098fc4a4b51e0535c32236c71f1ea4d61a2d603352b)
+static const uint8_t TO_SPEND_TXID_HELLO_WORLD[32] = {
+ 0x2b, 0x35, 0x03, 0xd6, 0xa2, 0x61, 0x4d, 0xea, 0xf1, 0x71, 0x6c, 0x23, 0x32, 0x5c, 0x53, 0xe0,
+ 0x51, 0x4b, 0x4a, 0xfc, 0x98, 0x10, 0x1c, 0x77, 0x17, 0x52, 0xad, 0x40, 0x67, 0x19, 0x9d, 0xb7};
+
+// Computes the BIP-322 message hash of a C string.
+static void message_hash(const char *message, uint8_t out[static 32]) {
+ cx_sha256_t hash_context;
+ bip322_message_hash_init(&hash_context);
+ crypto_hash_update(&hash_context.header, message, strlen(message));
+ crypto_hash_digest(&hash_context.header, out, 32);
+}
+
+static void test_bip322_message_hash(void **state) {
+ (void) state;
+
+ uint8_t hash[32];
+
+ message_hash("", hash);
+ assert_memory_equal(hash, MSG_HASH_EMPTY, 32);
+
+ message_hash("Hello World", hash);
+ assert_memory_equal(hash, MSG_HASH_HELLO_WORLD, 32);
+}
+
+static void test_bip322_serialize_to_spend(void **state) {
+ (void) state;
+
+ uint8_t out[BIP322_TO_SPEND_MAX_LEN];
+ int len = bip322_serialize_to_spend(MSG_HASH_HELLO_WORLD,
+ CHALLENGE_P2WPKH,
+ sizeof(CHALLENGE_P2WPKH),
+ out);
+
+ assert_int_equal(len, sizeof(TO_SPEND_HELLO_WORLD));
+ assert_memory_equal(out, TO_SPEND_HELLO_WORLD, sizeof(TO_SPEND_HELLO_WORLD));
+}
+
+static void test_bip322_serialize_to_spend_rejects_long_script(void **state) {
+ (void) state;
+
+ uint8_t long_script[MAX_PREVOUT_SCRIPTPUBKEY_LEN + 1] = {0};
+ uint8_t out[BIP322_TO_SPEND_MAX_LEN];
+ assert_int_equal(
+ bip322_serialize_to_spend(MSG_HASH_HELLO_WORLD, long_script, sizeof(long_script), out),
+ -1);
+}
+
+static void test_bip322_compute_to_spend_txid(void **state) {
+ (void) state;
+
+ uint8_t txid[32];
+
+ assert_int_equal(bip322_compute_to_spend_txid(MSG_HASH_EMPTY,
+ CHALLENGE_P2WPKH,
+ sizeof(CHALLENGE_P2WPKH),
+ txid),
+ 0);
+ assert_memory_equal(txid, TO_SPEND_TXID_EMPTY, 32);
+
+ assert_int_equal(bip322_compute_to_spend_txid(MSG_HASH_HELLO_WORLD,
+ CHALLENGE_P2WPKH,
+ sizeof(CHALLENGE_P2WPKH),
+ txid),
+ 0);
+ assert_memory_equal(txid, TO_SPEND_TXID_HELLO_WORLD, 32);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_bip322_message_hash),
+ cmocka_unit_test(test_bip322_serialize_to_spend),
+ cmocka_unit_test(test_bip322_serialize_to_spend_rejects_long_script),
+ cmocka_unit_test(test_bip322_compute_to_spend_txid),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.