Skip to content

Commit 222b1c2

Browse files
Update for MapToCurve changes (#1206)
Signed-off-by: Andrew Whitehead <[email protected]>
1 parent 99bbeac commit 222b1c2

File tree

6 files changed

+58
-89
lines changed

6 files changed

+58
-89
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

k256/src/arithmetic/hash2curve.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@ use elliptic_curve::Field;
22
use elliptic_curve::array::Array;
33
use elliptic_curve::bigint::{ArrayEncoding, U256};
44
use elliptic_curve::consts::{U4, U16, U48};
5-
use elliptic_curve::group::cofactor::CofactorGroup;
65
use elliptic_curve::hash2curve::{
76
FromOkm, GroupDigest, Isogeny, IsogenyCoefficients, MapToCurve, OsswuMap, OsswuMapParams, Sgn0,
87
};
9-
use elliptic_curve::subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
8+
use elliptic_curve::subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
109

1110
use crate::{AffinePoint, ProjectivePoint, Scalar, Secp256k1};
1211

1312
use super::FieldElement;
1413

1514
impl GroupDigest for Secp256k1 {
16-
type FieldElement = FieldElement;
17-
1815
type K = U16;
1916
}
2017

@@ -130,11 +127,12 @@ impl OsswuMap for FieldElement {
130127
}
131128
}
132129

133-
impl MapToCurve for FieldElement {
134-
type Output = ProjectivePoint;
130+
impl MapToCurve for Secp256k1 {
131+
type CurvePoint = ProjectivePoint;
132+
type FieldElement = FieldElement;
135133

136-
fn map_to_curve(&self) -> Self::Output {
137-
let (rx, ry) = self.osswu();
134+
fn map_to_curve(element: FieldElement) -> Self::CurvePoint {
135+
let (rx, ry) = element.osswu();
138136
let (qx, qy) = FieldElement::isogeny(rx, ry);
139137

140138
AffinePoint {
@@ -144,6 +142,10 @@ impl MapToCurve for FieldElement {
144142
}
145143
.into()
146144
}
145+
146+
fn map_to_subgroup(point: Self::CurvePoint) -> ProjectivePoint {
147+
point
148+
}
147149
}
148150

149151
impl FromOkm for Scalar {
@@ -258,31 +260,14 @@ impl Isogeny for FieldElement {
258260
};
259261
}
260262

261-
impl CofactorGroup for ProjectivePoint {
262-
type Subgroup = ProjectivePoint;
263-
264-
fn clear_cofactor(&self) -> Self::Subgroup {
265-
*self
266-
}
267-
268-
fn into_subgroup(self) -> CtOption<Self::Subgroup> {
269-
CtOption::new(self, 1.into())
270-
}
271-
272-
fn is_torsion_free(&self) -> Choice {
273-
1.into()
274-
}
275-
}
276-
277263
#[cfg(test)]
278264
mod tests {
279-
use crate::{FieldElement, Scalar, Secp256k1, U256};
265+
use crate::{Scalar, Secp256k1, U256, arithmetic::field::FieldElement};
280266
use elliptic_curve::{
281267
Curve,
282268
array::Array,
283269
bigint::{ArrayEncoding, NonZero, U384},
284270
consts::U48,
285-
group::cofactor::CofactorGroup,
286271
hash2curve::{FromOkm, GroupDigest, MapToCurve},
287272
};
288273
use hex_literal::hex;
@@ -377,17 +362,17 @@ mod tests {
377362
assert_eq!(u[0].to_bytes().as_slice(), test_vector.u_0);
378363
assert_eq!(u[1].to_bytes().as_slice(), test_vector.u_1);
379364

380-
let q0 = u[0].map_to_curve();
365+
let q0 = Secp256k1::map_to_curve(u[0]);
381366
let aq0 = q0.to_affine();
382367
assert_eq!(aq0.x.to_bytes().as_slice(), test_vector.q0_x);
383368
assert_eq!(aq0.y.to_bytes().as_slice(), test_vector.q0_y);
384369

385-
let q1 = u[1].map_to_curve();
370+
let q1 = Secp256k1::map_to_curve(u[1]);
386371
let aq1 = q1.to_affine();
387372
assert_eq!(aq1.x.to_bytes().as_slice(), test_vector.q1_x);
388373
assert_eq!(aq1.y.to_bytes().as_slice(), test_vector.q1_y);
389374

390-
let p = q0.clear_cofactor() + q1.clear_cofactor();
375+
let p = Secp256k1::add_and_map_to_subgroup(q0, q1);
391376
let ap = p.to_affine();
392377
assert_eq!(ap.x.to_bytes().as_slice(), test_vector.p_x);
393378
assert_eq!(ap.y.to_bytes().as_slice(), test_vector.p_y);

p256/src/arithmetic/hash2curve.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use elliptic_curve::{
1010
};
1111

1212
impl GroupDigest for NistP256 {
13-
type FieldElement = FieldElement;
14-
1513
type K = U16;
1614
}
1715

@@ -60,17 +58,22 @@ impl OsswuMap for FieldElement {
6058
};
6159
}
6260

63-
impl MapToCurve for FieldElement {
64-
type Output = ProjectivePoint;
61+
impl MapToCurve for NistP256 {
62+
type CurvePoint = ProjectivePoint;
63+
type FieldElement = FieldElement;
6564

66-
fn map_to_curve(&self) -> Self::Output {
67-
let (qx, qy) = self.osswu();
65+
fn map_to_curve(element: Self::FieldElement) -> Self::CurvePoint {
66+
let (qx, qy) = element.osswu();
6867

6968
// TODO(tarcieri): assert that `qy` is correct? less circuitous conversion?
7069
AffinePoint::decompress(&qx.to_bytes(), qy.is_odd())
7170
.unwrap()
7271
.into()
7372
}
73+
74+
fn map_to_subgroup(point: Self::CurvePoint) -> ProjectivePoint {
75+
point
76+
}
7477
}
7578

7679
impl FromOkm for Scalar {
@@ -102,7 +105,6 @@ mod tests {
102105
array::Array,
103106
bigint::{ArrayEncoding, CheckedSub, NonZero, U384},
104107
consts::U48,
105-
group::cofactor::CofactorGroup,
106108
hash2curve::{self, ExpandMsgXmd, FromOkm, GroupDigest, MapToCurve, OsswuMap},
107109
sec1::{self, ToEncodedPoint},
108110
};
@@ -227,13 +229,13 @@ mod tests {
227229
assert_eq!(u[0].to_bytes().as_slice(), test_vector.u_0);
228230
assert_eq!(u[1].to_bytes().as_slice(), test_vector.u_1);
229231

230-
let q0 = u[0].map_to_curve();
232+
let q0 = NistP256::map_to_curve(u[0]);
231233
assert_point_eq!(q0, test_vector.q0_x, test_vector.q0_y);
232234

233-
let q1 = u[1].map_to_curve();
235+
let q1 = NistP256::map_to_curve(u[1]);
234236
assert_point_eq!(q1, test_vector.q1_x, test_vector.q1_y);
235237

236-
let p = q0.clear_cofactor() + q1.clear_cofactor();
238+
let p = q0 + q1;
237239
assert_point_eq!(p, test_vector.p_x, test_vector.p_y);
238240

239241
// complete run

p384/src/arithmetic/hash2curve.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use elliptic_curve::{
1111
};
1212

1313
impl GroupDigest for NistP384 {
14-
type FieldElement = FieldElement;
15-
1614
type K = U24;
1715
}
1816

@@ -63,17 +61,22 @@ impl OsswuMap for FieldElement {
6361
};
6462
}
6563

66-
impl MapToCurve for FieldElement {
67-
type Output = ProjectivePoint;
64+
impl MapToCurve for NistP384 {
65+
type CurvePoint = ProjectivePoint;
66+
type FieldElement = FieldElement;
6867

69-
fn map_to_curve(&self) -> Self::Output {
70-
let (qx, qy) = self.osswu();
68+
fn map_to_curve(element: FieldElement) -> Self::CurvePoint {
69+
let (qx, qy) = element.osswu();
7170

7271
// TODO(tarcieri): assert that `qy` is correct? less circuitous conversion?
7372
AffinePoint::decompress(&qx.to_bytes(), qy.is_odd())
7473
.unwrap()
7574
.into()
7675
}
76+
77+
fn map_to_subgroup(point: Self::CurvePoint) -> ProjectivePoint {
78+
point
79+
}
7780
}
7881

7982
impl FromOkm for Scalar {
@@ -98,13 +101,15 @@ impl FromOkm for Scalar {
98101

99102
#[cfg(test)]
100103
mod tests {
101-
use crate::{FieldElement, NistP384, Scalar, arithmetic::field::MODULUS};
104+
use crate::{
105+
NistP384, Scalar,
106+
arithmetic::field::{FieldElement, MODULUS},
107+
};
102108
use elliptic_curve::{
103109
Curve,
104110
array::Array,
105111
bigint::{ArrayEncoding, CheckedSub, NonZero, U384, U576},
106112
consts::U72,
107-
group::cofactor::CofactorGroup,
108113
hash2curve::{self, ExpandMsgXmd, FromOkm, GroupDigest, MapToCurve, OsswuMap},
109114
ops::Reduce,
110115
sec1::{self, ToEncodedPoint},
@@ -229,13 +234,13 @@ mod tests {
229234
assert_eq!(u[0].to_bytes().as_slice(), test_vector.u_0);
230235
assert_eq!(u[1].to_bytes().as_slice(), test_vector.u_1);
231236

232-
let q0 = u[0].map_to_curve();
237+
let q0 = NistP384::map_to_curve(u[0]);
233238
assert_point_eq!(q0, test_vector.q0_x, test_vector.q0_y);
234239

235-
let q1 = u[1].map_to_curve();
240+
let q1 = NistP384::map_to_curve(u[1]);
236241
assert_point_eq!(q1, test_vector.q1_x, test_vector.q1_y);
237242

238-
let p = q0.clear_cofactor() + q1.clear_cofactor();
243+
let p = NistP384::add_and_map_to_subgroup(q0, q1);
239244
assert_point_eq!(p, test_vector.p_x, test_vector.p_y);
240245

241246
// complete run

p521/src/arithmetic/hash2curve.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use elliptic_curve::{
1111
};
1212

1313
impl GroupDigest for NistP521 {
14-
type FieldElement = FieldElement;
15-
1614
type K = U32;
1715
}
1816

@@ -66,17 +64,22 @@ impl OsswuMap for FieldElement {
6664
};
6765
}
6866

69-
impl MapToCurve for FieldElement {
70-
type Output = ProjectivePoint;
67+
impl MapToCurve for NistP521 {
68+
type CurvePoint = ProjectivePoint;
69+
type FieldElement = FieldElement;
7170

72-
fn map_to_curve(&self) -> Self::Output {
73-
let (qx, qy) = self.osswu();
71+
fn map_to_curve(element: FieldElement) -> Self::CurvePoint {
72+
let (qx, qy) = element.osswu();
7473

7574
// TODO(tarcieri): assert that `qy` is correct? less circuitous conversion?
7675
AffinePoint::decompress(&qx.to_bytes(), qy.is_odd())
7776
.unwrap()
7877
.into()
7978
}
79+
80+
fn map_to_subgroup(point: Self::CurvePoint) -> ProjectivePoint {
81+
point
82+
}
8083
}
8184

8285
impl FromOkm for Scalar {
@@ -110,7 +113,6 @@ mod tests {
110113
array::Array,
111114
bigint::{ArrayEncoding, CheckedSub, NonZero, U576, U896},
112115
consts::U98,
113-
group::cofactor::CofactorGroup,
114116
hash2curve::{self, ExpandMsgXmd, FromOkm, GroupDigest, MapToCurve, OsswuMap},
115117
ops::Reduce,
116118
sec1::{self, ToEncodedPoint},
@@ -235,13 +237,13 @@ mod tests {
235237
assert_eq!(u[0].to_bytes().as_slice(), test_vector.u_0);
236238
assert_eq!(u[1].to_bytes().as_slice(), test_vector.u_1);
237239

238-
let q0 = u[0].map_to_curve();
240+
let q0 = NistP521::map_to_curve(u[0]);
239241
assert_point_eq!(q0, test_vector.q0_x, test_vector.q0_y);
240242

241-
let q1 = u[1].map_to_curve();
243+
let q1 = NistP521::map_to_curve(u[1]);
242244
assert_point_eq!(q1, test_vector.q1_x, test_vector.q1_y);
243245

244-
let p = q0.clear_cofactor() + q1.clear_cofactor();
246+
let p = NistP521::add_and_map_to_subgroup(q0, q1);
245247
assert_point_eq!(p, test_vector.p_x, test_vector.p_y);
246248

247249
// complete run

primeorder/src/projective.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use elliptic_curve::{
1414
bigint::ArrayEncoding,
1515
group::{
1616
self, Group, GroupEncoding,
17-
cofactor::CofactorGroup,
1817
prime::{PrimeCurve, PrimeGroup},
1918
},
2019
ops::{BatchInvert, LinearCombination},
@@ -149,30 +148,6 @@ where
149148
}
150149
}
151150

152-
impl<C> CofactorGroup for ProjectivePoint<C>
153-
where
154-
Self: Double,
155-
C: PrimeCurveParams,
156-
CompressedPoint<C>: Copy + Send + Sync,
157-
FieldBytes<C>: Copy,
158-
FieldBytesSize<C>: ModulusSize,
159-
<UncompressedPointSize<C> as ArraySize>::ArrayType<u8>: Copy,
160-
{
161-
type Subgroup = Self;
162-
163-
fn clear_cofactor(&self) -> Self::Subgroup {
164-
*self
165-
}
166-
167-
fn into_subgroup(self) -> CtOption<Self> {
168-
CtOption::new(self, 1.into())
169-
}
170-
171-
fn is_torsion_free(&self) -> Choice {
172-
1.into()
173-
}
174-
}
175-
176151
impl<C> ConditionallySelectable for ProjectivePoint<C>
177152
where
178153
C: PrimeCurveParams,

0 commit comments

Comments
 (0)