//! 整数分组码(`GroupMode::IntegerCode`)的对拍与契约测试。 //! //! 这里的每一条都在回答「这条捷径到底能不能用」: //! //! * 与逐组比较(`GroupMode::Exact`)**逐位相同** —— 捷径不是新算法,是同一条 //! 语义的另一条建图路径; //! * 越界码被丢掉、非整数码落到上方最近一组 —— 这是**契约**,不是 bug,所以 //! 用测试钉住它,而不是等它在生产里悄悄发生; //! * 轮数确实更少,且少多少能报出来。 use mpc_core::ring; use mpc_sql::secure::{ query_rounds, secure_group_by, secure_group_by_coded, secure_group_by_pred, secure_group_by_pred_coded, Agg, CmpOp, GroupMode, Pred, }; use mpc_vault::{Pipe, Vault, VaultError}; const FXP: u32 = 18; fn enc(v: &[f64]) -> Vec { v.iter().map(|x| ring::encode(*x, FXP).unwrap()).collect() } /// 把一份明文分成两片(用一个临时 Vault 发牌)。 fn split(vals: &[f64]) -> (Vec, Vec) { let mut v = Vault::new(Pipe::pair().0, "alice", &["alice", "bob"], [7u8; 32], FXP).unwrap(); let all = v.split(&enc(vals)).unwrap(); let len = vals.len(); (all[0..len].to_vec(), all[len..2 * len].to_vec()) } /// 两方跑同一段查询,返回 `(alice 侧结果, bob 侧结果)`。 /// /// 用 `thread::scope` 而不是 `spawn`:闭包要借本地的 `codes` 切片, /// `'static` 那条约束是多余的。 fn run_pair(cols: &[&[f64]], f: F) -> (Vec>, Vec>) where F: Fn(usize, &mut Vault, &[Vec]) -> Result>, VaultError> + Sync, { let shares: Vec<(Vec, Vec)> = cols.iter().map(|c| split(c)).collect(); let sites = ["alice", "bob"]; let seed = [13u8; 32]; let (pa, pb) = Pipe::pair(); let mut va = Vault::new(pa, "alice", &sites, seed, FXP).unwrap(); let mut vb = Vault::new(pb, "bob", &sites, seed, FXP).unwrap(); let a: Vec> = shares.iter().map(|(x, _)| x.clone()).collect(); let b: Vec> = shares.iter().map(|(_, y)| y.clone()).collect(); std::thread::scope(|s| { let h = s.spawn(|| f(1, &mut vb, &b)); let out = f(0, &mut va, &a).unwrap(); (out, h.join().unwrap().unwrap()) }) } /// 只要 alice 侧结果。 fn run_one(cols: &[&[f64]], f: F) -> Vec> where F: Fn(usize, &mut Vault, &[Vec]) -> Result>, VaultError> + Sync, { run_pair(cols, f).0 } /// 两方都要拿到的错误(只在一侧报错说明建图不一致)。 fn run_err(cols: &[&[f64]], f: F) -> VaultError where F: Fn(usize, &mut Vault, &[Vec]) -> Result>, VaultError> + Sync, { let shares: Vec<(Vec, Vec)> = cols.iter().map(|c| split(c)).collect(); let sites = ["alice", "bob"]; let seed = [17u8; 32]; let (pa, pb) = Pipe::pair(); let mut va = Vault::new(pa, "alice", &sites, seed, FXP).unwrap(); let mut vb = Vault::new(pb, "bob", &sites, seed, FXP).unwrap(); let a: Vec> = shares.iter().map(|(x, _)| x.clone()).collect(); let b: Vec> = shares.iter().map(|(_, y)| y.clone()).collect(); std::thread::scope(|s| { let h = s.spawn(|| f(1, &mut vb, &b)); let out = f(0, &mut va, &a); let other = h.join().unwrap(); match (out, other) { (Err(e), Err(_)) => e, (Err(e), Ok(_)) => e, (Ok(_), Err(e)) => e, (Ok(_), Ok(_)) => panic!("期望两侧都报错,实际都成功了"), } }) } // --------------------------------------------------------------------------- // // 1. 与逐组比较对拍 // --------------------------------------------------------------------------- // const AMOUNTS: [f64; 5] = [100.0, 500.0, 250.0, 600.0, 300.0]; const CODES_3: [f64; 5] = [0.0, 1.0, 0.0, 2.0, 1.0]; const AGGS: [Agg; 3] = [Agg::Count, Agg::Sum, Agg::Avg]; fn both_modes(pred: Option<(f64, CmpOp)>, n_groups: usize, codes: &[f64]) -> (Vec>, Vec>) { let exact = run_one(&[&AMOUNTS, codes], |_, v, cs| { secure_group_by(v, &cs[0], &cs[1], n_groups, pred, &AGGS, 0) }); let coded = run_one(&[&AMOUNTS, codes], |_, v, cs| { secure_group_by_coded(v, &cs[0], &cs[1], n_groups, pred, &AGGS, 0) }); (exact, coded) } fn assert_same(exact: &[Vec], coded: &[Vec], what: &str) { assert_eq!(exact.len(), coded.len(), "{what}:组数不同"); for (g, (e, c)) in exact.iter().zip(coded.iter()).enumerate() { for (k, (a, b)) in e.iter().zip(c.iter()).enumerate() { match AGGS[k] { // COUNT / SUM 是整数,两条路径必须**逐位相同**。 Agg::Count | Agg::Sum => assert_eq!( a, b, "{what}:组 {g} 的 {:?} 不同(exact={a} coded={b})", AGGS[k] ), // AVG 的两条路径除法不一样,这是**唯一**会有差的地方: // Exact 走 `scale(sum, 1/count)`,系数被量化到 `2^-fxp` // (`1/5` 这种除不尽的最多差 `sum·2^-fxp`);coded 拿到精确整数和 // 之后在明文侧除,没有这层量化。所以按**相对**误差比。 Agg::Avg => { if a.is_nan() { assert!(b.is_nan(), "{what}:组 {g} 空组语义不同"); } else { let tol = 1e-4 * a.abs().max(1.0); assert!( (a - b).abs() <= tol, "{what}:组 {g} 的 AVG 差太多(exact={a} coded={b},容差 {tol})" ); } } Agg::Min | Agg::Max => unreachable!("这组测试不请求 MIN/MAX"), } } } } #[test] fn coded_matches_exact_without_where() { let (exact, coded) = both_modes(None, 3, &CODES_3); assert_same(&exact, &coded, "无 WHERE"); // 顺手把期望值钉住:组 0 两行(100+250)、组 1 两行(500+300)、组 2 一行。 assert_eq!(coded[0][0], 2.0); assert_eq!(coded[0][1], 350.0); assert_eq!(coded[1][1], 800.0); assert_eq!(coded[2][1], 600.0); assert_eq!(coded[2][2], 600.0); } #[test] fn coded_matches_exact_with_where() { let (exact, coded) = both_modes(Some((300.0, CmpOp::Lt)), 3, &CODES_3); assert_same(&exact, &coded, "带 WHERE"); // col < 300 只剩 100、250,都在 0 号组。 assert_eq!(coded[0][0], 2.0); assert_eq!(coded[0][1], 350.0); assert_eq!(coded[1][0], 0.0, "1 号组应当被 WHERE 清空"); } /// 复合谓词版(`secure_group_by_pred_coded`):与 `secure_group_by_pred` 对拍。 /// /// 这条路径多了两件事:谓词作用在**多列**上、聚合列在下标 `agg_col` 而不是 0。 /// 前缀段的输入布局是「用户列 ++ 分组码」,这两点任一处错位都只表现为数值不对 /// (不会崩),所以必须对着 Exact 跑一遍。 #[test] fn coded_pred_matches_exact() { let spend: [f64; 5] = [50.0, 100.0, 10.0, 700.0, 20.0]; let cols: [&[f64]; 3] = [&AMOUNTS, &spend, &CODES_3]; // WHERE amount < 400 AND spend > 5 let pred = Pred::And( Box::new(Pred::Cmp { col: 0, op: CmpOp::Lt, threshold: 400.0, }), Box::new(Pred::Cmp { col: 1, op: CmpOp::Gt, threshold: 5.0, }), ); let exact = run_one(&cols, |_, v, cs| { secure_group_by_pred(v, cs, 0, &cs[2], 3, Some(&pred), &AGGS, 0) }); let coded = run_one(&cols, |_, v, cs| { secure_group_by_pred_coded(v, cs, 0, &cs[2], 3, Some(&pred), &AGGS, 0) }); assert_same(&exact, &coded, "复合谓词"); // 命中第 0/2/4 行(100、250、300),码分别是 0、0、1: // 组 0 两行 350,组 1 一行 300,组 2 空。 assert_eq!(coded[0][0], 2.0); assert_eq!(coded[0][1], 350.0); assert_eq!(coded[1][0], 1.0); assert_eq!(coded[1][1], 300.0); assert_eq!(coded[2][0], 0.0); assert!(coded[2][2].is_nan(), "空组的 AVG 是 NaN"); } // --------------------------------------------------------------------------- // // 2. 契约:越界码与非整数码 // --------------------------------------------------------------------------- // /// 越界码(`< 0` 或 `>= n_groups`)被丢掉 —— 与逐组比较一致。 #[test] fn coded_drops_out_of_range_codes() { let codes = [0.0, 1.0, 5.0, -1.0, 2.0]; let (exact, coded) = both_modes(None, 2, &codes); assert_same(&exact, &coded, "越界码"); // `5` 和 `−1` 都越界(`5` ≥ N=2、`−1` < 0),只剩码 0 和 1 两行。 assert_eq!(coded[0][0], 1.0); assert_eq!(coded[1][0], 1.0); let total: f64 = coded.iter().map(|r| r[0]).sum(); assert_eq!(total, 2.0, "5 行里只有 2 行的码落在 0..2 内"); } /// **非整数码落到上方最近的一组**(`1.5 → 2 号组`)。 /// /// 这是 `GroupMode::IntegerCode` 与 `Exact` 唯一会给出不同数字的地方,属于契约, /// 不是缺陷:浮点码当分组码用,本来就该先用 `binning` 落成整数码再进密态。 #[test] fn coded_treats_non_integer_code_as_ceiling() { let codes = [0.0, 1.5, 2.0, 0.4, 0.6]; let (exact, coded) = both_modes(None, 3, &codes); // 逐组比较:1.5 / 0.4 / 0.6 谁都不属于,只有 0.0 与 2.0 两行有组。 assert_eq!(exact[0][0], 1.0); assert_eq!(exact[1][0], 0.0); assert_eq!(exact[2][0], 1.0); // 前缀和:它们被吸到上方最近的整数组。 assert_eq!(coded[0][0], 2.0, "0.0 和 0.4 都算 0 号组"); assert_eq!(coded[1][0], 1.0, "0.6 落到 1 号组"); assert_eq!(coded[2][0], 2.0, "1.5 和 2.0 落到 2 号组"); } /// `MIN` / `MAX` 不能用前缀和还原 —— 直接拒,不静默降级。 #[test] fn coded_rejects_min_max() { let err = run_err(&[&AMOUNTS, &CODES_3], |_, v, cs| { secure_group_by_coded(v, &cs[0], &cs[1], 3, None, &[Agg::Min], 0) }); let msg = err.to_string(); assert!( msg.contains("MIN") && msg.contains("Exact"), "报错该说清「不支持 MIN/MAX,请改用 Exact」,实际:{msg}" ); } /// 空组的语义与 `Exact` 一致:`COUNT`/`SUM` 是 0,`AVG` 是 `NaN`。 #[test] fn coded_empty_group_keeps_exact_semantics() { let codes = [0.0, 0.0, 0.0, 0.0, 0.0]; let (exact, coded) = both_modes(None, 2, &codes); assert_same(&exact, &coded, "空组"); assert_eq!(coded[1][0], 0.0); assert_eq!(coded[1][1], 0.0); assert!(coded[1][2].is_nan()); } /// k-匿名抑制:组太小整行 `NaN`,与 `Exact` 一致。 #[test] fn coded_respects_k_anonymity() { let rows = run_one(&[&AMOUNTS, &CODES_3], |_, v, cs| { secure_group_by_coded(v, &cs[0], &cs[1], 3, None, &AGGS, 3) }); // 每组最多 2 行,阈值 3 → 全部抑制。 for (g, row) in rows.iter().enumerate() { assert!( row.iter().all(|v| v.is_nan()), "组 {g} 应当被抑制,实际 {row:?}" ); } } /// 单组(`n_groups == 1`)时前缀只剩两个边界,结果应与整表聚合完全一致。 /// /// 注意 `n_groups == 1` 只认码 `0`:码 `1`、`2` 都算越界(`≥ N`)被丢掉 —— /// 边界值就是 `−0.5` 与 `0.5`,没有第三个边界可给它们。 #[test] fn coded_single_group_equals_whole_table() { let all_zero = [0.0; 5]; let rows = run_one(&[&AMOUNTS, &all_zero], |_, v, cs| { secure_group_by_coded(v, &cs[0], &cs[1], 1, None, &AGGS, 0) }); assert_eq!(rows.len(), 1); assert_eq!(rows[0][0], 5.0, "5 行全在唯一的一组里"); assert_eq!(rows[0][1], 1750.0); assert!((rows[0][2] - 350.0).abs() < 1e-9); } // --------------------------------------------------------------------------- // // 3. 轮数:确实更少,且能报出来 // --------------------------------------------------------------------------- // #[test] fn coded_costs_fewer_rounds_and_the_number_is_reported() { let agg = [Agg::Count, Agg::Sum]; for n in [2usize, 4, 8] { let exact = query_rounds(8, FXP, 1, None, Some(n), &agg, GroupMode::Exact).unwrap(); let coded = query_rounds(8, FXP, 1, None, Some(n), &agg, GroupMode::IntegerCode).unwrap(); assert!( coded.rounds() < exact.rounds(), "{n} 组时整数码模式应当更少:exact {} vs coded {}", exact.rounds(), coded.rounds() ); // 整数码模式下没有第二段:所有组的输出在一次批量 open 里出来。 assert_eq!(coded.agg_rounds(), 0); assert!(coded.aggs.is_none()); // **组数不再影响轮数**:同阶段的比较合成一批(`sec_cmp_batch`), // 9 轮封顶,与有几个组无关。批量之前这里是「每组 18~20 轮」。 assert_eq!( coded.rounds(), 12, "{n} 组的轮数应当与组数无关(批量比较),实际 {}", coded.rounds() ); assert_eq!(exact.rounds(), 13, "{n} 组的逐组比较也应是常数轮"); } // 钉住实测值:8 组逐组 162(未做批量比较时)→ 现在 13 / 12。 let exact = query_rounds(8, FXP, 1, None, Some(8), &agg, GroupMode::Exact).unwrap(); let coded = query_rounds(8, FXP, 1, None, Some(8), &agg, GroupMode::IntegerCode).unwrap(); assert_eq!(exact.rounds(), 13); assert_eq!(coded.rounds(), 12); } /// 带 WHERE 只多「一次比较 + 一次码搬家乘」。 #[test] fn coded_where_costs_only_the_park_mul() { let agg = [Agg::Count, Agg::Sum]; let free = query_rounds(8, FXP, 1, None, Some(4), &agg, GroupMode::IntegerCode).unwrap(); let pred = Pred::Cmp { col: 0, op: CmpOp::Lt, threshold: 300.0, }; let with_where = query_rounds( 8, FXP, 1, Some(&pred), Some(4), &agg, GroupMode::IntegerCode, ) .unwrap(); // WHERE 本身一次比较(9 轮)+ 码搬家一次 mul_raw(2 轮)= 11 轮; // 实测 10,因为有一轮 `open` 恰好并进了搬家那个阶段。 let extra = with_where.rounds() - free.rounds(); assert!( (9..=11).contains(&extra), "带 WHERE 只该多「一次比较 + 一次搬家乘」(9~11 轮),实际 {extra}:{} → {}", free.rounds(), with_where.rounds() ); } /// 整数码模式下 `fxp == 0` 必须报错:`g + 0.5` 会被舍成整数,边界全错一位。 #[test] fn coded_rejects_fxp_zero() { let err = match query_rounds(8, 0, 1, None, Some(4), &[Agg::Count], GroupMode::IntegerCode) { Ok(_) => panic!("fxp=0 必须报错"), Err(e) => e, }; assert!(err.to_string().contains("fxp"), "实际:{err}"); }