> For the complete documentation index, see [llms.txt](https://docs.ducatprotocol.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ducatprotocol.com/how-ducat-works/oracles/generating-keys-and-signatures.md).

# Generating Keys and Signatures

The reference price-contract implementation uses BIP-340 Schnorr signatures on secp256k1. The following byte-level derivation defines the commitment; signing a JSON string or using ECDSA is not compatible.

### Encoding

* `oracle_pubkey`: the 32 raw bytes of the x-only public key.
* `network_bytes`: UTF-8 bytes of the SDK's contract-compatible network string, without a length prefix or separator.
* `base_price`, `base_stamp`, `thold_price`: unsigned 32-bit integers, each encoded in big-endian byte order.
* `||`: raw byte concatenation. Hex text must be decoded before hashing.
* `HASH160(x)`: `RIPEMD160(SHA256(x))`.

The contract-compatible network strings are:

| Network               | String included in the hash |
| --------------------- | --------------------------- |
| Bitcoin mainnet       | `main`                      |
| Testnet3              | `testnet3`                  |
| Testnet4              | `testnet4`                  |
| Signet                | `signet`                    |
| Mutinynet             | `mutiny`                    |
| Regtest               | `regtest`                   |
| Alpha mainnet profile | `alpha-mainnet`             |

For example, substituting the JSON enum string `bitcoin` for `main` changes the commitment and invalidates compatibility.

### Derivation

```
tagged_hash(tag, message) =
    SHA256(SHA256(UTF8(tag)) || SHA256(UTF8(tag)) || message)

commit_hash = tagged_hash(
    "ducat/price_contract_commit",
    oracle_pubkey ||
    network_bytes ||
    uint32_be(base_price) ||
    uint32_be(base_stamp) ||
    uint32_be(thold_price)
)

thold_key = HMAC_SHA256(
    key = oracle_secret_key_32_bytes,
    message = commit_hash
)

thold_hash = HASH160(thold_key)

contract_id = tagged_hash(
    "ducat/price_contract_id",
    commit_hash || thold_hash
)

oracle_sig = BIP340_SIGN(oracle_signing_key, contract_id)
```

The reference implementation derives the threshold key from the same oracle secret key used to construct its signing keypair. A separately provisioned HMAC secret would produce different threshold keys and is not the documented derivation.

The output sizes are 32 bytes for `commit_hash`, `thold_key` and `contract_id`; 20 bytes for `thold_hash`; and 64 bytes for `oracle_sig`.

### Signing and verification

The Rust implementation uses deterministic Schnorr signing without auxiliary randomness for reproducible signatures. Compatible verifiers should verify BIP-340 validity, not require that every valid signer produce one identical signature byte string.

Verification requires the authorised oracle public key, reconstructed commitment and contract identifier. The threshold key is not required to verify a sealed contract. Once revealed, it must additionally satisfy the committed HASH160 value.

The oracle's secret key is never part of a contract response. The threshold key is a derived, contract-specific preimage, not the oracle's signing key. Its publication enables only matching hashlocked paths, each of which still requires a guardian-group signature.

### What a signature establishes

The signature binds the oracle to the encoded price, timestamp, network and hash commitment. It does not independently establish market-price correctness, timely publication, honest threshold release or guardian availability. Those remain part of the [oracle and liquidation trust assumptions](https://docs.ducatprotocol.com/trust-assumptions/liquidations.md).
