chore: checkpoint before Python removal

This commit is contained in:
2026-03-26 22:33:59 +00:00
parent 683cec9307
commit e568ddf82a
29972 changed files with 11269302 additions and 2 deletions

136
vendor/p256/tests/affine.rs vendored Normal file
View File

@@ -0,0 +1,136 @@
//! Affine arithmetic tests.
#![cfg(all(feature = "arithmetic"))]
use elliptic_curve::{
group::{prime::PrimeCurveAffine, GroupEncoding},
sec1::{FromEncodedPoint, ToCompactEncodedPoint, ToEncodedPoint},
};
use hex_literal::hex;
use p256::{AffinePoint, EncodedPoint};
const UNCOMPRESSED_BASEPOINT: &[u8] = &hex!(
"04 6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296
4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
);
const COMPRESSED_BASEPOINT: &[u8] =
&hex!("03 6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
// Tag compact with 05 as the first byte, to trigger tag based compaction
const COMPACT_BASEPOINT: &[u8] =
&hex!("05 8e38fc4ffe677662dde8e1a63fbcd45959d2a4c3004d27e98c4fedf2d0c14c01");
// Tag uncompact basepoint with 04 as the first byte as it is uncompressed
const UNCOMPACT_BASEPOINT: &[u8] = &hex!(
"04 8e38fc4ffe677662dde8e1a63fbcd45959d2a4c3004d27e98c4fedf2d0c14c0
13ca9d8667de0c07aa71d98b3c8065d2e97ab7bb9cb8776bcc0577a7ac58acd4e"
);
#[test]
fn uncompressed_round_trip() {
let pubkey = EncodedPoint::from_bytes(UNCOMPRESSED_BASEPOINT).unwrap();
let point = AffinePoint::from_encoded_point(&pubkey).unwrap();
assert_eq!(point, AffinePoint::generator());
let res: EncodedPoint = point.into();
assert_eq!(res, pubkey);
}
#[test]
fn compressed_round_trip() {
let pubkey = EncodedPoint::from_bytes(COMPRESSED_BASEPOINT).unwrap();
let point = AffinePoint::from_encoded_point(&pubkey).unwrap();
assert_eq!(point, AffinePoint::generator());
let res: EncodedPoint = point.to_encoded_point(true);
assert_eq!(res, pubkey);
}
#[test]
fn uncompressed_to_compressed() {
let encoded = EncodedPoint::from_bytes(UNCOMPRESSED_BASEPOINT).unwrap();
let res = AffinePoint::from_encoded_point(&encoded)
.unwrap()
.to_encoded_point(true);
assert_eq!(res.as_bytes(), COMPRESSED_BASEPOINT);
}
#[test]
fn compressed_to_uncompressed() {
let encoded = EncodedPoint::from_bytes(COMPRESSED_BASEPOINT).unwrap();
let res = AffinePoint::from_encoded_point(&encoded)
.unwrap()
.to_encoded_point(false);
assert_eq!(res.as_bytes(), UNCOMPRESSED_BASEPOINT);
}
#[test]
fn affine_negation() {
let basepoint = AffinePoint::generator();
assert_eq!(-(-basepoint), basepoint);
}
#[test]
fn compact_round_trip() {
let pubkey = EncodedPoint::from_bytes(COMPACT_BASEPOINT).unwrap();
assert!(pubkey.is_compact());
let point = AffinePoint::from_encoded_point(&pubkey).unwrap();
let res = point.to_compact_encoded_point().unwrap();
assert_eq!(res, pubkey)
}
#[test]
fn uncompact_to_compact() {
let pubkey = EncodedPoint::from_bytes(UNCOMPACT_BASEPOINT).unwrap();
assert_eq!(false, pubkey.is_compact());
let point = AffinePoint::from_encoded_point(&pubkey).unwrap();
let res = point.to_compact_encoded_point().unwrap();
assert_eq!(res.as_bytes(), COMPACT_BASEPOINT)
}
#[test]
fn compact_to_uncompact() {
let pubkey = EncodedPoint::from_bytes(COMPACT_BASEPOINT).unwrap();
assert!(pubkey.is_compact());
let point = AffinePoint::from_encoded_point(&pubkey).unwrap();
// Do not do compact encoding as we want to keep uncompressed point
let res = point.to_encoded_point(false);
assert_eq!(res.as_bytes(), UNCOMPACT_BASEPOINT);
}
#[test]
fn identity_encoding() {
// This is technically an invalid SEC1 encoding, but is preferable to panicking.
assert_eq!([0; 33], AffinePoint::IDENTITY.to_bytes().as_slice());
assert!(bool::from(
AffinePoint::from_bytes(&AffinePoint::IDENTITY.to_bytes())
.unwrap()
.is_identity()
))
}
#[test]
fn noncompatible_is_none() {
use elliptic_curve::generic_array::GenericArray;
let noncompactable_secret = GenericArray::from([
175, 232, 180, 255, 91, 106, 124, 191, 224, 31, 177, 208, 236, 127, 191, 169, 201, 217, 75,
141, 184, 175, 120, 85, 171, 8, 54, 57, 33, 177, 83, 211,
]);
let public_key = p256::SecretKey::from_bytes(&noncompactable_secret)
.unwrap()
.public_key();
let is_compactable = public_key
.as_affine()
.to_compact_encoded_point()
.is_some()
.unwrap_u8();
assert_eq!(is_compactable, 0);
}

26
vendor/p256/tests/ecdsa.rs vendored Normal file
View File

@@ -0,0 +1,26 @@
//! ECDSA tests.
#![cfg(feature = "arithmetic")]
use elliptic_curve::ops::Reduce;
use p256::{
ecdsa::{SigningKey, VerifyingKey},
NonZeroScalar, U256,
};
use proptest::prelude::*;
prop_compose! {
fn signing_key()(bytes in any::<[u8; 32]>()) -> SigningKey {
<NonZeroScalar as Reduce<U256>>::reduce_bytes(&bytes.into()).into()
}
}
proptest! {
#[test]
fn recover_from_msg(sk in signing_key()) {
let msg = b"example";
let (signature, v) = sk.sign_recoverable(msg).unwrap();
let recovered_vk = VerifyingKey::recover_from_msg(msg, &signature, v).unwrap();
prop_assert_eq!(sk.verifying_key(), &recovered_vk);
}
}

Binary file not shown.

View File

@@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgaWJBcVYaYzQN4OfY
afKgVJJVjhoEhotqn4VKhmeIGI2hRANCAAQcrP+1Xy8s79idies3SyaBFSRSgC3u
oJkWBoE32DnPf8SBpESSME1+9mrBF77+g6jQjxVfK1L59hjdRHApBI4P
-----END PRIVATE KEY-----

Binary file not shown.

View File

@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEHKz/tV8vLO/YnYnrN0smgRUkUoAt
7qCZFgaBN9g5z3/EgaREkjBNfvZqwRe+/oOo0I8VXytS+fYY3URwKQSODw==
-----END PUBLIC KEY-----

99
vendor/p256/tests/pkcs8.rs vendored Normal file
View File

@@ -0,0 +1,99 @@
//! PKCS#8 tests
#![cfg(feature = "pkcs8")]
use hex_literal::hex;
use p256::{
elliptic_curve::sec1::ToEncodedPoint,
pkcs8::{DecodePrivateKey, DecodePublicKey},
};
#[cfg(feature = "pem")]
use p256::elliptic_curve::pkcs8::{EncodePrivateKey, EncodePublicKey};
/// DER-encoded PKCS#8 private key
const PKCS8_PRIVATE_KEY_DER: &[u8; 138] = include_bytes!("examples/pkcs8-private-key.der");
/// DER-encoded PKCS#8 public key
const PKCS8_PUBLIC_KEY_DER: &[u8; 91] = include_bytes!("examples/pkcs8-public-key.der");
/// PEM-encoded PKCS#8 private key
#[cfg(feature = "pem")]
const PKCS8_PRIVATE_KEY_PEM: &str = include_str!("examples/pkcs8-private-key.pem");
/// PEM-encoded PKCS#8 public key
#[cfg(feature = "pem")]
const PKCS8_PUBLIC_KEY_PEM: &str = include_str!("examples/pkcs8-public-key.pem");
#[test]
fn decode_pkcs8_private_key_from_der() {
let secret_key = p256::SecretKey::from_pkcs8_der(&PKCS8_PRIVATE_KEY_DER[..]).unwrap();
let expected_scalar = hex!("69624171561A63340DE0E7D869F2A05492558E1A04868B6A9F854A866788188D");
assert_eq!(secret_key.to_bytes().as_slice(), &expected_scalar[..]);
}
#[test]
fn decode_pkcs8_public_key_from_der() {
let public_key = p256::PublicKey::from_public_key_der(&PKCS8_PUBLIC_KEY_DER[..]).unwrap();
let expected_sec1_point = hex!("041CACFFB55F2F2CEFD89D89EB374B2681152452802DEEA09916068137D839CF7FC481A44492304D7EF66AC117BEFE83A8D08F155F2B52F9F618DD447029048E0F");
assert_eq!(
public_key.to_encoded_point(false).as_bytes(),
&expected_sec1_point[..]
);
}
#[test]
#[cfg(feature = "pem")]
fn decode_pkcs8_private_key_from_pem() {
let secret_key = PKCS8_PRIVATE_KEY_PEM.parse::<p256::SecretKey>().unwrap();
// Ensure key parses equivalently to DER
let der_key = p256::SecretKey::from_pkcs8_der(&PKCS8_PRIVATE_KEY_DER[..]).unwrap();
assert_eq!(secret_key.to_bytes(), der_key.to_bytes());
}
#[test]
#[cfg(feature = "pem")]
fn decode_pkcs8_public_key_from_pem() {
let public_key = PKCS8_PUBLIC_KEY_PEM.parse::<p256::PublicKey>().unwrap();
// Ensure key parses equivalently to DER
let der_key = p256::PublicKey::from_public_key_der(&PKCS8_PUBLIC_KEY_DER[..]).unwrap();
assert_eq!(public_key, der_key);
}
#[test]
#[cfg(feature = "pem")]
fn encode_pkcs8_private_key_to_der() {
let original_secret_key = p256::SecretKey::from_pkcs8_der(&PKCS8_PRIVATE_KEY_DER[..]).unwrap();
let reencoded_secret_key = original_secret_key.to_pkcs8_der().unwrap();
assert_eq!(reencoded_secret_key.as_bytes(), &PKCS8_PRIVATE_KEY_DER[..]);
}
#[test]
#[cfg(feature = "pem")]
fn encode_pkcs8_public_key_to_der() {
let original_public_key =
p256::PublicKey::from_public_key_der(&PKCS8_PUBLIC_KEY_DER[..]).unwrap();
let reencoded_public_key = original_public_key.to_public_key_der().unwrap();
assert_eq!(reencoded_public_key.as_ref(), &PKCS8_PUBLIC_KEY_DER[..]);
}
#[test]
#[cfg(feature = "pem")]
fn encode_pkcs8_private_key_to_pem() {
let original_secret_key = p256::SecretKey::from_pkcs8_der(&PKCS8_PRIVATE_KEY_DER[..]).unwrap();
let reencoded_secret_key = original_secret_key
.to_pkcs8_pem(Default::default())
.unwrap();
assert_eq!(reencoded_secret_key.as_str(), PKCS8_PRIVATE_KEY_PEM);
}
#[test]
#[cfg(feature = "pem")]
fn encode_pkcs8_public_key_to_pem() {
let original_public_key =
p256::PublicKey::from_public_key_der(&PKCS8_PUBLIC_KEY_DER[..]).unwrap();
let reencoded_public_key = original_public_key.to_string();
assert_eq!(reencoded_public_key.as_str(), PKCS8_PUBLIC_KEY_PEM);
}

27
vendor/p256/tests/projective.rs vendored Normal file
View File

@@ -0,0 +1,27 @@
//! Projective arithmetic tests.
#![cfg(all(feature = "arithmetic", feature = "test-vectors"))]
use elliptic_curve::{
group::{ff::PrimeField, GroupEncoding},
sec1::{self, ToEncodedPoint},
};
use p256::{
test_vectors::group::{ADD_TEST_VECTORS, MUL_TEST_VECTORS},
AffinePoint, ProjectivePoint, Scalar,
};
use primeorder::{impl_projective_arithmetic_tests, Double};
impl_projective_arithmetic_tests!(
AffinePoint,
ProjectivePoint,
Scalar,
ADD_TEST_VECTORS,
MUL_TEST_VECTORS
);
#[test]
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());
}

22
vendor/p256/tests/scalar.rs vendored Normal file
View File

@@ -0,0 +1,22 @@
//! Scalar arithmetic tests.
#![cfg(feature = "arithmetic")]
use elliptic_curve::ops::{Invert, Reduce};
use p256::{Scalar, U256};
use proptest::prelude::*;
prop_compose! {
fn scalar()(bytes in any::<[u8; 32]>()) -> Scalar {
<Scalar as Reduce<U256>>::reduce_bytes(&bytes.into())
}
}
proptest! {
#[test]
fn invert_and_invert_vartime_are_equivalent(w in scalar()) {
let inv: Option<Scalar> = w.invert().into();
let inv_vartime: Option<Scalar> = w.invert_vartime().into();
prop_assert_eq!(inv, inv_vartime);
}
}