-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScyWebImageTester.html
More file actions
315 lines (281 loc) · 12.1 KB
/
Copy pathScyWebImageTester.html
File metadata and controls
315 lines (281 loc) · 12.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Optimized Unscrambler with brute-force Complexity Analysis</title>
<style>
* { box-sizing:border-box; margin:0; padding:0; }
body { font-family:'Segoe UI',sans-serif; background:#121212; color:#ddd; display:flex; flex-direction:column; align-items:center; padding:20px; }
h1 { color:#bb86fc; margin-bottom:10px; text-align:center; }
p { margin-bottom:20px; text-align:center; }
input[type="file"] { margin-bottom:20px; }
#container { display:flex; flex-wrap:wrap; gap:20px; justify-content:center; width:100%; max-width:1200px; }
.canvas-wrapper { display:flex; flex-direction:column; align-items:center; flex:1 1 300px; }
canvas { border:2px solid #bb86fc; border-radius:6px; width:100%; height:auto; background:#1e1e1e; }
#status-container { width:100%; max-width:1200px; margin-top:20px; display:flex; flex-direction:column; gap:10px; }
#status { padding:15px; background:#1e1e1e; border:2px solid #bb86fc; border-radius:8px; font-family:monospace; white-space:pre-wrap; max-height:300px; overflow-y:auto; color:#ddd; line-height: 1.4; }
#progress-wrapper { width:100%; background:#2a2a2a; border-radius:6px; border:1px solid #bb86fc; height:24px; position:relative; }
#progress-bar { width:0%; height:100%; background:#bb86fc; border-radius:6px; transition:width 0.1s; }
#progress-text { text-align:center; color:#ddd; font-weight:bold; position:absolute; width:100%; top:0; left:0; line-height:24px; }
#controls { margin-bottom: 20px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: center; }
.btn-suspend {
background: #03dac6; color: #000; border: none; padding: 8px 16px;
border-radius: 4px; cursor: pointer; font-weight: bold; transition: all 0.2s;
}
.btn-suspend.active { background: #cf6679; color: #fff; }
#chart-wrapper { width:100%; max-width:1200px; margin-top:20px; }
#feasibilityChart { width:100%; height:150px; border:2px solid #bb86fc; border-radius:8px; background:#1e1e1e; cursor:crosshair; }
.stat-highlight { color: #03dac6; font-weight: bold; }
.warning-highlight { color: #cf6679; font-weight: bold; }
</style>
</head>
<body>
<h1>Optimized Unscrambler + Complexity Auditor</h1>
<p>Visualizing the statistical impossibility of brute-force reconstruction.</p>
<div id="controls">
<input type="file" id="originalInput" accept="image/*">
<input type="file" id="scrambledInput" accept="image/*">
<button id="suspendBtn" class="btn-suspend">Suspend Early Stop: OFF</button>
</div>
<div id="container">
<div class="canvas-wrapper">
<h3 style="color:#bb86fc;">Scrambled Input</h3>
<canvas id="originalCanvas" width="256" height="256"></canvas>
</div>
<div class="canvas-wrapper">
<h3 style="color:#bb86fc;">Target Template</h3>
<canvas id="scrambledCanvas" width="256" height="256"></canvas>
</div>
<div class="canvas-wrapper">
<h3 style="color:#bb86fc;">Reconstructed Output</h3>
<canvas id="outputCanvas" width="256" height="256"></canvas>
</div>
</div>
<div id="status-container">
<div id="status"><span>Waiting for images...</span></div>
<div id="progress-wrapper">
<div id="progress-bar"></div>
<div id="progress-text">0%</div>
</div>
<div id="chart-wrapper">
<canvas id="feasibilityChart"></canvas>
</div>
</div>
<script>
const blockSize = 8;
const earlyStopThreshold = 10;
const minBlocksBeforeCheck = 20;
const slidingWindowSize = 10;
const status = document.getElementById('status');
const progressBar = document.getElementById('progress-bar');
const progressText = document.getElementById('progress-text');
const chartCanvas = document.getElementById('feasibilityChart');
const suspendBtn = document.getElementById('suspendBtn');
const chartCtx = chartCanvas.getContext('2d');
let feasibilityData = [];
let chartPoints = [];
let isEarlyStopSuspended = false;
suspendBtn.addEventListener('click', () => {
isEarlyStopSuspended = !isEarlyStopSuspended;
suspendBtn.textContent = `Suspend Early Stop: ${isEarlyStopSuspended ? 'ON' : 'OFF'}`;
suspendBtn.classList.toggle('active', isEarlyStopSuspended);
});
function updateStatus(msg){ status.innerHTML = `<span>${msg}</span>`; }
function appendStatus(msg){ status.innerHTML += `\n<span>${msg}</span>`; status.scrollTop = status.scrollHeight; }
function setProgress(percent){ progressBar.style.width = percent+'%'; progressText.textContent = percent.toFixed(1)+'%'; }
/**
* Calculates brute force complexity using Stirling's Approximation
* Complexity = N! * 4^N
*/
function getBruteForceStats(numBlocks) {
// Benchmark hardware: How many MSE comparisons per second?
const start = performance.now();
for(let i=0; i<500000; i++) { Math.sqrt(i) * Math.sqrt(i); }
const end = performance.now();
const opsPerSec = 500000 / ((end - start) / 1000);
// Log10(N!) ≈ N log10(N) - N log10(e)
const logNFact = numBlocks * Math.log10(numBlocks) - (numBlocks * Math.log10(Math.E));
const logRotations = numBlocks * Math.log10(4);
const totalLog10 = logNFact + logRotations;
const secondsInYear = 31536000;
// Classical: Total States / OpsPerSec
const yearsClassicalLog = totalLog10 - Math.log10(opsPerSec) - Math.log10(secondsInYear);
// Quantum (Grover's): sqrt(Total States) / OpsPerSec
const yearsQuantumLog = (totalLog10 * 0.5) - Math.log10(opsPerSec) - Math.log10(secondsInYear);
return {
totalLog10: totalLog10.toFixed(0),
yearsClassical: yearsClassicalLog.toFixed(0),
yearsQuantum: yearsQuantumLog.toFixed(0)
};
}
function rotateBlock(block,size,rot){
const res = new Uint8ClampedArray(block.length);
for(let y=0;y<size;y++){
for(let x=0;x<size;x++){
let srcX,srcY;
if(rot===0){ srcX=x; srcY=y; }
else if(rot===1){ srcX=y; srcY=size-1-x; }
else if(rot===2){ srcX=size-1-x; srcY=size-1-y; }
else if(rot===3){ srcX=size-1-y; srcY=x; }
const srcIdx = (srcY*size+srcX)*4;
const dstIdx = (y*size+x)*4;
for(let i=0;i<4;i++) res[dstIdx+i]=block[srcIdx+i];
}
}
return res;
}
function getBlocks(imgData,w,h){
const cols = Math.ceil(w/blockSize), rows = Math.ceil(h/blockSize);
const blocks = [];
for(let by=0;by<rows;by++){
for(let bx=0;bx<cols;bx++){
const block = new Uint8ClampedArray(blockSize*blockSize*4);
for(let y=0;y<blockSize;y++){
for(let x=0;x<blockSize;x++){
const idx = ((by*blockSize+y)*w+(bx*blockSize+x))*4;
for(let c=0;c<4;c++) block[(y*blockSize+x)*4+c]=imgData.data[idx+c];
}
}
blocks.push({block,bx,by});
}
}
return {blocks,cols,rows};
}
function mse(a,b){
let sum=0;
for(let i=0;i<a.length;i++){ let diff=a[i]-b[i]; sum+=diff*diff; }
return sum/a.length;
}
function drawChart(){
chartCanvas.width = chartCanvas.clientWidth;
chartCanvas.height = chartCanvas.clientHeight;
chartCtx.clearRect(0,0,chartCanvas.width,chartCanvas.height);
chartCtx.strokeStyle='#bb86fc';
chartCtx.lineWidth=2;
chartCtx.beginPath();
chartPoints = [];
for(let i=0;i<feasibilityData.length;i++){
const x = (i/(feasibilityData.length-1))*chartCanvas.width;
const y = chartCanvas.height - (feasibilityData[i]/100)*chartCanvas.height;
chartPoints.push({x,y,block:i+1,feasibility:feasibilityData[i]});
if(i===0) chartCtx.moveTo(x,y);
else chartCtx.lineTo(x,y);
}
chartCtx.stroke();
}
function appendExplanatoryStatus(i, total, feasibleBlocks, w, h, earlyStop=false){
const feasibilityPercent = (feasibleBlocks/(i+1))*100;
feasibilityData.push(feasibilityPercent);
drawChart();
if(earlyStop){
const bf = getBruteForceStats(total);
appendStatus(`\n<span class="warning-highlight">!!! EARLY STOP TRIGGERED !!!</span>`);
appendStatus(`Feasibility dropped to ${feasibilityPercent.toFixed(1)}%. Visual reconstruction failed.`);
appendStatus(`-----------------------------------------`);
appendStatus(`BRUTE FORCE AUDIT FOR THIS IMAGE:`);
appendStatus(`Total Permutations: <span class="stat-highlight">10^${bf.totalLog10}</span>`);
appendStatus(`Classical PC Time: <span class="stat-highlight">10^${bf.yearsClassical} years</span>`);
appendStatus(`Quantum (Grover): <span class="stat-highlight">10^${bf.yearsQuantum} years</span>`);
appendStatus(`-----------------------------------------`);
appendStatus(`Conclusion: Brute force is mathematically impossible on any known hardware.`);
} else if(i % Math.ceil(total/10) === 0) {
appendStatus(`Progress: ${((i/total)*100).toFixed(0)}% | Feasibility: ${feasibilityPercent.toFixed(1)}%`);
}
}
function reconstruct(original, scrambled, w, h, outputCanvas){
const {blocks: origBlocks} = getBlocks(original,w,h);
const {blocks: scrBlocks} = getBlocks(scrambled,w,h);
const ctx = outputCanvas.getContext('2d');
const total = scrBlocks.length;
const batchSize = Math.max(1, Math.floor(total/50));
const tolerance = 20;
let feasibleBlocks = 0;
let i = 0;
const imageData = ctx.getImageData(0,0,w,h);
const lastBlocks = [];
function processBatch(){
const end = Math.min(i+batchSize,total);
for(;i<end;i++){
const sblock = scrBlocks[i].block;
let bestMSE = Infinity, bestBlock = null;
for(let r=0;r<4;r++){
const rotated = rotateBlock(sblock,blockSize,r);
for(const ob of origBlocks){
const e = mse(rotated,ob.block);
if(e<bestMSE){ bestMSE=e; bestBlock=rotated; }
}
}
const x=scrBlocks[i].bx*blockSize;
const y=scrBlocks[i].by*blockSize;
for(let byy=0;byy<blockSize;byy++){
for(let bxx=0;bxx<blockSize;bxx++){
const idx=(byy*blockSize+bxx)*4;
const outIdx=((y+byy)*w+(x+bxx))*4;
for(let c=0;c<4;c++) imageData.data[outIdx+c]=bestBlock[idx+c];
}
}
if(bestMSE<=tolerance) feasibleBlocks++;
lastBlocks.push(bestMSE<=tolerance?1:0);
if(lastBlocks.length>slidingWindowSize) lastBlocks.shift();
const smoothedFeasibility = (lastBlocks.reduce((a,b)=>a+b,0)/lastBlocks.length)*100;
if(!isEarlyStopSuspended && i>=minBlocksBeforeCheck && smoothedFeasibility<earlyStopThreshold){
appendExplanatoryStatus(i, total, feasibleBlocks, w, h, true);
ctx.putImageData(imageData,0,0);
setProgress((i/total)*100);
return;
}
if(i % Math.ceil(total/50) === 0) appendExplanatoryStatus(i, total, feasibleBlocks, w, h, false);
}
ctx.putImageData(imageData,0,0);
setProgress((i/total)*100);
if(i<total) requestAnimationFrame(processBatch);
else {
const bf = getBruteForceStats(total);
appendStatus(`\n<span class="stat-highlight">RECONSTRUCTION COMPLETE</span>`);
appendStatus(`Final Feasibility Score: ${(feasibleBlocks/total*100).toFixed(1)}%`);
appendStatus(`Search Space Exhausted: 1 in 10^${bf.totalLog10}`);
}
}
processBatch();
}
function handleFileInput(input, callback){
const file = input.files[0];
if(!file) return;
const img = new Image();
img.onload = ()=>{
const canvas = document.createElement('canvas');
canvas.width = img.width; canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img,0,0);
callback(ctx.getImageData(0,0,img.width,img.height),img.width,img.height);
};
img.src = URL.createObjectURL(file);
}
document.getElementById('originalInput').addEventListener('change', e=>{
handleFileInput(e.target,(data,w,h)=>{
originalImgData=data;
const canvas = document.getElementById('originalCanvas');
canvas.width=w; canvas.height=h;
canvas.getContext('2d').putImageData(data,0,0);
});
});
document.getElementById('scrambledInput').addEventListener('change', e=>{
handleFileInput(e.target,(data,w,h)=>{
scrambledImgData=data;
const canvas = document.getElementById('scrambledCanvas');
canvas.width=w; canvas.height=h;
canvas.getContext('2d').putImageData(data,0,0);
if(originalImgData && scrambledImgData){
feasibilityData=[];
const outputCanvas = document.getElementById('outputCanvas');
outputCanvas.width=w; outputCanvas.height=h;
updateStatus("Initializing heuristic analysis...");
reconstruct(originalImgData,scrambledImgData,w,h,outputCanvas);
}
});
});
let originalImgData=null;
let scrambledImgData=null;
</script>
</body>
</html>