// Non-radio checks for the worked arithmetic and byte examples in the article.
import assert from 'node:assert/strict';
import crypto from 'node:crypto';
const hex=s=>Buffer.from(s.replace(/\s/g,''),'hex');
const hmac=(key,data)=>crypto.createHmac('sha1',key).update(data).digest();
const pmk=crypto.pbkdf2Sync('password','IEEE',4096,32,'sha1');
assert.equal(pmk.toString('hex'),'f42c6fc52df0ebef9ebb4b90b38a5f902e83fe1b135a70e23aed762e9710a12e');
const ap=hex('001122334455'),sta=hex('66778899aabb');
const anonce=Buffer.from(Array.from({length:32},(_,i)=>i));
const snonce=Buffer.from(Array.from({length:32},(_,i)=>i+32));
const context=Buffer.concat([ap,sta,anonce,snonce]);
const ptk=Buffer.concat([0,1,2].map(c=>hmac(pmk,Buffer.concat([Buffer.from('Pairwise key expansion\0'),context,Buffer.from([c])])))).subarray(0,48);
assert.equal(ptk.toString('hex'),'85c98eca56145629359ac8830bb66a59c5562d473fddcb4eee9ce4de54e1cb1a12cdd4448325c84079abcd76b1b89f8f');
const plain=Buffer.from(Array.from({length:23},(_,i)=>i+8));
const key=hex('c0c1c2c3c4c5c6c7c8c9cacbcccdcecf'),nonce=hex('00000003020100a0a1a2a3a4a5');
const enc=crypto.createCipheriv('aes-128-ccm',key,nonce,{authTagLength:8});
enc.setAAD(hex('0001020304050607'),{plaintextLength:plain.length});
const ciphertext=Buffer.concat([enc.update(plain),enc.final()]);
assert.equal(ciphertext.toString('hex'),'588c979a61c663d2f066d0c2c0f989806d5f6b61dac384');
assert.equal(enc.getAuthTag().toString('hex'),'17e8d12cfdf926e0');
function crc32(bytes,final=true){let r=0xffffffff;for(const b of bytes){r^=b;for(let i=0;i<8;i++)r=(r>>>1)^((r&1)?0xedb88320:0);}return (final?r^0xffffffff:r)>>>0;}
assert.equal(crc32(Buffer.from('123456789')),0xcbf43926);
assert.equal(crc32(Buffer.concat([Buffer.from('123456789'),hex('2639f4cb')]),false),0xdebb20e3);
const barker=[1,-1,1,1,-1,1,1,1,-1,-1,-1];
for(let shift=1;shift<11;shift++)assert(Math.abs(barker.slice(shift).reduce((s,x,i)=>s+x*barker[i],0))<=1);
assert.deepEqual(Array.from({length:20},(_,s)=>Math.floor(11*s/20)),[0,0,1,1,2,2,3,3,4,4,5,6,6,7,7,8,8,9,9,10]);
const bits=[...hex('a0f3')].flatMap(b=>Array.from({length:8},(_,i)=>(b>>>i)&1));
assert.equal(bits.reduce((a,b)=>(a<<1)|b,0),0x05cf);
assert.equal(20*(192+8*14),6080);
assert.equal(255/20,12.75);
assert.equal(1000*1000-999*1001,1);
assert.equal(39+14,53);
assert.equal(3421-2956,465);assert.equal(6398-4710,1688);
assert.equal(1327-631,696);assert.equal(5397-5185,212);
const rsn=hex('30140100000fac040100000fac040100000fac020000');
assert.equal(rsn.length,22);assert.equal(rsn[1],20);
const kde=Buffer.concat([hex('dd16000fac010100'),Buffer.alloc(16)]);
assert.equal(rsn.length+kde.length+2,48);
assert.equal(99+48+8,155);
const offer=hex('3501023604c0a82c01330400000e100104ffffff000304c0a82c010604c0a82c011c04c0a82cff3a04000007083b0400000c4eff');
let i=0,codes=[];while(offer[i]!==255){const c=offer[i++],n=offer[i++];codes.push(c);assert(i+n<=offer.length);i+=n;}assert.equal(i,offer.length-1);
assert.deepEqual(codes,[53,54,51,1,3,6,28,58,59]);
console.log('PASS: PMK/PTK, CCM, CRC/FCS residue, Barker sidelobes, sample map, delimiter, timing, resource deltas, RSN/M3 lengths and DHCP options. No radio accessed.');
