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/crypto/proto/proto_util.h"
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include <memory>
22
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 #include "private_join_and_compute/crypto/context.h"
28 #include "private_join_and_compute/crypto/ec_group.h"
29 #include "private_join_and_compute/crypto/ec_point.h"
30 #include "private_join_and_compute/crypto/openssl.inc"
31
32 #include "private_join_and_compute/crypto/pedersen_over_zn.h"
33 #include "private_join_and_compute/crypto/proto/big_num.pb.h"
34 #include "private_join_and_compute/crypto/proto/ec_point.pb.h"
35 #include "private_join_and_compute/crypto/proto/pedersen.pb.h"
36
37 #include "private_join_and_compute/util/status.inc"
38 #include "private_join_and_compute/util/status_testing.inc"
39
40 namespace private_join_and_compute {
41 namespace {
42
43 const int kTestCurveId = NID_secp384r1;
44
TEST(ProtoUtilTest,ToBigNumVectorAndBack)45 TEST(ProtoUtilTest, ToBigNumVectorAndBack) {
46 Context ctx;
47 std::vector<BigNum> big_num_vector = {ctx.One(), ctx.Two(), ctx.Three()};
48
49 proto::BigNumVector big_num_vector_proto =
50 BigNumVectorToProto(big_num_vector);
51 std::vector<BigNum> deserialized =
52 ParseBigNumVectorProto(&ctx, big_num_vector_proto);
53
54 EXPECT_EQ(big_num_vector, deserialized);
55 }
56
TEST(ProtoUtilTest,ParseEmptyBigNumVector)57 TEST(ProtoUtilTest, ParseEmptyBigNumVector) {
58 Context ctx;
59 std::vector<BigNum> empty_big_num_vector = {};
60 proto::BigNumVector big_num_vector_proto; // Default instance.
61 std::vector<BigNum> deserialized =
62 ParseBigNumVectorProto(&ctx, big_num_vector_proto);
63
64 EXPECT_EQ(empty_big_num_vector, deserialized);
65 }
66
TEST(ProtoUtilTest,ToECPointVectorAndBack)67 TEST(ProtoUtilTest, ToECPointVectorAndBack) {
68 Context ctx;
69 ASSERT_OK_AND_ASSIGN(ECGroup ec_group, ECGroup::Create(kTestCurveId, &ctx));
70 std::vector<ECPoint> ec_point_vector;
71 ec_point_vector.reserve(3);
72 for (int i = 0; i < 3; ++i) {
73 ASSERT_OK_AND_ASSIGN(ECPoint point, ec_group.GetPointByHashingToCurveSha256(
74 absl::StrCat("point_", i)));
75 ec_point_vector.emplace_back(std::move(point));
76 }
77
78 ASSERT_OK_AND_ASSIGN(proto::ECPointVector ec_point_vector_proto,
79 ECPointVectorToProto(ec_point_vector));
80 ASSERT_OK_AND_ASSIGN(
81 std::vector<ECPoint> deserialized,
82 ParseECPointVectorProto(&ctx, &ec_group, ec_point_vector_proto));
83
84 EXPECT_EQ(ec_point_vector, deserialized);
85 }
86
TEST(ProtoUtilTest,ParseEmptyECPointVector)87 TEST(ProtoUtilTest, ParseEmptyECPointVector) {
88 Context ctx;
89 ASSERT_OK_AND_ASSIGN(ECGroup ec_group, ECGroup::Create(kTestCurveId, &ctx));
90 std::vector<ECPoint> empty_ec_point_vector = {};
91 proto::ECPointVector ec_point_vector_proto; // Default instance.
92 ASSERT_OK_AND_ASSIGN(
93 std::vector<ECPoint> deserialized,
94 ParseECPointVectorProto(&ctx, &ec_group, ec_point_vector_proto));
95
96 EXPECT_EQ(empty_ec_point_vector, deserialized);
97 }
98
TEST(ProtoUtilTest,SerializeAsStringInOrderIsConsistent)99 TEST(ProtoUtilTest, SerializeAsStringInOrderIsConsistent) {
100 Context ctx;
101 std::vector<BigNum> big_num_vector = {ctx.One(), ctx.Two(), ctx.Three()};
102
103 proto::PedersenParameters pedersen_parameters_proto;
104 pedersen_parameters_proto.set_n(ctx.CreateBigNum(37).ToBytes());
105 *pedersen_parameters_proto.mutable_gs() = BigNumVectorToProto(big_num_vector);
106 pedersen_parameters_proto.set_h(ctx.CreateBigNum(4).ToBytes());
107
108 const std::string kExpectedSerialized =
109 "\n\x1%\x12\t\n\x1\x1\n\x1\x2\n\x1\x3\x1A\x1\x4";
110 std::string serialized = SerializeAsStringInOrder(pedersen_parameters_proto);
111
112 EXPECT_EQ(serialized, kExpectedSerialized);
113 }
114
115 } // namespace
116 } // namespace private_join_and_compute
117