Files
voprf-vx/src/group/tests.rs
T

54 lines
1.6 KiB
Rust
Raw Normal View History

2021-09-27 18:29:08 -07:00
// Copyright (c) Facebook, Inc. and its affiliates.
//
2021-09-27 18:53:06 -07:00
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree and the Apache
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
// of this source tree.
2021-09-27 18:29:08 -07:00
//! Includes a series of tests for the group implementations
use crate::errors::InternalError;
use crate::group::Group;
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]
fn test_group_properties() -> Result<(), InternalError> {
2021-12-23 07:50:48 +01:00
#[cfg(feature = "ristretto255")]
{
2021-12-21 20:17:02 +01:00
use curve25519_dalek::ristretto::RistrettoPoint;
2021-09-27 18:29:08 -07:00
2021-12-21 20:17:02 +01:00
test_identity_element_error::<RistrettoPoint>()?;
test_zero_scalar_error::<RistrettoPoint>()?;
2021-12-23 07:50:48 +01:00
}
2021-09-27 18:29:08 -07:00
#[cfg(feature = "p256")]
{
2021-10-06 00:53:18 +02:00
use p256_::ProjectivePoint;
2021-09-27 18:29:08 -07:00
2021-10-06 00:53:18 +02:00
test_identity_element_error::<ProjectivePoint>()?;
test_zero_scalar_error::<ProjectivePoint>()?;
2021-09-27 18:29:08 -07:00
}
Ok(())
}
// Checks that the identity element cannot be deserialized
2021-10-06 00:53:18 +02:00
fn test_identity_element_error<G: Group>() -> Result<(), InternalError> {
let identity = G::identity();
let result = G::from_element_slice(&identity.to_arr());
2021-10-06 00:19:20 +02:00
assert!(matches!(result, Err(InternalError::PointError)));
2021-09-27 18:29:08 -07:00
Ok(())
}
// Checks that the zero scalar cannot be deserialized
2021-10-06 00:53:18 +02:00
fn test_zero_scalar_error<G: Group>() -> Result<(), InternalError> {
let zero_scalar = G::scalar_zero();
let result = G::from_scalar_slice(&G::scalar_as_bytes(zero_scalar));
2021-10-06 00:19:20 +02:00
assert!(matches!(result, Err(InternalError::ZeroScalarError)));
2021-09-27 18:29:08 -07:00
Ok(())
}