Browse Source

dpf: use u16 for hashes

should work up to domains of size ca. 2^26; 2^28 needs larger hashes
Lennart Braun 2 years ago
parent
commit
428a3abfb3
3 changed files with 26 additions and 21 deletions
  1. 1 1
      cuckoo/src/bin/params.rs
  2. 1 0
      cuckoo/src/hash.rs
  3. 24 20
      dpf/src/mpdpf.rs

+ 1 - 1
cuckoo/src/bin/params.rs

@@ -8,7 +8,7 @@ fn main() {
 
     for log_domain_size in log_domain_sizes {
         let log_number_inputs = log_domain_size / 2;
-        let params = Parameters::<AesHashFunction<u32>, _>::sample(1 << log_number_inputs);
+        let params = Parameters::<AesHashFunction<u16>, _>::sample(1 << log_number_inputs);
         let number_buckets = params.get_number_buckets();
         let hasher = Hasher::new(params);
         let buckets = hasher.hash_domain_into_buckets(1 << log_domain_size);

+ 1 - 0
cuckoo/src/hash.rs

@@ -13,6 +13,7 @@ where
 {
 }
 
+impl HashFunctionValue for u16 {}
 impl HashFunctionValue for u32 {}
 impl HashFunctionValue for u64 {}
 

+ 24 - 20
dpf/src/mpdpf.rs

@@ -142,19 +142,19 @@ where
 pub struct SmartMpDpfKey<SPDPF, H>
 where
     SPDPF: SinglePointDpf,
-    H: HashFunction<u32>,
+    H: HashFunction<u16>,
 {
     party_id: usize,
     domain_size: usize,
     number_points: usize,
     spdpf_keys: Vec<Option<SPDPF::Key>>,
-    cuckoo_parameters: CuckooParameters<H, u32>,
+    cuckoo_parameters: CuckooParameters<H, u16>,
 }
 
 impl<SPDPF, H> Debug for SmartMpDpfKey<SPDPF, H>
 where
     SPDPF: SinglePointDpf,
-    H: HashFunction<u32>,
+    H: HashFunction<u16>,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
         let (newline, indentation) = if f.alternate() {
@@ -199,7 +199,7 @@ where
 impl<SPDPF, H> Clone for SmartMpDpfKey<SPDPF, H>
 where
     SPDPF: SinglePointDpf,
-    H: HashFunction<u32>,
+    H: HashFunction<u16>,
 {
     fn clone(&self) -> Self {
         Self {
@@ -215,7 +215,7 @@ where
 impl<SPDPF, H> MultiPointDpfKey for SmartMpDpfKey<SPDPF, H>
 where
     SPDPF: SinglePointDpf,
-    H: HashFunction<u32>,
+    H: HashFunction<u16>,
 {
     fn get_party_id(&self) -> usize {
         self.party_id
@@ -228,10 +228,10 @@ where
     }
 }
 
-struct SmartMpDpfPrecomputationData<H: HashFunction<u32>> {
-    pub cuckoo_parameters: CuckooParameters<H, u32>,
-    pub hasher: CuckooHasher<H, u32>,
-    pub hashes: [Vec<u32>; CUCKOO_NUMBER_HASH_FUNCTIONS],
+struct SmartMpDpfPrecomputationData<H: HashFunction<u16>> {
+    pub cuckoo_parameters: CuckooParameters<H, u16>,
+    pub hasher: CuckooHasher<H, u16>,
+    pub hashes: [Vec<u16>; CUCKOO_NUMBER_HASH_FUNCTIONS],
     pub simple_htable: Vec<Vec<u64>>,
     pub bucket_sizes: Vec<usize>,
     pub position_map_lookup_table: Vec<[(usize, usize); 3]>,
@@ -241,7 +241,7 @@ pub struct SmartMpDpf<V, SPDPF, H>
 where
     V: Add<Output = V> + AddAssign + Copy + Debug + Eq + Zero,
     SPDPF: SinglePointDpf<Value = V>,
-    H: HashFunction<u32>,
+    H: HashFunction<u16>,
 {
     domain_size: usize,
     number_points: usize,
@@ -255,7 +255,7 @@ impl<V, SPDPF, H> SmartMpDpf<V, SPDPF, H>
 where
     V: Add<Output = V> + AddAssign + Copy + Debug + Eq + Zero,
     SPDPF: SinglePointDpf<Value = V>,
-    H: HashFunction<u32>,
+    H: HashFunction<u16>,
 {
     fn precompute_hashes(
         domain_size: usize,
@@ -266,13 +266,17 @@ where
             42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
         ];
         let cuckoo_parameters = CuckooParameters::from_seed(number_points, seed);
-        let hasher = CuckooHasher::<H, u32>::new(cuckoo_parameters);
+        assert!(
+            cuckoo_parameters.get_number_buckets() < (1 << u16::BITS),
+            "too many buckets, use larger type for hash values"
+        );
+        let hasher = CuckooHasher::<H, u16>::new(cuckoo_parameters);
         let hashes = hasher.hash_domain(domain_size as u64);
         let simple_htable =
             hasher.hash_domain_into_buckets_given_hashes(domain_size as u64, &hashes);
-        let bucket_sizes = CuckooHasher::<H, u32>::compute_bucket_sizes(&simple_htable);
+        let bucket_sizes = CuckooHasher::<H, u16>::compute_bucket_sizes(&simple_htable);
         let position_map_lookup_table =
-            CuckooHasher::<H, u32>::compute_pos_lookup_table(domain_size as u64, &simple_htable);
+            CuckooHasher::<H, u16>::compute_pos_lookup_table(domain_size as u64, &simple_htable);
         SmartMpDpfPrecomputationData {
             cuckoo_parameters,
             hasher,
@@ -288,7 +292,7 @@ impl<V, SPDPF, H> MultiPointDpf for SmartMpDpf<V, SPDPF, H>
 where
     V: Add<Output = V> + AddAssign + Copy + Debug + Eq + Zero,
     SPDPF: SinglePointDpf<Value = V>,
-    H: HashFunction<u32>,
+    H: HashFunction<u16>,
 {
     type Key = SmartMpDpfKey<SPDPF, H>;
     type Value = V;
@@ -344,7 +348,7 @@ where
         let (cuckoo_table_items, cuckoo_table_indices) = hasher.cuckoo_hash_items(alphas);
         let position_map_lookup_table = &precomputation_data.position_map_lookup_table;
         let pos = |bucket_i: usize, item: u64| -> u64 {
-            CuckooHasher::<H, u32>::pos_lookup(position_map_lookup_table, bucket_i, item)
+            CuckooHasher::<H, u16>::pos_lookup(position_map_lookup_table, bucket_i, item)
         };
 
         let number_buckets = hasher.get_parameters().get_number_buckets();
@@ -363,7 +367,7 @@ where
             }
 
             let (alpha, beta) =
-                if cuckoo_table_items[bucket_i] != CuckooHasher::<H, u32>::UNOCCUPIED {
+                if cuckoo_table_items[bucket_i] != CuckooHasher::<H, u16>::UNOCCUPIED {
                     let alpha = pos(bucket_i, cuckoo_table_items[bucket_i]);
                     let beta = betas[cuckoo_table_indices[bucket_i]];
                     (alpha, beta)
@@ -399,7 +403,7 @@ where
         assert_eq!(key.domain_size, self.domain_size);
         assert!(index < self.domain_size as u64);
 
-        let hasher = CuckooHasher::<H, u32>::new(key.cuckoo_parameters);
+        let hasher = CuckooHasher::<H, u16>::new(key.cuckoo_parameters);
 
         let hashes = hasher.hash_items(&[index]);
         let simple_htable = hasher.hash_domain_into_buckets(self.domain_size as u64);
@@ -466,7 +470,7 @@ where
         let simple_htable = &precomputation_data.simple_htable;
         let position_map_lookup_table = &precomputation_data.position_map_lookup_table;
         let pos = |bucket_i: usize, item: u64| -> u64 {
-            CuckooHasher::<H, u32>::pos_lookup(position_map_lookup_table, bucket_i, item)
+            CuckooHasher::<H, u16>::pos_lookup(position_map_lookup_table, bucket_i, item)
         };
 
         let mut outputs = Vec::<Self::Value>::with_capacity(domain_size as usize);
@@ -588,7 +592,7 @@ mod tests {
             for log_number_points in 0..5 {
                 for precomputation in [false, true] {
                     test_mpdpf_with_param::<
-                        SmartMpDpf<Value, DummySpDpf<Value>, AesHashFunction<u32>>,
+                        SmartMpDpf<Value, DummySpDpf<Value>, AesHashFunction<u16>>,
                     >(log_domain_size, 1 << log_number_points, precomputation);
                 }
             }