Skip to content

Commit

Permalink
ec: additional P521 tests from generate_tests.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
vkrasnov committed Jan 25, 2024
1 parent 7f35cec commit 932beef
Show file tree
Hide file tree
Showing 8 changed files with 2,461 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/ec/suite_b/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ mod tests {
q_minus_n_plus_n_equals_0_test(&p384::PUBLIC_SCALAR_OPS);
}

#[test]
fn p521_q_minus_n_plus_n_equals_0_test() {
q_minus_n_plus_n_equals_0_test(&p521::PUBLIC_SCALAR_OPS);
}

#[test]
fn p256_elem_add_test() {
elem_add_test(
Expand All @@ -521,6 +526,15 @@ mod tests {
);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn p521_elem_add_test() {
elem_add_test(
&p521::PUBLIC_SCALAR_OPS,
test_file!("ops/p521_elem_sum_tests.txt"),
);
}

fn elem_add_test(ops: &PublicScalarOps, test_file: test::File) {
test::run(test_file, |section, test_case| {
assert_eq!(section, "");
Expand Down Expand Up @@ -557,6 +571,19 @@ mod tests {
);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn p521_elem_sub_test() {
prefixed_extern! {
fn p521_elem_sub(r: *mut Limb, a: *const Limb, b: *const Limb);
}
elem_sub_test(
&p521::COMMON_OPS,
p521_elem_sub,
test_file!("ops/p521_elem_sum_tests.txt"),
);
}

fn elem_sub_test(
ops: &CommonOps,
elem_sub: unsafe extern "C" fn(r: *mut Limb, a: *const Limb, b: *const Limb),
Expand Down Expand Up @@ -608,6 +635,19 @@ mod tests {
);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn p521_elem_div_by_2_test() {
prefixed_extern! {
fn p521_elem_div_by_2(r: *mut Limb, a: *const Limb);
}
elem_div_by_2_test(
&p521::COMMON_OPS,
p521_elem_div_by_2,
test_file!("ops/p521_elem_div_by_2_tests.txt"),
);
}

fn elem_div_by_2_test(
ops: &CommonOps,
elem_div_by_2: unsafe extern "C" fn(r: *mut Limb, a: *const Limb),
Expand Down Expand Up @@ -655,6 +695,19 @@ mod tests {
);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn p521_elem_neg_test() {
prefixed_extern! {
fn p521_elem_neg(r: *mut Limb, a: *const Limb);
}
elem_neg_test(
&p521::COMMON_OPS,
p521_elem_neg,
test_file!("ops/p521_elem_neg_tests.txt"),
);
}

fn elem_neg_test(
ops: &CommonOps,
elem_neg: unsafe extern "C" fn(r: *mut Limb, a: *const Limb),
Expand Down Expand Up @@ -698,6 +751,12 @@ mod tests {
elem_mul_test(&p384::COMMON_OPS, test_file!("ops/p384_elem_mul_tests.txt"));
}

#[cfg(target_pointer_width = "64")]
#[test]
fn p521_elem_mul_test() {
elem_mul_test(&p521::COMMON_OPS, test_file!("ops/p521_elem_mul_tests.txt"));
}

fn elem_mul_test(ops: &CommonOps, test_file: test::File) {
test::run(test_file, |section, test_case| {
assert_eq!(section, "");
Expand Down Expand Up @@ -728,6 +787,15 @@ mod tests {
);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn p521_scalar_mul_test() {
scalar_mul_test(
&p521::SCALAR_OPS,
test_file!("ops/p521_scalar_mul_tests.txt"),
);
}

fn scalar_mul_test(ops: &ScalarOps, test_file: test::File) {
test::run(test_file, |section, test_case| {
assert_eq!(section, "");
Expand Down Expand Up @@ -801,6 +869,12 @@ mod tests {
let _ = p384::PRIVATE_SCALAR_OPS.scalar_inv_to_mont(&ZERO_SCALAR);
}

#[test]
#[should_panic(expected = "!self.scalar_ops.common.is_zero(a)")]
fn p521_scalar_inv_to_mont_zero_panic_test() {
let _ = p521::PRIVATE_SCALAR_OPS.scalar_inv_to_mont(&ZERO_SCALAR);
}

#[test]
fn p256_point_sum_test() {
point_sum_test(
Expand All @@ -817,6 +891,15 @@ mod tests {
);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn p521_point_sum_test() {
point_sum_test(
&p521::PRIVATE_KEY_OPS,
test_file!("ops/p521_point_sum_tests.txt"),
);
}

fn point_sum_test(ops: &PrivateKeyOps, test_file: test::File) {
test::run(test_file, |section, test_case| {
assert_eq!(section, "");
Expand Down Expand Up @@ -907,6 +990,22 @@ mod tests {
);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn p521_point_double_test() {
prefixed_extern! {
fn p521_point_double(
r: *mut Limb,
a: *const Limb,
);
}
point_double_test(
&p521::PRIVATE_KEY_OPS,
p521_point_double,
test_file!("ops/p521_point_double_tests.txt"),
);
}

fn point_double_test(
ops: &PrivateKeyOps,
point_double: unsafe extern "C" fn(
Expand Down
27 changes: 27 additions & 0 deletions src/ec/suite_b/ops/p521_elem_div_by_2_tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

a = 00
r = 00

a = 00
r = 00

a = 01
r = 010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

a = 02
r = 01

a = 03
r = 010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001

a = 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
r = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

a = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
r = 017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

a = 010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
r = 8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

a = 010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
r = 8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
32 changes: 32 additions & 0 deletions src/ec/suite_b/ops/p521_elem_mul_tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

a = 00
b = 00
r = 00

a = 00
b = 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
r = 00

a = 01
b = 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
r = 01fffffffffffffbffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

a = 02
b = 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
r = 01fffffffffffff7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

a = 03
b = 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
r = 01fffffffffffff3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

a = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
b = 02
r = 01fffffffffffffbffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

a = 010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
b = 02
r = 0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

a = 010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
b = 02
r = 0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
24 changes: 24 additions & 0 deletions src/ec/suite_b/ops/p521_elem_neg_tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

a = 00
b = 00

a = 01
b = 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe

a = 02
b = 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd

a = 03
b = 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc

a = 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
b = 01

a = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
b = 010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

a = 010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
b = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

a = 010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
b = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Loading

0 comments on commit 932beef

Please sign in to comment.