1use core::{cmp::Ordering, convert::TryInto, fmt, panic};
2use serde::{de, Deserialize, Serialize};
3
4use crate::{
5 curve::{Affine, Jacobian, Scalar},
6 field::Field,
7};
8
9impl From<&Jacobian> for Affine {
10 fn from(value: &Jacobian) -> Self {
11 let mut ra = Affine::from_gej(value);
12 ra.x.normalize();
13 ra.y.normalize();
14 ra
15 }
16}
17
18impl From<Jacobian> for Affine {
19 fn from(value: Jacobian) -> Self {
20 Affine::from(&value)
21 }
22}
23
24impl From<&[u8; 64]> for Affine {
25 fn from(value: &[u8; 64]) -> Self {
26 let mut x = Field::default();
27 let mut y = Field::default();
28 if x.set_b32(value[0..32].try_into().unwrap())
29 && y.set_b32(value[32..64].try_into().unwrap())
30 {
31 let mut r = Affine::default();
32 r.set_xy(&x, &y);
33 r.x.normalize();
34 r.y.normalize();
35 return r;
36 }
37 panic!("Failed to construct Affine from bytes")
38 }
39}
40
41impl From<&[u8]> for Affine {
42 fn from(value: &[u8]) -> Self {
43 if value.len() != 64 {
44 panic!("Bytes length must be 64 for Affine")
45 }
46 let mut tmp_bytes = [0u8; 64];
47 tmp_bytes[0..64].copy_from_slice(value);
48 Affine::from(&tmp_bytes)
49 }
50}
51
52impl Into<[u8; 64]> for Affine {
53 fn into(self) -> [u8; 64] {
54 let mut ret = [0u8; 64];
55 ret[0..32].copy_from_slice(&self.x.b32());
56 ret[32..64].copy_from_slice(&self.y.b32());
57 ret
58 }
59}
60
61impl Affine {
62 pub fn compose(x: &Field, z: &Field) -> Affine {
63 let mut r = Affine::default();
64 r.x = x.clone();
65 r.y = z.clone();
66 r.x.normalize();
67 r.y.normalize();
68 r
69 }
70}
71
72struct AffineBytesVisitor;
73
74#[cfg(feature = "std")]
75impl<'de> de::Visitor<'de> for AffineBytesVisitor {
76 type Value = Affine;
77
78 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
79 formatter.write_str("a byte slice that is 64 bytes in length")
80 }
81
82 fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
83 where
84 E: de::Error,
85 {
86 Ok(Affine::from(value))
87 }
88}
89
90impl Serialize for Affine {
91 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
92 where
93 S: serde::Serializer,
94 {
95 [self.x.b32(), self.y.b32()].concat().serialize(serializer)
96 }
97}
98
99impl<'de> Deserialize<'de> for Affine {
100 fn deserialize<D>(deserializer: D) -> Result<Affine, D::Error>
101 where
102 D: serde::Deserializer<'de>,
103 {
104 deserializer.deserialize_bytes(AffineBytesVisitor)
105 }
106}
107
108impl PartialOrd for Scalar {
109 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
110 Some(self.cmp(other))
111 }
112}
113
114impl Ord for Scalar {
115 fn cmp(&self, other: &Self) -> Ordering {
116 let mut ret = Ordering::Equal;
117 for i in (0..8).rev() {
118 if self.0[i] < other.0[i] {
119 ret = Ordering::Less;
120 break;
121 } else if self.0[i] > other.0[i] {
122 ret = Ordering::Greater;
123 break;
124 }
125 }
126 ret
127 }
128}
129
130impl From<&[u8]> for Scalar {
131 fn from(bytes: &[u8]) -> Self {
132 if bytes.len() != 32 {
133 panic!("Bytes length must be 32")
134 }
135 let mut tmp_bytes = [0u8; 32];
136 tmp_bytes[0..32].copy_from_slice(bytes);
137 Scalar::from(&tmp_bytes)
138 }
139}
140
141impl From<&[u8; 32]> for Scalar {
142 fn from(bytes: &[u8; 32]) -> Self {
143 let mut r = Scalar::default();
144 r.set_b32(bytes).unwrap_u8();
145 r
146 }
147}
148
149impl Serialize for Scalar {
150 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
151 where
152 S: serde::Serializer,
153 {
154 self.0.serialize(serializer)
155 }
156}
157
158impl<'de> Deserialize<'de> for Scalar {
159 fn deserialize<D>(deserializer: D) -> Result<Scalar, D::Error>
160 where
161 D: serde::Deserializer<'de>,
162 {
163 let bytes = <[u8; 32]>::deserialize(deserializer)?;
164 Ok(Scalar::from(&bytes[..]))
165 }
166}