-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cat_dual_eye.js
More file actions
678 lines (571 loc) Β· 27.8 KB
/
test_cat_dual_eye.js
File metadata and controls
678 lines (571 loc) Β· 27.8 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#!/usr/bin/env node
/**
* Cat Mode DUAL-EYE Roundtrip Test
*
* Tests the full dual-eye encode β signal β decode pipeline:
* 1. Encrypt message β CatProtocol packets β binary framing
* 2. Split into header (single-eye) + 0xFD marker + payload (dual-eye interleaved)
* 3. Build transmission schedule: {left, right} per interval
* 4. Simulate video with independent left/right green channels
* 5. Run NRZ on combined channel β detect 0xFD marker
* 6. Re-decode payload from independent L/R channels
* 7. Verify CRC + decrypt matches original
*
* Also tests:
* - Backward compatibility: single-eye videos still decode correctly
* - 0xFD marker detection accuracy
* - Interleave/deinterleave correctness
* - Multiple blink speeds
*/
'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');
const { computeHistogram, findPeaks, findValley } = require('./web_demo/adaptive-threshold.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]);
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);
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) };
}
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,
packetsBinary
};
}
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// DUAL-EYE ENCODER (mirrors wasm_browser_example_FULL.html)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function buildDualEyeSchedule(totalBinary, preambleBits, syncBits) {
// Split into header and payload (exactly matching HTML)
const headerLen = 8 + preambleBits + syncBits; // lead-in + preamble + sync
let headerBinary = totalBinary.substring(0, headerLen);
let payloadBinary = totalBinary.substring(headerLen);
// Append 0xFD dual-eye mode marker to header
// (0xFD avoids collision with CatProtocol magic 0xCAFE LE = 0xFE first byte)
headerBinary += '11111101'; // 0xFD
// Pad payload to even length
if (payloadBinary.length % 2 !== 0) {
payloadBinary += '0';
}
// Build schedule
const schedule = [];
// Phase 1: Header β single-eye (both eyes same)
for (let i = 0; i < headerBinary.length; i++) {
schedule.push({ left: headerBinary[i], right: headerBinary[i] });
}
// Phase 2: Payload β dual-eye interleaved
for (let i = 0; i < payloadBinary.length; i += 2) {
schedule.push({
left: payloadBinary[i] || '0',
right: payloadBinary[i + 1] || '0'
});
}
return {
schedule,
headerBinary,
payloadBinary,
headerLen: headerBinary.length,
payloadLen: payloadBinary.length,
intervalCount: schedule.length
};
}
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// DUAL-EYE VIDEO SIGNAL SIMULATOR
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Produces frameSamples with:
// - greenLevel (combined) for NRZ preamble/sync detection
// - leftGreenLevel / rightGreenLevel for dual-eye decode
function simulateDualEyeVideoSignal(schedule, blinkSpeedMs, opts = {}) {
const fps = Math.max(30, Math.round(5000 / blinkSpeedMs));
const sampleInterval = 1 / fps;
const bitDuration = blinkSpeedMs / 1000;
const totalDuration = schedule.length * bitDuration;
const totalSamples = Math.ceil(totalDuration / sampleInterval) + 10;
const baseGreen = opts.baseGreen || 0.25;
const peakGreen = opts.peakGreen || 0.80;
const noiseLevel = opts.noiseLevel || 0.015;
const samples = [];
for (let i = 0; i < totalSamples; i++) {
const time = i * sampleInterval;
const intervalIdx = Math.floor(time / bitDuration);
let leftTarget, rightTarget, combinedState;
if (intervalIdx < 0 || intervalIdx >= schedule.length) {
leftTarget = baseGreen;
rightTarget = baseGreen;
combinedState = 'off';
} else {
const entry = schedule[intervalIdx];
leftTarget = entry.left === '1' ? peakGreen : baseGreen;
rightTarget = entry.right === '1' ? peakGreen : baseGreen;
// Combined state: "on" if either eye is green (for NRZ on combined channel)
combinedState = (entry.left === '1' || entry.right === '1') ? 'on' : 'off';
}
// Add independent noise per channel
const leftNoise = (Math.random() - 0.5) * noiseLevel * 2;
const rightNoise = (Math.random() - 0.5) * noiseLevel * 2;
const leftGreenLevel = Math.max(0, Math.min(1, leftTarget + leftNoise));
const rightGreenLevel = Math.max(0, Math.min(1, rightTarget + rightNoise));
// Combined green: average of both eyes (what the single-channel analyzer sees)
const greenLevel = (leftGreenLevel + rightGreenLevel) / 2;
// For the header portion (both eyes same), the combined channel
// has the same signal as either eye. For the payload, it's averaged.
// The NRZ decoder uses `state` for preamble detection.
// During header: left===right, so combined threshold works.
// During payload: left/right diverge, combined is ambiguous β but
// NRZ only processes up through sync+mode byte (all single-eye).
const headerState = intervalIdx < schedule.length
? (schedule[intervalIdx].left === schedule[intervalIdx].right
? (schedule[intervalIdx].left === '1' ? 'on' : 'off')
: 'on') // ambiguous during dual-eye payload
: 'off';
samples.push({
frame: i,
time,
greenLevel,
leftGreenLevel,
rightGreenLevel,
greenScore: greenLevel,
rawGreenLevel: greenLevel,
state: headerState,
confidence: 0.95,
isGreen: headerState === 'on'
});
}
return { samples, fps };
}
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// DUAL-EYE DECODER (mirrors wasm_browser_example_FULL.html)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function decodeDualEyePipeline(frameSamples, blinkSpeedMs) {
const bitDuration = blinkSpeedMs / 1000;
// Normalize combined green levels to 0-100 (for NRZ/preamble)
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;
}
const greenLevels = frameSamples.map(s => s.greenLevel);
// Preamble detection (uses combined channel)
suppressLogs = true;
const preambleResult = PreambleCalibration.detectPreambleWithFallback(
frameSamples, blinkSpeedMs, greenLevels
);
suppressLogs = false;
const leadInBits = 8;
let preambleBits;
if (preambleResult.found && preambleResult.preamble) {
preambleBits = Math.round(preambleResult.preamble.duration / bitDuration);
} else {
preambleBits = 32;
}
const syncStartTime = (leadInBits + preambleBits) * bitDuration;
const threshold = preambleResult.threshold || 50;
// NRZ decode on combined channel β finds sync word, returns bits after it
suppressLogs = true;
const nrzResult = NRZDecoder.decodeNRZ(
frameSamples,
preambleResult.bitRate || 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;
// ββ DUAL-EYE DETECTION (mirrors HTML exactly) ββββββββββββββββ
let isDualEye = false;
const modeByteCandidate = binary.substring(0, 8);
if (modeByteCandidate === '11111101') {
isDualEye = true;
// nrzResult.stats.bitPeriod is in ms; convert to seconds
const bitDur = (nrzResult.stats.bitPeriod / 1000) || bitDuration;
const payloadStartTime = nrzResult.t0 + 8 * bitDur;
// Normalize left and right channels independently
const leftLevels = frameSamples.map(s => s.leftGreenLevel);
const rightLevels = frameSamples.map(s => s.rightGreenLevel);
function normalizeChannel(levels) {
const min = Math.min(...levels);
const max = Math.max(...levels);
const range = max - min;
if (range < 0.001) return levels.map(() => 50);
return levels.map(v => ((v - min) / range) * 100);
}
const normLeft = normalizeChannel(leftLevels);
const normRight = normalizeChannel(rightLevels);
// Per-channel bimodal thresholds
function channelThreshold(normLevels) {
const hist = computeHistogram(normLevels, 50, 0, 100);
const peaks = findPeaks(hist.bins);
if (peaks.length >= 2) {
const sorted = peaks.slice().sort((a, b) => b.height - a.height);
return findValley(hist.bins, sorted[0], sorted[1]);
}
return 50;
}
const leftThreshold = channelThreshold(normLeft);
const rightThreshold = channelThreshold(normRight);
// Sample L/R channels at bit-duration intervals
const dualBits = [];
let sampleTime = payloadStartTime + bitDur / 2;
let searchStart = 0;
while (sampleTime < frameSamples[frameSamples.length - 1].time) {
let bestIdx = searchStart;
let bestDist = Math.abs(frameSamples[searchStart].time - sampleTime);
for (let j = searchStart + 1; j < frameSamples.length; j++) {
const dist = Math.abs(frameSamples[j].time - sampleTime);
if (dist < bestDist) {
bestDist = dist;
bestIdx = j;
} else if (frameSamples[j].time > sampleTime + bitDur) {
break;
}
}
searchStart = Math.max(0, bestIdx - 1);
const leftBit = normLeft[bestIdx] >= leftThreshold ? '1' : '0';
const rightBit = normRight[bestIdx] >= rightThreshold ? '1' : '0';
dualBits.push(leftBit);
dualBits.push(rightBit);
sampleTime += bitDur;
}
binary = dualBits.join('');
}
// Strip end marker
let payload = binary;
const endIdx = payload.lastIndexOf('0101010101010101');
if (endIdx !== -1) {
payload = payload.substring(0, endIdx);
}
// Pad to byte boundary
while (payload.length % 8 !== 0) payload += '0';
// Decode CatProtocol packets
let bytes = binaryToBytes(payload);
const packetDecoder = new CatProtocol.Decoder();
let currentOffset = 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);
if (processResult.accepted) crcPasses++;
else crcFails++;
currentOffset += packetResult.packet_size;
if (processResult.complete) {
bytes = processResult.message;
break;
}
}
return { bytes, binary, payload, isDualEye, crcPasses, crcFails };
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// TEST RUNNER
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let totalPass = 0;
let totalFail = 0;
function assert(label, condition, detail) {
if (condition) {
totalPass++;
_origLog(` β
${label}`);
} else {
totalFail++;
_origLog(` β ${label}: ${detail || 'FAILED'}`);
}
}
_origLog('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
_origLog('β Cat Mode: Dual-Eye Roundtrip Test β');
_origLog('β encrypt β dual-eye signal β NRZ+L/R β CRC β decrypt β');
_origLog('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
// ββ Test 1: Basic interleave/deinterleave ββββββββββββββββββββββββ
_origLog('\nββ Test 1: Interleave / Deinterleave Logic ββ');
{
const payloadBinary = '1100101011110000'; // 16 bits
// Interleave: left=even bits (1,0,1,1,0,0,0,0), right=odd bits (1,0,0,1,1,0,0,0)
const schedule = [];
for (let i = 0; i < payloadBinary.length; i += 2) {
schedule.push({ left: payloadBinary[i], right: payloadBinary[i + 1] || '0' });
}
// Deinterleave
let recovered = '';
for (const entry of schedule) {
recovered += entry.left + entry.right;
}
assert('Interleave roundtrip', recovered === payloadBinary,
`got '${recovered}', expected '${payloadBinary}'`);
assert('Schedule has half the intervals', schedule.length === payloadBinary.length / 2,
`${schedule.length} vs ${payloadBinary.length / 2}`);
}
// ββ Test 2: 0xFE mode byte detection βββββββββββββββββββββββββββββ
_origLog('\nββ Test 2: 0xFE Mode Byte Detection ββ');
{
assert('0xFE = 11111110', parseInt('11111110', 2) === 0xFE,
`got 0x${parseInt('11111110', 2).toString(16)}`);
const marker = '11111101';
assert('Dual-eye marker = 0xFD (11111101)', parseInt(marker, 2) === 0xFD,
`got 0x${parseInt(marker, 2).toString(16)}`);
assert('Marker is exactly 8 bits', marker.length === 8, `${marker.length}`);
// 0xFD must not collide with CatProtocol magic first byte (0xFE)
assert('0xFD β 0xFE (CatProtocol magic)', marker !== '11111110', 'collision with CatProtocol');
}
// ββ Test 3: Dual-eye schedule construction βββββββββββββββββββββββ
_origLog('\nββ Test 3: Dual-Eye Schedule Construction ββ');
{
const password = crypto.randomBytes(4).toString('hex');
const msg = 'Dual-eye schedule test message';
const { packed } = simulateEncrypt(msg, password);
const buildResult = buildCatBinaryPayload(packed);
const dualResult = buildDualEyeSchedule(
buildResult.binary,
buildResult.preambleBits,
buildResult.syncBits
);
// Header should be single-eye (left === right)
const headerEntries = dualResult.schedule.slice(0, dualResult.headerLen);
const allHeaderSingleEye = headerEntries.every(e => e.left === e.right);
assert('Header is single-eye (L===R)', allHeaderSingleEye, 'header has divergent entries');
// Header ends with 0xFD marker
const lastHeaderBits = dualResult.headerBinary.slice(-8);
assert('Header ends with 0xFD', lastHeaderBits === '11111101',
`got '${lastHeaderBits}'`);
// Payload entries may have divergent L/R
const payloadEntries = dualResult.schedule.slice(dualResult.headerLen);
assert('Payload intervals = ceil(payloadLen/2)',
payloadEntries.length === Math.ceil(dualResult.payloadLen / 2),
`${payloadEntries.length} vs ${Math.ceil(dualResult.payloadLen / 2)}`);
// Payload is even length
assert('Payload padded to even', dualResult.payloadLen % 2 === 0,
`length is ${dualResult.payloadLen}`);
// Total intervals < total bits (compression from dual-eye)
const totalBits = dualResult.headerLen + dualResult.payloadLen;
assert('Fewer intervals than total bits (2Γ speedup)',
dualResult.intervalCount < totalBits,
`${dualResult.intervalCount} intervals >= ${totalBits} bits`);
_origLog(` (Header: ${dualResult.headerLen} bits, Payload: ${dualResult.payloadLen} bits, Intervals: ${dualResult.intervalCount})`);
}
// ββ Test 4-6: Full roundtrip at multiple speeds ββββββββββββββββββ
const SPEEDS = [
{ ms: 200, label: 'Medium', noise: 0.015 },
{ ms: 100, label: 'Fast', noise: 0.012 },
{ ms: 50, label: 'Very Fast', noise: 0.008 },
];
for (let t = 0; t < SPEEDS.length; t++) {
const { ms, label, noise } = SPEEDS[t];
const fps = Math.max(30, Math.round(5000 / ms));
const msg = `DualEye-${ms}ms: ${crypto.randomBytes(8).toString('hex')} ts=${Date.now()}`;
const password = `pw-${crypto.randomBytes(4).toString('hex')}`;
_origLog(`\nββ Test ${4 + t}: Dual-Eye Full Roundtrip β ${label} (${ms}ms @ ${fps}fps) ββ`);
_origLog(` Message: "${msg}"`);
try {
// Step 1: Encrypt
const { packed } = simulateEncrypt(msg, password);
// Step 2: Build binary framing
const buildResult = buildCatBinaryPayload(packed);
// Step 3: Build dual-eye schedule (encoder side)
const dualResult = buildDualEyeSchedule(
buildResult.binary,
buildResult.preambleBits,
buildResult.syncBits
);
_origLog(` Schedule: ${dualResult.intervalCount} intervals (header=${dualResult.headerLen} + payload=${dualResult.payloadLen} interleaved)`);
// Step 4: Simulate dual-eye video
const { samples: frameSamples } = simulateDualEyeVideoSignal(dualResult.schedule, ms, {
noiseLevel: noise,
baseGreen: 0.20 + Math.random() * 0.10,
peakGreen: 0.75 + Math.random() * 0.10,
});
_origLog(` Signal: ${frameSamples.length} frames, noise=${(noise*100).toFixed(1)}%`);
// Step 5: Run dual-eye decoder
const result = decodeDualEyePipeline(frameSamples, ms);
_origLog(` Decoded: ${result.bytes.length} bytes, dualEye=${result.isDualEye}, CRC ${result.crcPasses}β
/${result.crcFails}β`);
// Assertions
assert('Dual-eye mode detected', result.isDualEye === true, 'isDualEye=false');
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)}` : '');
const decrypted = simulateDecrypt(new Uint8Array(result.bytes), password);
assert('Decrypted message matches', decrypted === msg,
`got "${decrypted.substring(0, 40)}..."`);
assert('CRC passes > 0', result.crcPasses > 0, `crcPasses=${result.crcPasses}`);
assert('No CRC failures', result.crcFails === 0, `crcFails=${result.crcFails}`);
} catch (err) {
totalFail++;
_origLog(` β PIPELINE ERROR: ${err.message}`);
if (err.stack) _origLog(` ${err.stack.split('\n').slice(1, 3).join('\n ')}`);
}
}
// ββ Test 7: Single-eye backward compatibility ββββββββββββββββββββ
// A video WITHOUT the 0xFD marker should still decode correctly
_origLog('\nββ Test 7: Single-Eye Backward Compatibility ββ');
{
const msg = `SingleEye-compat: ${crypto.randomBytes(4).toString('hex')}`;
const password = 'backcompat-pw';
const ms = 100;
try {
const { packed } = simulateEncrypt(msg, password);
const buildResult = buildCatBinaryPayload(packed);
// Build single-eye schedule (NO 0xFD marker, both eyes same)
const schedule = [];
for (let i = 0; i < buildResult.binary.length; i++) {
schedule.push({ left: buildResult.binary[i], right: buildResult.binary[i] });
}
const { samples: frameSamples } = simulateDualEyeVideoSignal(schedule, ms, {
noiseLevel: 0.012
});
const result = decodeDualEyePipeline(frameSamples, ms);
assert('Single-eye: NOT detected as dual-eye', result.isDualEye === false, `isDualEye=${result.isDualEye}`);
assert('Single-eye: payload recovered', result.bytes.length === packed.length,
`got ${result.bytes.length}, expected ${packed.length}`);
let byteMatch = true;
for (let i = 0; i < packed.length; i++) {
if (result.bytes[i] !== packed[i]) { byteMatch = false; break; }
}
assert('Single-eye: byte-perfect', byteMatch, 'mismatch');
const decrypted = simulateDecrypt(new Uint8Array(result.bytes), password);
assert('Single-eye: decrypts correctly', decrypted === msg, 'decryption failed');
} catch (err) {
totalFail++;
_origLog(` β PIPELINE ERROR: ${err.message}`);
}
}
// ββ Test 8: Speed comparison βββββββββββββββββββββββββββββββββββββ
_origLog('\nββ Test 8: Dual-Eye Speed Advantage ββ');
{
const msg = `Speed test: ${crypto.randomBytes(16).toString('hex')}`;
const password = 'speed-pw';
const ms = 100;
const { packed } = simulateEncrypt(msg, password);
const buildResult = buildCatBinaryPayload(packed);
// Single-eye: 1 bit per interval = binary.length intervals
const singleEyeIntervals = buildResult.binary.length;
// Dual-eye
const dualResult = buildDualEyeSchedule(
buildResult.binary,
buildResult.preambleBits,
buildResult.syncBits
);
const dualEyeIntervals = dualResult.intervalCount;
const speedup = singleEyeIntervals / dualEyeIntervals;
assert(`Dual-eye is faster (${speedup.toFixed(2)}Γ speedup)`,
dualEyeIntervals < singleEyeIntervals,
`dual=${dualEyeIntervals} >= single=${singleEyeIntervals}`);
// Header is the same, payload is halved β speedup should be >1 and <2
assert('Speedup > 1.3Γ (payload is dominant)', speedup > 1.3,
`only ${speedup.toFixed(2)}Γ`);
assert('Speedup < 2.0Γ (header not doubled)', speedup < 2.0,
`${speedup.toFixed(2)}Γ exceeds theoretical max`);
_origLog(` (Single-eye: ${singleEyeIntervals} intervals, Dual-eye: ${dualEyeIntervals} intervals)`);
}
// ββ Summary βββββββββββββββββββββββββββββββββββββββββββββββββββββ
_origLog(`\n${'β'.repeat(60)}`);
_origLog(` Results: ${totalPass} passed, ${totalFail} failed`);
_origLog(`${'β'.repeat(60)}`);
if (totalFail > 0) {
_origLog('\nπ Some dual-eye tests failed.\n');
process.exit(1);
} else {
_origLog('\nβ
All dual-eye tests passed!\n');
_origLog(' β Interleave/deinterleave logic correct');
_origLog(' β 0xFE mode byte detection works');
_origLog(' β Header single-eye, payload dual-eye');
_origLog(' β Full encrypt β dual-eye β decrypt at 3 speeds');
_origLog(' β Single-eye backward compatibility preserved');
_origLog(' β ~1.7Γ speedup confirmed\n');
process.exit(0);
}