From b3bb21812f8f7d99c0b57e8deafb1198d5fb67b5 Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Thu, 30 Sep 2021 11:55:17 +0200 Subject: [PATCH 1/2] Fix `to_bytes()` for `ProjectivePoint::identity()` --- p256/src/arithmetic/projective.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/p256/src/arithmetic/projective.rs b/p256/src/arithmetic/projective.rs index 05d26ad89..d319196bd 100644 --- a/p256/src/arithmetic/projective.rs +++ b/p256/src/arithmetic/projective.rs @@ -75,7 +75,11 @@ impl GroupEncoding for ProjectivePoint { } fn to_bytes(&self) -> Self::Repr { - CompressedPoint::clone_from_slice(self.to_affine().to_encoded_point(true).as_bytes()) + let bytes = self.to_affine().to_encoded_point(true); + let bytes = bytes.as_bytes(); + let mut result = CompressedPoint::default(); + result[..bytes.len()].copy_from_slice(bytes); + result } } @@ -521,7 +525,7 @@ impl<'a> Neg for &'a ProjectivePoint { mod tests { use super::{AffinePoint, ProjectivePoint, Scalar}; use crate::test_vectors::group::{ADD_TEST_VECTORS, MUL_TEST_VECTORS}; - use elliptic_curve::group::{ff::PrimeField, prime::PrimeCurveAffine}; + use elliptic_curve::group::{ff::PrimeField, prime::PrimeCurveAffine, GroupEncoding}; #[test] fn affine_to_projective() { @@ -665,4 +669,9 @@ mod tests { assert_eq!(res.y.to_bytes(), coords.1.into()); } } + + #[test] + fn projective_to_bytes() { + assert_eq!([0; 33], ProjectivePoint::identity().to_bytes().as_slice()); + } } From 0ba5440254df0928c69b80cf6e74182931182141 Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Thu, 30 Sep 2021 14:47:33 +0200 Subject: [PATCH 2/2] Add a note explaining the test --- p256/src/arithmetic/projective.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/p256/src/arithmetic/projective.rs b/p256/src/arithmetic/projective.rs index d319196bd..24fc76191 100644 --- a/p256/src/arithmetic/projective.rs +++ b/p256/src/arithmetic/projective.rs @@ -671,7 +671,8 @@ mod tests { } #[test] - fn projective_to_bytes() { + fn projective_identity_to_bytes() { + // This is technically an invalid SEC1 encoding, but is preferable to panicking. assert_eq!([0; 33], ProjectivePoint::identity().to_bytes().as_slice()); } }