ec: Allow test vectors to be decoded as unencoded elements

This commit is contained in:
Vlad Krasnov 2024-01-04 12:26:57 -05:00 committed by Brian Smith
parent e8a5d1138c
commit 5644cb94d6

View File

@ -467,6 +467,22 @@ mod tests {
encoding: PhantomData,
};
trait Convert<E: Encoding> {
fn convert(self, cops: &CommonOps) -> Elem<E>;
}
impl Convert<R> for Elem<R> {
fn convert(self, _cops: &CommonOps) -> Elem<R> {
self
}
}
impl Convert<Unencoded> for Elem<R> {
fn convert(self, cops: &CommonOps) -> Elem<Unencoded> {
cops.elem_unencoded(&self)
}
}
fn q_minus_n_plus_n_equals_0_test(ops: &PublicScalarOps) {
let cops = ops.scalar_ops.common;
let mut x = ops.q_minus_n;
@ -802,7 +818,7 @@ mod tests {
let a = consume_jacobian_point(ops, test_case, "a");
let b = consume_jacobian_point(ops, test_case, "b");
let r_expected = consume_point(ops, test_case, "r");
let r_expected: TestPoint<R> = consume_point(ops, test_case, "r");
let r_actual = ops.common.point_sum(&a, &b);
assert_point_actual_equals_expected(ops, &r_actual, &r_expected);
@ -843,7 +859,7 @@ mod tests {
let a = consume_jacobian_point(ops, test_case, "a");
let b = consume_affine_point(ops, test_case, "b");
let r_expected = consume_point(ops, test_case, "r");
let r_expected: TestPoint<R> = consume_point(ops, test_case, "r");
let mut r_actual = Point::new_at_infinity();
unsafe {
@ -898,7 +914,7 @@ mod tests {
assert_eq!(section, "");
let a = consume_jacobian_point(ops, test_case, "a");
let r_expected = consume_point(ops, test_case, "r");
let r_expected: TestPoint<R> = consume_point(ops, test_case, "r");
let mut r_actual = Point::new_at_infinity();
unsafe {
@ -1006,18 +1022,20 @@ mod tests {
test::run(test_file, |section, test_case| {
assert_eq!(section, "");
let g_scalar = consume_scalar(ops.common, test_case, "g_scalar");
let expected_result = consume_point(ops, test_case, "r");
let expected_result: TestPoint<R> = consume_point(ops, test_case, "r");
let actual_result = f(&g_scalar);
assert_point_actual_equals_expected(ops, &actual_result, &expected_result);
Ok(())
})
}
fn assert_point_actual_equals_expected(
fn assert_point_actual_equals_expected<E: Encoding>(
ops: &PrivateKeyOps,
actual_point: &Point,
expected_point: &TestPoint,
) {
expected_point: &TestPoint<E>,
) where
Elem<R>: Convert<E>,
{
let cops = ops.common;
let actual_x = &cops.point_x(actual_point);
let actual_y = &cops.point_y(actual_point);
@ -1036,6 +1054,9 @@ mod tests {
cops.elem_product(actual_y, &zzz_inv)
};
let x_aff = x_aff.convert(cops);
let y_aff = y_aff.convert(cops);
assert_elems_are_equal(cops, &x_aff, expected_x);
assert_elems_are_equal(cops, &y_aff, expected_y);
}
@ -1086,13 +1107,17 @@ mod tests {
.copy_from_slice(&r.limbs[..ops.num_limbs]);
}
enum TestPoint {
enum TestPoint<E: Encoding> {
Infinity,
Affine(Elem<R>, Elem<R>),
Affine(Elem<E>, Elem<E>),
}
fn consume_point(ops: &PrivateKeyOps, test_case: &mut test::TestCase, name: &str) -> TestPoint {
fn consume_point_elem(ops: &CommonOps, elems: &[&str], i: usize) -> Elem<R> {
fn consume_point<E: Encoding>(
ops: &PrivateKeyOps,
test_case: &mut test::TestCase,
name: &str,
) -> TestPoint<E> {
fn consume_point_elem<E: Encoding>(ops: &CommonOps, elems: &[&str], i: usize) -> Elem<E> {
let bytes = test::from_hex(elems[i]).unwrap();
let bytes = untrusted::Input::from(&bytes);
let unencoded: Elem<Unencoded> =
@ -1116,7 +1141,7 @@ mod tests {
TestPoint::Affine(x, y)
}
fn assert_elems_are_equal(ops: &CommonOps, a: &Elem<R>, b: &Elem<R>) {
fn assert_elems_are_equal<E: Encoding>(ops: &CommonOps, a: &Elem<E>, b: &Elem<E>) {
assert_limbs_are_equal(ops, &a.limbs, &b.limbs)
}