import assert from 'node:assert/strict'; import {inspectPhysicalCheckpoint} from './physical-checkpoint-proof.mjs'; import {rngSourceSymbols,rngSourceGlobals} from './rng-source-capture.mjs'; import {rngSourceFixture} from './rng-source-fixture-data.mjs'; const W=b=>Array.from({length:b.length/8},(_,i)=>b.readBigUInt64LE(i*8)),u=(b,at)=>b.readBigUInt64LE(at),word=(b,i)=>u(b,i*8),mask=0x0000fffffffff000n,zero=b=>assert.ok(b.every(v=>v===0),'zero bytes'); function ledger(raw,l){ assert.equal(raw.length,l[0]);assert.equal(l[4],64);assert.equal(l[10],80);assert.equal(l[16],64);const free=[],live=new Map(),reserved=[]; const count=Number(u(raw,l[1]+l[3]));assert.ok(count>0&&count<=64);let pages=0n; for(let i=0;ifree[i-1][1]);free.push([a,b]);pages+=(b-a)/4096n;} for(let i=0;iaddress>=a&&address=0x40000000n&&address+BigInt(size)<=end);assert.ok(size>224&&size<=4096);assert.equal(raw.length,size);assert.equal(stride,32);assert.equal(stateBytes,1);assert.equal(count,7);assert.ok(recordsAt>=0&&recordsAt+stride*count<=size);assert.ok(idAt>=0&&idAt+8<=size&&backendAt>=0&&backendAtArray.from({length:n},(_,i)=>at+i));assert.ok(positions.every(v=>v>=0&&v({name,symbol,val:symbols[symbol].address,size:symbols[symbol].size})); plan.push({name:'registry',val:Number(e[44]),size:Number(e[45])},{name:'authority',val:Number(e[100]),size:Number(e[101])},{name:'kernelRoot',val:Number(e[6]),size:4096},{name:'kernelL2',val:Number(u(s.kernelRoot,8)&mask),size:4096}); for(let i=0;i<7;i++)plan.push({name:'frame'+i,val:Number(e[24+i]),size:4096});return plan; } export function inspectRngSourceSnapshots(snapshots,memoryMiB,bootId,symbols,compiled){ assert.equal(snapshots.length,12);assert.ok([256,512,1024].includes(memoryMiB));assert.match(bootId,/^[a-f0-9]{16}$/); assert.deepEqual(compiled.rngFixture,rngSourceFixture()); const layout=[168,0,104,104,...Array.from({length:13},(_,i)=>i*8)];assert.deepEqual(W(compiled.sourceLayout).map(Number),layout); const all=snapshots.map(s=>W(s.evidence)),first=all[0],last=all[11],pages=first.slice(24,31),end=0x40000000n+BigInt(memoryMiB)*1048576n,al=W(compiled.authorityLayout).map(Number); assert.equal(first.length,128);assert.equal(al[0],6080);assert.equal(first[101],6080n);assert.ok(first[3]>7n&&first[3]<=BigInt(memoryMiB)*256n); assert.ok(first[7]>=0xa000000n&&first[7]<0xa004000n&&first[7]%512n===0n);assert.equal(first[13]&(1n<<32n),1n<<32n); assert.equal(first[60],BigInt(symbols.OS_K1905_SOURCE.address));assert.equal(first[61],168n);assert.equal(first[62],BigInt(symbols.OS_K1905_SOURCE_LAYOUT.address)); for(const p of [first[6],...pages])assert.ok(p%4096n===0n&&p>=0x40000000n&&p+4096n<=end);assert.equal(new Set([first[6],...pages]).size,8); const initial=ledger(snapshots[0].authority,al),final=ledger(snapshots[11].authority,al);assert.equal(initial.pages,first[3]-7n);assert.equal(final.pages,first[3]);assert.ok(initial.owner>0);assert.equal(initial.owner,final.owner);assert.equal(initial.generation,final.generation);assert.deepEqual(initial.reserved,final.reserved); for(const p of pages){assert.ok(initial.live.has(p));assert.ok(!final.live.has(p));assert.ok(final.free.some(([a,b])=>p>=a&&p!pages.includes(p)),[...final.live]); const intervals=[...initial.free,...pages.map(p=>[p,p+4096n])].sort((a,b)=>a[0]=previous[1]);if(a===previous[1]){previous[1]=b;continue;}}merged.push([a,b]);}assert.deepEqual(merged,final.free); const f=last[63];assert.ok(f>0n);let lastClock=0n;for(let i=0;i<4;i++){const begin=last[64+i*2],completed=last[65+i*2];assert.ok(begin>=lastClock&&completed>=begin&&completed-begin<=10n*f);lastClock=completed;} for(let i=0;i<12;i++){ const stage=i+1,s=snapshots[i],w=all[i],done=Math.min(4,Math.floor((stage-1)/2)),pending=stage<=8&&stage%2===0,operation=done+(pending?1:0),live=stage<12,active=stage>=2&&stage<=10,phase=stage===1?1:stage<=10?2:stage===11?3:4,expected=Array(128).fill(0n); for(const[k,v]of [[0,1905],[1,stage],[2,BigInt('0x'+bootId)],[3,first[3]],[4,first[3]-(live?7n:0n)],[5,live?0:first[3]],[6,first[6]],[7,first[7]],[8,1905],[9,live?7:0],[10,live?7:0],[11,active?15:0],[12,active?1:0],[13,first[13]],[14,1n<<32n],[15,phase],[16,done],[17,stage>=10?6:0],[18,stage===12?7:0],[44,first[44]],[60,first[60]],[61,168],[62,first[62]],[63,stage>=2?f:0],[73,BigInt(symbols.OS_K1905_OUTPUT.address)],[100,first[100]],[101,6080]])expected[k]=BigInt(v); pages.forEach((p,j)=>expected[24+j]=p);for(let j=0;jexpected[102+j]=BigInt(v));assert.deepEqual(w,expected,'RNG metadata '+stage); assert.deepEqual(s.sourceLayout,compiled.sourceLayout);assert.deepEqual(s.authorityLayout,compiled.authorityLayout);assert.equal(word(s.nonceMask,0),0x3c0n);assert.deepEqual(s.output,Buffer.alloc(64,0xa5));assert.deepEqual(s.kernelRoot,snapshots[0].kernelRoot);assert.deepEqual(s.kernelL2,snapshots[0].kernelL2);if(live)assert.deepEqual(s.authority,snapshots[0].authority); const raw=Buffer.alloc(168);[1,first[7],1905,operation,pending?1:0,pending?32:0,done,done*32,done&&stage<11?32:0,Math.max(0,done-2),done===4?1:0,done>=3?1:0,0].forEach((v,j)=>raw.writeBigUInt64LE(BigInt(v),j*8)); if(done&&stage<11)compiled.rngFixture.subarray((done-1)*32,done*32).copy(raw,104);assert.deepEqual(s.source,raw,'raw source '+stage); const owners=Buffer.alloc(224);if(live)pages.forEach((p,j)=>[p,1905n,1n,1n].forEach((v,k)=>owners.writeBigUInt64LE(v,(j*4+k)*8)));assert.deepEqual(s.owners,owners);registry(s.registry,first.slice(45,57).map(Number),first[44],end,1905,1905,pages,live); if(stage===1||stage===12){for(let j=0;j<7;j++)zero(s['frame'+j]);continue;} const desc=Buffer.alloc(4096),avail=Buffer.alloc(4096),used=Buffer.alloc(4096);avail.writeUInt16LE(done,2);assert.ok([0,1].includes(s.frame2.readUInt16LE(0)));used.writeUInt16LE(s.frame2.readUInt16LE(0));used.writeUInt16LE(done,2); for(let j=0;j<4;j++){ desc.writeBigUInt64LE(pages[3+j],j*16);desc.writeUInt32LE(32,j*16+8);desc.writeUInt16LE(2,j*16+12);avail.writeUInt16LE(j,4+j*2); if(j[k,readArtifact(v)]));const expected=rngSourcePhysicalPlan(data,symbols),stage=19051+i; const snapshot=inspectPhysicalCheckpoint({capture,expected,stage,qmp,timeline,wholeRsp,readArtifact,gateAddress:symbols.OS_K1905_CAPTURE_GATE.address}); const steps=timeline.filter(r=>r.stage===stage),start=qmp.findIndex(r=>r.direction==='tx'&&r.message.id===steps[0].id),end=qmp.findIndex(r=>r.direction==='rx'&&r.message.id===steps.at(-1).id);assert.ok(start>previousQmp);previousQmp=end; const rsp=capture.rsp.filter(r=>r.id!==null),rs=wholeRsp.findIndex(r=>r.id===rsp[0].id&&r.direction==='tx'),re=wholeRsp.findIndex(r=>r.id===rsp.at(-1).id&&r.direction==='rx');assert.ok(rs>previousRsp);previousRsp=re;snapshots.push(snapshot); } return inspectRngSourceSnapshots(snapshots,memoryMiB,bootId,symbols,compiled); }