tiny_ec_core/
extend.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use core::{cmp::Ordering, convert::TryInto, fmt, panic};
use serde::{de, Deserialize, Serialize};

use crate::{
    curve::{Affine, Jacobian, Scalar},
    field::Field,
};

impl From<&Jacobian> for Affine {
    fn from(value: &Jacobian) -> Self {
        let mut ra = Affine::from_gej(value);
        ra.x.normalize();
        ra.y.normalize();
        ra
    }
}

impl From<Jacobian> for Affine {
    fn from(value: Jacobian) -> Self {
        Affine::from(&value)
    }
}

impl From<&[u8; 64]> for Affine {
    fn from(value: &[u8; 64]) -> Self {
        let mut x = Field::default();
        let mut y = Field::default();
        if x.set_b32(value[0..32].try_into().unwrap())
            && y.set_b32(value[32..64].try_into().unwrap())
        {
            let mut r = Affine::default();
            r.set_xy(&x, &y);
            r.x.normalize();
            r.y.normalize();
            return r;
        }
        panic!("Failed to construct Affine from bytes")
    }
}

impl From<&[u8]> for Affine {
    fn from(value: &[u8]) -> Self {
        if value.len() != 64 {
            panic!("Bytes length must be 64 for Affine")
        }
        let mut tmp_bytes = [0u8; 64];
        tmp_bytes[0..64].copy_from_slice(value);
        Affine::from(&tmp_bytes)
    }
}

impl Into<[u8; 64]> for Affine {
    fn into(self) -> [u8; 64] {
        let mut ret = [0u8; 64];
        ret[0..32].copy_from_slice(&self.x.b32());
        ret[32..64].copy_from_slice(&self.y.b32());
        ret
    }
}

struct AffineBytesVisitor;

#[cfg(feature = "std")]
impl<'de> de::Visitor<'de> for AffineBytesVisitor {
    type Value = Affine;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a byte slice that is 64 bytes in length")
    }

    fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(Affine::from(value))
    }
}

impl Serialize for Affine {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        [self.x.b32(), self.y.b32()].concat().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Affine {
    fn deserialize<D>(deserializer: D) -> Result<Affine, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_bytes(AffineBytesVisitor)
    }
}

impl PartialOrd for Scalar {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Scalar {
    fn cmp(&self, other: &Self) -> Ordering {
        let mut ret = Ordering::Equal;
        for i in (0..8).rev() {
            if self.0[i] < other.0[i] {
                ret = Ordering::Less;
                break;
            } else if self.0[i] > other.0[i] {
                ret = Ordering::Greater;
                break;
            }
        }
        ret
    }
}

impl From<&[u8]> for Scalar {
    fn from(bytes: &[u8]) -> Self {
        if bytes.len() != 32 {
            panic!("Bytes length must be 32")
        }
        let mut tmp_bytes = [0u8; 32];
        tmp_bytes[0..32].copy_from_slice(bytes);
        Scalar::from(&tmp_bytes)
    }
}

impl From<&[u8; 32]> for Scalar {
    fn from(bytes: &[u8; 32]) -> Self {
        let mut r = Scalar::default();
        r.set_b32(bytes).unwrap_u8();
        r
    }
}

impl Serialize for Scalar {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.0.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Scalar {
    fn deserialize<D>(deserializer: D) -> Result<Scalar, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let bytes = <[u8; 32]>::deserialize(deserializer)?;
        Ok(Scalar::from(&bytes[..]))
    }
}