# Post-Quantum Cryptography & Hegotá

Guillotine implements Ethereum's execution-layer post-quantum (PQ) architecture end to end across the **Amsterdam (Glamsterdam)** and **Bogotá (Hegotá)** hardforks, paired with **Voltaire's** post-quantum cryptography primitives (`ML-DSA`, `ML-KEM`, and Keccak-256 `LeanSphincs`).

## Why Ethereum's PQ execution layer is frame-based

Standard Ethereum Externally Owned Accounts (EOAs) hardcode `secp256k1` ECDSA verification, which is vulnerable to Shor's algorithm on a cryptographically relevant quantum computer. Post-quantum signature schemes eliminate elliptic-curve assumptions, but introduce much larger payloads and verification costs:

| Primitive | Public Key | Signature | Verification profile |
| --- | ---: | ---: | --- |
| `secp256k1` ECDSA (legacy) | 33 / 64 B | 65 B | ~4,160 gas (`ECRECOVER`) |
| FIPS-204 `ML-DSA-44` | 1,312 B | 2,420 B | Lattice NTT (opt-in precompile `0x0200`) |
| FIPS-204 `ML-DSA-65` | 1,952 B | 3,309 B | Lattice NTT (opt-in precompile `0x0201`) |
| FIPS-204 `ML-DSA-87` | 2,592 B | 4,627 B | Lattice NTT (opt-in precompile `0x0202`) |
| Keccak-256 `LeanSphincs` V2 (`128f` / `128s`) | 32 B | 17,088 B (`128f`) / 13,168 B (`128s`) | Pure hash-based 128-bit FIPS-205-class (WOTS+ / XMSS / FORS) |

Rather than hardcoding a single lattice signature into L1 consensus, Ethereum's **Lean Ethereum** roadmap decouples accounts from `secp256k1` using three composable mechanisms—all supported in Guillotine:

1. **EIP-8141 Frame Transactions (`tx_type = 0x06`, `BOGOTA`)** — separates validation (`VERIFY`), gas sponsorship, and execution (`SENDER` / `DEFAULT`) into up to 64 ordered frames so accounts natively verify hash-based (`LeanSphincs`) or lattice (`ML-DSA`) signatures.
2. **EIP-8250 Keyed Nonces (`BOGOTA`)** — splits the 256-bit account nonce into a 192-bit parallel nonce key and 64-bit sequence counter, eliminating sequential nonce contention for PQ smart wallets and Block-STM parallel execution.
3. **EIP-8288 Proof Aggregation Hook (`BOGOTA`)** — validates compact 65-byte `(scheme, data_hash, vk_hash)` dependency claims against a block-level recursive STARK (`leanPCS` / `leanVM`) via a pluggable `ProofVerifier` host interface.

## Hardfork activation (`AMSTERDAM` and `BOGOTA`)

`OSAKA` remains `Hardfork.DEFAULT`. Enable `AMSTERDAM` (Glamsterdam) or `BOGOTA` (Hegotá) at comptime, or enable individual EIPs via `Eips.overrides`:

```zig
const evm = @import("evm");

// Full Bogotá (Hegotá) EVM: EIP-8141 Frame Transactions, EIP-8250 Keyed Nonces, EIP-8288 hook
const BogotaEvm = evm.Evm(.{
    .eips = evm.Eips{ .hardfork = .BOGOTA },
});

// Or activate EIP-8141 selectively on Osaka via overrides
const CustomPqEvm = evm.Evm(.{
    .eips = evm.Eips{
        .hardfork = .OSAKA,
        .overrides = &.{
            .{ .eip = 8141, .enabled = true },
            .{ .eip = 8250, .enabled = true },
            .{ .eip = 8288, .enabled = true },
        },
    },
});
```

### Hardfork progression

| Hardfork | Key Execution EIPs |
| --- | --- |
| `OSAKA` (default) | `CLZ` (EIP-7939), `P256VERIFY` (EIP-7951), EIP-7823/7825/7883 |
| `AMSTERDAM` | Glamsterdam: `EIP-7928` (Block-Level Access Lists), `EIP-8037` (state creation gas accounting), `EIP-2780`, `EIP-7843` |
| `BOGOTA` | Hegotá: `EIP-8141` (Frame Transactions `0x06`), `EIP-8250` (Keyed Nonces), `EIP-8288` (Recursive PQ/STARK proof aggregation hook) |

## EIP-8141 Frame Transactions (`tx_type = 0x06`)

`evm.tx_runner.executeFrameTx` (and `evm.tx_runner.executeTx` when `tx.tx_type == 6`) executes up to 64 ordered frames in one atomic transaction envelope:

| Mode | Value | Semantics |
| --- | ---: | --- |
| `DEFAULT` | `0` | Standard call (deployment, paymaster post-op, or auxiliary step) |
| `VERIFY` | `1` | Strict `STATICCALL` isolation bounded by `MAX_VERIFY_GAS`; validates custom/PQ signatures and terminates with `APPROVE` |
| `SENDER` | `2` | Executes with `msg.sender = tx.sender`; rejected unless a prior `VERIFY` frame executed `APPROVE` with execution scope |

### Frame opcodes (active on `BOGOTA` or EIP-8141 override)

| Hex | Opcode | Stack | Gas | Semantics |
| --- | --- | --- | --- | --- |
| `0xAA` | `APPROVE` | `3 → 0` | `0` + mem | Halt current `VERIFY` frame and set tx approval scope (`0` = execution, `1` = payment, `2` = both). Double approval fails the frame. |
| `0xB0` | `TXPARAMLOAD` | `1 → 1` | `2` | Load 32-byte transaction parameter by index (tx type, sender, nonce, gas fees, frame count, sig hash, approval flags). |
| `0xB1` | `TXPARAMSIZE` | `1 → 1` | `2` | Byte length of the requested transaction parameter. |
| `0xB2` | `TXPARAMCOPY` | `4 → 0` | `3 + 3×W` | Copy transaction parameter bytes into memory. |
| `0xB3` | `FRAMEDATALOAD` | `2 → 1` | `3` | Load 32 bytes from frame `i`'s input payload at `offset` (`VERIFY` frames are masked outside their own frame). |
| `0xB4` | `FRAMEDATACOPY` | `4 → 0` | `3 + 3×W` | Copy bytes from frame `i`'s input payload into memory. |
| `0xB5` | `FRAMEPARAM` | `2 → 1` | `2` | Read metadata for frame `i` (`0` = mode, `1` = target, `2` = gas limit, `3` = status). |

Every frame opcode is implemented across Tier 1 fused dispatch and `MinimalEvm` for lockstep differential verification, with `APPROVE` authority and monotonicity formally proved via Bitwuzla/Z3 SMT.

```zig
const std = @import("std");
const evm = @import("evm");
const voltaire = @import("voltaire");

const BogotaEvm = evm.Evm(.{ .eips = .{ .hardfork = .BOGOTA } });

const frames = [_]evm.tx_runner.TxFrame{
    .{
        .mode = .verify,
        .target = pq_wallet_address,
        .gas_limit = 500_000,
        .data = lean_sphincs_signature_bytes,
    },
    .{
        .mode = .sender,
        .target = target_contract,
        .gas_limit = 150_000,
        .data = swap_calldata,
    },
};

var result = try evm.tx_runner.executeTx(BogotaEvm, allocator, &vm, .{
    .tx_type = 6,
    .from = pq_wallet_address,
    .nonce = 0,
    .nonce_key = 1, // EIP-8250 parallel nonce lane
    .gas_limit = 700_000,
    .max_fee_per_gas = 20_000_000_000,
    .max_priority_fee_per_gas = 1_000_000_000,
    .frames = &frames,
});
defer result.deinit(allocator);
```

## Opt-in PQ Precompile Pack (`evm.pq_precompiles`)

Mainnet L1 hardforks (`OSAKA`, `AMSTERDAM`, `BOGOTA`) deliberately keep experimental lattice precompiles **disabled by default** to match Ethereum L1 consensus. For Layer 2 rollups (OP Stack, Base, Arbitrum), enterprise chains, and wallet benchmarks, Guillotine ships an opt-in precompile pack built on Voltaire's pure-Zig FIPS-204 `ML-DSA`, EIP-7885 `NTT`, and Keccak-256 `LeanSphincs` implementations:

| Address | Name | Spec / Algorithm | Base Gas |
| --- | --- | --- | ---: |
| `0x0200` | `MLDSA44_VERIFY` | NIST FIPS-204 ML-DSA-44 (`EIP-8051` / `EIP-8355`) | `20,000 + 12/word` |
| `0x0201` | `MLDSA65_VERIFY` | NIST FIPS-204 ML-DSA-65 (`EIP-8355`) | `28,000 + 12/word` |
| `0x0202` | `MLDSA87_VERIFY` | NIST FIPS-204 ML-DSA-87 (`EIP-8355`) | `38,000 + 12/word` |
| `0x0203` | `NTT` | Experimental 2-op negacyclic NTT (`NTT_FWD` / `NTT_INV` over $\mathbb{Z}\_q\[X]/(X^n+1)$, Falcon $q=12289, n=512$ & ML-DSA $q=8380417, n=256$) | `1,000 + 16/coeff` |
| `0x0204` | `LEAN_SPHINCS_VERIFY` | Keccak-256 stateless hash signature verification (`LeanSphincs` V2 128-bit) | `150,000 + 6/word` |

Mount the full pack via `EvmConfig.precompile_overrides`:

```zig
const evm = @import("evm");

const PqL2Evm = evm.Evm(.{
    .eips = .{ .hardfork = .BOGOTA },
    .precompile_overrides = &evm.pq_precompiles.all_overrides,
});
```

All ML-DSA, ML-KEM, and LeanSphincs implementations run in pure Zig (`std.crypto.sign.mldsa`, `std.crypto.kem.ml_kem`, and Voltaire's hardware-accelerated `keccak_asm` / `keccak_fast`), so they work identically across native x86\_64/ARM64 binaries, C ABI/FFI consumers, and `wasm32` targets.

## EIP-8288 Pluggable `ProofVerifier` Hook

When blocks aggregate post-quantum signatures or privacy STARKs off-chain via `EIP-8288`, individual transactions carry 65-byte `Eip8288Claim` entries `(scheme, data_hash, vk_hash)`. Guillotine keeps proof-system dependencies decoupled from the EVM core through `evm.tx_runner.ProofVerifier`:

```zig
const verifier = evm.tx_runner.ProofVerifier{
    .context = my_lean_pcs_ctx,
    .verify = myLeanPcsVerifyFn,
};

var result = try evm.tx_runner.executeTxWithOptions(BogotaEvm, allocator, &vm, tx, .{
    .proof_verifier = verifier,
});
```

When a transaction declares `eip8288_claims` and a `proof_verifier` is attached, `tx_runner` verifies the aggregated claim proof before executing `SENDER` frames and fails admission cleanly (`error.InvalidProof`) if verification returns `false` or no verifier is configured.

## Keccak-256 Fast Paths & Formal Verification

Because Ethereum's `LeanSphincs` and hash-based STARKs rely on **Keccak-256** (rather than algebraic hashes like Poseidon), on-chain PQ verification is dominated by small 32-byte and 64-byte `KECCAK256` calls inside WOTS+ and Merkle loops:

* **Small-input `KECCAK256` fast path (`src/instructions/handlers_keccak.zig`):** Inputs ≤ 64 bytes bypass general memory slice dispatch and hash directly into a stack-resident Keccak state.
* **Formal verification (`verify/smt/proofs/eip8141_frame_approval_invariants.smt2`):** Bitwuzla/Z3 SMT proofs verify the EIP-8141 approval state machine: `VERIFY` frames cannot mutate state, `SENDER` frames cannot execute without prior execution approval, and double-approval transitions are unreachable.
