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 <memory>
19 #include <utility>
20
21 namespace private_join_and_compute::elgamal_proto_util {
22
SerializePublicKey(const elgamal::PublicKey & public_key_struct)23 StatusOr<ElGamalPublicKey> SerializePublicKey(
24 const elgamal::PublicKey& public_key_struct) {
25 ElGamalPublicKey public_key_proto;
26 ASSIGN_OR_RETURN(auto serialized_g, public_key_struct.g.ToBytesCompressed());
27 public_key_proto.set_g(serialized_g);
28 ASSIGN_OR_RETURN(auto serialized_y, public_key_struct.y.ToBytesCompressed());
29 public_key_proto.set_y(serialized_y);
30 return public_key_proto;
31 }
32
SerializeCiphertext(const elgamal::Ciphertext & ciphertext_struct)33 StatusOr<ElGamalCiphertext> SerializeCiphertext(
34 const elgamal::Ciphertext& ciphertext_struct) {
35 ElGamalCiphertext ciphertext_proto;
36 ASSIGN_OR_RETURN(auto serialized_u, ciphertext_struct.u.ToBytesCompressed());
37 ciphertext_proto.set_u(serialized_u);
38 ASSIGN_OR_RETURN(auto serialized_e, ciphertext_struct.e.ToBytesCompressed());
39 ciphertext_proto.set_e(serialized_e);
40 return ciphertext_proto;
41 }
42
SerializePrivateKey(const elgamal::PrivateKey & private_key_struct)43 StatusOr<ElGamalSecretKey> SerializePrivateKey(
44 const elgamal::PrivateKey& private_key_struct) {
45 ElGamalSecretKey private_key_proto;
46 private_key_proto.set_x(private_key_struct.x.ToBytes());
47 return private_key_proto;
48 }
49
DeserializePublicKey(const ECGroup * ec_group,const ElGamalPublicKey & public_key_proto)50 StatusOr<std::unique_ptr<elgamal::PublicKey>> DeserializePublicKey(
51 const ECGroup* ec_group, const ElGamalPublicKey& public_key_proto) {
52 ASSIGN_OR_RETURN(ECPoint public_key_struct_g,
53 ec_group->CreateECPoint(public_key_proto.g()));
54 ASSIGN_OR_RETURN(ECPoint public_key_struct_y,
55 ec_group->CreateECPoint(public_key_proto.y()));
56 return absl::WrapUnique(new elgamal::PublicKey(
57 {std::move(public_key_struct_g), std::move(public_key_struct_y)}));
58 }
59
DeserializePrivateKey(Context * context,const ElGamalSecretKey & private_key_proto)60 StatusOr<std::unique_ptr<elgamal::PrivateKey>> DeserializePrivateKey(
61 Context* context, const ElGamalSecretKey& private_key_proto) {
62 BigNum x = context->CreateBigNum(private_key_proto.x());
63 return absl::WrapUnique(new elgamal::PrivateKey({std::move(x)}));
64 }
65
DeserializeCiphertext(const ECGroup * ec_group,const ElGamalCiphertext & ciphertext_proto)66 StatusOr<elgamal::Ciphertext> DeserializeCiphertext(
67 const ECGroup* ec_group, const ElGamalCiphertext& ciphertext_proto) {
68 ASSIGN_OR_RETURN(ECPoint ciphertext_struct_u,
69 ec_group->CreateECPoint(ciphertext_proto.u()));
70 ASSIGN_OR_RETURN(ECPoint ciphertext_struct_e,
71 ec_group->CreateECPoint(ciphertext_proto.e()));
72 return elgamal::Ciphertext{std::move(ciphertext_struct_u),
73 std::move(ciphertext_struct_e)};
74 }
75
76 } // namespace private_join_and_compute::elgamal_proto_util
77