1 /*
2  * Copyright 2019 Google LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     https://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "private_join_and_compute/util/elgamal_proto_util.h"
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 
21 #include <utility>
22 
23 #include "private_join_and_compute/util/status_testing.inc"
24 
25 namespace private_join_and_compute::elgamal_proto_util {
26 namespace {
27 
28 using ::testing::Test;
29 
30 const int kTestCurveId = NID_X9_62_prime256v1;
31 
TEST(ElGamalProtoUtilTest,PublicKeyConversion)32 TEST(ElGamalProtoUtilTest, PublicKeyConversion) {
33   Context context;
34   ASSERT_OK_AND_ASSIGN(auto ec_group, ECGroup::Create(kTestCurveId, &context));
35   ASSERT_OK_AND_ASSIGN(auto key_pair, elgamal::GenerateKeyPair(ec_group));
36   auto public_key_struct = std::move(key_pair.first);
37   ASSERT_OK_AND_ASSIGN(
38       auto public_key_proto,
39       elgamal_proto_util::SerializePublicKey(*public_key_struct));
40   ASSERT_OK_AND_ASSIGN(
41       auto public_key_struct_2,
42       elgamal_proto_util::DeserializePublicKey(&ec_group, public_key_proto));
43   EXPECT_EQ(public_key_struct->g, public_key_struct_2->g);
44   EXPECT_EQ(public_key_struct->y, public_key_struct_2->y);
45 }
46 
TEST(ElGamalProtoUtilTest,PrivateKeyConversion)47 TEST(ElGamalProtoUtilTest, PrivateKeyConversion) {
48   Context context;
49   ASSERT_OK_AND_ASSIGN(auto ec_group, ECGroup::Create(kTestCurveId, &context));
50   ASSERT_OK_AND_ASSIGN(auto key_pair, elgamal::GenerateKeyPair(ec_group));
51   auto private_key_struct = std::move(key_pair.second);
52   ASSERT_OK_AND_ASSIGN(
53       auto private_key_proto,
54       elgamal_proto_util::SerializePrivateKey(*private_key_struct));
55   ASSERT_OK_AND_ASSIGN(
56       auto private_key_struct_2,
57       elgamal_proto_util::DeserializePrivateKey(&context, private_key_proto));
58   EXPECT_EQ(private_key_struct->x, private_key_struct_2->x);
59 }
60 
TEST(ElGamalProtoUtilTest,CiphertextConversion)61 TEST(ElGamalProtoUtilTest, CiphertextConversion) {
62   Context context;
63   ASSERT_OK_AND_ASSIGN(auto ec_group, ECGroup::Create(kTestCurveId, &context));
64   ASSERT_OK_AND_ASSIGN(ECPoint u, ec_group.GetRandomGenerator());
65   ASSERT_OK_AND_ASSIGN(ECPoint e, ec_group.GetRandomGenerator());
66   elgamal::Ciphertext ciphertext_struct{std::move(u), std::move(e)};
67   ASSERT_OK_AND_ASSIGN(
68       auto ciphertext_proto,
69       elgamal_proto_util::SerializeCiphertext(ciphertext_struct));
70   ASSERT_OK_AND_ASSIGN(
71       auto ciphertext_struct_2,
72       elgamal_proto_util::DeserializeCiphertext(&ec_group, ciphertext_proto));
73   EXPECT_EQ(ciphertext_struct.u, ciphertext_struct_2.u);
74   EXPECT_EQ(ciphertext_struct.e, ciphertext_struct_2.e);
75 }
76 
77 }  // namespace
78 }  // namespace private_join_and_compute::elgamal_proto_util
79