ark_ec/models/twisted_edwards/
serialization_flags.rs

1use ark_ff::Field;
2use ark_serialize::Flags;
3
4/// Flags to be encoded into the serialization.
5/// The default flags (empty) should not change the binary representation.
6#[derive(Clone, Copy, PartialEq, Eq, Debug)]
7pub enum TEFlags {
8    XIsPositive = 0,
9    XIsNegative = 1,
10}
11
12impl TEFlags {
13    #[inline]
14    pub fn from_x_coordinate(x: impl Field) -> Self {
15        if x <= -x {
16            TEFlags::XIsPositive
17        } else {
18            TEFlags::XIsNegative
19        }
20    }
21
22    #[inline]
23    pub fn is_negative(&self) -> bool {
24        matches!(*self, TEFlags::XIsNegative)
25    }
26}
27
28impl Default for TEFlags {
29    #[inline]
30    fn default() -> Self {
31        // XIsPositive doesn't change the serialization
32        TEFlags::XIsPositive
33    }
34}
35
36impl Flags for TEFlags {
37    const BIT_SIZE: usize = 1;
38
39    #[inline]
40    fn u8_bitmask(&self) -> u8 {
41        let mut mask = 0;
42        if let Self::XIsNegative = self {
43            mask |= 1 << 7;
44        }
45        mask
46    }
47
48    #[inline]
49    fn from_u8(value: u8) -> Option<Self> {
50        let x_sign = (value >> 7) & 1 == 1;
51        if x_sign {
52            Some(Self::XIsNegative)
53        } else {
54            Some(Self::XIsPositive)
55        }
56    }
57}