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
use ark_ff::Field;
use ark_serialize::Flags;

/// Flags to be encoded into the serialization.
/// The default flags (empty) should not change the binary representation.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum TEFlags {
    XIsPositive = 0,
    XIsNegative = 1,
}

impl TEFlags {
    #[inline]
    pub fn from_x_coordinate(x: impl Field) -> Self {
        if x <= -x {
            TEFlags::XIsPositive
        } else {
            TEFlags::XIsNegative
        }
    }

    #[inline]
    pub fn is_negative(&self) -> bool {
        matches!(*self, TEFlags::XIsNegative)
    }
}

impl Default for TEFlags {
    #[inline]
    fn default() -> Self {
        // XIsPositive doesn't change the serialization
        TEFlags::XIsPositive
    }
}

impl Flags for TEFlags {
    const BIT_SIZE: usize = 1;

    #[inline]
    fn u8_bitmask(&self) -> u8 {
        let mut mask = 0;
        if let Self::XIsNegative = self {
            mask |= 1 << 7;
        }
        mask
    }

    #[inline]
    fn from_u8(value: u8) -> Option<Self> {
        let x_sign = (value >> 7) & 1 == 1;
        if x_sign {
            Some(Self::XIsNegative)
        } else {
            Some(Self::XIsPositive)
        }
    }
}