-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
156 lines (136 loc) · 4.78 KB
/
Copy pathmain.js
File metadata and controls
156 lines (136 loc) · 4.78 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
const CONFIG = {
sequenceLength: 10,
bases: ['+', 'x'],
symbols: {
'+': { '0': '|', '1': '-' },
'x': { '0': '\\', '1': '/' }
}
};
let aliceData = [];
let bobData = [];
let currentStep = 1;
// UI Elements
const aliceBtn = document.getElementById('alice-send-btn');
const bobBtn = document.getElementById('bob-measure-btn');
const siftBtn = document.getElementById('sift-btn');
const resetBtn = document.getElementById('reset-btn');
const photonStream = document.getElementById('photon-stream');
const tableBody = document.getElementById('table-body');
const finalKeyDisplay = document.getElementById('final-key-display');
const stepGuides = document.querySelectorAll('.step-guide');
const tabBtns = document.querySelectorAll('.tab-btn');
const tabContents = document.querySelectorAll('.tab-content');
function init() {
aliceData = [];
bobData = [];
currentStep = 1;
// Tab switching
tabBtns.forEach(btn => {
btn.addEventListener('click', () => {
const target = btn.dataset.tab;
tabBtns.forEach(b => b.classList.toggle('active', b === btn));
tabContents.forEach(c => c.classList.toggle('active', c.id === `${target}-tab`));
});
});
updateUI();
}
function updateUI() {
// Update step guide states
stepGuides.forEach(guide => {
const step = parseInt(guide.dataset.step);
guide.classList.toggle('active', step === currentStep);
});
// Update buttons
aliceBtn.disabled = currentStep !== 1;
bobBtn.disabled = currentStep !== 2;
siftBtn.disabled = currentStep !== 3;
// Render Stream and Table
renderStream();
renderTable();
}
function renderStream() {
if (aliceData.length === 0) {
photonStream.innerHTML = '<div class="empty-state">Click "Alice Sends Photons" to begin.</div>';
return;
}
photonStream.innerHTML = '';
aliceData.forEach((item, i) => {
const row = document.createElement('div');
row.className = 'photon-row';
const bobItem = bobData[i];
row.innerHTML = `
<div class="photon-pair">
<div class="photon-box active">
<span class="photon-icon">${CONFIG.symbols[item.basis][item.bit]}</span>
<span class="photon-meta">${item.basis} (${item.bit})</span>
</div>
</div>
<div class="photon-pair">
<div class="photon-box ${bobItem ? 'active' : ''}">
${bobItem ? `
<span class="photon-icon">${CONFIG.symbols[bobItem.basis][bobItem.result]}</span>
<span class="photon-meta">${bobItem.basis} (${bobItem.result})</span>
` : '?'}
</div>
</div>
`;
photonStream.appendChild(row);
});
}
function renderTable() {
tableBody.innerHTML = '';
aliceData.forEach((item, i) => {
const bobItem = bobData[i];
const isSifted = currentStep >= 4;
const match = bobItem && item.basis === bobItem.basis;
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${i + 1}</td>
<td>${item.basis}</td>
<td>${item.bit}</td>
<td>${bobItem ? bobItem.basis : '-'}</td>
<td>${bobItem ? bobItem.result : '-'}</td>
<td>${bobItem && currentStep >= 3 ? (match ? '<span class="match-yes">YES</span>' : '<span class="match-no">NO</span>') : '-'}</td>
<td>${isSifted && match ? `<span class="final-key-bit">${item.bit}</span>` : '-'}</td>
`;
tableBody.appendChild(tr);
});
if (currentStep === 4) {
const key = aliceData
.filter((item, i) => bobData[i] && item.basis === bobData[i].basis)
.map(item => item.bit)
.join('');
finalKeyDisplay.textContent = key || 'No matching bases found!';
} else {
finalKeyDisplay.textContent = '';
}
}
aliceBtn.addEventListener('click', () => {
aliceData = Array.from({ length: CONFIG.sequenceLength }, () => ({
bit: Math.floor(Math.random() * 2).toString(),
basis: CONFIG.bases[Math.floor(Math.random() * 2)]
}));
currentStep = 2;
updateUI();
});
bobBtn.addEventListener('click', () => {
bobData = aliceData.map(alice => {
const basis = CONFIG.bases[Math.floor(Math.random() * 2)];
let result;
if (basis === alice.basis) {
result = alice.bit;
} else {
// Mismatch: 50/50 chance
result = Math.floor(Math.random() * 2).toString();
}
return { basis, result };
});
currentStep = 3;
updateUI();
});
siftBtn.addEventListener('click', () => {
currentStep = 4;
updateUI();
});
resetBtn.addEventListener('click', init);
init();