myl7/fss 1.1.0
Function secret sharing (FSS) primitives including distributed point/comparison function (DPF/DCF)
Loading...
Searching...
No Matches
cuckoo_hash.cuh
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
20#pragma once
21#include <cuda_runtime.h>
22#include <cmath>
23#include <span>
24#include <utility>
25#include <random>
26#include <fss/prp.cuh>
27
28namespace fss::cuckoo_hash {
29
30namespace detail {
31
32constexpr double Log2(double x) {
33 if (x <= 0) return -1e308;
34 int e = 0;
35 double m = x;
36 while (m >= 2.0) {
37 m /= 2.0;
38 ++e;
39 }
40 while (m < 1.0) {
41 m *= 2.0;
42 --e;
43 }
44 double y = (m - 1.0) / (m + 1.0);
45 double y2 = y * y, sum = 0, term = y;
46 for (int k = 0; k < 40; ++k) {
47 sum += term / (2 * k + 1);
48 term *= y2;
49 }
50 return e + 2.0 * sum / 0.6931471805599453;
51}
52
53constexpr double Ceil(double x) {
54 auto i = static_cast<long long>(x);
55 return (x > static_cast<double>(i)) ? static_cast<double>(i + 1) : static_cast<double>(i);
56}
57
58} // namespace detail
59
76constexpr int ChBucket(int t, int lambda) {
77 // Remark 1 applies for t >= 30.
78 // For smaller t, the full Lemma 5 formula with Normal CDF factors is needed,
79 // but the paper considers t >= 30 to capture nearly all practical use cases.
80 assert(t >= 30);
81 double td = static_cast<double>(t);
82 double e = (static_cast<double>(lambda) + 130.0 + detail::Log2(td)) / 123.5;
83 return static_cast<int>(detail::Ceil(e * td));
84}
85
94template <typename Prp, typename In, int kappa = 3>
95 requires Permutable<Prp>
96struct PrpHash {
97 Prp prp;
98
114 std::pair<int, int> Locate(int4 sigma, In x, int k, __uint128_t n, int b_size) {
115 __uint128_t val = static_cast<__uint128_t>(x) + n * k;
116 __uint128_t domain = n * kappa;
117 __uint128_t y = prp.Permu(sigma, val, domain);
118
119 auto bs = static_cast<__uint128_t>(b_size);
120 int bucket = static_cast<int>(y / bs);
121 int index = static_cast<int>(y % bs);
122 return {bucket, index};
123 }
124};
125
135template <typename Prp, typename In, int kappa = 3>
136 requires Permutable<Prp>
137struct Compact {
138 Prp prp;
139
154 int Run(std::span<const In> as, int m, int4 sigma, __uint128_t n, int b_size, int ch_retry,
155 std::span<std::pair<int, int>> table) {
156 PrpHash<Prp, In, kappa> hasher{prp};
157 int t = static_cast<int>(as.size());
158
159 // Initialize table to empty.
160 for (int i = 0; i < m; ++i) {
161 table[i] = {-1, -1};
162 }
163
164 std::mt19937 rng(42);
165
166 for (int omega = 0; omega < t; ++omega) {
167 int cur_idx = omega;
168 int cur_k = static_cast<int>(rng() % kappa);
169 int evictions = 0;
170
171 for (;;) {
172 auto [bucket, _index] = hasher.Locate(sigma, as[cur_idx], cur_k, n, b_size);
173 // Clamp bucket to [0, m).
174 bucket = bucket % m;
175
176 if (table[bucket].first == -1) {
177 // Empty slot found.
178 table[bucket] = {cur_idx, cur_k};
179 break;
180 }
181
182 // Evict the existing entry and take its place.
183 int evicted_idx = table[bucket].first;
184 int evicted_k = table[bucket].second;
185 table[bucket] = {cur_idx, cur_k};
186
187 // The evicted element becomes the current element with a new random hash.
188 cur_idx = evicted_idx;
189 cur_k = static_cast<int>(rng() % kappa);
190
191 ++evictions;
192 if (evictions > ch_retry) {
193 return 1;
194 }
195 }
196 }
197
198 return 0;
199 }
200};
201
202} // namespace fss::cuckoo_hash
Small-domain pseudorandom permutation (PRP) interface.
Definition prp.cuh:23
constexpr int ChBucket(int t, int lambda)
Compute the number of Cuckoo-hash buckets from Lemma 5 of the paper, simplified per Remark 1.
Definition cuckoo_hash.cuh:76
Pseudorandom permutation (PRP) interface.
Compact Cuckoo hashing (Algorithm 4 from the paper).
Definition cuckoo_hash.cuh:137
int Run(std::span< const In > as, int m, int4 sigma, __uint128_t n, int b_size, int ch_retry, std::span< std::pair< int, int > > table)
Run Cuckoo hashing to place elements into a table.
Definition cuckoo_hash.cuh:154
PRP-based hash for Cuckoo hashing.
Definition cuckoo_hash.cuh:96
std::pair< int, int > Locate(int4 sigma, In x, int k, __uint128_t n, int b_size)
Compute bucket index and within-bucket index for hash function k.
Definition cuckoo_hash.cuh:114