/** * GridStamp v1.1.0 — Production-Grade 100K Stress Test * * Proves GridStamp handles enterprise fleet scale: * - 200 concurrent robots * - 100,000+ mixed spatial operations * - Proof chain integrity verification across full fleet * - 100% replay detection, 100% HMAC tamper detection * - Settlement gating under load * - Spatial code computation at scale * * Workload mix (modeled on real fleet operations): * - 80,000 spatial proofs (one per delivery / route checkpoint) * - 5,000 settlements (billable events) * - 6,000 integrity verifications (auditor spot-checks) * - 4,000 replay/spoof attempts (must ALL be caught) * - 2,000 HMAC tamper attempts (must ALL be caught) * - 3,000 spatial code computations * * Target: 100,000 operations, 100% security, >500 ops/sec */ import { describe, it, expect } from 'vitest'; import { generateSpatialProof, verifySpatialProofIntegrity, createSettlement, } from '../../src/verification/spatial-proof.js'; import { ReplayDetector, FrameIntegrityChecker, } from '../../src/antispoofing/detector.js'; import { ProofChain, hashProof } from '../../src/verification/proof-chain.js'; import { signFrame, generateNonce } from '../../src/utils/crypto.js'; import { computeSpatialCode, PlaceCellPopulation, GridCellSystem, } from '../../src/memory/place-cells.js'; import type { CameraFrame, Pose, RenderedView } from '../../src/types/index.js'; const SECRET = 'a]9#kL2$mP7xQ4vB8nR1wF5yH3jT6dG0'; function makePose(x: number, y: number, ts: number): Pose { return { position: { x, y, z: 0 }, orientation: { w: 1, x: 0, y: 0, z: 0 }, timestamp: ts, }; } function makeFrame(seed: number, seq: number, ts: number): CameraFrame { const w = 32; const h = 32; const rgb = new Uint8Array(w * h * 3); for (let i = 0; i < rgb.length; i++) rgb[i] = (i * 17 + seed * 31) % 256; const depth = new Float32Array(w * h); for (let i = 0; i < depth.length; i++) depth[i] = 1 + (seed % 5); return { id: generateNonce(8), timestamp: ts, rgb, width: w, height: h, depth, pose: makePose(seed % 200, (seed * 7) % 200, ts), hmac: signFrame(rgb, ts, seq, SECRET), sequenceNumber: seq, }; } function makeIdenticalRender(frame: CameraFrame): RenderedView { return { rgb: frame.rgb, depth: frame.depth!, width: frame.width, height: frame.height, pose: frame.pose, renderTimeMs: 1, }; } function pct(arr: number[], p: number): number { if (arr.length === 0) return 0; const sorted = [...arr].sort((a, b) => a - b); const idx = Math.min(sorted.length - 1, Math.floor((p / 100) * sorted.length)); return sorted[idx]; } describe('Production 100K Real-World Stress', () => { it( 'executes 100,000 mixed spatial-proof ops across 200 robots with 0 false negatives', async () => { const ROBOT_COUNT = 200; const PROOFS_PER_ROBOT = 400; // 200 * 400 = 80,000 proofs const SETTLE_RATE = 0.0625; // ~5,000 settlements const VERIFY_RATE = 0.075; // ~6,000 verifications const SPOOF_EVERY = 20; // 4,000 spoof attempts const HMAC_CHECK_EVERY = 40; // 2,000 HMAC checks const SPATIAL_CODE_EVERY = 27; // ~3,000 spatial code computes const placeCells = new PlaceCellPopulation(1.5); placeCells.coverRegion({ x: 0, y: 0, z: 0 }, { x: 200, y: 200, z: 0 }, 4.0, 4.0); const gridCells = new GridCellSystem(0.5, Math.SQRT2, 4, 16); const proofLatencies: number[] = []; let totalProofs = 0; let totalSettlements = 0; let totalVerifications = 0; let totalReplayAttempts = 0; let replayAttemptsCaught = 0; let totalHmacChecks = 0; let hmacTamperingCaught = 0; let totalSpatialCodes = 0; let verificationsPassed = 0; let totalProofChainLinks = 0; let proofChainsValid = 0; const start = performance.now(); const work = Array.from({ length: ROBOT_COUNT }, async (_, r) => { const replayDetector = new ReplayDetector(30); const integrityChecker = new FrameIntegrityChecker(SECRET, 30); const proofChain = new ProofChain(); let lProofs = 0; let lSettle = 0; let lVerify = 0; let lVerifyOk = 0; let lReplay = 0; let lReplayCaught = 0; let lHmac = 0; let lHmacCaught = 0; let lSpatial = 0; const robotId = `robot-${r}`; const baseTs = Date.now(); for (let j = 0; j < PROOFS_PER_ROBOT; j++) { const seed = r * 1000 + j; const ts = baseTs + j * 50; const seq = j + 1; const frame = makeFrame(seed, seq, ts); const render = makeIdenticalRender(frame); const claimedPose = makePose(seed % 200, (seed * 7) % 200, ts); // 1. SPATIAL PROOF const t0 = performance.now(); const proof = generateSpatialProof( robotId, claimedPose, frame, render, `merkle-${r}`, [], SECRET, ); proofLatencies.push(performance.now() - t0); lProofs++; // 1b. PROOF CHAIN — append every proof (first has no prevProofHash) if (j === 0) { // Genesis — no prev hash proofChain.append(proof); } else { // Link to previous const linkedProof = { ...proof, prevProofHash: proofChain.tipHash }; proofChain.append(linkedProof); } // 2. VERIFY proof integrity (~7.5%) if (Math.random() < VERIFY_RATE) { const result = verifySpatialProofIntegrity(proof, SECRET); lVerify++; if (result.valid) lVerifyOk++; } // 3. SETTLEMENT (~6.25%) if (Math.random() < SETTLE_RATE && proof.passed) { const settlement = createSettlement( proof, 10.0, 'USD', `merchant-${j % 10}`, SECRET, ); expect(settlement).toBeDefined(); lSettle++; } // 4. REPLAY ATTACK every 20th iter if (j % SPOOF_EVERY === SPOOF_EVERY - 1) { const attackDetector = new ReplayDetector(30); const cleanFrame = { ...frame, sequenceNumber: 100, timestamp: ts }; attackDetector.check(cleanFrame); const replayedFrame = { ...frame, sequenceNumber: 100, timestamp: ts + 50 }; const threats = attackDetector.check(replayedFrame); lReplay++; if (threats.some((t) => t.type === 'replay')) lReplayCaught++; } // 5. HMAC TAMPER every 40th iter if (j % HMAC_CHECK_EVERY === HMAC_CHECK_EVERY - 1) { const tampered: CameraFrame = { ...frame, rgb: new Uint8Array(frame.rgb.length).fill(0xff), }; const result = integrityChecker.check(tampered); lHmac++; if (!result.hmacValid) lHmacCaught++; } // 6. SPATIAL CODE every 27th iter if (j % SPATIAL_CODE_EVERY === SPATIAL_CODE_EVERY - 1) { const code = computeSpatialCode(claimedPose.position, placeCells, gridCells); expect(code).toBeDefined(); lSpatial++; } } // Verify this robot's full proof chain const chainResult = proofChain.verify(); return { lProofs, lSettle, lVerify, lVerifyOk, lReplay, lReplayCaught, lHmac, lHmacCaught, lSpatial, chainLength: proofChain.length, chainValid: chainResult.valid, chainIntegrity: chainResult.chainIntegrity, }; }); const results = await Promise.all(work); for (const r of results) { totalProofs += r.lProofs; totalSettlements += r.lSettle; totalVerifications += r.lVerify; verificationsPassed += r.lVerifyOk; totalReplayAttempts += r.lReplay; replayAttemptsCaught += r.lReplayCaught; totalHmacChecks += r.lHmac; hmacTamperingCaught += r.lHmacCaught; totalSpatialCodes += r.lSpatial; totalProofChainLinks += r.chainLength; if (r.chainValid) proofChainsValid++; } const elapsed = performance.now() - start; const totalOps = totalProofs + totalSettlements + totalVerifications + totalReplayAttempts + totalHmacChecks + totalSpatialCodes; const opsPerSec = Math.round(totalOps / (elapsed / 1000)); const p50 = pct(proofLatencies, 50); const p95 = pct(proofLatencies, 95); const p99 = pct(proofLatencies, 99); console.log(''); console.log('══════════════════════════════════════════════════════════════════════'); console.log(' GridStamp v1.1.0 — 100K PRODUCTION STRESS REPORT'); console.log('══════════════════════════════════════════════════════════════════════'); console.log(` Wall clock: ${(elapsed / 1000).toFixed(2)}s`); console.log(` Total operations: ${totalOps.toLocaleString()}`); console.log(` Throughput: ${opsPerSec.toLocaleString()} ops/sec`); console.log(''); console.log(' Operation breakdown:'); console.log(` spatial proofs: ${totalProofs.toLocaleString()}`); console.log(` settlements: ${totalSettlements.toLocaleString()}`); console.log(` verifications: ${totalVerifications.toLocaleString()}`); console.log(` replay attempts: ${totalReplayAttempts.toLocaleString()}`); console.log(` HMAC checks: ${totalHmacChecks.toLocaleString()}`); console.log(` spatial codes: ${totalSpatialCodes.toLocaleString()}`); console.log(''); console.log(' Latency (spatial proof generation):'); console.log(` p50: ${p50.toFixed(2)} ms`); console.log(` p95: ${p95.toFixed(2)} ms`); console.log(` p99: ${p99.toFixed(2)} ms`); console.log(''); console.log(' Security gates:'); console.log( ` replay caught: ${replayAttemptsCaught}/${totalReplayAttempts} (${( (replayAttemptsCaught / Math.max(1, totalReplayAttempts)) * 100 ).toFixed(1)}%)`, ); console.log( ` HMAC tamper caught: ${hmacTamperingCaught}/${totalHmacChecks} (${( (hmacTamperingCaught / Math.max(1, totalHmacChecks)) * 100 ).toFixed(1)}%)`, ); console.log( ` legit verify pass: ${verificationsPassed}/${totalVerifications} (${( (verificationsPassed / Math.max(1, totalVerifications)) * 100 ).toFixed(1)}%)`, ); console.log(''); console.log(' Proof chains:'); console.log(` robots verified: ${proofChainsValid}/${ROBOT_COUNT} (${((proofChainsValid / ROBOT_COUNT) * 100).toFixed(1)}%)`); console.log(` total chain links: ${totalProofChainLinks.toLocaleString()}`); console.log('══════════════════════════════════════════════════════════════════════'); console.log(''); // SLO gates expect(replayAttemptsCaught).toBe(totalReplayAttempts); expect(hmacTamperingCaught).toBe(totalHmacChecks); expect(verificationsPassed).toBe(totalVerifications); expect(proofChainsValid).toBe(ROBOT_COUNT); expect(totalProofChainLinks).toBe(ROBOT_COUNT * PROOFS_PER_ROBOT); expect(opsPerSec).toBeGreaterThan(200); expect(totalOps).toBeGreaterThan(90_000); expect(p99).toBeLessThan(500); }, 600_000, // 10min timeout ); });