Skip to content

Commit 9a93c97

Browse files
authored
Fix to_bytes() for ProjectivePoint::identity() (#443)
1 parent 2d91c16 commit 9a93c97

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

p256/src/arithmetic/projective.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ impl GroupEncoding for ProjectivePoint {
7575
}
7676

7777
fn to_bytes(&self) -> Self::Repr {
78-
CompressedPoint::clone_from_slice(self.to_affine().to_encoded_point(true).as_bytes())
78+
let bytes = self.to_affine().to_encoded_point(true);
79+
let bytes = bytes.as_bytes();
80+
let mut result = CompressedPoint::default();
81+
result[..bytes.len()].copy_from_slice(bytes);
82+
result
7983
}
8084
}
8185

@@ -521,7 +525,7 @@ impl<'a> Neg for &'a ProjectivePoint {
521525
mod tests {
522526
use super::{AffinePoint, ProjectivePoint, Scalar};
523527
use crate::test_vectors::group::{ADD_TEST_VECTORS, MUL_TEST_VECTORS};
524-
use elliptic_curve::group::{ff::PrimeField, prime::PrimeCurveAffine};
528+
use elliptic_curve::group::{ff::PrimeField, prime::PrimeCurveAffine, GroupEncoding};
525529

526530
#[test]
527531
fn affine_to_projective() {
@@ -665,4 +669,10 @@ mod tests {
665669
assert_eq!(res.y.to_bytes(), coords.1.into());
666670
}
667671
}
672+
673+
#[test]
674+
fn projective_identity_to_bytes() {
675+
// This is technically an invalid SEC1 encoding, but is preferable to panicking.
676+
assert_eq!([0; 33], ProjectivePoint::identity().to_bytes().as_slice());
677+
}
668678
}

0 commit comments

Comments
 (0)