Metadata-Version: 2.5
Name: ample-receipts-verifier
Version: 0.2.0rc3
Summary: Ample receipts verifier: checks receipt bundles offline with the Python standard library alone.
Project-URL: Homepage, https://ample.isrm.ai/receipts
Author: ISRM Labs AB
Maintainer: ISRM Labs AB
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: ample,audit,ed25519,offline,receipts,transparency-log,verifier
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Security :: Cryptography
Requires-Python: >=3.12
Provides-Extra: export
Requires-Dist: psycopg[binary]>=3.1; extra == 'export'
Description-Content-Type: text/markdown

# ample-receipts-verifier

The verifier of Ample receipts as one wheel: the `ample_replay` package without its arbiter
(`ample_replay/arbiter/`) or its artifacts (`ample_replay/artifacts/`). It checks a bundle offline, on your own
machine, with the Python standard library alone: Python 3.12 and no dependencies (one optional extra, `export`,
adds a database driver for exporting a bundle yourself). It is Apache-2.0, by ISRM Labs
AB, so an auditor can read and run the code that judged a receipt. Ample receipts are described at
https://ample.isrm.ai/receipts.

The import package keeps its name, so the commands are the ones the specifications give:
`python -m ample_replay.verify`.

## What it verifies

`python -m ample_replay.verify bundle DIR` checks one bundle directory, the bundle layout of receipts v3
section 8:

- the hash chain and the ledger's signatures, under the public key set you give with `--keys`, taken out of band.
  The bundle's own `keys.json` is not a trust anchor; `--trust-bundle-keys` uses it instead, and the report says so;
- for a tenant ledger of call receipts (the call-receipt specification, section 12): each call receipt's statement,
  the customer's client signature under the client key file you give with `--client-keys`, the client-key
  introductions and rotations on the ledger, each instance's sequence and its declared losses, and the level each
  call receipt reached, from C1 to C4;
- with `--check-anchors --tsa-cert FILE`: each `ANCHOR_PUBLISHED` notice, its checkpoint and its time-stamp token,
  under the certificates you pin (needed for C3);
- with `--bucket-listing DIR`: the version listing of the write-once bucket that you took with your own read access
  (the auditor's guide says how; needed for C4);
- with `--check-anchors --genesis-pin FILE`: that the bundle holds the first checkpoint a time-stamp authority
  stamped, and its token, as you pinned them from the ledger's first export (the call-receipt specification,
  section 15.1). It catches a ledger started again under the same key; it does not catch a fork after that
  checkpoint;
- routing receipt bundles (receipts v3, and v4 and v5 with `--host-keys`), as their specifications describe.

`python -m ample_replay.verify reveal-call` checks one part of one call that the customer reveals to you (below).
`genesis-pin` writes that pin from the ledger's first export, only when the export is intact:
`python -m ample_replay.verify genesis-pin DIR --keys FILE --tsa-cert FILE --out FILE`.
`python -m ample_replay.verify --help` lists the other
commands this wheel carries: `reveal`, `weights`, `fingerprint` and `reveal-trace`.

## Install

Install it in a virtual environment of its own:

```sh
python3 -m venv verifier-venv
verifier-venv/bin/pip install ample-receipts-verifier
```

This version is a release candidate, not a stable release; a stable release follows. Pin the exact version you
install (`ample-receipts-verifier==` and the version this page names).

It needs nothing else to verify. To export bundles yourself from the ledger database (below), install it with the
`export` extra, which adds the PostgreSQL driver psycopg:
`verifier-venv/bin/pip install 'ample-receipts-verifier[export]'`. An auditor who installs with no network takes the wheel
(`ample_receipts_verifier-<version>-py3-none-any.whl`) and its SHA-256 from Ample by a channel of their own, checks
the sum (`shasum -a 256`), and installs that file with `verifier-venv/bin/pip install --no-index` and the wheel's
path.

The project is published as a wheel only, with no source distribution: the wheel is built from files across Ample's
repository, which a source distribution of this project cannot carry. The wheel is pure Python and holds every
source file it runs, so an auditor can read exactly what judged a receipt (`unzip -l` lists it). Its import package,
`ample_replay`, is also part of Ample's full distribution: two packages that ship the same files overwrite each
other in one environment, which is another reason to keep the verifier in a venv of its own.

## Run it on a call bundle

Take every input yourself, through channels the receipts service's operator does not control: the bundle of the
customer's tenant ledger, the operator's ledger public key set, the client public key file from the customer and,
for C4, the bucket listing. The operator is whoever runs the receipts service and holds its ledger key: a provider
the customer chose, or the customer itself when it hosts the service.
The auditor's guide (`auditor.md`, which Ample gives you with each release) says where each one comes from.

```sh
verifier-venv/bin/python -I -m ample_replay.verify bundle BUNDLE_DIR \
    --keys ledger-keys.json --client-keys client-keys.json --bucket-listing listing/
```

`-I` keeps the current directory, `PYTHONPATH` and the user site out of the import path, so the code that runs is
the installed wheel's.

On the vector ledger of the call-receipt specification (`ledger-v1.json`, written out as a bundle), with `--keys`
and `--client-keys` and no other input, the report ends:

```text
    seq 3 C2 75cd218915288826f7b303b185ba8848e5c7b41e2e65968184b71575fcc7150f
    seq 4 C2 98a83e26816337f3a40c732ba804a9b9bf917a8dd07e280ca6ac78d77306649c
    seq 13 C2 1962ce328b3d1f9629d951ba2fae4f764cc44ae27b5724ba7d5be8c6f48ce822
    declared loss: instance e54387b2-3bcc-473f-841f-e0d79acc48b0 seq 8-8 (NOT_SPOOLED)
    C2 Chained: the receipts service received these exact statement bytes and chained them, and a change to them shows unless the ledger key's holder signs the chain again. The time and the order are the word of the ledger key's holder unless an anchor backs them, or a held ack that someone else kept.
verdict: INTACT; call receipts by level: C4 0, C3 0, C2 10, C1 0, NONE 0
Tamper-evident, not tamper-proof.
```

(The lines for seqs 5 to 11 are left out here.) Each call receipt gets one line: its ledger seq, its level token
and its `statement_sha256`. The report prints the sentence of each level a receipt reached, names each input it was
not given and the levels it therefore could not reach (here C3 and C4), and never prints a bare pass. The exit code
is 0 with `INTACT`, 1 with `FINDINGS` and 2 for an unreadable input or a usage error. A one-byte change to the
bundle, in its chain, its signatures or a receipt, is a finding and exit 1. Without `--client-keys`, every call
receipt is at `NONE`, since C1 needs the customer's key file. On this bundle that run reports `FINDINGS` and exits
1: the customer's declared loss at seq 8 counts only once its own statement reaches C1 (section 12, MUST-115), so
without the key file the hole shows as `CALL_GAP_UNEXPLAINED`.

The levels, in short (section 12.4 of the specification has the rules, and 12.5 the sentences the report prints):

- **C1 Signed**: the holder of the customer's client key signed the record. It is the customer's own account of
  the call.
- **C2 Chained**: the receipts service received these exact statement bytes and chained them. A change shows
  unless the ledger key's holder signs the chain again, and the time and the order are the word of the ledger key's
  holder unless an anchor backs them, or a held ack that someone else kept.
- **C3 Time-stamped**: the record existed by a time-stamp authority's time. That clock is independent of the ledger
  key's holder only if the authority you pinned is, so take its certificate from the authority's own publication.
  The certificate is judged by its pin only: its validity dates and revocation are not checked.
- **C4 Anchored in write-once storage**: a later rewrite by the ledger key's holder alone shows in the bucket
  listing you took, if someone else owns the bucket (you or a third party: independent storage). It does not hold
  against the ledger key's holder and the bucket's owner together, or against the ledger key's holder alone when it
  owns the bucket: label such a bucket as its own storage, "customer-owned storage" when the customer runs the
  service. The listing does not show who owns the bucket, so the report prints the same C4 sentence for both, and
  the label is yours to give from the bucket's declaration (MUST-B6 of the bucket specification).

## Check one part of a call: reveal-call

A reveal is four things the customer hands you: the call receipt's canonical payload exactly as stored (its F1
text, with no final line feed), the part's name, its data and its salt (section 5.8, MUST-62). The customer's client
library writes them as three files with `python -m ample_receipts_client.reveal`, which derives the salt again
from the customer's salt key and reads the part from the customer's vault. You need no salt key, no other part and
no contact with the receipts service or its operator (MUST-63):

```sh
verifier-venv/bin/python -I -m ample_replay.verify reveal-call --receipt receipt.txt \
    --part response_body --data-file response_body.bin --salt-file response_body.salt
```

On the vectors' first reveal (`reveals` in the specification's `statements-v1.json`, on the receipt of S1 in
`ledger-v1.json`), it prints:

```text
MATCH
```

and exits 0. Any other answer is one line, `MISMATCH: <reason>`, and exit 1 (MUST-126): `UNKNOWN_PART`,
`PART_NOT_COMMITTED`, `ROOT_DIFFERS`, `COMMITMENT_DIFFERS` (a wrong salt, wrong bytes or the wrong part), or
`CALL_RECEIPT_MALFORMED` for a receipt that section 12.2 cannot read, decided before the data or the salt is read.
Exit 2 is a usage error or an input that cannot be read: an option missing, unknown or given twice, a receipt that
is not F1 text, a salt file that does not hold exactly 64 lower-case hex characters (one final line feed allowed),
or the data of a JSON part that is not exactly its JCS bytes. The salt is read from `--salt-file` only, never from
the command line, and neither the salt nor the data is ever printed. A `MATCH` shows that these bytes are what the
customer committed to, as far as the receipt's level fixes the time; it shows nothing about any other copy of
them (section 13).

## The customer's held acks: --held-acks

`--held-acks FILE` gives the bundle command the acks the customer holds: a UTF-8 file, one ack's JCS text per line,
each line ended by a line feed, as the client library's `python -m ample_receipts_client.held_acks` writes it
(MUST-125). Each ack is first checked by itself (its JCS text, its cap, its payload hash, its row, its chain hash and
its signature under the `--keys` set), and an ack that fails is `HELD_ACK_INVALID`: at its seq when it has one, with
no seq when the line is not JCS text or is empty. Each other ack is compared with the bundle's ledger line at its
seq, its receipt line and its signature line: `HELD_ACK_MISSING` when the link or the row is gone,
`HELD_ACK_DIFFERS` when they differ (MUST-109). A call receipt a held ack backs is marked `ACK_HELD` on its line;
held-ack findings change no level (MUST-121). A held-acks file that cannot be read or is not UTF-8 is exit 2.

## Export a bundle yourself: export

When the customer hosts the receipts service, it can give you a read-only role on its ledger database, and you
write the bundle yourself, on your own machine, instead of taking one the customer exported. With the `export`
extra installed (above), put the connection string in `AMPLE_RECEIPTS_DATABASE_URL`, or use libpq's own
environment (`PGHOST`, `PGUSER`, `PGDATABASE`, `PGPASSFILE`, `PGSSLMODE` and the rest), never the command line:

```sh
verifier-venv/bin/python -I -m ample_replay.verify export --out bundle --issuer ISSUER --keys ledger-keys.json
```

It reads the three ledger tables (`ledger_events`, `receipts`, `ledger_signatures`) in one read-only
`REPEATABLE READ` snapshot, so it can write nothing to the database and needs only `SELECT` on those tables, which
the service's read-only role holds. The bundle is the one the service's own export writes from the same snapshot.

- `--out DIR` must not exist: every file is written once, and the manifest last.
- `--issuer NAME` is the name the bundle's manifest gives: the issuer the customer's service runs as.
- `--keys FILE` is the ledger public key set you were handed, written as the bundle's `keys.json`. Without it,
  `keys.json` holds the keys the ledger's own notices bring in. Either way you verify with the set you were handed.
- `--anchor-dir DIR` copies the checkpoints of the write-once directory, when you can read it, into `anchors/`.
  The signer certificates of the time-stamp tokens go into `tsa/` whatever the flags. Both are copies only: you
  still pin the TSA certificate with `--tsa-cert`.
- `--role ROLE` sets the session's role when it connects, for a login that does not inherit the read-only role.

It prints a summary of what it wrote (exit 0); it refuses with exit 1 and leaves no directory, and exit 2 is a
usage error. A database failure prints its exception type only, never the driver's text, which can quote the
connection string. A ledger with a receipt whose model weights were observed by Ample or attested by a host is
refused: its bundle needs a manifest or a signed statement that this command does not take. Then verify the bundle
with `bundle` or the auditor's script.

## The auditor's script

`python -m ample_replay.verify.audit` runs the auditor's path in one command: the bundle command with `--keys`,
`--client-keys` and `--held-acks` (with `--check-anchors --tsa-cert` and `--bucket-listing` when given), then one
`reveal-call`, then the revealed receipt's level in the bundle. It prints each step with its output, the count of
call receipts at each level and `ACK_HELD`, and `ALL PASS` or the steps that failed (exit 0, 1, or 2 for an input
that cannot be read):

    python -m ample_replay.verify.audit --bundle DIR --keys ledger-keys.json \
        --client-keys client-keys.json --held-acks held-acks.jsonl \
        --reveal-receipt DIR2/receipt.txt --reveal-part request_body \
        --reveal-data-file DIR2/request_body.bin --reveal-salt-file DIR2/request_body.salt

## What it cannot do

- **`--replay` needs Ample's full distribution.** Replaying a routing receipt re-decides it with the arbiter,
  which this wheel leaves out, with the artifacts and the receipt reader. Here `bundle --replay` refuses, exit 2,
  and says so: the full distribution, which carries all of `ample_replay`, is on no package index, so ask Ample for
  it. Every other bundle check runs here. A call receipt is never replayed, in either distribution.
- **The re-run needs the full distribution and the model-proof environment.** Re-running a call
  (`python -m ample_replay.rerun`, receipts v5 section 3.2) loads the model with torch, transformers and toploc,
  which this wheel does not carry, so it leaves out `ample_replay/rerun/check.py` and the command. It does carry the
  re-run policy and the reader of the host's signed call record, which `weights --call-record` uses.
- **It fetches nothing.** It makes no network call, and takes no key from the receipts service, from the bundle or
  from the ledger as a trust anchor for C1: the client keys come only from the file you give.
- **C3 needs a real time-stamp token** and the authority's certificate, given with `--check-anchors --tsa-cert`.
  C4 needs C3 as well as the listing.
- **A level is not a proof of the call.** What a call receipt does not show at any level (that the upstream sent
  those bytes, completeness, which weights ran, that the answer is true, and the rest) is listed in section 13 of
  the specification and explained in `proves.md`, which Ample gives you with each release.

## How this wheel is tested

Ample's test suite builds this wheel offline from the committed tree, installs it with `--no-deps` into a
throwaway virtual environment that holds nothing else of Ample's, and runs the commands above on the vector bundle:
`INTACT` with the levels printed, a finding and exit 1 for a one-byte change, and an import probe after a full run
that finds no module of Ample's routing code, `ample_replay.arbiter` or `ample_replay.artifacts` loaded. It also
runs `reveal-call` on each of the vectors' seven reveals and on the example above. For `export`, it installs the
wheel and the extra's driver offline into a throwaway virtual environment, exports a hosted test ledger as a login
that holds only the read-only role, and requires the bundle to be the service's own export byte for byte and
`INTACT`, with no module of Ample's service loaded. The release build makes the
published wheel from a fresh clone of the release commit, with hatchling pinned by hash, and installs it with no
index and no dependencies before anything is uploaded.

Tamper-evident, not tamper-proof.
