2026-07-02 15:16:25 +02:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
// Copyright (c) VexaHub and contributors.
|
2023-05-22 23:04:39 -07:00
|
|
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
2021-09-27 18:29:08 -07:00
|
|
|
|
|
|
|
|
//! Includes a series of tests for the group implementations
|
|
|
|
|
|
2021-12-25 22:54:27 +01:00
|
|
|
use crate::{Error, Group, Result};
|
2021-09-27 18:29:08 -07:00
|
|
|
|
2021-12-23 01:17:03 +01:00
|
|
|
// Test that the deserialization of a group element should throw an error if the
|
|
|
|
|
// identity element can be deserialized properly
|
2021-09-27 18:29:08 -07:00
|
|
|
|
|
|
|
|
#[test]
|
2021-12-25 22:54:27 +01:00
|
|
|
fn test_group_properties() -> Result<()> {
|
2022-01-21 22:52:09 +01:00
|
|
|
use p256::NistP256;
|
2023-02-09 00:48:12 +01:00
|
|
|
use p384::NistP384;
|
2023-11-13 01:14:52 +01:00
|
|
|
use p521::NistP521;
|
2022-01-21 22:52:09 +01:00
|
|
|
|
2021-12-23 07:50:48 +01:00
|
|
|
#[cfg(feature = "ristretto255")]
|
|
|
|
|
{
|
2022-01-18 12:34:28 +01:00
|
|
|
use crate::Ristretto255;
|
2021-09-27 18:29:08 -07:00
|
|
|
|
2022-01-18 12:34:28 +01:00
|
|
|
test_identity_element_error::<Ristretto255>()?;
|
|
|
|
|
test_zero_scalar_error::<Ristretto255>()?;
|
2021-12-23 07:50:48 +01:00
|
|
|
}
|
2021-09-27 18:29:08 -07:00
|
|
|
|
2022-01-21 22:52:09 +01:00
|
|
|
test_identity_element_error::<NistP256>()?;
|
|
|
|
|
test_zero_scalar_error::<NistP256>()?;
|
2021-09-27 18:29:08 -07:00
|
|
|
|
2023-02-09 00:48:12 +01:00
|
|
|
test_identity_element_error::<NistP384>()?;
|
|
|
|
|
test_zero_scalar_error::<NistP384>()?;
|
|
|
|
|
|
2023-11-13 01:14:52 +01:00
|
|
|
test_identity_element_error::<NistP521>()?;
|
|
|
|
|
test_zero_scalar_error::<NistP521>()?;
|
|
|
|
|
|
2021-09-27 18:29:08 -07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks that the identity element cannot be deserialized
|
2021-12-25 22:54:27 +01:00
|
|
|
fn test_identity_element_error<G: Group>() -> Result<()> {
|
2022-01-18 12:34:28 +01:00
|
|
|
let identity = G::identity_elem();
|
|
|
|
|
let result = G::deserialize_elem(&G::serialize_elem(identity));
|
2022-01-25 05:55:02 +01:00
|
|
|
assert!(matches!(result, Err(Error::Deserialization)));
|
2021-09-27 18:29:08 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks that the zero scalar cannot be deserialized
|
2021-12-25 22:54:27 +01:00
|
|
|
fn test_zero_scalar_error<G: Group>() -> Result<()> {
|
2022-01-18 12:34:28 +01:00
|
|
|
let zero_scalar = G::zero_scalar();
|
|
|
|
|
let result = G::deserialize_scalar(&G::serialize_scalar(zero_scalar));
|
2022-01-25 05:55:02 +01:00
|
|
|
assert!(matches!(result, Err(Error::Deserialization)));
|
2021-09-27 18:29:08 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|