Metadata-Version: 2.5
Name: vauban-verify
Version: 2.0.0
Summary: Offline cryptographic verifier for Vauban Command Center proof certificates.
Project-URL: Homepage, https://vauban.tech
Author-email: Vauban <ops@vauban.tech>
License: MIT
License-File: LICENSE
Keywords: merkle,poseidon,proof,starknet,verifier,zk
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Security :: Cryptography
Requires-Python: >=3.10
Requires-Dist: poseidon-py==0.1.5
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == 'test'
Description-Content-Type: text/markdown

# vauban-verify

Offline cryptographic verifier for Vauban Command Center proof certificates.

Recomputes every step leaf from its `payload` preimage, then the Poseidon
Merkle root from a certificate's `decision_chain`, and asserts it matches the
certificate's anchored `merkle_root` ; verifies the Ed25519 `signature` when
the certificate carries one. **No network calls. No trust in our
infrastructure or keys.** Trust derives entirely from your independent
on-chain read of the anchored root.

**2.0.0:** leaves are recomputed from their payload when present; entries without
payload are refused unless `--allow-unbound-steps` or a pinned signature; Merkle
tree is position-binding (not compatible with 1.0.0 certificates).

**What a passing verification binds, and what it does not** :

| Data                                  | Bound by                                  | Verified by this tool when                      |
|---------------------------------------|-------------------------------------------|-------------------------------------------------|
| `merkle_root`                         | the on-chain anchor                       | you read the root on Starknet and compare (out of band) |
| the ordered `leaf_hash_poseidon` list | `merkle_root`                             | always                                          |
| a chain entry's fields (type, agent, amounts, timestamps…) | its leaf, **only through the `payload` preimage** ; or the signature pinned to the issuer's full key | the entry carries `payload` ; otherwise the entry is **unbound to the root** and refused unless the signature is pinned (`--pubkey-spki-b64` / `--pubkey-sha256`) or `--allow-unbound-steps` is passed |
| `run_id` and every top-level field    | the Ed25519 `signature` **only**          | the certificate is signed ; pin the issuer's FULL key with `--pubkey-spki-b64` / `--pubkey-sha256`, otherwise the key is self-asserted (`NOT pinned`). `--expected-kid` (16 hex = 64 bits) only selects a key : a colliding key costs ~2^64 trials, so a kid never pins. Naming a key for an unsigned certificate is a refusal. |

Certificates emitted by the Command Center (`GET /proof/:runId`) carry no
`payload` (the preimage is the `run_step.payload` column, not exported) and,
on the public route, no `signature` : for them this tool verifies the leaf
hashes against the root and **nothing else** ; `--allow-unbound-steps` makes
that explicit on stdout (`bound=0/N`) and stderr (`WARNING`).

Pure Python; the only runtime dependency is
[`poseidon-py`](https://pypi.org/project/poseidon-py/) for felt252 Poseidon
arithmetic, exact-pinned to `0.1.5` (zero transitive dependencies) with a
hash-locked `requirements.lock`, so the verifier is reproducible: an
independent rebuild resolves the same trust-critical code (threat R-d.3).
`starknet-py` is NOT a dependency of this verifier; if you want to read the
anchored root on-chain yourself, use any Starknet client out of band.

This verifier matches the issuer's reference implementation. Cross-language
parity is enforced by deterministic test vectors shipped in `tests/` — any drift
between the reference and Python fails the build.

## Install

```bash
pip install vauban-verify
```

**Reproducible install (recommended for verification ; threat R-d.3).** Install
the trust-critical dependency from the hash-locked file so an independent party
gets the exact same bytes:

```bash
pip download --no-deps --no-binary :all: vauban-verify==2.0.0   # the sdist, which ships requirements.lock
tar xzf vauban_verify-2.0.0.tar.gz && cd vauban_verify-2.0.0
pip install --require-hashes -r requirements.lock   # poseidon-py==0.1.5, hash-pinned
pip install --no-deps .                             # the verifier itself
```

Or from source for development:

```bash
pip install -e ".[test]"
```

Requires Python ≥ 3.10.

## CLI usage

```bash
vauban-verify path/to/cert.json
# → vauban-verify: OK — run_id=<…> leaves=<N> bound=<N>/<N> root=0x… signature=none (run_id and entry fields are NOT bound)
echo $?  # 0 = valid, 1 = tampered/invalid/unbound, 2 = usage error

# Command Center shape (no `payload` in the entries) : verify the leaves alone, say so
vauban-verify path/to/cert.json --allow-unbound-steps
# stderr → vauban-verify: WARNING — N step(s) without `payload` (indices […]): only their leaf hashes are bound to the root ; …
# stdout → vauban-verify: OK — run_id=<…> leaves=<N> bound=0/<N> root=0x… signature=…

# Signed certificate, issuer key pinned out of band (full key : no --allow-unbound-steps needed)
vauban-verify path/to/signed-cert.json --pubkey-sha256 <64 hex of SHA-256(SPKI DER)>
# → … signature=Ed25519 kid=<16 hex> (pinned)
# --expected-kid <16 hex> only selects the key ; it refuses an unsigned certificate but never pins
```

## Library usage

```python
from vauban_verify import (
    compute_step_leaf_hash,
    compute_poseidon_merkle_root,
    verify_poseidon_merkle_proof,
    load_certificate,
    verify_offline,
)

# Recompute a leaf from a run_step payload
leaf = compute_step_leaf_hash({"step_id": "s1", "type": "decision", "agent": "BUILDER"})

# Recompute a Merkle root from a list of leaves
root = compute_poseidon_merkle_root([leaf, ...])

# Verify a sibling-path inclusion proof
ok = verify_poseidon_merkle_proof(leaf, proof, root)

# Verify a full certificate offline
cert = load_certificate("cert.json")
verify_offline(cert)  # True, or raises VerifierError (forged leaf, unbound entry, bad signature…)

# Same, with the report of what is bound
from vauban_verify import verify_certificate
report = verify_certificate(cert, expected_pubkey_sha256="…")  # full key pinned ; expected_kid only selects
report.bound_steps, report.unbound_steps, report.signature.pinned, report.run_id_bound
```

## Security model

| Threat                                  | Defended by                                                |
|-----------------------------------------|-------------------------------------------------------------|
| Compromised Vauban infrastructure       | This verifier runs offline; never contacts our servers      |
| Compromised Vauban signing keys         | The verifier asserts only Merkle consistency; trust the chain anchor, not us |
| Hash-function preimage attacks          | Poseidon over felt252 — STARK-friendly, post-quantum sound  |
| Drift between TS reference and verifier | 30 cross-language vectors gated in CI (regen + diff)        |
| Rewritten entry fields, leaves and root kept | Each leaf is recomputed from its `payload` preimage and compared ; entries without preimage are refused by default (`--allow-unbound-steps` opts in and the output then says the fields are unverified ; a signature **pinned** to the issuer's FULL key also vouches for them ; a 64-bit kid does not) |
| Rewritten `run_id` / top-level fields   | Only the Ed25519 `signature` binds them ; verified when present, with optional full-key pinning ; a certificate stripped of its signature is refused as soon as the caller names a key |
| Second-preimage on the 64-bit `kid`     | A kid selects a key, never pins it ; only the full SPKI DER or its full SHA-256 pins |

The only thing that *should* be trusted is the on-chain `merkle_root`. Read it
yourself from Starknet (e.g. via `starknet-py`'s `read_contract`) and compare
to `cert.merkle_root` — this verifier proves which parts of the certificate
are bound to that root, and refuses to call "OK" what is not.

## Algorithm reference

* **JCS canonicalisation** (RFC 8785 subset): keys sorted recursively, `-0 → 0`,
  arrays preserve order, `JSON.stringify`-equivalent emission.
* **Step leaf**: `Poseidon([0x1, sha256_to_31_felt(JCS(payload)), step_marker])`,
  `step_marker` = UTF-8 `run_step` as felt. Recomputed by the verifier from a
  chain entry's `payload` when present.
* **Merkle** (position-binding, matches the on-chain `verify_inclusion`; not the
  sorted scheme of 1.0.0): leaves kept in `decision_chain` order (NOT sorted),
  padded at the end with `Poseidon([0, 0])` to the next power of 2, each parent
  `Poseidon([left, right])` in positional order.
* **Certificate hash** (signed form): `Poseidon([0x1, sha256_to_31_felt(JCS(cert
  without "signature")), cert_marker])`, `cert_marker` = UTF-8 `run_cert` as
  felt ; Ed25519 (RFC 8032, pure) over its 32 big-endian bytes ;
  `kid` = first 16 hex of SHA-256(SPKI DER).

Hex output matches `starknet.js`: lowercase, `0x`-prefixed, no leading-zero
padding (e.g. `POSEIDON_NULL_LEAF =
0x1fb7169b936dd880cb7ebc50e932a495a60e0084cdab94a681040cb4006e1a0`).

## Publishing

Releases are published to PyPI via Trusted Publishers (sigstore-signed) using
the `.github/workflows/vauban-verify-publish.yml` workflow. Tag a release with
`vauban-verify-vX.Y.Z` to trigger publication.

## License

MIT.
