xref: /aosp_15_r20/external/private-join-and-compute/private_join_and_compute/server_impl.h (revision a6aa18fbfbf9cb5cd47356a9d1b057768998488c)
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 #ifndef PRIVATE_JOIN_AND_COMPUTE_PRIVATE_INTERSECTION_SUM_SERVER_IMPL_H_
17 #define PRIVATE_JOIN_AND_COMPUTE_PRIVATE_INTERSECTION_SUM_SERVER_IMPL_H_
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "private_join_and_compute/crypto/context.h"
25 #include "private_join_and_compute/crypto/ec_commutative_cipher.h"
26 #include "private_join_and_compute/crypto/paillier.h"
27 #include "private_join_and_compute/match.pb.h"
28 #include "private_join_and_compute/message_sink.h"
29 #include "private_join_and_compute/private_intersection_sum.pb.h"
30 #include "private_join_and_compute/private_join_and_compute.pb.h"
31 #include "private_join_and_compute/protocol_server.h"
32 #include "private_join_and_compute/util/status.inc"
33 
34 namespace private_join_and_compute {
35 
36 // The "server side" of the intersection-sum protocol.  This represents the
37 // party that will receive the size of the intersection as its output.  The
38 // values that will be summed are supplied by the other party; this party will
39 // only supply set elements as its inputs.
40 class PrivateIntersectionSumProtocolServerImpl : public ProtocolServer {
41  public:
PrivateIntersectionSumProtocolServerImpl(::private_join_and_compute::Context * ctx,std::vector<std::string> inputs)42   PrivateIntersectionSumProtocolServerImpl(
43       ::private_join_and_compute::Context* ctx, std::vector<std::string> inputs)
44       : ctx_(ctx), inputs_(std::move(inputs)) {}
45 
46   ~PrivateIntersectionSumProtocolServerImpl() override = default;
47 
48   // Executes the next Server round and creates a response.
49   //
50   // If the ClientMessage is StartProtocol, a ServerRoundOne will be sent to the
51   // message sink, containing the encrypted server identifiers.
52   //
53   // If the ClientMessage is ClientRoundOne, a ServerRoundTwo will be sent to
54   // the message sink, containing the intersection size, and encrypted
55   // intersection-sum.
56   //
57   // Fails with InvalidArgument if the message is not a
58   // PrivateIntersectionSumClientMessage of the expected round, or if the
59   // message is otherwise not as expected. Forwards all other failures
60   // encountered.
61   Status Handle(const ClientMessage& request,
62                 MessageSink<ServerMessage>* server_message_sink) override;
63 
protocol_finished()64   bool protocol_finished() override { return protocol_finished_; }
65 
66   // Utility function, used for testing.
GetECCipher()67   ECCommutativeCipher* GetECCipher() { return ec_cipher_.get(); }
68 
69  private:
70   // Encrypts the server's identifiers.
71   StatusOr<PrivateIntersectionSumServerMessage::ServerRoundOne> EncryptSet();
72 
73   // Computes the intersection size and encrypted intersection_sum.
74   StatusOr<PrivateIntersectionSumServerMessage::ServerRoundTwo>
75   ComputeIntersection(const PrivateIntersectionSumClientMessage::ClientRoundOne&
76                           client_message);
77 
78   Context* ctx_;  // not owned
79   std::unique_ptr<ECCommutativeCipher> ec_cipher_;
80 
81   // inputs_ will first contain the plaintext server identifiers, and later
82   // contain the encrypted server identifiers.
83   std::vector<std::string> inputs_;
84   bool protocol_finished_ = false;
85 };
86 
87 }  // namespace private_join_and_compute
88 
89 #endif  // PRIVATE_JOIN_AND_COMPUTE_PRIVATE_INTERSECTION_SUM_SERVER_IMPL_H_
90