field-cat 0.1.0

Finite field algebra shared across plonkish-cat, proof-cat, and stark-cat
Documentation
//! Byte serialization for field elements.
//!
//! [`FieldBytes`] extends [`Field`] with byte-level
//! serialization, enabling field elements to be absorbed into and
//! squeezed from a Fiat-Shamir transcript.

use crate::error::Error;
use crate::field::Field;

/// Byte serialization for field elements.
///
/// Required by proof-system transcripts to absorb and squeeze
/// field elements deterministically.
///
/// # Examples
///
/// ```
/// use field_cat::{BabyBear, FieldBytes};
///
/// let a = BabyBear::new(123_456);
/// let bytes = a.to_le_bytes();
/// let b = BabyBear::from_le_bytes(&bytes)?;
/// assert_eq!(a, b);
/// # Ok::<(), field_cat::Error>(())
/// ```
pub trait FieldBytes: Field {
    /// Serialize this element to little-endian bytes.
    #[must_use]
    fn to_le_bytes(&self) -> Vec<u8>;

    /// Deserialize from little-endian bytes.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidFieldEncoding`] if the bytes cannot
    /// be interpreted as a valid field element of this type.
    fn from_le_bytes(bytes: &[u8]) -> Result<Self, Error>;
}