mirror of
https://github.com/luxfi/chains.git
synced 2026-09-24 00:26:00 +00:00
The proof cache was keyed on tx.ID as read off the wire, plus the proof type, the proof and its public inputs — all four the sender's to choose. Copy an accepted transaction's four onto a transaction spending entirely different notes and the key matched; the hit returned nil from the cache, ahead of verifyPublicInputs, which is the only thing tying those inputs to what the transaction spends. Shielded value, backed by a proof that attests to nothing about it. The id is no longer on the wire. parseTransaction derives it, the cache is keyed on ComputeID(), and a block's id folds in ComputeID() rather than whatever id arrived — which also ends the block and vertex id malleability that one flipped byte bought. Every block and vertex id, and the first public input of every proof, now open with sha256(ChainID ‖ NetworkID). It is not on the wire, so a block from another chain has no parent here and its proofs do not verify, rather than passing a check someone can forget to write. Also on the consensus surface: Verify required height == parent+1 and nothing more, so a block whose parent was a long-accepted ancestor verified, and accepting it rewound the tip and left the height index naming an orphan to every peer that bootstraps from it. Verify now requires the parent to be the tip or a block above it, and Accept re-checks against the tip, which moves between the two. Vertex.Verify checked its transactions and nothing else — no parent, no height — so a vertex naming no parent at height 1<<40 verified, and accepting it set the store's height to 1<<40 and left the chain unable to propose again. Its decoder also ignored trailing bytes, so one logical vertex had unboundedly many encodings under one id, each of which the store writes to disk. BuildBlock skipped the ValidateBasic that Verify runs, so a transaction with an out-of-range Type — read straight off the wire — was assembled into every block and refused by every node including the proposer, and nothing evicted it. admit is now that one predicate, assembly drops what it cannot build, and a transaction names an expiry so a bounded pool drains. BuildBlock stamped an unclamped time.Now(), which is below a parent legally up to maxClockSkew ahead — a block its own Verify refuses. ParseBlock never tracked what it parsed and SetPreference discarded it, so a follower could verify the first block of a run and not the second. A genesis naming no timestamp was stamped with the wall clock, hashed into the genesis id: one genesis file, a different chain per node and per restart. VerifyBlockProof took a GPU path on accel.Available() — a second, inline copy of the Groth16 checks that consulted neither the cache nor the dummy-key refusal — so whether a block was accepted turned on whether the node had an accelerator. Reads that failed were reported as absence: the spent set answered "not spent" for a set it could not read, which is the whole double-spend gate; the state root answered "empty" at boot AND on rollback, where it also returned nil; the UTXO store answered "not found". Each now reports the failure. Deleted, none of it reachable: a node-side wallet holding plaintext spending keys behind an HTTP endpoint, with a loadAddresses that returned nil so the keys were unusable anyway; note construction and the package's only panic; an FHE processor whose verifier tested len(proof) >= 128 and whose "encryption" wrote the plaintext into the first eight bytes; a zwitness carrying a TODO; a 256-level sparse Merkle tree that computed no root and wrote its cache under a read lock; six endpoints answering "Not implemented"; a Signature field nothing verified; and eight config knobs nothing read. The endpoints that remain answer at the path the node mounts them on, rather than behind a mux that dispatches on a path it never sees. Coverage 58.9% -> 76.7% on a denominator 539 statements smaller.
167 lines
6.0 KiB
Go
167 lines
6.0 KiB
Go
// Copyright (C) 2025-2026, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package zkvm
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/luxfi/chains/zkvm/precompiles"
|
|
"github.com/luxfi/database/memdb"
|
|
"github.com/luxfi/ids"
|
|
"github.com/luxfi/log"
|
|
"github.com/luxfi/runtime"
|
|
vmcore "github.com/luxfi/vm"
|
|
)
|
|
|
|
// strict_pq_wiring_test.go proves Red's wiring task 1 deliverable (a):
|
|
// on a real Z-Chain (initialized via the production Factory -> VM ->
|
|
// Initialize path, NOT a hand-built ZConfig), the strict-PQ posture is
|
|
// active by default and BOTH switches it drives are set from the single
|
|
// config.StrictPQ field:
|
|
//
|
|
// (1) the shielded-proof verifier is strict-PQ (refuses classical), and
|
|
// (2) RegisterZKPrecompiles ran with strictPQ=true, so the classical
|
|
// Groth16 (0x80) / PLONK (0x81) verifiers are NOT registered
|
|
// (fail-closed by absence) and only STARK/FRI (0x82) exists.
|
|
//
|
|
// The Z-Chain is DEFINITIVELY strict-PQ: a genesis with no explicit
|
|
// ZConfig must yield a strict-PQ chain — the default is not permissive.
|
|
|
|
// newDefaultZKVM initializes a Z-Chain VM through the production Factory
|
|
// with genesis bytes that carry NO ZConfig, exercising the default-config
|
|
// path in Initialize.
|
|
func newDefaultZKVM(t *testing.T) *VM {
|
|
t.Helper()
|
|
logger := log.NewNoOpLogger()
|
|
rt := &runtime.Runtime{
|
|
ChainID: ids.GenerateTestID(),
|
|
NetworkID: 96369,
|
|
Log: logger,
|
|
}
|
|
|
|
// Build through the real Factory the node wires at vms.go (&zkvm.Factory{}).
|
|
f := &Factory{}
|
|
vmAny, err := f.New(logger)
|
|
if err != nil {
|
|
t.Fatalf("Factory.New: %v", err)
|
|
}
|
|
v, ok := vmAny.(*VM)
|
|
if !ok {
|
|
t.Fatalf("Factory.New returned %T, want *VM", vmAny)
|
|
}
|
|
|
|
if err := v.Initialize(context.Background(), vmcore.Init{
|
|
Runtime: rt,
|
|
DB: memdb.New(),
|
|
ToEngine: make(chan vmcore.Message, 8),
|
|
Log: logger,
|
|
Genesis: []byte(`{"timestamp":0}`), // no ZConfig ⇒ default-config path
|
|
}); err != nil {
|
|
t.Fatalf("init zkvm: %v", err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// TestZChain_DefaultIsStrictPQ proves the Z-Chain default profile is
|
|
// strict-PQ (deliverable a, the "factory/genesis sets StrictPQ" half).
|
|
func TestZChain_DefaultIsStrictPQ(t *testing.T) {
|
|
v := newDefaultZKVM(t)
|
|
if !v.StrictPQ() {
|
|
t.Fatal("Z-Chain default config must be strict-PQ (StrictPQ==true)")
|
|
}
|
|
// Which system a chain accepts is decided per proof by the strict-PQ gate,
|
|
// not by a config string nothing reads.
|
|
if err := v.proofVerifier.refuseClassicalUnderStrictPQ("groth16"); err == nil {
|
|
t.Fatal("Z-Chain default must refuse a classical proof system")
|
|
}
|
|
if err := v.proofVerifier.refuseClassicalUnderStrictPQ("stark"); err != nil {
|
|
t.Fatalf("Z-Chain default must accept STARK: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestZChain_RegistersPrecompilesStrictPQ proves the SAME StrictPQ bit
|
|
// drove RegisterZKPrecompiles(strictPQ=true): the classical Groth16/PLONK
|
|
// verifiers are absent (fail-closed) while STARK/FRI and the always-
|
|
// fail-closed stubs are present (deliverable a, the "registers strictPQ=
|
|
// true" half).
|
|
func TestZChain_RegistersPrecompilesStrictPQ(t *testing.T) {
|
|
v := newDefaultZKVM(t)
|
|
reg := v.ZKPrecompiles()
|
|
if reg == nil {
|
|
t.Fatal("Z-Chain must register ZK precompiles at Initialize")
|
|
}
|
|
|
|
// Classical (quantum-breakable) verifiers MUST be absent on a strict-PQ
|
|
// Z-Chain: a call to 0x80/0x81 hits "no precompile at address".
|
|
for _, addr := range []byte{precompiles.Groth16VerifierAddr, precompiles.PLONKVerifierAddr} {
|
|
if _, err := reg.Get(addr); err == nil {
|
|
t.Fatalf("classical verifier 0x%02x must NOT be registered on a strict-PQ Z-Chain", addr)
|
|
}
|
|
}
|
|
|
|
// The post-quantum STARK/FRI verifier + the always-fail-closed stubs
|
|
// MUST be present on every chain.
|
|
for _, addr := range []byte{precompiles.STARKVerifierAddr, precompiles.Halo2VerifierAddr, precompiles.NovaVerifierAddr} {
|
|
if _, err := reg.Get(addr); err != nil {
|
|
t.Fatalf("verifier 0x%02x must be registered on every chain, got: %v", addr, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestZChain_StrictPQ_ShieldedRefusesGroth16 is deliverable (c) at the VM
|
|
// level (also the empirical Red case): a groth16 shielded proof on the
|
|
// default (strict-PQ) Z-Chain is REFUSED with the strict-PQ error, before
|
|
// reaching any bn254 verification. The proof verifier is the one the VM
|
|
// actually constructed from its config — not a hand-built one.
|
|
func TestZChain_StrictPQ_ShieldedRefusesGroth16(t *testing.T) {
|
|
v := newDefaultZKVM(t)
|
|
tx := shieldedTx("groth16", make([]byte, 544))
|
|
err := v.proofVerifier.VerifyTransactionProof(tx)
|
|
if err == nil || err != errStrictPQClassicalForbidden {
|
|
t.Fatalf("default Z-Chain must refuse a groth16 shielded proof with errStrictPQClassicalForbidden, got: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestZChain_ExplicitNonStrictGenesisOptsOut confirms a permissive
|
|
// deployment is still possible but MUST be explicit: a genesis ZConfig
|
|
// with StrictPQ=false yields a non-strict chain. This proves the wiring
|
|
// reads the profile bit (not a hardcode) — flipping the bit flips both
|
|
// switches in lockstep.
|
|
func TestZChain_ExplicitNonStrictGenesisOptsOut(t *testing.T) {
|
|
// Build a ZConfig with StrictPQ=false and feed it as init.Config.
|
|
cfg := ZConfig{
|
|
StrictPQ: false,
|
|
MaxTxPerBlock: 100,
|
|
ProofCacheSize: 1000,
|
|
}
|
|
cfgBytes, err := json.Marshal(cfg)
|
|
if err != nil {
|
|
t.Fatalf("marshal ZConfig: %v", err)
|
|
}
|
|
|
|
logger := log.NewNoOpLogger()
|
|
rt := &runtime.Runtime{ChainID: ids.GenerateTestID(), NetworkID: 96369, Log: logger}
|
|
v := &VM{}
|
|
if err := v.Initialize(context.Background(), vmcore.Init{
|
|
Runtime: rt,
|
|
DB: memdb.New(),
|
|
ToEngine: make(chan vmcore.Message, 8),
|
|
Log: logger,
|
|
Config: cfgBytes,
|
|
Genesis: []byte(`{"timestamp":0}`),
|
|
}); err != nil {
|
|
t.Fatalf("init zkvm (non-strict): %v", err)
|
|
}
|
|
|
|
if v.StrictPQ() {
|
|
t.Fatal("explicit StrictPQ=false genesis must yield a non-strict chain")
|
|
}
|
|
// Both switches track the bit: classical verifiers ARE registered now.
|
|
if _, err := v.ZKPrecompiles().Get(precompiles.Groth16VerifierAddr); err != nil {
|
|
t.Fatalf("non-strict chain must register the classical Groth16 verifier, got: %v", err)
|
|
}
|
|
}
|