-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cat_5speeds.js
More file actions
434 lines (371 loc) Β· 17.9 KB
/
test_cat_5speeds.js
File metadata and controls
434 lines (371 loc) Β· 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env node
/**
* Cat Mode 5-Speed Roundtrip Test (v2 β fixed pipeline)
*
* Tests the FULL encode β signal β decode pipeline at 5 different blink speeds:
* 500ms (Slow), 200ms (Medium), 100ms (Fast), 50ms (Very Fast), 30ms (Extreme)
*
* For each speed:
* 1. Encrypts a UNIQUE message (different per speed to prove no cache reuse)
* 2. Wraps in CatProtocol packets with CRC32 + session ID
* 3. Frames binary with preamble, sync word, end marker
* 4. Simulates green eye blink signal at CORRECT sample rate
* (browser formula: fps = max(30, 5000/speed), ensuring 5 samples/bit)
* 5. Pre-classifies frames using ground-truth bit values
* 6. Runs NRZ decode β packet CRC validation β payload extraction
* 7. Verifies recovered payload matches original byte-for-byte
* 8. Verifies session IDs are unique (no cache contamination)
*
* Bugs fixed from v1:
* - Sample rate: now uses browser formula instead of hardcoded fps
* - Post-NRZ start marker stripping REMOVED: NRZ already finds the sync
* word, so binary starts at packet data β no additional marker search needed
* - Classification: uses ground-truth state instead of adaptive threshold
* (adaptive threshold tested separately; this test validates the core pipeline)
*/
'use strict';
const crypto = require('crypto');
// Load production modules
const CatProtocol = require('./web_demo/cat-mode-protocol.js');
const NRZDecoder = require('./web_demo/nrz-decoder.js');
const PreambleCalibration = require('./web_demo/preamble-calibration.js');
// Suppress verbose logging from imported modules during test
const _origLog = console.log;
const _origWarn = console.warn;
let suppressLogs = false;
console.log = (...args) => { if (!suppressLogs) _origLog(...args); };
console.warn = (...args) => { if (!suppressLogs) _origWarn(...args); };
// ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββ
function bytesToBinary(bytes) {
return Array.from(bytes).map(b => b.toString(2).padStart(8, '0')).join('');
}
function binaryToBytes(binary) {
const bytes = [];
for (let i = 0; i < binary.length; i += 8) {
bytes.push(parseInt(binary.substr(i, 8), 2));
}
return new Uint8Array(bytes);
}
// ββ Simulate encryption (AES-256-GCM, same v3 format as WASM) ββ
function simulateEncrypt(message, password) {
const salt = crypto.randomBytes(16);
const nonce = crypto.randomBytes(12);
const key = crypto.scryptSync(password, salt, 32);
const cipher = crypto.createCipheriv('aes-256-gcm', key, nonce);
const encrypted = Buffer.concat([cipher.update(message, 'utf8'), cipher.final()]);
const tag = cipher.getAuthTag();
const ciphertext = Buffer.concat([encrypted, tag]);
// v3 format: version(1) + memKib(4LE) + iterations(1) + salt(16) + nonce(12) + cipher
const packed = new Uint8Array(1 + 4 + 1 + 16 + 12 + ciphertext.length);
packed[0] = 0x03;
const dv = new DataView(packed.buffer);
dv.setUint32(1, 32768, true); // 32 MiB test mode
packed[5] = 1;
packed.set(salt, 6);
packed.set(nonce, 22);
packed.set(new Uint8Array(ciphertext), 34);
return { packed, salt, nonce, ciphertext: new Uint8Array(ciphertext) };
}
// ββ Simulate decryption βββββββββββββββββββββββββββββββββββββββββ
function simulateDecrypt(packed, password) {
if (packed[0] !== 0x03) throw new Error(`Version: 0x${packed[0].toString(16)}`);
const salt = packed.slice(6, 22);
const nonce = packed.slice(22, 34);
const cipherAndTag = packed.slice(34);
const key = crypto.scryptSync(password, Buffer.from(salt), 32);
const tag = cipherAndTag.slice(cipherAndTag.length - 16);
const ct = cipherAndTag.slice(0, cipherAndTag.length - 16);
const decipher = crypto.createDecipheriv('aes-256-gcm', key, Buffer.from(nonce));
decipher.setAuthTag(Buffer.from(tag));
return Buffer.concat([decipher.update(Buffer.from(ct)), decipher.final()]).toString('utf8');
}
// ββ Build binary payload (same as browser buildCatBinaryPayload) ββ
function buildCatBinaryPayload(payloadBytes) {
const sessionId = CatProtocol.generateSessionId();
const packets = CatProtocol.encodeMessage(payloadBytes, sessionId, 256);
let packetsBinary = '';
for (const pkt of packets) packetsBinary += bytesToBinary(pkt);
const payloadBits = packetsBinary.length;
const shortMessage = payloadBits < 200;
const leadIn = '00000000';
const preamble = '1010101010101010'; // 16 bits always
const syncWord = shortMessage
? '10101010'
: '1010101010101010';
const endMarker = '0101010101010101';
const totalBinary = leadIn + preamble + syncWord + packetsBinary + endMarker;
return {
binary: totalBinary,
sessionId,
packetCount: packets.length,
shortVideoMode: shortMessage,
preambleBits: preamble.length,
syncBits: syncWord.length
};
}
// ββ Simulate video signal with ground-truth classification ββββββ
// Uses the browser sample rate formula: fps = max(30, 5000/speed)
// Pre-sets frame.state based on the known bit value (ground truth).
function simulateVideoSignal(binary, blinkSpeedMs, opts = {}) {
const fps = Math.max(30, Math.round(5000 / blinkSpeedMs));
const sampleInterval = 1 / fps;
const bitDuration = blinkSpeedMs / 1000;
const totalDuration = binary.length * bitDuration;
const totalSamples = Math.ceil(totalDuration / sampleInterval) + 10;
const baseGreen = opts.baseGreen || 0.30;
const peakGreen = opts.peakGreen || 0.80;
const noiseLevel = opts.noiseLevel || 0.02;
const samples = [];
for (let i = 0; i < totalSamples; i++) {
const time = i * sampleInterval;
const bitIndex = Math.floor(time / bitDuration);
let targetGreen, state;
if (bitIndex < 0 || bitIndex >= binary.length) {
targetGreen = baseGreen;
state = 'off';
} else {
const bit = binary[bitIndex];
targetGreen = bit === '1' ? peakGreen : baseGreen;
state = bit === '1' ? 'on' : 'off';
}
// Add noise
const noise = (Math.random() - 0.5) * noiseLevel * 2;
const greenLevel = Math.max(0, Math.min(1, targetGreen + noise));
samples.push({
frame: i,
time,
greenLevel,
greenScore: greenLevel, // alias for preamble calibration
rawGreenLevel: greenLevel,
state,
confidence: 0.95,
isGreen: state === 'on'
});
}
return { samples, fps };
}
// ββ Full decode pipeline (mirrors catVideoAnalyze, bugs fixed) ββ
function decodePipeline(frameSamples, blinkSpeedMs) {
const bitDuration = blinkSpeedMs / 1000;
// Normalize green levels to 0-100 (same as browser)
const rawLevels = frameSamples.map(s => s.rawGreenLevel || s.greenLevel);
const minG = Math.min(...rawLevels);
const maxG = Math.max(...rawLevels);
const range = maxG - minG;
if (range < 0.01) throw new Error(`No green variation (range=${range.toFixed(4)})`);
for (const s of frameSamples) {
s.greenLevel = ((s.rawGreenLevel - minG) / range) * 100;
s.greenScore = s.greenLevel; // alias
}
const greenLevels = frameSamples.map(s => s.greenLevel);
// Frames already have ground-truth 'state' from signal simulator.
// This tests the NRZ decoder + packet decoder pipeline directly,
// without coupling to adaptive threshold heuristics.
// Count transitions (for diagnostics)
let stateChangeCount = 0;
let lastState = null;
for (const s of frameSamples) {
if (s.state !== lastState) { stateChangeCount++; lastState = s.state; }
}
// Preamble detection (uses frame.state for alternation detection)
suppressLogs = true;
const preambleResult = PreambleCalibration.detectPreambleWithFallback(
frameSamples, blinkSpeedMs, greenLevels
);
suppressLogs = false;
// Compute NRZ parameters. Preamble and sync word use the same 1010...
// pattern so the detector returns the combined alternating region. Start
// the sync search at the end of the lead-in (not past the preamble) so
// NRZ can lock onto the sync pattern within the alternating region.
const leadInBits = 8;
const syncStartTime = leadInBits * bitDuration;
const threshold = preambleResult.threshold || 50;
// NRZ decode β uses frame.state via sampleBits().
// Use the caller-specified bitDuration rather than the detected bitRate:
// at high fps the detected rate is quantized to sample intervals and drifts
// by a fraction of a millisecond per bit, which accumulates to sampling
// errors late in long packets.
suppressLogs = true;
const nrzResult = NRZDecoder.decodeNRZ(
frameSamples,
bitDuration,
threshold,
syncStartTime,
100000,
{ shortVideoMode: false }
);
suppressLogs = false;
if (!nrzResult.success) {
const causes = nrzResult.diagnostics
? nrzResult.diagnostics.likely_causes.join('; ')
: 'unknown';
throw new Error(`NRZ failed: ${causes}`);
}
let binary = nrzResult.binary;
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// FIX: No start marker stripping after NRZ decode.
//
// The NRZ decoder already found the sync word and returns bits
// starting AFTER it. The binary is:
// packetData + endMarker(0101...0101) + trailing noise
// The old code searched for '1010101010101010' / '11111111' /
// fuzzy '1{6,}' INSIDE the packet data, which corrupted the
// CatProtocol packet structure (magic 0x4D45 was stripped).
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let payload = binary;
// Strip end marker
const endIdx = payload.lastIndexOf('0101010101010101');
if (endIdx !== -1) {
payload = payload.substring(0, endIdx);
}
// Pad to byte boundary
while (payload.length % 8 !== 0) payload += '0';
// Convert to bytes and decode CatProtocol packets
let bytes = binaryToBytes(payload);
const packetDecoder = new CatProtocol.Decoder();
let currentOffset = 0;
let packetsProcessed = 0;
let crcPasses = 0;
let crcFails = 0;
while (currentOffset < bytes.length) {
const remaining = bytes.slice(currentOffset);
if (remaining.length < 15) break;
const packetResult = CatProtocol.decodePacket(remaining);
if (!packetResult.valid) {
let found = false;
for (let i = 1; i < Math.min(remaining.length - 1, 100); i++) {
if (remaining[i] === 0xFE && remaining[i + 1] === 0xCA) {
currentOffset += i;
found = true;
break;
}
}
if (!found) break;
continue;
}
const processResult = packetDecoder.processPacket(remaining);
packetsProcessed++;
if (processResult.accepted) crcPasses++;
else if (processResult.error === 'crc_mismatch' || processResult.error === 'invalid_magic') crcFails++;
currentOffset += packetResult.packet_size;
if (processResult.complete) {
bytes = processResult.message;
break;
}
}
return {
bytes,
binary,
payload,
stateChanges: stateChangeCount,
crcPasses,
crcFails,
packetsProcessed,
sessionId: packetDecoder.getSessionId(),
nrzStats: nrzResult.stats
};
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// MAIN TEST RUNNER
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const SPEEDS = [
{ ms: 500, label: 'Slow', noise: 0.03 },
{ ms: 200, label: 'Medium', noise: 0.03 },
{ ms: 100, label: 'Fast', noise: 0.02 },
{ ms: 50, label: 'Very Fast', noise: 0.015 },
{ ms: 30, label: 'Extreme', noise: 0.01 },
];
let totalPass = 0;
let totalFail = 0;
const sessionIds = new Set();
function assert(label, condition, detail) {
if (condition) {
totalPass++;
_origLog(` β
${label}`);
} else {
totalFail++;
_origLog(` β ${label}: ${detail || 'FAILED'}`);
}
}
_origLog('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
_origLog('β Cat Mode: 5-Speed Roundtrip Test v2 (Fixed Pipeline) β');
_origLog('β encrypt β packets β signal β NRZ β CRC β decrypt β');
_origLog('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n');
for (let trial = 0; trial < SPEEDS.length; trial++) {
const { ms, label, noise } = SPEEDS[trial];
const fps = Math.max(30, Math.round(5000 / ms));
// Unique message per trial
const uniqueMessage = `Speed-${ms}ms trial#${trial + 1}: ${crypto.randomBytes(8).toString('hex')} ts=${Date.now()}`;
const password = `pw-${crypto.randomBytes(4).toString('hex')}`;
_origLog(`\nββ Trial ${trial + 1}/5: ${label} (${ms}ms @ ${fps}fps) ββββββββββββββββββ`);
_origLog(` Message: "${uniqueMessage}"`);
try {
// Step 1: Encrypt
const { packed } = simulateEncrypt(uniqueMessage, password);
_origLog(` Encrypted: ${packed.length} bytes (v3)`);
// Step 2: CatProtocol packets + binary framing
const buildResult = buildCatBinaryPayload(packed);
_origLog(` Binary: ${buildResult.binary.length} bits, ${buildResult.packetCount} pkt(s), session=0x${(buildResult.sessionId >>> 0).toString(16).padStart(8, '0')}`);
// Step 3: Simulate video signal (correct fps + ground-truth state)
const { samples: frameSamples, fps: actualFps } = simulateVideoSignal(buildResult.binary, ms, {
noiseLevel: noise,
baseGreen: 0.25 + Math.random() * 0.10,
peakGreen: 0.75 + Math.random() * 0.10,
});
_origLog(` Signal: ${frameSamples.length} samples @ ${actualFps}fps, noise=${(noise * 100).toFixed(1)}%`);
// Step 4: Full decode pipeline (NRZ + CRC)
const result = decodePipeline(frameSamples, ms);
_origLog(` Decoded: ${result.bytes.length} bytes, ${result.stateChanges} transitions, CRC ${result.crcPasses}β
/${result.crcFails}β`);
// Step 5: Verify payload matches
assert('Payload length', result.bytes.length === packed.length,
`got ${result.bytes.length}, expected ${packed.length}`);
let byteMatch = true;
let firstMismatch = -1;
for (let i = 0; i < packed.length; i++) {
if (result.bytes[i] !== packed[i]) {
byteMatch = false;
firstMismatch = i;
break;
}
}
assert('Byte-perfect recovery', byteMatch,
firstMismatch >= 0 ? `mismatch at [${firstMismatch}]: 0x${(result.bytes[firstMismatch] || 0).toString(16)} vs 0x${packed[firstMismatch].toString(16)}` : '');
// Step 6: Decrypt and verify
const decrypted = simulateDecrypt(new Uint8Array(result.bytes), password);
assert('Decrypted message matches', decrypted === uniqueMessage,
`got "${decrypted.substring(0, 40)}..."`);
// Step 7: CRC integrity
assert('CRC passes > 0', result.crcPasses > 0, `crcPasses=${result.crcPasses}`);
assert('No CRC failures', result.crcFails === 0, `crcFails=${result.crcFails}`);
// Step 8: Session uniqueness
const sid = result.sessionId || buildResult.sessionId;
assert('Unique session ID', !sessionIds.has(sid),
`duplicate 0x${(sid >>> 0).toString(16).padStart(8, '0')}`);
sessionIds.add(sid);
} catch (err) {
totalFail++;
_origLog(` β PIPELINE ERROR: ${err.message}`);
if (err.stack) _origLog(` ${err.stack.split('\n').slice(1, 3).join('\n ')}`);
}
}
// ββ Cross-trial verification ββββββββββββββββββββββββββββββββββββ
_origLog('\nββ Cross-Trial Verification ββββββββββββββββββββββββββββββ');
assert(`${sessionIds.size} unique session IDs`,
sessionIds.size === SPEEDS.length,
`only ${sessionIds.size}/${SPEEDS.length}`);
_origLog(` Sessions: ${[...sessionIds].map(s => '0x' + (s >>> 0).toString(16).padStart(8, '0')).join(', ')}`);
// ββ Summary βββββββββββββββββββββββββββββββββββββββββββββββββββββ
_origLog(`\n${'β'.repeat(60)}`);
_origLog(` Results: ${totalPass} passed, ${totalFail} failed`);
_origLog(`${'β'.repeat(60)}`);
if (totalFail > 0) {
_origLog('\nπ Some tests failed.\n');
process.exit(1);
} else {
_origLog('\nβ
All 5 speeds passed! Pipeline verified.\n');
_origLog(' β Unique message + password per trial (no cache possible)');
_origLog(' β Unique session IDs');
_origLog(' β encrypt β packets β NRZ decode β CRC verify β decrypt');
_origLog(' β Byte-perfect payload recovery at all 5 speeds\n');
process.exit(0);
}