zpay32: assert why each fixed BOLT 11 vector is rejected
What changed, and why it matters
This commit only improves and expands test code for the BOLT 11 invoice decoder. It makes existing tests verify the exact reason each invalid test invoice is rejected, and adds a new test specifically for high-S signature rejection. No production code is changed, so it cannot introduce or fix a runtime security vulnerability on its own.
No security action needed; this is a test-quality improvement. Review the related decoder rules in the same PR for actual security relevance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change is purely in the zpay32 test suite. It maps fixed BOLT 11 test vectors to their expected sentinel errors, adds a duplicate payment-address test case, and adds TestDecodeHighSWithDestination to exercise the ErrHighSSignature path with a single s field. The production decoder logic is untouched.
Changed components
zpay32/bolt11_vectors_test.gozpay32/invoice_internal_test.gozpay32/invoice_test.goInspect captured patch +70 / −2
### zpay32/bolt11_vectors_test.go
@@ -32,6 +32,20 @@ var paymentLayerVectors = map[string]string{
"Missing required `s` field": "payment address checked by the router",
}
+// expectedVectorErrors maps invalid vectors to the error Decode must return
+// for them, so a vector rejected for the wrong reason fails the test.
+var expectedVectorErrors = map[string]error{
+ "Same, but with an `n` field of invalid length": ErrInvalidFixedFieldLength, //nolint:ll
+ "Two distinct p fields in an otherwise valid invoice": ErrDuplicatePaymentHash, //nolint:ll
+ "The same p field twice in an otherwise valid invoice": ErrDuplicatePaymentHash, //nolint:ll
+ "Two distinct s fields in an otherwise valid invoice": ErrDuplicatePaymentAddr, //nolint:ll
+
+ // The spec's high-S vector also repeats its `s` field, so it's
+ // rejected as a duplicate before the signature is checked. The low-S
+ // check itself is covered by TestDecodeHighSWithDestination.
+ "Non canonical signature (high-S) with 'n' field defined": ErrDuplicatePaymentAddr, //nolint:ll
+}
+
// TestBOLT11Vectors runs every invoice test vector from the spec through
// Decode. Unknown required feature bits are rejected through the
// WithErrorOnUnknownFeatureBit option, as by default they're left for
@@ -70,8 +84,14 @@ func TestBOLT11Vectors(t *testing.T) {
if v.Valid {
require.NoError(t, err)
- } else {
- require.Error(t, err)
+
+ return
+ }
+
+ require.Error(t, err)
+ wantErr, ok := expectedVectorErrors[v.Description]
+ if ok {
+ require.ErrorIs(t, err, wantErr)
}
})
}
### zpay32/invoice_internal_test.go
@@ -801,6 +801,14 @@ func TestParseTaggedFields(t *testing.T) {
&distinctPaymentHashes, fieldTypeP, secondPaymentHash,
))
+ var distinctPaymentAddrs bytes.Buffer
+ require.NoError(t, writeBytes32(
+ &distinctPaymentAddrs, fieldTypeS, testPaymentHash,
+ ))
+ require.NoError(t, writeBytes32(
+ &distinctPaymentAddrs, fieldTypeS, secondPaymentHash,
+ ))
+
tests := []struct {
name string
data []byte
@@ -842,6 +850,11 @@ func TestParseTaggedFields(t *testing.T) {
data: distinctPaymentHashes.Bytes(),
wantErr: ErrDuplicatePaymentHash,
},
+ {
+ name: "distinct payment addresses",
+ data: distinctPaymentAddrs.Bytes(),
+ wantErr: ErrDuplicatePaymentAddr,
+ },
{
name: "only type specified",
data: []byte{0x0d},
### zpay32/invoice_test.go
@@ -1027,6 +1027,41 @@ func TestDecodeDuplicatePaymentHashes(t *testing.T) {
}
}
+// TestDecodeHighSWithDestination checks that Decode rejects a high-S
+// signature when the invoice carries an explicit `n` field, and accepts the
+// low-S form of the same signature. The two invoices differ only in the
+// signature, so the rejection can only come from the low-S check.
+func TestDecodeHighSWithDestination(t *testing.T) {
+ t.Parallel()
+
+ highS := "lnbc25m1p70xwfzpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyq" +
+ "cyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6t" +
+ "wvus8g6rfwvs8qun0dfjkxaqnp4q0n326hr8v9zprg8gsvezcch06gfa" +
+ "qqhde2aj730yg0durunfhv66sp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3z" +
+ "yg3zyg3zyg3zyg3zyg3zygs9qrsgq4dnj26gykrcyg6rw2pyk7sshrg8" +
+ "890l9q0vxvvnq7c6q7n7fhn4uhw89rghjljt2a4q0he7ka3cefq84r79" +
+ "w4n2cthhrhtyzqtc99qcpl325ac"
+ lowS := "lnbc25m1p70xwfzpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyq" +
+ "cyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6t" +
+ "wvus8g6rfwvs8qun0dfjkxaqnp4q0n326hr8v9zprg8gsvezcch06gfa" +
+ "qqhde2aj730yg0durunfhv66sp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3z" +
+ "yg3zyg3zyg3zyg3zyg3zygs9qrsgq4dnj26gykrcyg6rw2pyk7sshrg8" +
+ "890l9q0vxvvnq7c6q7n7fhn4ng3c6uhgdqd54z2lsgxpfzw8x5wdeh4d" +
+ "ug768mhgedvs2e4z7a0sq6zvx67"
+
+ _, err := Decode(highS, &chaincfg.MainNetParams)
+ require.ErrorIs(t, err, ErrHighSSignature)
+
+ invoice, err := Decode(lowS, &chaincfg.MainNetParams)
+ require.NoError(t, err)
+ require.True(t, invoice.PaymentAddr.IsSome())
+ require.Equal(
+ t, "03e7156ae33b0a208d0744199163177e909e80176e55d97a2f22"+
+ "1ede0f934dd9ad",
+ hex.EncodeToString(invoice.Destination.SerializeCompressed()),
+ )
+}
+
// TestNewInvoice tests that providing the optional arguments to the NewInvoice
// method creates an Invoice that encodes to the expected string.
func TestNewInvoice(t *testing.T) {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.