Skip to content

Commit e86df2e

Browse files
schoppmpcopybara-github
authored andcommitted
Add tests to ensure this remains unchanged when {Add,Sub,Mul,FusedMulAdd}InPlace fail
This invariant holds because a valid RnsPolynomial will always have coefficient vectors of the same length, and batch operations only fail on length mismatch. PiperOrigin-RevId: 850507664
1 parent 5a73855 commit e86df2e

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

shell_encryption/rns/rns_polynomial_test.cc

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,5 +2610,87 @@ TYPED_TEST(RnsPolynomialTest, SerializedDeserializes) {
26102610
EXPECT_EQ(deserialized2, a);
26112611
}
26122612

2613+
TYPED_TEST(RnsPolynomialTest, AddInPlaceLeavesPolynomialUnchangedOnFailure) {
2614+
ASSERT_OK_AND_ASSIGN(RnsPolynomial<TypeParam> a, this->SampleRnsPolynomial());
2615+
ASSERT_OK_AND_ASSIGN(RnsPolynomial<TypeParam> b, this->SampleRnsPolynomial());
2616+
2617+
// Double the length of the coefficient vectors of b.
2618+
std::vector<std::vector<TypeParam>> b_coeffs = b.Coeffs();
2619+
for (int i = 0; i < b_coeffs.size(); ++i) {
2620+
b_coeffs[i].insert(b_coeffs[i].end(), b.Coeffs()[i].begin(),
2621+
b.Coeffs()[i].end());
2622+
}
2623+
ASSERT_OK_AND_ASSIGN(
2624+
b, RnsPolynomial<TypeParam>::Create(std::move(b_coeffs), b.IsNttForm()));
2625+
2626+
auto a2 = a.Clone();
2627+
EXPECT_THAT(
2628+
a.AddInPlace(b, this->moduli_),
2629+
StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("same size")));
2630+
EXPECT_EQ(a, a2);
2631+
}
2632+
2633+
TYPED_TEST(RnsPolynomialTest, SubInPlaceLeavesPolynomialUnchangedOnFailure) {
2634+
ASSERT_OK_AND_ASSIGN(RnsPolynomial<TypeParam> a, this->SampleRnsPolynomial());
2635+
ASSERT_OK_AND_ASSIGN(RnsPolynomial<TypeParam> b, this->SampleRnsPolynomial());
2636+
2637+
// Double the length of the coefficient vectors of b.
2638+
std::vector<std::vector<TypeParam>> b_coeffs = b.Coeffs();
2639+
for (int i = 0; i < b_coeffs.size(); ++i) {
2640+
b_coeffs[i].insert(b_coeffs[i].end(), b.Coeffs()[i].begin(),
2641+
b.Coeffs()[i].end());
2642+
}
2643+
ASSERT_OK_AND_ASSIGN(
2644+
b, RnsPolynomial<TypeParam>::Create(std::move(b_coeffs), b.IsNttForm()));
2645+
2646+
auto a2 = a.Clone();
2647+
EXPECT_THAT(
2648+
a.SubInPlace(b, this->moduli_),
2649+
StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("same size")));
2650+
EXPECT_EQ(a, a2);
2651+
}
2652+
2653+
TYPED_TEST(RnsPolynomialTest, MulInPlaceLeavesPolynomialUnchangedOnFailure) {
2654+
ASSERT_OK_AND_ASSIGN(RnsPolynomial<TypeParam> a, this->SampleRnsPolynomial());
2655+
ASSERT_OK_AND_ASSIGN(RnsPolynomial<TypeParam> b, this->SampleRnsPolynomial());
2656+
2657+
// Double the length of the coefficient vectors of b.
2658+
std::vector<std::vector<TypeParam>> b_coeffs = b.Coeffs();
2659+
for (int i = 0; i < b_coeffs.size(); ++i) {
2660+
b_coeffs[i].insert(b_coeffs[i].end(), b.Coeffs()[i].begin(),
2661+
b.Coeffs()[i].end());
2662+
}
2663+
ASSERT_OK_AND_ASSIGN(
2664+
b, RnsPolynomial<TypeParam>::Create(std::move(b_coeffs), b.IsNttForm()));
2665+
2666+
auto a2 = a.Clone();
2667+
EXPECT_THAT(
2668+
a.MulInPlace(b, this->moduli_),
2669+
StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("same size")));
2670+
EXPECT_EQ(a, a2);
2671+
}
2672+
2673+
TYPED_TEST(RnsPolynomialTest,
2674+
FusedMulAddInPlaceLeavesPolynomialUnchangedOnFailure) {
2675+
ASSERT_OK_AND_ASSIGN(RnsPolynomial<TypeParam> a, this->SampleRnsPolynomial());
2676+
ASSERT_OK_AND_ASSIGN(RnsPolynomial<TypeParam> b, this->SampleRnsPolynomial());
2677+
ASSERT_OK_AND_ASSIGN(RnsPolynomial<TypeParam> c, this->SampleRnsPolynomial());
2678+
2679+
// Double the length of the coefficient vectors of b.
2680+
std::vector<std::vector<TypeParam>> b_coeffs = b.Coeffs();
2681+
for (int i = 0; i < b_coeffs.size(); ++i) {
2682+
b_coeffs[i].insert(b_coeffs[i].end(), b.Coeffs()[i].begin(),
2683+
b.Coeffs()[i].end());
2684+
}
2685+
ASSERT_OK_AND_ASSIGN(
2686+
b, RnsPolynomial<TypeParam>::Create(std::move(b_coeffs), b.IsNttForm()));
2687+
2688+
auto a2 = a.Clone();
2689+
EXPECT_THAT(
2690+
a.FusedMulAddInPlace(b, c, this->moduli_),
2691+
StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("same size")));
2692+
EXPECT_EQ(a, a2);
2693+
}
2694+
26132695
} // namespace
26142696
} // namespace rlwe

0 commit comments

Comments
 (0)