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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use crate::{
backend::{self, BackendXOnly, TimeSensitive},
hash::HashInto,
marker::*,
Point, Scalar,
};
use core::marker::PhantomData;
use rand_core::{CryptoRng, RngCore};
#[derive(Clone)]
pub struct XOnly<YChoice = ()>(pub(crate) backend::XOnly, PhantomData<YChoice>);
impl<Y> XOnly<Y> {
pub fn from_bytes(bytes: [u8; 32]) -> Option<Self> {
backend::XOnly::from_bytes(bytes).map(Self::from_inner)
}
pub fn from_slice(slice: &[u8]) -> Option<Self> {
if slice.len() != 32 {
return None;
}
let mut bytes = [0u8; 32];
bytes.copy_from_slice(slice);
Self::from_bytes(bytes)
}
pub(crate) fn from_inner(inner: backend::XOnly) -> Self {
XOnly(inner, PhantomData)
}
pub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let mut bytes = [0u8; 32];
rng.fill_bytes(&mut bytes);
Self::from_bytes(bytes).unwrap_or_else(|| Self::random(rng))
}
pub fn as_bytes(&self) -> &[u8; 32] {
self.0.as_bytes()
}
pub fn into_bytes(self) -> [u8; 32] {
self.0.into_bytes()
}
}
impl<Y: YChoice> XOnly<Y> {
pub fn to_point(&self) -> Point<Y, Public, NonZero> {
Y::xonly_into_point(self.clone())
}
pub fn from_scalar_mul<GT>(G: &Point<GT>, x: &mut Scalar<impl Secrecy>) -> Self {
let X = crate::op::scalar_mul_point(x, G).mark::<Normal>();
let needs_negation = !Y::norm_point_matches(&X);
x.conditional_negate(needs_negation);
X.to_xonly().mark::<Y>()
}
}
impl<Y> HashInto for XOnly<Y> {
fn hash_into(&self, hash: &mut impl digest::Digest) {
hash.update(self.as_bytes())
}
}
impl<Y> PartialEq<XOnly<Y>> for XOnly<Y> {
fn eq(&self, rhs: &XOnly<Y>) -> bool {
crate::backend::ConstantTime::xonly_eq(&self.0, &rhs.0)
}
}
impl<T, Z, S> PartialEq<XOnly<SquareY>> for Point<T, S, Z> {
fn eq(&self, rhs: &XOnly<SquareY>) -> bool {
crate::op::EqXOnlySquareY::eq_xonly_square_y(self, rhs)
}
}
impl<T, Z, S> PartialEq<Point<T, S, Z>> for XOnly<SquareY> {
fn eq(&self, rhs: &Point<T, S, Z>) -> bool {
rhs == self
}
}
crate::impl_fromstr_deserailize! {
name => "secp256k1 x-coordinate",
fn from_bytes<Y>(bytes: [u8;32]) -> Option<XOnly<Y>> {
XOnly::from_bytes(bytes)
}
}
crate::impl_display_debug_serialize! {
fn to_bytes<Y>(xonly: &XOnly<Y>) -> &[u8;32] {
xonly.as_bytes()
}
}
#[cfg(test)]
mod test {
use super::*;
crate::test_plus_wasm! {
fn xonly_random() {
let _ = XOnly::<()>::random(&mut rand::thread_rng());
}
fn from_str() {
use crate::G;
use core::str::FromStr;
assert_eq!(
XOnly::<EvenY>::from_str(
"79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
)
.unwrap()
.to_point(),
*G
);
}
fn xonly_to_point() {
for _ in 0..crate::TEST_SOUNDNESS {
let xonly_even = XOnly::<EvenY>::random(&mut rand::thread_rng());
let xonly_square = XOnly::<SquareY>::random(&mut rand::thread_rng());
let point_even = xonly_even.to_point();
assert!(EvenY::norm_point_matches(&point_even));
let point_square = xonly_square.to_point();
assert!(SquareY::norm_point_matches(&point_square));
}
}
}
}