# Signed receipts

> Every decision comes with a signed receipt that anyone can check offline with the server's public key, without trusting Curva's server or database.

Source: https://docs.tarkova.com/curva/guides/receipts/

## How it works, step by step

1. **Decision answered** (Model, Cache, Rule, Learned, Fallback): Curva answers the decision and notes what gave each answer. A receipt proves what the server said and when. It does not prove the answer is correct. In the example: A ticket about a double charge: team is billing at 0.9992, refund is yes at 0.998, both by the model.
2. **Audit row** (Salted hash, Answers, Model, Time): The audit log gets one row for the decision. It holds a salted SHA-256 of the state and not the state itself, so the receipt can name the state without revealing it. In the example: The state is stored only as the hash a65a3472…9b3e.
3. **Chain link** (SHA-256, Previous link, Row fields): Each row's link is computed from the link before it and the row's own fields. Changing an old row breaks every link after it. In the example: This decision's link starts 37e5f510. It is the last of 72 in this run's log.
4. **Sign** (Ed25519, id, at, by, answers, chain): The server signs the exact JSON text of the id, time, project, model, mode, what gave each answer, state hash, answers and chain link. Extras such as explain or evidence are not signed. On the router, a receipt comes back only if you set receipt: true. In the example: The signed text is 646 bytes, and the whole receipt adds 869 bytes to the reply.
5. **Save public keys** (receipts/keys, No API key, Rotation): GET /v1/receipts/keys returns public keys only, and your saved copy is all a check needs. Without CURVA_MASTER_KEY the signing key sits unencrypted in the database, so anyone with a copy of the database can sign receipts that verify. In the example: The test server had no master key and warned about this at start. A real deployment should set one.
6. **Verify offline** (curva receipt, Python, TypeScript): A check needs the receipt and a public key. It needs no network, no server and no database, so an auditor can run it without trusting either. In the example: With the server stopped, curva receipt verify says "valid receipt, signed by key e74247ac6a21392f". The Python verify_receipt accepts it too.
7. **Catch an edit** (Changed answer, Changed text, Retired key): Editing the decision next to the receipt, or the signed text, fails with a sentence naming what happened. Checking a decision against its receipt needs the decision passed in. Alone, a receipt proves only its own signed fields. In the example: Team changed to sales in the decision: "answer `team`: its `choice` is not the one its receipt signed". Changed in the signed text: "it was changed after it was signed".
8. **Audit chain check** (curva audit, verify, --head): curva audit verify recomputes the chain on the server's database and fails at the first changed row. Deleting the newest rows leaves a shorter chain that still holds, so save a head with --head, and keep your receipts. In the example: On a copy of the database, a changed row fails at its decision. With the newest row deleted, the plain check still passes and only --head with the earlier head fails.

Every decision Curva answers comes with a `receipt`: an Ed25519 signature over what was decided.
Anyone holding the server's public key (an auditor, a regulator, a court) can check it later,
offline, without trusting Curva's server or its database. Change one answer, one probability, the
model, the time or the id, and the receipt no longer verifies. It also says what gave each answer:
the model, the cache, one of your rules, a learned answer, the local model or a fallback.

```python
from curva import verify_receipt
claims = verify_receipt(d.receipt, keys, decision=d)   # keys: GET /v1/receipts/keys, or one hex public key
```

```ts

const claims = await verifyReceipt(d.receipt!, keys, d);
```

```bash
curl -s localhost:7777/v1/receipts/keys > keys.json
curva receipt verify decision.json --key keys.json
```

Receipts are on for every `/v1/decide` and `/v1/systemone` request and for the decisions of a
live call; there is nothing to turn on. Signing adds about
0.015 ms to a decision (p50 0.115 ms without, 0.130 ms with, over 5,000 decisions against a
fake model; one signature takes 9.3 µs). It adds 600 to 890 bytes to a response: measured on
one- and two-question decisions on Bedrock (607 and 883 bytes), where full-precision
probabilities make up most of it.

## What is signed

```json
"receipt": {
  "signed": "{\"v\":2,\"key\":\"a0d326c45f6edccb\",\"id\":\"dec_1a1656f4ba2000074\",\"at\":1792703155107,\"project\":\"default\",\"model\":\"@bedrock/moonshotai.kimi-k2.5\",\"mode\":\"logprobs\",\"by\":{\"team\":\"model\",\"vip\":\"rule\"},\"state\":\"54c24249ba03be8aff9fd6dccfaa89f8ed9e6cf486f1e0ab710d1e94b4e9fca1\",\"answers\":{\"team\":{\"choice\":\"billing\",\"probabilities\":{\"billing\":0.9996971238009904,\"technical\":0.0000889697003360623,\"sales\":0.00016010414336989131,\"none_of_these\":0.000053802355303619606},\"confidence\":0.9996971238009904,\"calibrated\":false},\"vip\":{\"noul\":0.0,\"calibrated\":false}},\"chain\":\"8ef6510da6346b91a147013a2d75e6d2f98832ffb949b65ad2ef2a634b424bb9\"}",
  "sig": "3a9d1166ef0574c9fe7dce4c8c4a2a06822535451516db1e45300f131d20def1fce5af2a2e51c9e09db20794ef578f7bf0df6d0e1fc3f789ab0440b18b451408"
}
```

`signed` is the exact text that was signed, so checking it needs no agreement on how JSON is
written (Python would print the `0.0000889697003360623` above as `8.89697003360623e-05`): the
signature covers its UTF-8 bytes, and `sig` is the signature in hex.

| Field | Meaning |
|---|---|
| `v` | Receipt format: 2. Receipts made before `mode` and `by` were signed have 1, and still verify |
| `key` | Id of the signing key: the first 16 hex characters of the SHA-256 of its public key |
| `id` | The decision id |
| `at` | When the decision was written to the audit log, unix ms |
| `project`, `model`, `mode` | As in the response. `mode` is `fallback` when no model answered in time and `local` for the on-server model of `/v1/watch` |
| `by` | For each answered question, what gave its answer: `model`, `cache` (an earlier answer of the model), `rule`, `learned`, `local` or `fallback`. A skipped question has no entry |
| `state` | The salted SHA-256 of the state that the audit log keeps (the state itself is not kept) |
| `answers` | Each answer: its choice, value or score, probabilities, confidence, abstain and calibrated. Extras such as `explain` or `evidence` are not signed |
| `chain` | The decision's link in the audit chain |

Values are signed as returned, also with `privacy: "strict"`: the receipt goes to you, Curva
does not keep it. Share it with that in mind.

## Checking a receipt

`verify_receipt` (Python, standard library only) and `verifyReceipt` (TypeScript, WebCrypto
Ed25519: Node 20 and current browsers) return the signed fields, or raise. Given the decision the
receipt came with (`decision=` in Python, the third argument in TypeScript: the decision object or
the response JSON), they also check that its `id`, `model`, `project`, `mode` and every answer
field are the ones that were signed, so an edited decision fails too. Without it, what they
return is what is proven, and the decision next to it is not checked.

`curva receipt verify <file|->` takes a receipt or a whole decision response, and checks a
response against its receipt the same way. `--key` takes a hex public key, or a file holding one
or the JSON of `GET /v1/receipts/keys`.

```
valid receipt, signed by key a0d326c45f6edccb
It proves that the Curva server holding this key answered decision dec_1a1656f4ba2000074 in project `default` at 2026-10-22 21:05:55 UTC
(decision model @bedrock/moonshotai.kimi-k2.5, mode logprobs), for a state whose salted hash is 54c24249…fca1,
and wrote it to its audit log with chain link 8ef6510d…4bb9. The answers it signed, each with what gave it (model, cache, rule, learned, local or fallback):
  team: {"choice":"billing","probabilities":{"billing":0.9996971238009904,"technical":0.0000889697003360623,"sales":0.00016010414336989131,"none_of_these":0.000053802355303619606},"confidence":0.9996971238009904,"calibrated":false} (by model)
  vip: {"noul":0.0,"calibrated":false} (by rule)
```

All three say why a check failed:

| Message | What happened |
|---|---|
| the receipt is signed by key `<id>`, which is not among the keys given | Wrong key or key file, or a receipt from another server |
| the receipt does not verify: it was changed after it was signed | The signed text or the signature was edited |
| the receipt is dated `<time>`, after its key `<id>` was retired at `<time>` | Signed with a rotated-out key after it stopped being used: only a copy of the old key could have done that |
| the decision's `<field>` (or answer `<key>`: its `<field>`) is not the one its receipt signed | The decision next to the receipt was edited |

The CLI also refuses a `--key` that is neither a file nor 64 hex characters, so a mistyped path
is not reported as a bad receipt.

To tie a receipt to a state you hold, ask the operator to hash it with the install's audit salt.

## Keys

The server makes its signing key on first start and keeps it in its database, encrypted with
`CURVA_MASTER_KEY` when that is set (a key made before the master key was set is encrypted on the
next start). The private key never leaves the server. Without `CURVA_MASTER_KEY` the key is
stored unencrypted, and anyone with a copy of the database (a backup, a support copy) can sign
receipts that verify; `curva serve` says so in a warning at every start. Set the master key
(`openssl rand -hex 32`) and restart to encrypt it.

```
GET /v1/receipts/keys
{"keys": [{"id": "f669611a820cba91", "public": "5ba4239a66994ff6d86c82cca87a957a96acaeac30297a173d3253a57d7a698a", "created_at": 1792702688035, "retired_at": null},
          {"id": "fb85f2ad5009c4ea", "public": "4f15306d8c5b1c3ea5f7150181756a603ea4b10f6644f784de0a5c3cfa1a5be7", "created_at": 1792702279998, "retired_at": 1792702689530}]}
```

The route needs no API key: it holds only public keys, newest first. Keep a copy of it with
your receipts; that copy is all a check needs.

`curva receipt rotate --db curva.db` makes a new key and deletes the old private key; restart
the server to sign with it. The old key is retired when the server starts signing with the new
one: from then on `retired_at` holds that time, and a receipt signed with the old key and dated
after it does not verify. Receipts signed before still verify. Rotate when a database copy may
have leaked: someone holding the old key can still date a receipt before its retirement, but
not after it. Pass the keys JSON, not a single hex key, to get this check: a hex key carries no
retirement time.

## Receipts and the audit chain

They complement each other. `curva audit verify` checks, on the server, that no audited decision
was changed. Deleting the newest row still passes that check; only `--head` with an earlier head catches it. A receipt is held outside the server: its `chain` names the decision's
link in the audit chain, so a rewritten audit row no longer matches the receipts already handed
out, and a receipt proves its decision even if the database is lost.

## Live calls

In a call on `/v1/listen`, every decision Curva writes to the audit log carries a `receipt` in
the same format: answers from the live model, fallbacks when no answer came in time, and late
answers. An action names its decision's `id`, so the receipt of that decision proves which answers
the action fired on. When a bank or a phone app has to show why a call was blocked, the
`end_call` action points at the decision, and its receipt is the proof.

```python

call = curva.listen("scam", on={"end_call": hang_up})
...
call.close()
blocked = next(d for d in call.decisions if d["id"] == call.actions[-1]["decision"])
claims = verify_receipt(blocked["receipt"], keys)   # the verdict and its probabilities, signed
```

`curva receipt verify` takes the decision event as it came (it has no `model` or `project`, so
those are checked in the receipt only), or the receipt alone.

What a live-call receipt holds:

- `state` is the salted hash of `{"call": <call id>, "utterance": <n>}`. The audio, the
  transcript and the call id are not in it. The operator can tie a receipt to its call and
  utterance by hashing that state with the install's audit salt.
- `model` is the live model, also for a fallback. A fallback is signed as `"mode": "fallback"`,
  and each of its answers as `"by": "fallback"`.
- Actions are not signed; they are in the decision's audit row, which the chain covers.

An utterance over the key's rate limit gets its fallback with no receipt: it is not audited.

The end-of-call `receipt` event lists each decision with its `id` and the same `receipt`, so that
one event is enough to keep. On a very long call (receipts over 512 KiB: about 700 decisions
with the `scam` set, whose receipts measure 746 bytes each), the oldest decisions are listed with
their `id` only, and a line in `notes` says how many; their receipts came with their decision
events.

## `/v1/systemone`

A `/v1/systemone` response carries a `receipt` like `/v1/decide`. It signs the answers in the
`/v1/decide` shape: a Score's `probabilities` are a list by level, where the response keys them
`{"0": ..., "1": ...}`. `curva receipt verify` accepts either shape when it checks a response
against its receipt.
