Browse Source

utils: add PRF into finite field

Lennart Braun 2 years ago
parent
commit
50171e49aa
3 changed files with 24 additions and 0 deletions
  1. 2 0
      utils/Cargo.toml
  2. 1 0
      utils/src/lib.rs
  3. 21 0
      utils/src/prf.rs

+ 2 - 0
utils/Cargo.toml

@@ -7,5 +7,7 @@ edition = "2021"
 
 [dependencies]
 aes = "0.8.1"
+blake3 = "1.3.3"
+ff = "0.13.0"
 num = "0.4.0"
 rand = "0.8.5"

+ 1 - 0
utils/src/lib.rs

@@ -1,3 +1,4 @@
 pub mod bit_decompose;
 pub mod fixed_key_aes;
+pub mod prf;
 pub mod pseudorandom_conversion;

+ 21 - 0
utils/src/prf.rs

@@ -0,0 +1,21 @@
+use blake3;
+use ff::FromUniformBytes;
+use rand::{thread_rng, Rng};
+
+#[derive(Clone, Copy, Debug)]
+pub struct PrfKey([u8; blake3::KEY_LEN]);
+
+pub struct Prf {}
+
+impl Prf {
+    pub const OUT_LEN: usize = blake3::OUT_LEN;
+
+    pub fn key_gen() -> PrfKey {
+        PrfKey(thread_rng().gen())
+    }
+
+    pub fn eval<F: FromUniformBytes<{ blake3::OUT_LEN }>>(key: &PrfKey, index: u64) -> F {
+        let hash = blake3::keyed_hash(&key.0, &index.to_be_bytes());
+        F::from_uniform_bytes(hash.as_bytes())
+    }
+}