Skip to main content

field_cat/
bytes.rs

1//! Byte serialization for field elements.
2//!
3//! [`FieldBytes`] extends [`Field`] with byte-level
4//! serialization, enabling field elements to be absorbed into and
5//! squeezed from a Fiat-Shamir transcript.
6
7use crate::error::Error;
8use crate::field::Field;
9
10/// Byte serialization for field elements.
11///
12/// Required by proof-system transcripts to absorb and squeeze
13/// field elements deterministically.
14///
15/// # Examples
16///
17/// ```
18/// use field_cat::{BabyBear, FieldBytes};
19///
20/// let a = BabyBear::new(123_456);
21/// let bytes = a.to_le_bytes();
22/// let b = BabyBear::from_le_bytes(&bytes)?;
23/// assert_eq!(a, b);
24/// # Ok::<(), field_cat::Error>(())
25/// ```
26pub trait FieldBytes: Field {
27    /// Serialize this element to little-endian bytes.
28    #[must_use]
29    fn to_le_bytes(&self) -> Vec<u8>;
30
31    /// Deserialize from little-endian bytes.
32    ///
33    /// # Errors
34    ///
35    /// Returns [`Error::InvalidFieldEncoding`] if the bytes cannot
36    /// be interpreted as a valid field element of this type.
37    fn from_le_bytes(bytes: &[u8]) -> Result<Self, Error>;
38}