[#CKey-SignSchnorr]
= xref:CKey.adoc[CKey]::SignSchnorr
:relfileprefix: ../
:mrdocs:

Create a BIP&hyphen;340 Schnorr signature, for the xonly&hyphen;pubkey corresponding to &ast;this, optionally tweaked by &ast;merkle&lowbar;root&period; Additional nonce entropy is provided through aux&period;

== Synopsis

Declared in `&lt;key&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
bool
SignSchnorr(
    xref:uint256.adoc[uint256] const& hash,
    std::span&lt;unsigned char&gt; sig,
    xref:uint256.adoc[uint256] const* merkle&lowbar;root,
    xref:uint256.adoc[uint256] const& aux) const;
----

== Description

merkle&lowbar;root is used to optionally perform tweaking of the private key, as specified in BIP341&colon;

* If merkle&lowbar;root &equals;&equals; nullptr&colon; no tweaking is done, sign with key directly (this is used for signatures in BIP342 script)&period;
* If merkle&lowbar;root&hyphen;&gt;IsNull()&colon; sign with key &plus; H&lowbar;TapTweak(pubkey) (this is used for key path spending when no scripts are present)&period;
* Otherwise&colon; sign with key &plus; H&lowbar;TapTweak(pubkey &verbar;&verbar; &ast;merkle&lowbar;root) (this is used for key path spending, with specific Merkle root of the script tree)&period;

== Return Value

true if signing succeeded&period;

== Parameters

[cols="1,4"]
|===
| Name| Description
| *hash*
| The 32&hyphen;byte message hash to sign&period;
| *sig*
| Output span (64 bytes) receiving the Schnorr signature&period;
| *merkle&lowbar;root*
| Optional Merkle root controlling BIP341 tweaking (see above)&period;
| *aux*
| Additional nonce entropy&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
