Browse Source

utils: update prg/hashing into field

Lennart Braun 2 years ago
parent
commit
c51d9ba736
2 changed files with 24 additions and 1 deletions
  1. 17 1
      utils/src/field.rs
  2. 7 0
      utils/src/pseudorandom_conversion.rs

+ 17 - 1
utils/src/field.rs

@@ -1,5 +1,6 @@
 use blake3;
 use ff::{Field, PrimeField};
+use num;
 use rand::{thread_rng, Rng};
 
 #[allow(non_upper_case_globals)]
@@ -13,6 +14,16 @@ pub const p: u128 = 340282366920938462946865773367900766209;
 #[PrimeFieldReprEndianness = "little"]
 pub struct Fp([u64; 3]);
 
+impl num::traits::Zero for Fp {
+    fn zero() -> Self {
+        Self::ZERO
+    }
+
+    fn is_zero(&self) -> bool {
+        *self == Self::ZERO
+    }
+}
+
 pub trait FromPrf {
     type PrfKey: Copy;
     /// PRF key generation
@@ -54,6 +65,7 @@ impl Modulus128 for Fp {
 pub trait FromHash {
     /// Hash into Fp
     fn hash(input: u64) -> Self;
+    fn hash_bytes(input: &[u8]) -> Self;
 }
 
 pub trait LegendreSymbol: PrimeField {
@@ -146,8 +158,12 @@ impl FromPrf for Fp {
 impl FromHash for Fp {
     /// Hash into Fp
     fn hash(input: u64) -> Self {
+        Self::hash_bytes(&input.to_be_bytes())
+    }
+
+    fn hash_bytes(input: &[u8]) -> Self {
         let mut hasher = blake3::Hasher::new();
-        hasher.update(&input.to_be_bytes());
+        hasher.update(input);
         let mut xof = hasher.finalize_xof();
         Self::from_xof(&mut xof)
     }

+ 7 - 0
utils/src/pseudorandom_conversion.rs

@@ -1,3 +1,4 @@
+use crate::field::FromHash;
 use core::num::Wrapping;
 
 pub trait PRConvertTo<T> {
@@ -35,3 +36,9 @@ where
         Wrapping(<Self as PRConvertTo<T>>::convert(randomness))
     }
 }
+
+impl<F: FromHash> PRConvertTo<F> for PRConverter {
+    fn convert(randomness: u128) -> F {
+        F::hash_bytes(&randomness.to_be_bytes())
+    }
+}