Files
ssw-mpc/crates/nax-sql/Cargo.toml
T
rong 903f14efb2 feat(sql): GROUP BY 走整数码前缀和,8 组 162 → 92 轮
影响:`secure_group_by` 新增 `GroupMode::IntegerCode` 路径,每组相等判断
(`bit_eq` = 两次 `bit_lt`,18 轮/组、串行累加)换成 `N+1` 个单调前缀
「码 ≤ g」,`COUNT` 取前缀位之和、`SUM` 取 `mul_raw(前缀位, 列)`,所有 `open`
落在同一阶段合成一次,**组内值在明文侧差分**,没有第二段图。默认仍是
`GroupMode::Exact`,语义不变。

实测(`nax sql-plan --rows 8 --aggs count,sum`):2 组 42→32、4 组 82→52、
8 组 162→92;每组建图成本约 20 → 11 轮。带 WHERE 的增量是「一次比较 + 一次
搬家乘」10~11 轮。

新增:

- `secure_graph::build_prefix_stage` —— 谓词 → 码搬家 → N+1 个前缀 → 累积和
- `secure_group_by_coded` / `secure_group_by_pred_coded`,`query_rounds` 收
  `GroupMode`,`nax sql-plan --group-mode coded`、`nax sql --group-mode coded`
- `crates/nax-sql/tests/coded.rs` 12 条:与 Exact 对拍(`COUNT`/`SUM` 逐位、
  `AVG` 相对 1e-4)、复合谓词、越界/非整数码契约、空组、k-匿名、单组、轮数钉值

三条契约(写进 `docs/IR.md` §3.3,不隐藏代价):分组码必须是整数(非整数落
到上方最近一组);不支持 MIN/MAX(前缀和还原不出极值,直接报错不降级);
越界码丢弃(与逐组比较一致,两端边界 `−0.5` / `N−0.5` 就是为它留的)。

WHERE 未命中行不逐组乘 `base`(`2N` 轮),而是把码搬到 `−1000·2^fxp` 让它在
差分中自动抵消。**第一版把 `−1000` 当 raw 用**:分组码列是 fxp 编码,
`0.5` 编出来是 `131072`,于是 `−1000` 比每个边界都小,全部行被算进每个前缀
(行数变全表、SUM 变全表之和且不报错)。改为按 `2^fxp` 缩放,`fxp > 40` 拒绝。

变异验证 4 条全部按预期变红后还原:停放值不缩放(2 红)、边界少 `−0.5`
(只有非整数码契约那条红,整数码下 `<g` 与 `<g+0.5` 等价)、累积和忘记差分
(7 红)。

全量 462 用例通过 / 0 失败,`-Dwarnings` 零告警,`nax-core` no_std 守线过。
2026-11-01 19:49:16 +08:00

41 lines
1.4 KiB
TOML
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.
[package]
name = "nax-sql"
description = "SCQL 受限 SQL:AST、解析器、明文执行语义。"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[dependencies]
# 可选:把掩码 WHERE + 聚合接到真实的 Vault 密态算子上。
# 默认关闭,保持解析/明文引擎零密码学依赖。
nax-core = { workspace = true, optional = true }
nax-vault = { path = "../nax-vault", optional = true }
# 密态路径的协议序列不手写,改成「建图 → 编译 → 执行」,
# 交互轮数由编译器报出(见 `secure_graph.rs`)。
nax-ir = { path = "../nax-ir", optional = true }
[features]
secure = ["dep:nax-core", "dep:nax-vault", "dep:nax-ir"]
# 仅测试用:JOIN + PSI 的端到端对拍要真的跑一遍两方 PSI。
# **不是**库依赖 —— `nax-sql` 的库依赖图里没有 `nax-node`,
# 密态 SQL 仍然只吃「已对齐的分片」,PSI 由调用方(这里就是测试)驱动。
[dev-dependencies]
nax-node = { path = "../nax-node" }
nax-psi = { path = "../nax-psi" }
# 迁移回归:包一层传输层,数「手写序列」和「图」各花了几个往返。
nax-proto = { workspace = true }
[[test]]
name = "join_psi"
required-features = ["secure"]
[[test]]
name = "migration"
required-features = ["secure"]
[[test]]
name = "coded"
required-features = ["secure"]