import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {gunzipSync} from 'node:zlib'; import {inspectRngSourceEvidence,rngSourcePhysicalPlan} from './rng-source-proof.mjs'; const recorded=JSON.parse(gunzipSync(readFileSync(new URL('../../kernel/tests/os/fixtures/rng-source-physical-v1.bin',import.meta.url)))); assert.equal(recorded.schema,'turkos-rng-source-physical-fixture/v1'); function fixture(){ const symbols=structuredClone(recorded.symbols),captures=[],qmp=[],timeline=[],wholeRsp=[],files=new Map();let requestId=0,rspId=0,owned=[];const compiled=Object.fromEntries(Object.entries(recorded.compiled).map(([k,b])=>[k,Buffer.from(b,'base64')])); for(let stage=1;stage<=12;stage++){ const data=Object.fromEntries(Object.entries(recorded.snapshots[stage-1]).map(([k,v])=>[k,Buffer.from(v,'base64')])); const plan=rngSourcePhysicalPlan(data,symbols); const capture={stage,captures:[],dumps:{},gateAddress:symbols.OS_K1905_CAPTURE_GATE.address,rsp:[]}; const add=(execute,args,result={})=>{const id=++requestId;qmp.push({direction:'tx',message:{execute,...(args?{arguments:args}:{}),id}},{direction:'rx',message:{return:result,id}}); timeline.push({stage:19050+stage,kind:'qmp',id,execute});return id;}; add('stop');add('query-status',undefined,{status:'paused'}); for(const [i,[name,bytes]] of Object.entries(data).entries()){ const {symbol,val}=plan[i];assert.equal(plan[i].name,name); const path=`stage-${stage}/${name}.bin`,filename='/tmp/'+path,size=bytes.length; files.set(path,bytes);capture.dumps[name]={path};capture.captures.push({name,val,size,filename,requestId:add('pmemsave',{val,size,filename}),...(symbol?{symbol}:{})}); } const gate=capture.gateAddress.toString(16); for(const [tx,rx] of [[`m${gate},1`,'00'],[`M${gate},1:01`,'OK'],[`m${gate},1`,'01']]){ const id='K1080-BATCH:'+ ++rspId; for(const [direction,payload] of [['tx',tx],['rx',rx]]){const checksum=Buffer.from(payload).reduce((s,v)=>(s+v)&255,0).toString(16).padStart(2,'0'); const row={direction,id,payload,packet:`$${payload}#${checksum}`};capture.rsp.push(row);wholeRsp.push(row);timeline.push({stage:19050+stage,kind:'rsp',id,direction});} } add('cont');add('query-status',undefined,{status:'running'});captures.push(capture); } return {compiled,captures,memoryMiB:256,symbols,qmp,timeline,wholeRsp,files,bootId:recorded.bootId,readArtifact:i=>files.get(i.path)}; } assert.equal(inspectRngSourceEvidence(fixture()).rngCompletions,4); const flip=(f,s,name,index)=>{f.files.get(`stage-${s}/${name}.bin`)[index]^=1;}; const controls=[['wrong boot',f=>f.bootId='0000000000000000'],['foreign QMP address',f=>f.qmp.find(r=>r.message.execute==='pmemsave').message.arguments.val+=4096],['missing gate reply',f=>f.captures[2].rsp.splice(3,1)],['changed compiled layout',f=>f.compiled.sourceLayout[0]^=1],['changed fixture',f=>f.compiled.rngFixture[0]^=1]]; for(const stage of[1,2,3,7,9,10,11,12])for(const at of[...Array.from({length:13},(_,i)=>i*8),104,105,135,136,167])controls.push(['source '+stage+' byte '+at,f=>flip(f,stage,'source',at)]); for(const stage of[1,2,3,7,11,12])for(const index of[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,24,30,32,33,34,35,44,45,46,55,56,60,61,62,63,64,65,66,67,68,69,70,71,73,100,101,102,125,126,127])controls.push(['metadata '+stage+' word '+index,f=>flip(f,stage,'evidence',index*8)]); for(const stage of[1,3,7,10,11,12])for(const name of['output','nonceMask','sourceLayout','authorityLayout','owners'])controls.push(['global '+stage+'/'+name,f=>flip(f,stage,name,0)]); for(const stage of[2,3,5,7,9,11])for(const name of['frame0','frame1','frame2'])for(const at of[0,2,8,16,64,4095])controls.push(['ring '+stage+'/'+name+'/'+at,f=>{if(name==='frame2'&&at===0)f.files.get(`stage-${stage}/${name}.bin`).writeUInt16LE(2,0);else flip(f,stage,name,at);}]); for(const stage of[2,3,5,7,9,11])for(let page=3;page<7;page++)for(const at of[0,31,32,33,4095])controls.push(['buffer '+stage+'/'+page+'/'+at,f=>flip(f,stage,'frame'+page,at)]); for(const stage of[1,3,7,11,12])for(const at of[344,352,360,368,369,536,568])controls.push(['registry '+stage+'/'+at,f=>flip(f,stage,'registry',at)]); for(let page=0;page<7;page++)controls.push(['dirty returned page '+page,f=>flip(f,12,'frame'+page,4095)]); for(const[name,change]of controls){const f=fixture();change(f);assert.throws(()=>inspectRngSourceEvidence(f),undefined,name);} console.log(JSON.stringify({status:'RNG_SOURCE_PROOF_TESTS_PASSED',positive:1,forgedControlsRejected:controls.length}));