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
19 #include <string>
20 #include <utility>
21 #include <vector>
22
23 #include "private_join_and_compute/crypto/context.h"
24 #include "private_join_and_compute/crypto/ec_group.h"
25 #include "private_join_and_compute/crypto/ec_point.h"
26 #include "private_join_and_compute/crypto/proto/ec_point.pb.h"
27 #include "private_join_and_compute/util/status.inc"
28
29 namespace private_join_and_compute {
30
BigNumVectorToProto(absl::Span<const BigNum> big_num_vector)31 proto::BigNumVector BigNumVectorToProto(
32 absl::Span<const BigNum> big_num_vector) {
33 proto::BigNumVector big_num_vector_proto;
34 big_num_vector_proto.mutable_serialized_big_nums()->Reserve(
35 big_num_vector.size());
36 for (const auto& bn : big_num_vector) {
37 big_num_vector_proto.add_serialized_big_nums(bn.ToBytes());
38 }
39 return big_num_vector_proto;
40 }
41
ParseBigNumVectorProto(Context * context,const proto::BigNumVector & big_num_vector_proto)42 std::vector<BigNum> ParseBigNumVectorProto(
43 Context* context, const proto::BigNumVector& big_num_vector_proto) {
44 std::vector<BigNum> big_num_vector;
45 for (const auto& serialized_big_num :
46 big_num_vector_proto.serialized_big_nums()) {
47 big_num_vector.push_back(context->CreateBigNum(serialized_big_num));
48 }
49 return big_num_vector;
50 }
51
52 // Converts a std::vector<BigNum> into a protocol buffer BigNumVector.
ECPointVectorToProto(absl::Span<const ECPoint> ec_point_vector)53 StatusOr<proto::ECPointVector> ECPointVectorToProto(
54 absl::Span<const ECPoint> ec_point_vector) {
55 proto::ECPointVector ec_point_vector_proto;
56 ec_point_vector_proto.mutable_serialized_ec_points()->Reserve(
57 ec_point_vector.size());
58 for (const auto& ec_point : ec_point_vector) {
59 ASSIGN_OR_RETURN(std::string serialized_ec_point,
60 ec_point.ToBytesCompressed());
61 ec_point_vector_proto.add_serialized_ec_points(serialized_ec_point);
62 }
63 return std::move(ec_point_vector_proto);
64 }
65
66 // Converts a protocol buffer BigNumVector into a std::vector<BigNum>.
ParseECPointVectorProto(Context * context,ECGroup * ec_group,const proto::ECPointVector & ec_point_vector_proto)67 StatusOr<std::vector<ECPoint>> ParseECPointVectorProto(
68 Context* context, ECGroup* ec_group,
69 const proto::ECPointVector& ec_point_vector_proto) {
70 std::vector<ECPoint> ec_point_vector;
71 for (const auto& serialized_ec_point :
72 ec_point_vector_proto.serialized_ec_points()) {
73 ASSIGN_OR_RETURN(ECPoint ec_point,
74 ec_group->CreateECPoint(serialized_ec_point));
75 ec_point_vector.push_back(std::move(ec_point));
76 }
77 return std::move(ec_point_vector);
78 }
79
SerializeAsStringInOrder(const google::protobuf::MessageLite & proto)80 std::string SerializeAsStringInOrder(const google::protobuf::MessageLite& proto) {
81 return proto.SerializeAsString();
82 }
83
84 } // namespace private_join_and_compute
85