Browse Source

utils: add Fp::from/to_le_bytes

Lennart Braun 2 years ago
parent
commit
5aeae32895
1 changed files with 25 additions and 0 deletions
  1. 25 0
      utils/src/field.rs

+ 25 - 0
utils/src/field.rs

@@ -124,6 +124,21 @@ impl Fp {
             }
         }
     }
+
+    pub fn to_le_bytes(&self) -> [u8; 16] {
+        let mut bytes = [0u8; 16];
+        let repr = self.to_repr();
+        debug_assert_eq!(&repr.as_ref()[16..], &[0u8; 8]);
+        bytes.copy_from_slice(&repr.as_ref()[0..16]);
+        bytes
+    }
+
+    pub fn from_le_bytes_vartime(bytes: &[u8; 16]) -> Option<Self> {
+        let mut repr = <Self as PrimeField>::Repr::default();
+        debug_assert_eq!(repr.as_ref(), &[0u8; 24]);
+        repr.as_mut()[0..16].copy_from_slice(bytes);
+        Self::from_repr_vartime(repr)
+    }
 }
 
 impl FromPrf for Fp {
@@ -252,4 +267,14 @@ mod tests {
             assert_eq!(y, x);
         }
     }
+
+    #[test]
+    fn test_to_bytes() {
+        for _ in 0..100 {
+            let x = Fp::random(thread_rng());
+            let x_bytes = x.to_le_bytes();
+            let y = Fp::from_le_bytes_vartime(&x_bytes).expect("from_le_bytes_vartime failed");
+            assert_eq!(x, y);
+        }
+    }
 }