Skip to content

Add BIP-322 signature variant prefixes - #71

Merged
raphjaph merged 4 commits into
rust-bitcoin:masterfrom
aagbotemi:feat/signature-prefix
Aug 16, 2026
Merged

Add BIP-322 signature variant prefixes#71
raphjaph merged 4 commits into
rust-bitcoin:masterfrom
aagbotemi:feat/signature-prefix

Conversation

@aagbotemi

@aagbotemi aagbotemi commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Summary

The BIP-322 spec requires signers to prefix the base64 signature with a variant identifier (smp for simple, ful for full, or pof for proof of funds) and verifiers may only assume the simple variant when no prefix is present. The crate emitted bare base64 from all three sign_*_encoded functions and only accepted bare base64 on verification, so prefixed signatures from spec-compliant implementations failed to decode.

This prepends the variant prefix in sign_simple_encoded, sign_full_encoded, and sign_pof_encoded, and strips it in the corresponding verifiers, falling back to the unprefixed encoding when absent.

Rebased on #67.

Changes

  • Add SIMPLE_SIGNATURE_PREFIX, FULL_SIGNATURE_PREFIX, and POF_SIGNATURE_PREFIX constants

Closes #70

@aagbotemi
aagbotemi force-pushed the feat/signature-prefix branch from 61c3565 to 948187f Compare August 4, 2026 06:53

@sdmg15 sdmg15 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cAck 948187f

Comment thread src/verify.rs Outdated

let to_sign = to_sign
.strip_prefix(FULL_SIGNATURE_PREFIX)
.unwrap_or(to_sign);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of a situation where a user by mistake calls verify_simple_encoded but with a signature prefixed with pof. We could have a better error so the user knows what went wrong?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. I added SignatureVariantMismatch error.

@raphjaph raphjaph left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks correct and matches the finalized BIP-322 prefix scheme.

Could you add explicit tests for:

  1. The official no-prefix fallback vector from basic-test-vectors.json, so the unprefixed path is deliberate rather than incidental (verify_simple_encoded must return Ok(()).):
address:   bc1pss0zhytly75awhm6x2hhvd5lnzv3vssgrf9axfheq8ldyzn88ges79fler
message:   "No prefix fallback"
signature: "AUCJYOwOjxYAvatTAGYaVlNXBVyFuc4MwNQkOuK2tl8xhfKDONd0NjfYyNSYcRqeCp8hsAnCEPHAVEkO9h6vbQ/R"
  1. The official prefix error vectors from the same file. verify_simple_encoded this should produce Error::SignatureVariantMismatch:
"incorrect prefix type": fulAUDZwFXUp+adN+/UZj5dVrGAbB3zKs1Vcalz5fCF9srxS63eSWNGvH1NYbrBkPt1BJDUyWUz9zgUxfc63/QheT6M
message "incorrect prefix"
address: bc1pyrgrm6cu6n54jrvkdjd9rvyd3xfyu84s2623awu2srn6mxhscwpsm5644w
  1. An unknown prefix must still error later in decoding (any error variant is fine here):
"invalid signature prefix": fooAA== 
address: bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l 
  1. Extend verify_rejects_mismatched_variant_prefix to the pof permutations. Right now only simple <-> full mismatch is tested; verify_pof_encoded has the same prefix logic and no mismatch coverage (i.e. pof-sig -> verify_simple_encoded errors, smp/ful sig → verify_pof_encoded errors).

@aagbotemi

Copy link
Copy Markdown
Contributor Author

Thank you for the review @raphjaph. Done in db62e02

@sdmg15 sdmg15 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tAck db62e02

@raphjaph raphjaph left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@raphjaph
raphjaph merged commit 9f6874b into rust-bitcoin:master Aug 16, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add prefixes to signature

3 participants