Files
ssw-mvp/demo/secure_compare/index.html
T
admin eed3bc9b1d demo(secure_compare): 加示例数据预览面板,加载示例后能看见 alice/bob 5 行
原 #load 只改 threshold 一个数字 + 一闪而过的 toast,alice/bob 数据硬编码在 JS,用户看到表格空着以为没加载/没反应。
新增预览面板(双列 mini 表),#load 可见地填充 alice/bob 各 5 行;#run 状态徽标从等待→密文计算中…→完成 ms + 完整结果表。

Playwright 真实浏览器回归:点 #load 后 alice/bob 各 5 行可见、点 #run 后 127ms 完成、密文 amax=1408 (行 4) 正确。
2026-08-11 20:54:20 +08:00

215 lines
10 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>密文比较 · secure_compare</title>
<link rel="stylesheet" href="/_shared/demo.css" />
<style>
.layout { padding: 18px 28px; display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 14px 18px; }
.panel h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-dim); margin-bottom: 10px; }
table { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 12px; }
table th, table td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; }
table th { color: var(--text-faint); font-weight: 500; }
.row-hit { background: rgba(74, 222, 128, 0.08); }
.row-win { background: rgba(251, 191, 36, 0.10); }
.num { color: var(--text); font-weight: 600; }
.ok { color: var(--ok); }
.warn { color: var(--warn); }
.summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-top: 12px; }
.stat { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; }
.stat .label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.stat .value { font-size: 16px; font-weight: 700; margin-top: 4px; color: var(--text); font-family: var(--mono); }
.stat .value.ok { color: var(--ok); }
.stat .value.warn { color: var(--warn); }
.topbar { display: flex; align-items: center; gap: 14px; padding: 14px 28px; border-bottom: 1px solid var(--border); background: var(--panel); }
.topbar a.home { color: var(--text-dim); text-decoration: none; font-size: 12px; }
.topbar h1 { font-size: 18px; margin: 0; font-weight: 700; }
.topbar h1 small { font-size: 12px; color: var(--text-faint); margin-left: 6px; font-weight: 400; }
.topbar .right { margin-left: auto; display: flex; gap: 12px; align-items: center; }
.badge { background: var(--panel-2); border: 1px solid var(--border); border-radius: 5px; padding: 3px 9px; font-size: 11px; color: var(--text-dim); font-family: var(--mono); }
.badge.ok { color: var(--ok); border-color: rgba(74,222,128,0.3); }
.controls { display: flex; gap: 10px; align-items: center; padding: 12px 28px; background: var(--bg); border-bottom: 1px solid var(--border); }
.controls input, .controls button { background: var(--panel); border: 1px solid var(--border); border-radius: 5px; color: var(--text); padding: 6px 12px; font-family: var(--mono); font-size: 12px; }
.controls button { cursor: pointer; }
.controls button.primary { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.controls label { font-size: 11px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.5px; }
</style>
</head>
<body>
<style>
table.mini { width:100%; border-collapse:collapse; font-size:12px }
table.mini th, table.mini td { padding:4px 8px; border-bottom:1px solid var(--border) }
table.mini th { color:var(--text-dim); font-weight:500; text-align:left }
table.mini td.num { text-align:right; font-variant-numeric:tabular-nums }
</style>
<div class="topbar">
<a href="/" class="home"><img src="https://shangshuwang.cn/images/logo-white-data.png" alt="" style="height:22px;vertical-align:middle"></a>
<h1>🔍 密文比较 <small>edaBit + BitLT · 零泄露找出 "存款最高 VIP 客户"</small></h1>
<div class="right">
<span class="badge">nax.device.vault.lt/gt/eq/select/max/argmax</span>
<span class="badge" id="status">等待</span>
<a href="/" class="home">← 算子集市</a>
</div>
</div>
<div class="controls">
<label>门槛(共同客户"总存款 > X")</label>
<input id="threshold" type="number" value="500" step="50" style="width:90px" />
<button class="primary" id="run">▶ 跑密文比较</button>
<button id="load">📦 加载示例(5 共同客户)</button>
</div>
<div id="preview" class="panel" style="display:none; margin-bottom:14px">
<h2 style="margin-top:0">📋 已加载的示例数据 <span style="font-size:11px;color:var(--text-dim);font-weight:normal">(点击「▶ 跑密文比较」后下方会追加密文运算结果)</span></h2>
<div style="display:grid;grid-template-columns:1fr 1fr;gap:18px;font-size:13px">
<div>
<div style="color:var(--text-dim);font-size:11px;margin-bottom:6px">🏦 Alice 行存款</div>
<table class="mini"><thead><tr><th>#</th><th class="num">值</th></tr></thead><tbody id="prev-alice"></tbody></table>
</div>
<div>
<div style="color:var(--text-dim);font-size:11px;margin-bottom:6px">🏦 Bob 行存款</div>
<table class="mini"><thead><tr><th>#</th><th class="num">值</th></tr></thead><tbody id="prev-bob"></tbody></table>
</div>
</div>
</div>
<div class="layout">
<div class="panel">
<h2>共同客户(第 i 行) · 明文对照</h2>
<table id="result">
<thead><tr>
<th>#</th><th>A 行存款</th><th>B 行存款</th><th>总存款</th><th>总存款 &gt; 门槛?</th><th>select 保留</th>
</tr></thead>
<tbody></tbody>
</table>
<div class="summary">
<div class="stat"><div class="label">超过门槛的客户</div><div class="value ok" id="s-over">—</div></div>
<div class="stat"><div class="label">⓵ 密文 amax</div><div class="value" id="s-max">—</div></div>
<div class="stat"><div class="label">⓶ 密文 amin</div><div class="value warn" id="s-min">—</div></div>
<div class="stat"><div class="label">⓷ 共享 EQ(==max)</div><div class="value ok" id="s-eq">—</div></div>
</div>
</div>
<div class="panel">
<h2>密文比较协议栈(信息论零泄露)</h2>
<pre style="font-family:var(--mono); font-size: 11px; color: var(--text-dim); line-height: 1.6; white-space: pre-wrap;">
// 1. 准备:dealer 离线发 64 位随机 r(PIE 掩码)
// 所有共享 r 之后只各持一片,PIE 发生时不依赖 x。
// 2. 在线:c = x ⊕ r · c 与 x 统计独立(信息论零泄露)
// ├─ lt/gt/eq: 1 轮密文乘 + 1 次 open
// ├─ select(cond, x, y): cond ? x : y(不经过截断)
// ├─ amax/amin: ⌈log₂n⌉ 轮密文锦标赛(5 个 → 3 轮)
// └─ argmax: 返回 (密文极值, 明文下标)
// 3. result: 任何参与方拿到的中间产物都是均匀随机数环元素
// 唯一明文出口是 .reveal(to="alice") 那一刻。
</pre>
<h2 style="margin-top:18px">为什么"零泄露"?</h2>
<ul style="font-size: 12px; color: var(--text-dim); line-height: 1.7; padding-left: 20px;">
<li>BitLT 协议:x + r 打开后,任何参与方拿到的 c = x + r 模 2^64 看上去是均匀随机数</li>
<li>定理:P(c | x) = P(c | x') 对任意 x, x'(c 与 x 统计独立)</li>
<li>这意味着 Bob 永远无法从自己看到的数据反推 Alice 的存款</li>
<li>最终 reveal 时只输出一位数(max / over_mask),不暴露全部明细</li>
</ul>
</div>
</div>
<script>
const $ = (s) => document.querySelector(s);
const EXAMPLE = {
alice: [188, 245, 89, 723, 156], // 同 PSI 场景的 5 共同客户在 A 行
bob: [175, 198, 56, 685, 122], // 在 B 行
threshold: 500,
};
$('#load').onclick = () => {
$('#threshold').value = EXAMPLE.threshold;
// 把示例数据视觉化渲染进表格,让用户清楚看到「加载上了」
renderPreview(EXAMPLE.alice, EXAMPLE.bob);
toast('已加载 5 共同客户示例(alice/bob 各 5 行)');
};
function renderPreview(a, b) {
const pa = $('#prev-alice'); const pb = $('#prev-bob');
if (!pa || !pb) return;
pa.innerHTML = ''; pb.innerHTML = '';
a.forEach((v, i) => {
pa.insertAdjacentHTML('beforeend', `<tr><td>${i + 1}</td><td class="num">${v}</td></tr>`);
pb.insertAdjacentHTML('beforeend', `<tr><td>${i + 1}</td><td class="num">${b[i]}</td></tr>`);
});
$('#preview').style.display = '';
}
$('#run').onclick = async () => {
const a = EXAMPLE.alice;
const b = EXAMPLE.bob;
const th = parseFloat($('#threshold').value);
if (!a || !b) return toast('请先加载示例');
$('#status').textContent = '密文计算中…';
$('#status').className = 'badge';
const t0 = performance.now();
try {
const r = await fetch('/secure_compare/api/run', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ alice: a, bob: b, threshold: th }),
});
const j = await r.json();
if (j.error) throw new Error(j.error);
render(j);
const ms = (performance.now() - t0).toFixed(0);
$('#status').textContent = `完成 · ${ms} ms`;
$('#status').className = 'badge ok';
} catch (e) {
$('#status').textContent = '失败';
$('#status').className = 'badge';
toast('❌ ' + e.message);
}
};
function render(j) {
const tbody = $('#result tbody');
tbody.innerHTML = '';
j.total.forEach((t, i) => {
const tr = document.createElement('tr');
const over = j.over_threshold[i];
const eqMax = j.customers_eq_max[i];
const isWin = i === j.max_idx;
if (isWin) tr.className = 'row-win';
else if (over) tr.className = 'row-hit';
tr.innerHTML = `
<td>${i + 1}</td>
<td>${j.deposits_a[i]}</td>
<td>${j.deposits_b[i]}</td>
<td class="num">${t.toFixed(0)}</td>
<td class="num ${over ? 'ok' : 'warn'}">${over ? '✓ 1' : '✗ 0'}</td>
<td class="num">${j.masked_total[i].toFixed(0)}</td>
`;
tbody.appendChild(tr);
});
$('#s-over').textContent = j.over_count + ' 人';
$('#s-max').textContent = `${j.max_value} (行 ${j.max_idx + 1})`;
$('#s-min').textContent = `${j.min_value} (行 ${j.min_idx + 1})`;
$('#s-eq').textContent = j.customers_eq_max.filter(x => x).length + ' 人';
}
function toast(msg) {
const t = document.createElement('div');
t.textContent = msg;
t.style.cssText = 'position:fixed;bottom:24px;left:50%;transform:translateX(-50%);background:var(--panel);border:1px solid var(--border);padding:8px 16px;border-radius:6px;font-size:12px;z-index:1000';
document.body.appendChild(t);
setTimeout(() => t.remove(), 1800);
}
$('#load').click();
</script>
</body>
</html>