仅设置参数 + 一闪 toast 不够,用户要知道「跑完会出什么样的真实结果」。
为 secure_metrics / secure_stats / dp_primitives / aggregator_chain 加「🎯 跑样例看结果」按钮,用固定样本数据调后端跑一次,把完整结果渲染到页面(复用现有 render 函数)。
真实验证输出示例(来自后端):
secure_metrics: ACC=1.000 / AUC=1.000 / log_loss=0.198 / MSE=0.038
secure_stats: Pearson r=0.8807(密文与明文误差 6e-4)
dp_primitives: ε=22.918 / δ=1e-5 / raw=147.00 / noisy=1.15
aggregator_chain: plain_mean=[1.233,2.267,3.300,4.333] dp_mean=[-0.776,-0.154,0.184,1.212]
Playwright 真实浏览器验证:4 模块 sample 按钮点击后结果区填入数字、无 JS 报错。
227 lines
11 KiB
HTML
227 lines
11 KiB
HTML
<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<title>安全统计 · secure_stats</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; }
|
||
.topbar { display: flex; align-items: center; gap: 14px; padding: 14px 28px; border-bottom: 1px solid var(--border); background: var(--panel); }
|
||
.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; }
|
||
.controls { display: flex; gap: 10px; align-items: center; padding: 12px 28px; background: var(--bg); border-bottom: 1px solid var(--border); }
|
||
.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; 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; }
|
||
table { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 12px; margin-top: 6px; }
|
||
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; }
|
||
.summary { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-top: 10px; }
|
||
.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: 18px; font-weight: 700; margin-top: 4px; color: var(--text); font-family: var(--mono); }
|
||
.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); }
|
||
.corr-matrix { display: grid; grid-template-columns: auto 1fr 1fr; gap: 4px; font-family: var(--mono); font-size: 13px; }
|
||
.corr-cell { padding: 10px 14px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; text-align: center; }
|
||
.corr-diag { color: var(--text-faint); }
|
||
.row-ok { background: rgba(74, 222, 128, 0.08); }
|
||
.topbar .right { margin-left: auto; display: flex; gap: 12px; align-items: center; }
|
||
.topbar a.home { color: var(--text-dim); text-decoration: none; font-size: 12px; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
|
||
<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>secure_mean / var / pearson / correlation_matrix</small></h1>
|
||
<div class="right">
|
||
<span class="badge">nax.data.stats</span>
|
||
<span class="badge" id="status">等待</span>
|
||
<a href="/" class="home">← 算子集市</a>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="controls">
|
||
<label>样本数 n</label>
|
||
<input id="n" type="number" value="20" min="5" max="200" style="width:70px;background:var(--panel);border:1px solid var(--border);border-radius:5px;color:var(--text);padding:6px 10px;font-family:var(--mono);font-size:12px" />
|
||
<label>真实相关系数 r</label>
|
||
<input id="r" type="number" value="0.85" step="0.05" min="-1" max="1" style="width:70px;background:var(--panel);border:1px solid var(--border);border-radius:5px;color:var(--text);padding:6px 10px;font-family:var(--mono);font-size:12px" />
|
||
<button class="primary" id="run">▶ 跑密文统计</button>
|
||
<button id="sample">🎯 跑样例看结果</button>
|
||
<button id="load">📦 加载示例(医院年龄 vs 保险保费)</button>
|
||
</div>
|
||
|
||
<div id="load-info" style="display:none;margin:0 28px 14px;padding:12px 16px;background:var(--panel-2);border:1px solid var(--accent);border-radius:8px;font-size:12px;font-family:var(--mono);color:var(--text-dim)"></div>
|
||
|
||
<div class="layout">
|
||
<div class="panel">
|
||
<h2>密文均值 / 方差 / 皮尔逊</h2>
|
||
<table>
|
||
<thead><tr><th>指标</th><th>密文结果</th><th>明文校验</th><th>绝对误差</th></tr></thead>
|
||
<tbody id="stats-tbody"></tbody>
|
||
</table>
|
||
|
||
<h2 style="margin-top:18px">密文相关系数矩阵</h2>
|
||
<div class="corr-matrix">
|
||
<div></div><div class="corr-cell">Alice (年龄)</div><div class="corr-cell">Bob (保费)</div>
|
||
<div class="corr-cell">Alice (年龄)</div><div class="corr-cell corr-diag" id="m-00">—</div><div class="corr-cell" id="m-01">—</div>
|
||
<div class="corr-cell">Bob (保费)</div><div class="corr-cell" id="m-10">—</div><div class="corr-cell corr-diag" id="m-11">—</div>
|
||
</div>
|
||
|
||
<div class="summary">
|
||
<div class="stat"><div class="label">⓵ Pearson r (密文)</div><div class="value ok" id="s-pearson">—</div></div>
|
||
<div class="stat"><div class="label">⓶ 明文校验 r</div><div class="value" id="s-plain">—</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;">
|
||
// 典型场景:医院想知道"年龄 vs 保费"相关性
|
||
// ├─ 医院不愿交出患者年龄(HIPAA / GDPR)
|
||
// └─ 保险公司不愿交出保费明细(商业机密)
|
||
//
|
||
// 密文统计的做法:
|
||
// step 1: 各自把数据 share() 进 vault → 切分片
|
||
// step 2: 在密文上做 (x - μ_x) * (y - μ_y) 等去中心化运算
|
||
// step 3: secure_pearson = cov(x, y) / (σ_x * σ_y) 全部密文
|
||
// step 4: 最终 reveal 一个标量 r,不暴露任何原始数据
|
||
//
|
||
// 对比:明文统计 = Σ(x_i * y_i) / n - μ_x * μ_y
|
||
// 密文统计 = vault.mean(vault.mul(...)) 数值上等价
|
||
</pre>
|
||
|
||
<h2 style="margin-top:18px">底层协议</h2>
|
||
<ul style="font-size: 12px; color: var(--text-dim); line-height: 1.7; padding-left: 20px;">
|
||
<li>secret_ref 一加一减:vault.f_add(公开加法)</li>
|
||
<li>乘法:Beaver 三元组(n 轮交互,fxp=18 截断)</li>
|
||
<li>去中心化:x - mean(x) 不需要 reveal 中间值</li>
|
||
<li>reveal: r = secure_pearson(...).reveal(to="alice") 单点输出</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
const $ = (s) => document.querySelector(s);
|
||
|
||
// 固定示例 → 稳定的真实结果参考
|
||
const SAMPLE_STATS = {
|
||
alice: [25,32,45,28,55,38,67,29,40,50,33,42,60,30,48,36,52,44,27,41],
|
||
bob: [2000,4500,3500,2100,5200,3200,5000,1800,4000,4800,2800,3900,5400,2200,4300,3300,5100,4200,2400,3700],
|
||
};
|
||
$('#sample').onclick = async () => {
|
||
const btn = $('#sample');
|
||
const oldText = btn.textContent;
|
||
btn.disabled = true; btn.textContent = '⏳ 跑样例中…';
|
||
try {
|
||
const r = await fetch('/secure_stats/api/run', {
|
||
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(SAMPLE_STATS),
|
||
});
|
||
const j = await r.json();
|
||
if (j.error) throw new Error(j.error);
|
||
render(j);
|
||
toast(`✅ 样例跑完(n=${j.n}):Pearson r=${j.secure_pearson.toFixed(4)}`);
|
||
} catch (e) {
|
||
toast('❌ ' + e.message);
|
||
} finally {
|
||
btn.disabled = false; btn.textContent = oldText;
|
||
}
|
||
};
|
||
|
||
$('#load').onclick = () => {
|
||
$('#n').value = 20;
|
||
$('#r').value = 0.85;
|
||
$('#load-info').innerHTML =
|
||
`<b style="color:var(--text)">📦 已加载示例配置</b> · ` +
|
||
`n=<b style="color:var(--accent)">${$('#n').value}</b> 对 (Alice 年龄 + Bob 保费) · ` +
|
||
`真实相关系数 r=<b style="color:var(--accent)">${$('#r').value}</b> · ` +
|
||
`年龄分布 20-70 岁,保费 ≈ ${$('#r').value} × (年龄-45) × 200 + 噪声 · ` +
|
||
`点击「▶ 跑密文统计」真跑 nax.data.stats 算 secure_mean/var/pearson/correlation_matrix`;
|
||
$('#load-info').style.display = '';
|
||
toast('已加载:医院年龄 vs 保险保费示例');
|
||
};
|
||
|
||
$('#run').onclick = async () => {
|
||
const n = parseInt($('#n').value);
|
||
const r = parseFloat($('#r').value);
|
||
if (n < 5) return toast('n 至少 5');
|
||
|
||
// 服务端生成相关数据;用户不直接看到
|
||
// 但演示逻辑让前端展示"原始数据"
|
||
const alice_age = []; const bob_prem = [];
|
||
for (let i = 0; i < n; i++) {
|
||
const x = 20 + Math.random() * 50; // 20-70 岁
|
||
const noise = (Math.random() - 0.5) * 0.6;
|
||
const y = 2000 + r * (x - 45) * 200 + noise * 5000; // 保费 ≈ r × 年龄 + 噪声
|
||
alice_age.push(Number(x.toFixed(1)));
|
||
bob_prem.push(Number(y.toFixed(0)));
|
||
}
|
||
|
||
$('#status').textContent = '密文统计中…';
|
||
const t0 = performance.now();
|
||
try {
|
||
const resp = await fetch('/secure_stats/api/run', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ alice: alice_age, bob: bob_prem }),
|
||
});
|
||
const j = await resp.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 = '失败';
|
||
toast('❌ ' + e.message);
|
||
}
|
||
};
|
||
|
||
function render(j) {
|
||
const tbody = $('#stats-tbody');
|
||
tbody.innerHTML = '';
|
||
const rows = [
|
||
['mean (Alice / 年龄)', j.secure_mean_age, j.plain_mean_age],
|
||
['mean (Bob / 保费)', j.secure_mean_prem, j.plain_mean_prem],
|
||
['var (Alice / 年龄)', j.secure_var_age, j.plain_var_age],
|
||
['var (Bob / 保费)', j.secure_var_prem, j.plain_var_prem],
|
||
['Pearson r', j.secure_pearson, j.plain_pearson],
|
||
];
|
||
rows.forEach(([label, sec, plain]) => {
|
||
const tr = document.createElement('tr');
|
||
const diff = Math.abs(sec - plain);
|
||
tr.innerHTML = `
|
||
<td>${label}</td>
|
||
<td class="num ok">${sec.toFixed(4)}</td>
|
||
<td class="num">${plain.toFixed(4)}</td>
|
||
<td class="num ${diff < 0.01 ? '' : 'warn'}" style="color:${diff < 0.01 ? 'var(--text-faint)' : 'var(--warn)'}">${diff.toFixed(4)}</td>`;
|
||
tbody.appendChild(tr);
|
||
});
|
||
|
||
const m = j.secure_corr_matrix;
|
||
$('#m-00').textContent = m[0][0].toFixed(3);
|
||
$('#m-01').textContent = m[0][1].toFixed(3);
|
||
$('#m-10').textContent = m[1][0].toFixed(3);
|
||
$('#m-11').textContent = m[1][1].toFixed(3);
|
||
|
||
$('#s-pearson').textContent = j.secure_pearson.toFixed(4);
|
||
$('#s-plain').textContent = j.plain_pearson.toFixed(4);
|
||
}
|
||
|
||
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>
|