浏览代码

cuckoo: add serialization support

Lennart Braun 2 年之前
父节点
当前提交
af3b7dd5e3
共有 3 个文件被更改,包括 38 次插入2 次删除
  1. 1 0
      cuckoo/Cargo.toml
  2. 35 1
      cuckoo/src/cuckoo.rs
  3. 2 1
      cuckoo/src/hash.rs

+ 1 - 0
cuckoo/Cargo.toml

@@ -7,6 +7,7 @@ edition = "2021"
 
 [dependencies]
 utils = { path = "../utils" }
+bincode = "2.0.0-rc.2"
 funty = "2.0.0"
 libm = "0.2.5"
 rand = "0.8.5"

+ 35 - 1
cuckoo/src/cuckoo.rs

@@ -1,4 +1,5 @@
 use crate::hash::{HashFunction, HashFunctionValue};
+use bincode;
 use core::array;
 use libm::erf;
 use rand::{Rng, SeedableRng};
@@ -53,6 +54,40 @@ where
     }
 }
 
+impl<H: HashFunction<Value>, Value> bincode::Encode for Parameters<H, Value>
+where
+    Value: HashFunctionValue,
+    <Value as TryInto<usize>>::Error: Debug,
+    H::Description: bincode::Encode,
+{
+    fn encode<E: bincode::enc::Encoder>(
+        &self,
+        encoder: &mut E,
+    ) -> core::result::Result<(), bincode::error::EncodeError> {
+        bincode::Encode::encode(&self.number_inputs, encoder)?;
+        bincode::Encode::encode(&self.number_buckets, encoder)?;
+        bincode::Encode::encode(&self.hash_function_descriptions, encoder)?;
+        Ok(())
+    }
+}
+
+impl<H: HashFunction<Value>, Value> bincode::Decode for Parameters<H, Value>
+where
+    Value: HashFunctionValue,
+    <Value as TryInto<usize>>::Error: Debug,
+    H::Description: bincode::Decode + 'static,
+{
+    fn decode<D: bincode::de::Decoder>(
+        decoder: &mut D,
+    ) -> core::result::Result<Self, bincode::error::DecodeError> {
+        Ok(Self {
+            number_inputs: bincode::Decode::decode(decoder)?,
+            number_buckets: bincode::Decode::decode(decoder)?,
+            hash_function_descriptions: bincode::Decode::decode(decoder)?,
+        })
+    }
+}
+
 impl<H: HashFunction<Value>, Value> Parameters<H, Value>
 where
     Value: HashFunctionValue,
@@ -293,7 +328,6 @@ where
             }
             if try_k >= max_number_tries {
                 panic!("cycle detected"); // TODO: error handling
-                                                  // return absl::InvalidArgumentError("Cuckoo::HashCuckoo -- Cycle detected");
             }
         }
 

+ 2 - 1
cuckoo/src/hash.rs

@@ -1,3 +1,4 @@
+use bincode;
 use core::fmt::Debug;
 use core::ops::Range;
 use funty::Integral;
@@ -70,7 +71,7 @@ pub struct AesHashFunction<Value> {
     _phantom: PhantomData<Value>,
 }
 
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, PartialEq, Eq, bincode::Encode, bincode::Decode)]
 pub struct AesHashFunctionDescription {
     /// Size of the range.
     range_size: usize,