alloy_eips/eip4844/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Utilities for working with EIP-4844 field elements and implementing
//! [`SidecarCoder`].
//!
//! [`SidecarCoder`]: crate::eip4844::builder::SidecarCoder

use crate::eip4844::USABLE_BITS_PER_FIELD_ELEMENT;

/// Determine whether a slice of bytes can be contained in a field element.
pub const fn fits_in_fe(data: &[u8]) -> bool {
    match data.len() {
        33.. => false,
        32 => data[0] & 0b1100_0000 == 0, // first two bits must be zero
        _ => true,
    }
}

/// Calculate the number of field elements required to store the given
/// number of bytes.
pub const fn minimum_fe_for_bytes(bytes: usize) -> usize {
    (bytes * 8).div_ceil(USABLE_BITS_PER_FIELD_ELEMENT)
}

/// Calculate the number of field elements required to store the given data.
pub const fn minimum_fe(data: &[u8]) -> usize {
    minimum_fe_for_bytes(data.len())
}

/// A wrapper for a slice of bytes that is a whole, valid field element.
#[derive(Clone, Copy, Debug)]
pub struct WholeFe<'a>(&'a [u8]);

impl<'a> WholeFe<'a> {
    const fn new_unchecked(data: &'a [u8]) -> Self {
        Self(data)
    }

    /// Instantiate a new `WholeFe` from a slice of bytes, if it is a valid
    /// field element.
    pub const fn new(data: &'a [u8]) -> Option<Self> {
        if data.len() == 32 && fits_in_fe(data) {
            Some(Self::new_unchecked(data))
        } else {
            None
        }
    }
}

impl AsRef<[u8]> for WholeFe<'_> {
    fn as_ref(&self) -> &[u8] {
        self.0
    }
}

#[cfg(test)]
mod test {
    use crate::eip4844::{FIELD_ELEMENTS_PER_BLOB, USABLE_BYTES_PER_BLOB};

    use super::*;
    #[test]
    fn calc_required_fe() {
        assert_eq!(minimum_fe(&[0u8; 32]), 2);
        assert_eq!(minimum_fe(&[0u8; 31]), 1);
        assert_eq!(minimum_fe(&[0u8; 33]), 2);
        assert_eq!(minimum_fe(&[0u8; 64]), 3);
        assert_eq!(minimum_fe(&[0u8; 65]), 3);
        assert_eq!(minimum_fe_for_bytes(USABLE_BYTES_PER_BLOB), FIELD_ELEMENTS_PER_BLOB as usize);
    }

    #[test]
    fn calc_is_valid_field_element() {
        assert!(fits_in_fe(&[0u8; 32]));
        assert!(!fits_in_fe(&[0u8; 33]));

        assert!(WholeFe::new(&[0u8; 32]).is_some());
        assert!(WholeFe::new(&[0u8; 33]).is_none());
        assert!(WholeFe::new(&[0u8; 31]).is_none());
    }
}