<!--
{
  "documentType" : "article",
  "framework" : "P256K",
  "identifier" : "/documentation/P256K/SigningFixedSizeMessages",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Signing Fixed-Size Messages with Schnorr"
}
-->

# Signing Fixed-Size Messages with Schnorr

Produce and verify a BIP-340 Schnorr signature over an exactly-32-byte message — the flow Nostr event signing, Taproot sighashes, and most “sign the hash” protocols specify.

## Overview

The 32-byte discipline is a *protocol* requirement, not a BIP-340 one. BIP-340 itself permits messages of any length; what protocols built on it actually sign is almost always a 32-byte commitment — a Nostr event id, a Taproot sighash, a message hash. [`P256K.Schnorr.PrivateKey`](/documentation/P256K/P256K/Schnorr/PrivateKey) separates those intents by name so the wrong one cannot happen silently:

- `signature(message:)` — exactly 32 bytes, or it throws ``doc://P256K/documentation/P256K/secp256k1Error/incorrectParameterSize`` before any key material is touched.
- `signature(for:)` — a pre-computed `Digest` (for example a ``doc://P256K/documentation/P256K/SHA256Digest``), signed at its natural length.
- `signature(customMessage:)` — arbitrary-length input, for protocols whose spec genuinely signs unhashed bytes. Prefer the 32-byte discipline — with a BIP-340 tagged hash for domain separation — unless the signed protocol defines its own.

Every produced signature is verified against the signer’s `xonly` identity before it is returned (BIP-340 footnote 14’s verify-after-sign check), so a corrupt signature cannot escape through a signing call.

### Hash, sign, verify

The recipe a Nostr event signer performs: serialize the event, hash the serialization to the 32-byte event id, sign it, and publish the signature beside the x-only public key.

```swift
import Foundation
import P256K

let privateKey = try P256K.Schnorr.PrivateKey()

// The protocol's committed message is a 32-byte hash — or becomes one.
let messageData = "Vires in Numeris.".data(using: .utf8)!
let messageHash = SHA256.hash(data: messageData)  // exactly 32 bytes

// Sign — fresh auxiliary randomness is drawn internally.
let signature = try privateKey.signature(message: messageHash)

// Verify against the signer's x-only public identity — BIP-340's
// notion of "the public key", and what external verifiers consume.
let isValid = privateKey.xonly.isValid(signature, for: messageHash)  // true
```

A wrong-length input never reaches the signature step:

```swift
// A non-32-byte input through the 32-byte method throws — it does not
// silently truncate or pad.
let unhashed100 = Data(repeating: 0xAB, count: 100)
try privateKey.signature(message: unhashed100)
// ^ throws secp256k1Error.incorrectParameterSize
```

### Choosing the signing method

|Method                                   |Input                      |Auxiliary randomness      |
|-----------------------------------------|---------------------------|--------------------------|
|`signature(for:)`                        |any `Digest`               |fresh                     |
|`signature(for:auxiliaryRand:)`          |any `Digest`               |caller’s, exactly 32 bytes|
|`signature(message:)`                    |exactly 32 bytes           |fresh                     |
|`signature(message:auxiliaryRand:)`      |exactly 32 bytes           |caller’s, exactly 32 bytes|
|`signature(customMessage:)`              |any length, including empty|fresh                     |
|`signature(customMessage:auxiliaryRand:)`|any length, including empty|caller’s, exactly 32 bytes|

Caller-supplied auxiliary randomness exists mainly to reproduce published test vectors — a zeroed 32-byte container reproduces BIP-340’s all-zero-auxiliary case (vector index 0). For production signing, omit the parameter: the fresh randomness drawn internally is BIP-340’s fault-injection hedge, and a fixed auxiliary value trades that protection away for determinism. There is no `nil` form — “deterministic, zero-randomness nonces” is only expressible deliberately, via an explicit zero container.

### Domain separation

When your own protocol signs hashes, give the hash a BIP-340 tag so a signature for one purpose can never verify as another:

```swift
let eventId = SHA256.taggedHash(
    tag: "my-protocol/event".data(using: .utf8)!,
    data: messageData
)
let signature = try privateKey.signature(message: eventId)
```

Protocols like Nostr that commit to an *untagged* SHA-256 of a canonical serialization need the plain `SHA256.hash` shown above instead — match the signed protocol’s specification exactly. And a protocol that genuinely signs raw bytes (BIP-340’s [“Messages of Arbitrary Size”](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki)) signs through `signature(customMessage:)`, which accepts any length including zero.

> Note: `SHA256` here is ``doc://P256K/documentation/P256K/P256K``‘s own hasher — `SHA256.hash` binds to it unambiguously even beside `import CryptoKit`, since expression position resolves by overload. The collision only surfaces in *type* position — a generic argument like `HKDF<SHA256>` or a `let x: SHA256` annotation — where both modules’ hashers are candidates; qualify the CryptoKit side there (`CryptoKit.SHA256`, or `Crypto.SHA256` under `import Crypto`), or bind this side file-wide with `import enum P256K.SHA256` (`P256K.SHA256` is not a usable spelling — the `P256K` enum shares the module’s name).

### What throws, and what cannot

Two reachable failures throw [`secp256k1Error.incorrectParameterSize`](/documentation/P256K/secp256k1Error/incorrectParameterSize): a `message` that is not exactly 32 bytes, and an `auxiliaryRand` that is not exactly 32 bytes. An upstream signing failure after input validation is unreachable — it can only indicate output corruption or a library defect — so it reports through [`setInvariantViolationHandler(_:)`](/documentation/P256K/P256K/setInvariantViolationHandler(_:)) and terminates rather than returning a bad signature.

## See Also

[Getting Started with secp256k1 in Swift](/documentation/P256K/GettingStarted)

Install P256K via Swift Package Manager and produce your first verified secp256k1 signature — the entry point for Swift developers building Bitcoin, Nostr, or Lightning Network functionality.

[Deriving Keys from a Shared Secret](/documentation/P256K/DerivingKeysFromSharedSecrets)

Turn the [`SharedSecret`](/documentation/P256K/SharedSecret) produced by ECDH key agreement into symmetric key material with HKDF — the composition protocols like Nostr’s [NIP-44 v2](https://github.com/nostr-protocol/nips/blob/master/44.md) specify.

[MuSig2 Multi-Signatures](/documentation/P256K/MuSig2MultiSignatures)

Combine partial contributions from a fixed group of co-signers into a single Schnorr signature using the [BIP-327](https://github.com/bitcoin/bips/blob/master/bip-0327.mediawiki) MuSig2 protocol.

[Working with Keys](/documentation/P256K/WorkingWithKeys)

Encode [`P256K`](/documentation/P256K/P256K) secp256k1 keys in raw and x-only forms, import them from PEM and DER, and derive child keys via additive or multiplicative tweaks — the foundations under Bitcoin wallet storage, Lightning interop, Nostr key handling, BIP-32 derivation, BIP-341 Taproot output construction, and BIP-327 MuSig2 aggregation.

[Security Considerations](/documentation/P256K/SecurityConsiderations)

Understand the security properties of P256K and how to avoid common cryptographic pitfalls.

[`P256K.Schnorr.PrivateKey`](/documentation/P256K/P256K/Schnorr/PrivateKey)

secp256k1
[BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki) Schnorr
private key for signing messages with [`P256K.Schnorr.SchnorrSignature`](/documentation/P256K/P256K/Schnorr/SchnorrSignature) and for deriving the
x-only public key used in verification.

[`P256K.Schnorr.XonlyKey`](/documentation/P256K/P256K/Schnorr/XonlyKey)

The corresponding x-only public key for the secp256k1 curve, as defined by
[BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki).

[`P256K.Schnorr.SchnorrSignature`](/documentation/P256K/P256K/Schnorr/SchnorrSignature)

64-byte
[BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki) Schnorr
signature over the secp256k1 elliptic curve, produced by
`secp256k1_schnorrsig_sign_custom` and verified by `secp256k1_schnorrsig_verify`
(both declared in
[`secp256k1/include/secp256k1_schnorrsig.h`](https://github.com/bitcoin-core/secp256k1/blob/master/include/secp256k1_schnorrsig.h)).

[`SHA256`](/documentation/P256K/SHA256)

SHA-256 hash function
([RFC 6234](https://datatracker.ietf.org/doc/html/rfc6234))
backed by the `secp256k1_swift_sha256` shim
([`Sources/libsecp256k1/src/Utility.c`](https://github.com/21-DOT-DEV/swift-secp256k1/blob/main/Sources/libsecp256k1/src/Utility.c)),
producing 32-byte [`SHA256Digest`](/documentation/P256K/SHA256Digest) values; also provides BIP-340 tagged-hash support via
[`taggedHash(tag:data:)`](/documentation/P256K/SHA256/taggedHash(tag:data:)) using upstream `secp256k1_tagged_sha256`.

