Skip to content

Commit 7b4b59f

Browse files
fix(ecdsa): validate public key is not identity (#1743)
1 parent dc4d84a commit 7b4b59f

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

extensions/ecc/guest/src/ecdsa.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,17 @@ where
9191
C::Point: WeierstrassPoint + Group + FromCompressed<Coordinate<C>>,
9292
Coordinate<C>: IntMod,
9393
{
94-
pub fn new(point: <C as IntrinsicCurve>::Point) -> Self {
95-
Self { point }
94+
/// Convert an [`AffinePoint`] into a [`PublicKey`].
95+
/// In addition, for `Coordinate<C>` implementing `IntMod`, this function will assert that the
96+
/// affine coordinates of `point` are both in canonical form.
97+
pub fn from_affine(point: AffinePoint<C>) -> Result<Self> {
98+
// Internally this calls `is_eq` on `x` and `y` coordinates, which will assert `x, y` are
99+
// reduced.
100+
if point.is_identity() {
101+
Err(Error::new())
102+
} else {
103+
Ok(Self { point })
104+
}
96105
}
97106

98107
pub fn from_sec1_bytes(bytes: &[u8]) -> Result<Self>
@@ -188,7 +197,7 @@ where
188197
}
189198

190199
pub fn from_affine(point: <C as IntrinsicCurve>::Point) -> Result<Self> {
191-
let public_key = PublicKey::<C>::new(point);
200+
let public_key = PublicKey::<C>::from_affine(point)?;
192201
Ok(Self::new(public_key))
193202
}
194203

@@ -453,9 +462,9 @@ where
453462
let u2 = s.div_unsafe(&r);
454463
let NEG_G = C::Point::NEG_GENERATOR;
455464
let point = <C as IntrinsicCurve>::msm(&[neg_u1, u2], &[NEG_G, R]);
456-
let public_key = PublicKey { point };
465+
let vk = VerifyingKey::from_affine(point)?;
457466

458-
Ok(VerifyingKey { inner: public_key })
467+
Ok(vk)
459468
}
460469
}
461470

extensions/ecc/tests/src/test_vectors.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ pub struct RecoveryTestVector {
2020
pub const P256_RECOVERY_TEST_VECTORS: &[RecoveryTestVector] = &[RecoveryTestVector {
2121
pk: hex!("020000000000000000000000000000000000000000000000000000000000000000"),
2222
msg: hex!("00000000000000000000FFFFFFFF03030BFFFFFFFFFF030BFFFFFFFFFFFFF8FC"),
23-
sig: hex!("00000000ffffffff00000000000000004319055258e8617b0c46353d039cdaaf0000000000000000000000000000000000000000000000000000000000000001"
24-
),
23+
sig: hex!("00000000ffffffff00000000000000004319055258e8617b0c46353d039cdaaf0000000000000000000000000000000000000000000000000000000000000001"),
2524
recid: 2,
2625
ok: false,
26+
},
27+
RecoveryTestVector{
28+
pk: hex!("020000000000000000000000000000000000000000000000000000000000000000"),
29+
msg: hex!("000000000000000000000000000000000000000000000000000CFD5E267CBB5E"),
30+
sig: hex!("6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296000000000000000000000000000000000000000000000000000cfd5e267cbb5e"),
31+
recid: 1,
32+
ok: false
2733
}];

0 commit comments

Comments
 (0)