ark_ec/models/short_weierstrass/
serialization_flags.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
79
80
81
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 SWFlags {
    /// Represents a point with positive y-coordinate by setting all bits to 0.
    YIsPositive = 0,
    /// Represents the point at infinity by setting the setting the last-but-one bit to 1.
    PointAtInfinity = 1 << 6,
    /// Represents a point with negative y-coordinate by setting the MSB to 1.
    YIsNegative = 1 << 7,
}

impl SWFlags {
    #[inline]
    pub fn infinity() -> Self {
        SWFlags::PointAtInfinity
    }

    #[inline]
    pub fn from_y_coordinate(y: impl Field) -> Self {
        if y <= -y {
            Self::YIsPositive
        } else {
            Self::YIsNegative
        }
    }

    #[inline]
    pub fn is_infinity(&self) -> bool {
        matches!(self, SWFlags::PointAtInfinity)
    }

    #[inline]
    pub fn is_positive(&self) -> Option<bool> {
        match self {
            SWFlags::PointAtInfinity => None,
            SWFlags::YIsPositive => Some(true),
            SWFlags::YIsNegative => Some(false),
        }
    }
}

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

impl Flags for SWFlags {
    const BIT_SIZE: usize = 2;

    #[inline]
    fn u8_bitmask(&self) -> u8 {
        let mut mask = 0;
        match self {
            SWFlags::PointAtInfinity => mask |= 1 << 6,
            SWFlags::YIsNegative => mask |= 1 << 7,
            _ => (),
        }
        mask
    }

    #[inline]
    fn from_u8(value: u8) -> Option<Self> {
        let is_negative = (value >> 7) & 1 == 1;
        let is_infinity = (value >> 6) & 1 == 1;
        match (is_negative, is_infinity) {
            // This is invalid because we only want *one* way to serialize
            // the point at infinity.
            (true, true) => None,
            (false, true) => Some(SWFlags::PointAtInfinity),
            (true, false) => Some(SWFlags::YIsNegative),
            (false, false) => Some(SWFlags::YIsPositive),
        }
    }
}