<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "P256K",
  "identifier" : "/documentation/P256K/P256K/MuSig/Nonce/generate(sessionSecretRand:secretKey:publicKey:msg32:extraInput32:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "P256K"
    ],
    "preciseIdentifier" : "s:5P256KAAO5MuSigO5NonceV8generate17sessionSecretRand9secretKey06publicJ05msg3212extraInput32AD0D6ResultVAD07SessiongH0Vn_AB7SchnorrO07PrivateJ0VSgAR06PublicJ0V10Foundation15ContiguousBytes_pSgSays5UInt8VGSgtKFZ"
  },
  "title" : "generate(sessionSecretRand:secretKey:publicKey:msg32:extraInput32:)"
}
-->

# generate(sessionSecretRand:secretKey:publicKey:msg32:extraInput32:)

Generates a fresh nonce pair for one MuSig2 signing session using a caller-supplied [`P256K.MuSig.SessionSecretRand`](/documentation/P256K/P256K/MuSig/SessionSecretRand).

```
static func generate(sessionSecretRand: consuming P256K.MuSig.SessionSecretRand, secretKey: P256K.Schnorr.PrivateKey?, publicKey: P256K.Schnorr.PublicKey, msg32: (any ContiguousBytes)?, extraInput32: [UInt8]?) throws -> P256K.MuSig.NonceResult
```

## Parameters

`sessionSecretRand`

Secret, uniformly random session input consumed by this call — see [`P256K.MuSig.SessionSecretRand`](/documentation/P256K/P256K/MuSig/SessionSecretRand).

`secretKey`

The signer’s private key; providing it increases misuse-resistance. Must match `publicKey` when provided.

`publicKey`

The signer’s public key; the secret nonce is bound to this key.

`msg32`

The message to sign, or `nil` if not yet known; must be
exactly 32 bytes when provided — `secp256k1_musig_nonce_gen` reads a
fixed `msg32`, so hash arbitrary-length application messages to 32
bytes first (variable-length BIP-327 messages are unsupported by
libsecp256k1). Providing it binds the nonce to the message,
increasing misuse-resistance.

`extraInput32`

Optional 32 bytes of additional entropy passed to `secp256k1_musig_nonce_gen`.

## Return Value

A `~Copyable` [`P256K.MuSig.NonceResult`](/documentation/P256K/P256K/MuSig/NonceResult) with the public nonce (to share) and secret nonce (to consume).

## Discussion> Warning: **Nonce reuse leaks the secret signing key.** Each ``doc://P256K/documentation/P256K/P256K/MuSig/SessionSecretRand``
> must be used for exactly one call — the value is consumed, so reuse is prevented
> at compile time by the type’s `~Copyable` conformance rather than relying on the
> caller to discard the bytes. `secp256k1_musig_nonce_gen` additionally zeroes the
> value’s own `SecureBytes` buffer in place on success. Never store or serialize the secret nonce. The
> returned ``doc://P256K/documentation/P256K/P256K/MuSig/NonceResult`` is `~Copyable`.

> Throws: ``doc://P256K/documentation/P256K/secp256k1Error/incorrectParameterSize`` if `msg32` or
> `extraInput32` is present but not exactly 32 bytes; ``doc://P256K/documentation/P256K/secp256k1Error/underlyingCryptoError``
> if `secretKey` is provided but does not match `publicKey`. An upstream
> `secp256k1_musig_nonce_gen` failure is unreachable on the
> validated inputs and reports through
> ``doc://P256K/documentation/P256K/P256K/setInvariantViolationHandler(_:)`` instead.
> `sessionSecretRand` is consumed and zeroed on every exit including a
> throw — a rejected `msg32`/`extraInput32` requires fresh session
> randomness.