1use core::{
2 iter::Sum,
3 ops::{Mul, MulAssign},
4};
5
6use elliptic_curve::{
7 bigint::{ArrayEncoding, U256},
8 ops::{LinearCombination, MulByGenerator},
9 point::{AffineCoordinates, DecompactPoint, DecompressPoint},
10 rand_core::RngCore,
11 sec1::{FromEncodedPoint, ToEncodedPoint},
12 subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
13 zeroize::DefaultIsZeroes,
14 FieldBytesEncoding,
15};
16use openvm_algebra_guest::IntMod;
17use openvm_ecc_guest::{
18 weierstrass::{IntrinsicCurve, WeierstrassPoint},
19 CyclicGroup,
20};
21
22use crate::{
23 internal::{Secp256k1Coord, Secp256k1Point, Secp256k1Scalar},
24 EncodedPoint, Secp256k1,
25};
26
27pub type FieldBytes = elliptic_curve::FieldBytes<Secp256k1>;
33
34impl FieldBytesEncoding<Secp256k1> for U256 {
35 fn decode_field_bytes(field_bytes: &FieldBytes) -> Self {
36 U256::from_be_byte_array(*field_bytes)
37 }
38
39 fn encode_field_bytes(&self) -> FieldBytes {
40 self.to_be_byte_array()
41 }
42}
43
44impl AffineCoordinates for Secp256k1Point {
45 type FieldRepr = FieldBytes;
46
47 fn x(&self) -> FieldBytes {
48 *FieldBytes::from_slice(&<Self as WeierstrassPoint>::x(self).to_be_bytes())
49 }
50
51 fn y_is_odd(&self) -> Choice {
52 (self.y().as_le_bytes()[0] & 1).into()
53 }
54}
55
56impl Copy for Secp256k1Point {}
57
58impl ConditionallySelectable for Secp256k1Point {
59 fn conditional_select(
60 a: &Secp256k1Point,
61 b: &Secp256k1Point,
62 choice: Choice,
63 ) -> Secp256k1Point {
64 Secp256k1Point::from_xy_unchecked(
65 Secp256k1Coord::conditional_select(
66 <Self as WeierstrassPoint>::x(a),
67 <Self as WeierstrassPoint>::x(b),
68 choice,
69 ),
70 Secp256k1Coord::conditional_select(a.y(), b.y(), choice),
71 )
72 }
73}
74
75impl ConstantTimeEq for Secp256k1Point {
76 fn ct_eq(&self, other: &Secp256k1Point) -> Choice {
77 <Self as WeierstrassPoint>::x(self).ct_eq(<Self as WeierstrassPoint>::x(other))
78 & self.y().ct_eq(other.y())
79 }
80}
81
82impl Default for Secp256k1Point {
83 fn default() -> Self {
84 <Self as WeierstrassPoint>::IDENTITY
85 }
86}
87
88impl DefaultIsZeroes for Secp256k1Point {}
89
90impl Sum for Secp256k1Point {
91 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
92 iter.fold(<Self as WeierstrassPoint>::IDENTITY, |a, b| a + b)
93 }
94}
95
96impl<'a> Sum<&'a Secp256k1Point> for Secp256k1Point {
97 fn sum<I: Iterator<Item = &'a Secp256k1Point>>(iter: I) -> Self {
98 iter.cloned().sum()
99 }
100}
101
102impl Mul<Secp256k1Scalar> for Secp256k1Point {
103 type Output = Secp256k1Point;
104
105 fn mul(self, other: Secp256k1Scalar) -> Secp256k1Point {
106 Secp256k1::msm(&[other], &[self])
107 }
108}
109
110impl Mul<&Secp256k1Scalar> for &Secp256k1Point {
111 type Output = Secp256k1Point;
112
113 fn mul(self, other: &Secp256k1Scalar) -> Secp256k1Point {
114 Secp256k1::msm(&[*other], &[*self])
115 }
116}
117
118impl Mul<&Secp256k1Scalar> for Secp256k1Point {
119 type Output = Secp256k1Point;
120
121 fn mul(self, other: &Secp256k1Scalar) -> Secp256k1Point {
122 Secp256k1::msm(&[*other], &[self])
123 }
124}
125
126impl MulAssign<Secp256k1Scalar> for Secp256k1Point {
127 fn mul_assign(&mut self, rhs: Secp256k1Scalar) {
128 *self = Secp256k1::msm(&[rhs], &[*self]);
129 }
130}
131
132impl MulAssign<&Secp256k1Scalar> for Secp256k1Point {
133 fn mul_assign(&mut self, rhs: &Secp256k1Scalar) {
134 *self = Secp256k1::msm(&[*rhs], &[*self]);
135 }
136}
137
138impl elliptic_curve::Group for Secp256k1Point {
139 type Scalar = Secp256k1Scalar;
140
141 fn random(mut _rng: impl RngCore) -> Self {
142 unimplemented!()
144 }
145
146 fn identity() -> Self {
147 <Self as WeierstrassPoint>::IDENTITY
148 }
149
150 fn generator() -> Self {
151 Self::GENERATOR
152 }
153
154 fn is_identity(&self) -> Choice {
155 (<Self as openvm_ecc_guest::Group>::is_identity(self) as u8).into()
156 }
157
158 fn double(&self) -> Self {
159 self + self
160 }
161}
162
163impl elliptic_curve::group::Curve for Secp256k1Point {
164 type AffineRepr = Secp256k1Point;
165
166 fn to_affine(&self) -> Secp256k1Point {
167 *self
168 }
169}
170
171impl LinearCombination for Secp256k1Point {
172 fn lincomb(x: &Self, k: &Self::Scalar, y: &Self, l: &Self::Scalar) -> Self {
173 Secp256k1::msm(&[*k, *l], &[*x, *y])
174 }
175}
176
177impl MulByGenerator for Secp256k1Point {}
179
180impl DecompressPoint<Secp256k1> for Secp256k1Point {
181 fn decompress(x_bytes: &FieldBytes, y_is_odd: Choice) -> CtOption<Self> {
183 use openvm_ecc_guest::weierstrass::FromCompressed;
184
185 let x = Secp256k1Coord::from_be_bytes_unchecked(x_bytes.as_slice());
186 let rec_id = y_is_odd.unwrap_u8();
187 CtOption::new(x, (x.is_reduced() as u8).into()).and_then(|x| {
188 let y = <Secp256k1Point as FromCompressed<Secp256k1Coord>>::decompress(x, &rec_id);
189 match y {
190 Some(point) => CtOption::new(point, 1.into()),
191 None => CtOption::new(Secp256k1Point::default(), 0.into()),
192 }
193 })
194 }
195}
196
197impl DecompactPoint<Secp256k1> for Secp256k1Point {
199 fn decompact(x_bytes: &FieldBytes) -> CtOption<Self> {
200 Self::decompress(x_bytes, Choice::from(0))
201 }
202}
203
204impl FromEncodedPoint<Secp256k1> for Secp256k1Point {
205 fn from_encoded_point(encoded_point: &EncodedPoint) -> CtOption<Self> {
211 match openvm_ecc_guest::ecdsa::VerifyingKey::<Secp256k1>::from_sec1_bytes(
212 encoded_point.as_bytes(),
213 ) {
214 Ok(verifying_key) => CtOption::new(*verifying_key.as_affine(), 1.into()),
215 Err(_) => CtOption::new(Secp256k1Point::default(), 0.into()),
216 }
217 }
218}
219
220impl ToEncodedPoint<Secp256k1> for Secp256k1Point {
221 fn to_encoded_point(&self, compress: bool) -> EncodedPoint {
222 EncodedPoint::conditional_select(
223 &EncodedPoint::from_affine_coordinates(
224 &<Self as WeierstrassPoint>::x(self).to_be_bytes().into(),
225 &<Self as WeierstrassPoint>::y(self).to_be_bytes().into(),
226 compress,
227 ),
228 &EncodedPoint::identity(),
229 elliptic_curve::Group::is_identity(self),
230 )
231 }
232}
233
234impl TryFrom<EncodedPoint> for Secp256k1Point {
235 type Error = elliptic_curve::Error;
236
237 fn try_from(point: EncodedPoint) -> elliptic_curve::Result<Secp256k1Point> {
238 Secp256k1Point::try_from(&point)
239 }
240}
241
242impl TryFrom<&EncodedPoint> for Secp256k1Point {
243 type Error = elliptic_curve::Error;
244
245 fn try_from(point: &EncodedPoint) -> elliptic_curve::Result<Secp256k1Point> {
246 Option::from(Secp256k1Point::from_encoded_point(point)).ok_or(elliptic_curve::Error)
247 }
248}
249
250impl From<Secp256k1Point> for EncodedPoint {
251 fn from(affine_point: Secp256k1Point) -> EncodedPoint {
252 EncodedPoint::from(&affine_point)
253 }
254}
255
256impl From<&Secp256k1Point> for EncodedPoint {
257 fn from(affine_point: &Secp256k1Point) -> EncodedPoint {
258 affine_point.to_encoded_point(true)
259 }
260}