//! Distributed Point Function (DPF) implementation. //! //! This module builds 2-party DPF keys for point functions over a finite //! domain. Given two keys and an index, XORing the two evaluations yields 1 //! at the target index and 0 elsewhere. //! //! API surface: //! - `Dpf(n)` returns a type for a `2^n` domain with `init`, `generateKeys`, `evaluate`, //! `destroyKey`, and `deinit` methods. //! //! References: //! - Daniel Kales' [CPP DPF implementation](https://github.com/dkales/dpf-cpp/) //! - David Heath's Secure Computation slides on PIR, formerly in //! https://courses.grainger.illinois.edu/cs507/fa2025/ but has been moved. const std = @import("std"); const Io = std.Io; const Aes = std.crypto.core.aes.Aes128; const AesEncryptCtx = std.crypto.core.aes.AesEncryptCtx(Aes); /// Length of one AES key, the size of each half of a `PrgKey`. const aes_key_length = Aes.key_bits / 8; /// A PRG output is one cipher block wide, so the compound word follows the /// cipher's block size rather than a fixed width. const compound_bits = Aes.key_bits; /// One whole PRG block: a `Seed` together with the indicator bit above it. /// /// This is the transport type — what AES reads and writes, and what seeds are /// held in once their MSB is known to be clear. A plain integer rather than /// `Expansion` so it stays a single register-sized value through the hot loops. const Compound = std.meta.Int(.unsigned, compound_bits); /// A DPF seed: one PRG block minus the indicator bit. /// /// Widens freely to `Compound` for AES and correction-word arithmetic. Narrowing /// back needs `@intCast`, which asserts the MSB-clear invariant instead of /// silently dropping a set indicator bit. const Seed = std.meta.Int(.unsigned, compound_bits - 1); /// A PRG block, split into the two outputs a DPF node draws from it: the low /// `compound_bits - 1` bits become the child `Seed` and the top bit is the /// indicator. /// /// Packed struct fields run from least to most significant bit, so `indicator` /// is the block's top bit. Bit-casting a block to this is how seeds and /// indicators are separated, in place of shifting and masking by hand. The whole /// struct is one `Compound` wide, so passing it by value keeps it in a register. const Expansion = packed struct(Compound) { seed: Seed, indicator: u1, }; /// Whether `compound` carries no indicator bit, the invariant every DPF seed /// holds while it is held in a `Compound`. inline fn seedMsbClear(compound: Compound) bool { const parts: Expansion = @bitCast(compound); return parts.indicator == 0; } /// Correction word for DPF key, used during evaluation to correct the PRF output /// when constructing the DPF tree. const CorrectionWord = struct { /// Correction seed value seed: Seed, /// Left indicator bit correction left_indicator: u1, /// Right indicator bit correction right_indicator: u1, }; /// Number of tree nodes whose PRG expansion is issued as one batched AES call. /// /// The target's recommended AES parallelism, used as reported. A node's two /// children are encrypted under different keys, so the left and right halves of /// a batch are separate `encryptWide` calls and each wants the full recommended /// width. Levels are tiled by this width with a comptime-sized remainder, so the /// odd widths several targets report work as well as powers of two. const prg_batch = Aes.block.parallel.optimal_parallel_blocks; /// Byte budget for one subtree's seed working set, which the bottom of the tree /// is sized to fit. /// /// The standard library reports the target's AES parallelism but nothing about /// cache capacity, so this is the one figure that cannot be derived. It is /// deliberately conservative rather than tuned: 8 KiB fits inside the smallest /// L1 data cache found on cores with AES acceleration, and throughput measures /// flat across a wide range of larger budgets, so raising it for a particular /// machine buys nothing. const subtree_seed_budget = 8 << 10; comptime { if (subtree_seed_budget < @sizeOf(Compound)) { @compileError("subtree_seed_budget must have room for at least one seed"); } } /// 256-bit key for the PRG (Pseudo-Random Generator). /// Contains two 128-bit AES keys: one for left PRG, another for right PRG. pub const PrgKey = struct { left: [aes_key_length]u8, right: [aes_key_length]u8, }; /// AES-based PRG for DPF. Takes in a `key` AES block, a `seed` held in a whole /// block, and the `exp` that will be filled. /// /// Function expects `seed` MSB to be cleared. fn prg(ctx: AesEncryptCtx, seed: Seed, exp: *Expansion) void { var raw: Compound = undefined; ctx.encrypt(@ptrCast(&raw), @ptrCast(&seed)); exp.* = @bitCast(raw); } /// Expand `count` sibling-independent DPF tree nodes with a single pair of /// batched AES calls, apply the level's correction word, and write the /// `2 * count` children interleaved as left, right, left, right, ... /// /// `dst_seeds` is only written when `store_seeds` is set; the last tree level /// needs indicator bits alone. Destinations may overlap the sources, which is /// what lets a level be expanded in place. /// /// Expects every source seed's MSB to be cleared; children keep that invariant. inline fn prgBatch( left_ctx: AesEncryptCtx, right_ctx: AesEncryptCtx, cw: *const CorrectionWord, comptime count: usize, comptime store_seeds: bool, src_seeds: *const [count]Seed, src_indicators: *const [count]u1, dst_seeds: [*]Seed, dst_indicators: [*]u1, ) void { // Read the sources out before writing anything, so overlap is harmless. const seeds: [count]Seed = src_seeds.*; const indicators: [count]u1 = src_indicators.*; inline for (seeds) |seed| { std.debug.assert(seedMsbClear(seed)); } var left: [count]Compound = undefined; var right: [count]Compound = undefined; left_ctx.encryptWide(count, @ptrCast(&left), @ptrCast(&seeds)); right_ctx.encryptWide(count, @ptrCast(&right), @ptrCast(&seeds)); inline for (0..count) |i| { const indicator = indicators[i]; // All ones when the correction applies, all zeros otherwise. Keeps the // per-node correction branchless so batches stay uniform. const seed_correction = cw.seed & (@as(Seed, 0) -% @as(Seed, indicator)); // `Expansion` is a single `Seed`, so these stay in registers. const left_child: Expansion = @bitCast(left[i]); const right_child: Expansion = @bitCast(right[i]); dst_indicators[2 * i] = left_child.indicator ^ (cw.left_indicator & indicator); dst_indicators[2 * i + 1] = right_child.indicator ^ (cw.right_indicator & indicator); if (store_seeds) { dst_seeds[2 * i] = @as(Seed, left_child.seed) ^ seed_correction; dst_seeds[2 * i + 1] = @as(Seed, right_child.seed) ^ seed_correction; } } } /// Update the `indicator` and `seed` in the `generateKeys` function. /// /// Copies over the expanded seed and indicator, with an additional XOR with /// the `cw` if `indicator` is set. inline fn generateUpdateIndicatorSeed( indicator: *u1, seed: *Seed, prg_out: Expansion, cw: *const CorrectionWord, comptime indicator_side: enum { left_indicator, right_indicator }, ) void { if (indicator.* == 1) { seed.* = prg_out.seed ^ cw.seed; indicator.* = prg_out.indicator ^ @field(cw, @tagName(indicator_side)); } else { seed.* = prg_out.seed; indicator.* = prg_out.indicator; } } /// XORs the indicator bit in the expansion with the corresponding correction word indicator. /// Used during evaluation to apply correction when the indicator is set. inline fn expandKeyUpdateExpansionWithCorrectionWord( exp: *Expansion, cw: *const CorrectionWord, comptime indicator_side: enum { left_indicator, right_indicator }, ) void { exp.indicator ^= @field(cw, @tagName(indicator_side)); } /// DPF (Distributed Point Function) factory. /// Creates a DPF instance for a domain with `2^n` items. /// - `n`: The domain exponent / number of DPF tree levels. /// /// Returns a struct type with `init`, `generateKeys`, `evaluate`, and `deinit` methods. pub fn Dpf(comptime n: comptime_int) type { if (n <= 0) @compileError("Dpf(n) requires n > 0"); const IndexType = std.meta.Int(.unsigned, n); const size = @as(comptime_int, 1) << n; // DPF key pair containing the secret key material. const Key = struct { // Initial seed for PRG expansion seed: Seed, // Base indicator bit (starting point for tree traversal) base_indicator: u1, // Array of correction words for each level of the DPF tree correction_words: [n]CorrectionWord, }; // HashMap context for DPF keys, provides hash and equality functions // for using Keys as HashMap keys. const KeyContext = struct { const Self = @This(); pub fn hash(_: Self, key: *const Key) u32 { var h = std.hash.Fnv1a_32.init(); h.update(@ptrCast(&key.seed)); h.update(&[_]u8{key.base_indicator}); for (key.correction_words) |cw| { h.update(@ptrCast(&cw.seed)); h.update(&[_]u8{ cw.left_indicator, cw.right_indicator }); } return h.final(); } pub fn eql(_: Self, a: *const Key, b: *const Key) bool { if (a.correction_words.len != b.correction_words.len) return false; var result = std.mem.eql(u8, @ptrCast(&a.seed), @ptrCast(&b.seed)) and a.base_indicator == b.base_indicator; for (a.correction_words, b.correction_words) |acw, bcw| { result = result and std.mem.eql(u8, @ptrCast(&acw.seed), @ptrCast(&bcw.seed)) and acw.left_indicator == bcw.left_indicator and acw.right_indicator == bcw.right_indicator; } return result; } }; // Cache for memoizing full key evaluations (key -> array of indicators). // // The indicator arrays are `2^n` bytes each, so they are stored out of line: // an inline value type would size the map's value array at capacity times // the domain, reserving memory for slots that hold nothing. const Cache = std.HashMap(*const Key, *[size]u1, KeyContext, std.hash_map.default_max_load_percentage); // Levels expanded breadth-first in batches at the bottom of the tree, capped // so one subtree's stored seeds stay within `subtree_seed_budget`. The widest // stored level holds half the subtree's leaves, hence the extra level. // // Annotated `comptime_int`: `log2_int` returns a sized integer, which would // otherwise infect `@min` and the arithmetic below with a type too narrow to // hold `n`. const budgeted_bottom_levels: comptime_int = std.math.log2_int(usize, subtree_seed_budget / @sizeOf(Compound)) + 1; const bottom_levels: comptime_int = @min(n, budgeted_bottom_levels); // Levels above that, walked depth-first a node at a time. const top_levels = n - bottom_levels; // Indicator bits produced by one bottom subtree. const subtree_size = @as(comptime_int, 1) << bottom_levels; // Widest bottom level whose seeds must be kept: the last level needs bits // only, so its parents are the widest stored level. const subtree_buffer_len = subtree_size >> 1; return struct { const Self = @This(); pub const Index = IndexType; pub const domain_size = size; left_prg_key: AesEncryptCtx, right_prg_key: AesEncryptCtx, allocator: std.mem.Allocator, io: Io, cache: Cache, /// Initialize a DPF instance. /// - `allocator`: Memory allocator for key storage and caching /// - `prg_key`: 256-bit key for the PRG (first 128 bits = left key, second 128 bits = right key) /// Returns a DPF instance ready for key generation and evaluation. pub fn init(allocator: std.mem.Allocator, io: Io, prg_key: *const PrgKey) Self { return .{ .left_prg_key = Aes.initEnc(prg_key.left), .right_prg_key = Aes.initEnc(prg_key.right), .allocator = allocator, .io = io, .cache = Cache.init(allocator), }; } /// Free all allocated memory including cached evaluation results. pub fn deinit(self: *Self) void { var cached = self.cache.valueIterator(); while (cached.next()) |indicators| self.allocator.destroy(indicators.*); self.cache.deinit(); } /// Free a key and its correction words. /// /// Also drops the key's cached expansion: the cache stores borrowed key /// pointers, so leaving the entry behind would let a later `evaluate` /// compare against freed memory. pub fn destroyKey(self: *Self, key: *Key) void { if (self.cache.fetchRemove(key)) |cached| { self.allocator.destroy(cached.value); } self.allocator.destroy(key); } /// Generate a key pair for a specific `index` in the DPF domain. pub fn generateKeys(self: *const Self, index: Index) !struct { *Key, *Key } { var key0 = try self.allocator.create(Key); errdefer self.allocator.destroy(key0); var key1 = try self.allocator.create(Key); errdefer self.allocator.destroy(key1); const corr_words = &key0.correction_words; var random0: Compound = undefined; var random1: Compound = undefined; try self.io.randomSecure(@ptrCast(&random0)); try self.io.randomSecure(@ptrCast(&random1)); // Each party's random block splits the same way a PRG block does: the // top bit seeds the base indicator, the rest is the root seed. const root0: Expansion = @bitCast(random0); const root1: Expansion = @bitCast(random1); key0.seed = root0.seed; key1.seed = root1.seed; key0.base_indicator = root0.indicator; key1.base_indicator = ~key0.base_indicator; var indicator0 = key0.base_indicator; var indicator1 = key1.base_indicator; // Both parties' seeds for the current level, so each side of the PRG // covers them in one batched call. var seeds: [2]Seed = .{ key0.seed, key1.seed }; // Walks `index` from its most significant bit down, one bit per level. var level_mask: Index = @as(Index, 1) << (n - 1); for (corr_words) |*cw| { std.debug.assert(seedMsbClear(seeds[0])); std.debug.assert(seedMsbClear(seeds[1])); var left_raw: [2]Compound = undefined; var right_raw: [2]Compound = undefined; self.left_prg_key.encryptWide(2, @ptrCast(&left_raw), @ptrCast(&seeds)); self.right_prg_key.encryptWide(2, @ptrCast(&right_raw), @ptrCast(&seeds)); const left_output_for_key0: Expansion = @bitCast(left_raw[0]); const left_output_for_key1: Expansion = @bitCast(left_raw[1]); const right_output_for_key0: Expansion = @bitCast(right_raw[0]); const right_output_for_key1: Expansion = @bitCast(right_raw[1]); // bit checked if it is left or right partition in DPF tree if (index & level_mask == 0) { // left half // cancel out right values cw.seed = right_output_for_key0.seed ^ right_output_for_key1.seed; cw.left_indicator = left_output_for_key0.indicator ^ left_output_for_key1.indicator ^ 1; cw.right_indicator = right_output_for_key0.indicator ^ right_output_for_key1.indicator; // continue on to left partition generateUpdateIndicatorSeed(&indicator0, &seeds[0], left_output_for_key0, cw, .left_indicator); generateUpdateIndicatorSeed(&indicator1, &seeds[1], left_output_for_key1, cw, .left_indicator); } else { // right half // cancel out left values cw.seed = left_output_for_key0.seed ^ left_output_for_key1.seed; cw.left_indicator = left_output_for_key0.indicator ^ left_output_for_key1.indicator; cw.right_indicator = right_output_for_key0.indicator ^ right_output_for_key1.indicator ^ 1; // continue on to right partition generateUpdateIndicatorSeed(&indicator0, &seeds[0], right_output_for_key0, cw, .right_indicator); generateUpdateIndicatorSeed(&indicator1, &seeds[1], right_output_for_key1, cw, .right_indicator); } // `shr` rather than `>>=`: at n = 1 the shift amount type is u0, // which cannot represent 1. level_mask = std.math.shr(Index, level_mask, 1); } @memcpy(&key1.correction_words, corr_words); return .{ key0, key1 }; } /// Evaluate the DPF using `key` at point `i` and output a single bit. pub fn evaluate(self: *Self, key: *const Key, i: Index) !u1 { const response = try self.cache.getOrPut(key); if (!response.found_existing) { errdefer _ = self.cache.remove(key); const indicators = try self.allocator.create([size]u1); // Expanded straight into the cache's storage, so the domain-sized // array is never held in a temporary. self.expandKeyToIndicators(key, indicators); response.value_ptr.* = indicators; } return response.value_ptr.*[i]; } /// Expand a DPF key into `out`, one indicator bit per domain index. /// /// The tree is split in two: the top levels are walked depth-first one /// node at a time, and each node reached at level `top_levels` roots a /// bottom subtree that is expanded breadth-first with batched AES. The /// top holds only `2^top_levels - 1` of the `2^n - 1` interior nodes, /// so nearly every PRG call lands in a full batch. pub fn expandKeyToIndicators(self: *const Self, key: *const Key, out: *[size]u1) void { const StackNode = struct { exp: Expansion, lvl: usize, }; var stack: [top_levels + 1]StackNode = undefined; stack[0] = .{ // `key.seed` holds the MSB-clear invariant, so it narrows. .exp = .{ .seed = @intCast(key.seed), .indicator = key.base_indicator }, .lvl = 0, }; var stack_size: usize = 1; var indicators_idx: usize = 0; while (stack_size > 0) { const node = stack[stack_size - 1]; stack_size -= 1; if (node.lvl == top_levels) { self.expandSubtree(key, node.exp, out[indicators_idx..][0..subtree_size]); indicators_idx += subtree_size; continue; } var left_exp: Expansion = undefined; var right_exp: Expansion = undefined; prg(self.left_prg_key, node.exp.seed, &left_exp); prg(self.right_prg_key, node.exp.seed, &right_exp); if (node.exp.indicator == 1) { const cw = &key.correction_words[node.lvl]; // Correction seeds are XORs of PRG seeds, so they narrow too. left_exp.seed ^= cw.seed; right_exp.seed ^= cw.seed; left_exp.indicator ^= cw.left_indicator; right_exp.indicator ^= cw.right_indicator; } std.debug.assert(stack_size + 2 <= stack.len); stack[stack_size + 1] = .{ .exp = left_exp, .lvl = node.lvl + 1 }; stack[stack_size] = .{ .exp = right_exp, .lvl = node.lvl + 1 }; stack_size += 2; } std.debug.assert(indicators_idx == size); } /// Expand the subtree rooted at `root`, a node on level `top_levels`, /// filling `out` with its indicator bits in domain-index order. /// /// Each level is expanded in place from the back so the children of a /// batch never clobber nodes still to be read. The final level writes /// its bits straight into `out`, so no seeds are stored for it. /// /// A level of `count` nodes is tiled by `prg_batch` back to front. When /// the width does not divide the level, the leftover sits at the front /// and is expanded last, at a comptime-known smaller width. fn expandSubtree(self: *const Self, key: *const Key, root: Expansion, out: *[subtree_size]u1) void { var seeds: [subtree_buffer_len]Seed = undefined; var indicators: [subtree_buffer_len]u1 = undefined; seeds[0] = root.seed; indicators[0] = root.indicator; inline for (0..bottom_levels) |level| { const cw = &key.correction_words[top_levels + level]; const count = 1 << level; // Only the last level's bits are consumed directly; every // earlier level feeds the next one from the local buffers. const is_last = level == bottom_levels - 1; const dst_seeds: [*]Seed = &seeds; const dst_indicators: [*]u1 = if (is_last) out else &indicators; const leftover = count % prg_batch; const full_batches = count / prg_batch; if (full_batches > 0) { var offset: usize = leftover + (full_batches - 1) * prg_batch; while (true) { prgBatch( self.left_prg_key, self.right_prg_key, cw, prg_batch, !is_last, seeds[offset..][0..prg_batch], indicators[offset..][0..prg_batch], dst_seeds + 2 * offset, dst_indicators + 2 * offset, ); if (offset == leftover) break; offset -= prg_batch; } } if (leftover > 0) { prgBatch( self.left_prg_key, self.right_prg_key, cw, leftover, !is_last, seeds[0..leftover], indicators[0..leftover], dst_seeds, dst_indicators, ); } } } }; } fn testPrgKey(io: Io) !PrgKey { var prg_key: PrgKey = undefined; try io.randomSecure(&prg_key.left); try io.randomSecure(&prg_key.right); return prg_key; } test "expansion puts the indicator on the block's top bit" { const with_msb: Compound = (@as(Compound, 1) << (compound_bits - 1)) | 0x1234; const set: Expansion = @bitCast(with_msb); try std.testing.expectEqual(@as(u1, 1), set.indicator); try std.testing.expectEqual(@as(Seed, 0x1234), set.seed); try std.testing.expect(!seedMsbClear(with_msb)); const without_msb: Compound = 0x1234; const clear: Expansion = @bitCast(without_msb); try std.testing.expectEqual(@as(u1, 0), clear.indicator); try std.testing.expectEqual(@as(Seed, 0x1234), clear.seed); try std.testing.expect(seedMsbClear(without_msb)); } test "dpf point function correctness small sizes" { const allocator = std.testing.allocator; const io = std.testing.io; const prg_key = try testPrgKey(io); const logns = [_]comptime_int{ 1, 2, 3, 4 }; inline for (logns) |n| { const DpfN = Dpf(n); const size = DpfN.domain_size; for (0..size) |index| { var dpf = DpfN.init(allocator, io, &prg_key); defer dpf.deinit(); const key0, const key1 = try dpf.generateKeys(@intCast(index)); defer { dpf.destroyKey(key0); dpf.destroyKey(key1); } for (0..size) |i| { const e0 = try dpf.evaluate(key0, @intCast(i)); const e1 = try dpf.evaluate(key1, @intCast(i)); const result = e0 ^ e1; if (i == index) { try std.testing.expectEqual(@as(u1, 1), result); } else { try std.testing.expectEqual(@as(u1, 0), result); } } } } } test "dpf point function correctness larger exponent" { const allocator = std.testing.allocator; const io = std.testing.io; const prg_key = try testPrgKey(io); const n = 5; const DpfN = Dpf(n); const size = DpfN.domain_size; for (0..size) |index| { var dpf = DpfN.init(allocator, io, &prg_key); defer dpf.deinit(); const key0, const key1 = try dpf.generateKeys(@intCast(index)); defer { dpf.destroyKey(key0); dpf.destroyKey(key1); } for (0..size) |i| { const e0 = try dpf.evaluate(key0, @intCast(i)); const e1 = try dpf.evaluate(key1, @intCast(i)); const result = e0 ^ e1; if (i == index) { try std.testing.expectEqual(@as(u1, 1), result); } else { try std.testing.expectEqual(@as(u1, 0), result); } } } } test "dpf point function correctness across the batching split" { const allocator = std.testing.allocator; const io = std.testing.io; const prg_key = try testPrgKey(io); // Exponents straddling `bottom_levels`, so both the depth-first top of the // tree and the batched bottom subtrees are exercised. const logns = [_]comptime_int{ 10, 11, 12, 13 }; inline for (logns) |n| { const DpfN = Dpf(n); const size = DpfN.domain_size; var dpf = DpfN.init(allocator, io, &prg_key); defer dpf.deinit(); const indices = [_]DpfN.Index{ 0, 1, size / 3, size / 2, size - 2, size - 1 }; for (indices) |index| { const key0, const key1 = try dpf.generateKeys(index); defer { dpf.destroyKey(key0); dpf.destroyKey(key1); } for (0..size) |i| { const result = try dpf.evaluate(key0, @intCast(i)) ^ try dpf.evaluate(key1, @intCast(i)); const expected: u1 = if (i == index) 1 else 0; try std.testing.expectEqual(expected, result); } } } } test "dpf seed and indicator invariants" { const allocator = std.testing.allocator; const io = std.testing.io; const prg_key = try testPrgKey(io); const n = 3; const DpfN = Dpf(n); const size = DpfN.domain_size; var dpf = DpfN.init(allocator, io, &prg_key); defer dpf.deinit(); for (0..size) |index| { const key0, const key1 = try dpf.generateKeys(@intCast(index)); defer { dpf.destroyKey(key0); dpf.destroyKey(key1); } try std.testing.expect(seedMsbClear(key0.seed)); try std.testing.expect(seedMsbClear(key1.seed)); try std.testing.expectEqual(@as(u1, 1), key0.base_indicator ^ key1.base_indicator); } } test "dpf evaluation caches results" { const allocator = std.testing.allocator; const io = std.testing.io; const prg_key = try testPrgKey(io); const n = 4; const DpfN = Dpf(n); const size = DpfN.domain_size; var dpf = DpfN.init(allocator, io, &prg_key); defer dpf.deinit(); const key0, const key1 = try dpf.generateKeys(3); defer { dpf.destroyKey(key0); dpf.destroyKey(key1); } for (0..size) |i| { _ = try dpf.evaluate(key0, @intCast(i)); } try std.testing.expectEqual(@as(usize, 1), dpf.cache.count()); for (0..size) |i| { _ = try dpf.evaluate(key0, @intCast(i)); } try std.testing.expectEqual(@as(usize, 1), dpf.cache.count()); for (0..size) |i| { _ = try dpf.evaluate(key1, @intCast(i)); } try std.testing.expectEqual(@as(usize, 2), dpf.cache.count()); } test "dpf destroyKey drops the key's cached expansion" { const allocator = std.testing.allocator; const io = std.testing.io; const prg_key = try testPrgKey(io); const n = 6; const DpfN = Dpf(n); const size = DpfN.domain_size; var dpf = DpfN.init(allocator, io, &prg_key); defer dpf.deinit(); // Reusing one instance across many generate/evaluate/destroy rounds leaves // the cache holding freed key pointers unless `destroyKey` evicts them; a // later lookup then compares against that freed memory. for (0..size) |index| { const key0, const key1 = try dpf.generateKeys(@intCast(index)); for (0..size) |i| { const result = try dpf.evaluate(key0, @intCast(i)) ^ try dpf.evaluate(key1, @intCast(i)); const expected: u1 = if (i == index) 1 else 0; try std.testing.expectEqual(expected, result); } try std.testing.expectEqual(@as(usize, 2), dpf.cache.count()); dpf.destroyKey(key0); dpf.destroyKey(key1); try std.testing.expectEqual(@as(usize, 0), dpf.cache.count()); } }