1 /*
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "fcp/secagg/client/secagg_client_r2_masked_input_coll_waiting_for_input_state.h"
18
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <utility>
23
24 #include "absl/container/node_hash_map.h"
25 #include "fcp/base/monitoring.h"
26 #include "fcp/secagg/client/other_client_state.h"
27 #include "fcp/secagg/client/secagg_client_aborted_state.h"
28 #include "fcp/secagg/client/secagg_client_completed_state.h"
29 #include "fcp/secagg/client/secagg_client_r2_masked_input_coll_base_state.h"
30 #include "fcp/secagg/client/secagg_client_r3_unmasking_state.h"
31 #include "fcp/secagg/client/secagg_client_state.h"
32 #include "fcp/secagg/client/send_to_server_interface.h"
33 #include "fcp/secagg/client/state_transition_listener_interface.h"
34 #include "fcp/secagg/shared/input_vector_specification.h"
35 #include "fcp/secagg/shared/secagg_messages.pb.h"
36 #include "fcp/secagg/shared/secagg_vector.h"
37 #include "fcp/secagg/shared/shamir_secret_sharing.h"
38
39 namespace fcp {
40 namespace secagg {
41
42 SecAggClientR2MaskedInputCollWaitingForInputState::
SecAggClientR2MaskedInputCollWaitingForInputState(uint32_t client_id,uint32_t minimum_surviving_neighbors_for_reconstruction,uint32_t number_of_alive_neighbors,uint32_t number_of_neighbors,std::unique_ptr<std::vector<InputVectorSpecification>> input_vector_specs,std::unique_ptr<SecAggVectorMap> map_of_masks,std::unique_ptr<std::vector<OtherClientState>> other_client_states,std::unique_ptr<std::vector<ShamirShare>> pairwise_key_shares,std::unique_ptr<std::vector<ShamirShare>> self_key_shares,std::unique_ptr<SendToServerInterface> sender,std::unique_ptr<StateTransitionListenerInterface> transition_listener,AsyncAbort * async_abort)43 SecAggClientR2MaskedInputCollWaitingForInputState(
44 uint32_t client_id,
45 uint32_t minimum_surviving_neighbors_for_reconstruction,
46 uint32_t number_of_alive_neighbors, uint32_t number_of_neighbors,
47 std::unique_ptr<std::vector<InputVectorSpecification> >
48 input_vector_specs,
49 std::unique_ptr<SecAggVectorMap> map_of_masks,
50 std::unique_ptr<std::vector<OtherClientState> > other_client_states,
51 std::unique_ptr<std::vector<ShamirShare> > pairwise_key_shares,
52 std::unique_ptr<std::vector<ShamirShare> > self_key_shares,
53 std::unique_ptr<SendToServerInterface> sender,
54 std::unique_ptr<StateTransitionListenerInterface> transition_listener,
55
56 AsyncAbort* async_abort)
57 : SecAggClientR2MaskedInputCollBaseState(
58 std::move(sender), std::move(transition_listener), async_abort),
59 client_id_(client_id),
60 minimum_surviving_neighbors_for_reconstruction_(
61 minimum_surviving_neighbors_for_reconstruction),
62 number_of_alive_neighbors_(number_of_alive_neighbors),
63 number_of_neighbors_(number_of_neighbors),
64 input_vector_specs_(std::move(input_vector_specs)),
65 map_of_masks_(std::move(map_of_masks)),
66 other_client_states_(std::move(other_client_states)),
67 pairwise_key_shares_(std::move(pairwise_key_shares)),
68 self_key_shares_(std::move(self_key_shares)) {
69 FCP_CHECK(client_id_ >= 0)
70 << "Client id must not be negative but was " << client_id_;
71 }
72
73 SecAggClientR2MaskedInputCollWaitingForInputState::
74 ~SecAggClientR2MaskedInputCollWaitingForInputState() = default;
75
76 StatusOr<std::unique_ptr<SecAggClientState> >
HandleMessage(const ServerToClientWrapperMessage & message)77 SecAggClientR2MaskedInputCollWaitingForInputState::HandleMessage(
78 const ServerToClientWrapperMessage& message) {
79 // Handle abort messages only.
80 if (message.has_abort()) {
81 if (message.abort().early_success()) {
82 return {std::make_unique<SecAggClientCompletedState>(
83 std::move(sender_), std::move(transition_listener_))};
84 } else {
85 return {std::make_unique<SecAggClientAbortedState>(
86 "Aborting because of abort message from the server.",
87 std::move(sender_), std::move(transition_listener_))};
88 }
89 } else {
90 // Returns an error indicating that the message is of invalid type.
91 return SecAggClientState::HandleMessage(message);
92 }
93 }
94
95 StatusOr<std::unique_ptr<SecAggClientState> >
SetInput(std::unique_ptr<SecAggVectorMap> input_map)96 SecAggClientR2MaskedInputCollWaitingForInputState::SetInput(
97 std::unique_ptr<SecAggVectorMap> input_map) {
98 // Only need to do 3 things: Validate input, send message to server, and
99 // return the new state.
100 if (!ValidateInput(*input_map, *input_vector_specs_)) {
101 return FCP_STATUS(INVALID_ARGUMENT)
102 << "The input to SetInput does not match the "
103 "InputVectorSpecification.";
104 }
105
106 SendMaskedInput(std::move(input_map), std::move(map_of_masks_));
107
108 return {std::make_unique<SecAggClientR3UnmaskingState>(
109 client_id_, number_of_alive_neighbors_,
110 minimum_surviving_neighbors_for_reconstruction_, number_of_neighbors_,
111 std::move(other_client_states_), std::move(pairwise_key_shares_),
112 std::move(self_key_shares_), std::move(sender_),
113 std::move(transition_listener_), async_abort_)};
114 }
115
StateName() const116 std::string SecAggClientR2MaskedInputCollWaitingForInputState::StateName()
117 const {
118 return "R2_MASKED_INPUT_COLL_WAITING_FOR_INPUT";
119 }
120
121 } // namespace secagg
122 } // namespace fcp
123