xref: /aosp_15_r20/external/federated-compute/fcp/secagg/server/secagg_server.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1*14675a02SAndroid Build Coastguard Worker /*
2*14675a02SAndroid Build Coastguard Worker  * Copyright 2019 Google LLC
3*14675a02SAndroid Build Coastguard Worker  *
4*14675a02SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*14675a02SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*14675a02SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*14675a02SAndroid Build Coastguard Worker  *
8*14675a02SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*14675a02SAndroid Build Coastguard Worker  *
10*14675a02SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*14675a02SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*14675a02SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*14675a02SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*14675a02SAndroid Build Coastguard Worker  * limitations under the License.
15*14675a02SAndroid Build Coastguard Worker  */
16*14675a02SAndroid Build Coastguard Worker 
17*14675a02SAndroid Build Coastguard Worker #ifndef FCP_SECAGG_SERVER_SECAGG_SERVER_H_
18*14675a02SAndroid Build Coastguard Worker #define FCP_SECAGG_SERVER_SECAGG_SERVER_H_
19*14675a02SAndroid Build Coastguard Worker 
20*14675a02SAndroid Build Coastguard Worker #include <functional>
21*14675a02SAndroid Build Coastguard Worker #include <memory>
22*14675a02SAndroid Build Coastguard Worker #include <string>
23*14675a02SAndroid Build Coastguard Worker #include <vector>
24*14675a02SAndroid Build Coastguard Worker 
25*14675a02SAndroid Build Coastguard Worker #include "absl/container/flat_hash_set.h"
26*14675a02SAndroid Build Coastguard Worker #include "absl/container/node_hash_set.h"
27*14675a02SAndroid Build Coastguard Worker #include "fcp/base/monitoring.h"
28*14675a02SAndroid Build Coastguard Worker #include "fcp/base/scheduler.h"
29*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/server/experiments_interface.h"
30*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/server/experiments_names.h"
31*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/server/secagg_scheduler.h"
32*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/server/secagg_server_enums.pb.h"
33*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/server/secagg_server_messages.pb.h"
34*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/server/secagg_server_metrics_listener.h"
35*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/server/secagg_server_state.h"
36*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/server/secret_sharing_graph.h"
37*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/server/tracing_schema.h"
38*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/shared/aes_prng_factory.h"
39*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/shared/secagg_messages.pb.h"
40*14675a02SAndroid Build Coastguard Worker #include "fcp/tracing/tracing_span.h"
41*14675a02SAndroid Build Coastguard Worker 
42*14675a02SAndroid Build Coastguard Worker namespace fcp {
43*14675a02SAndroid Build Coastguard Worker namespace secagg {
44*14675a02SAndroid Build Coastguard Worker 
45*14675a02SAndroid Build Coastguard Worker // Represents a server for the Secure Aggregation protocol. Each instance of
46*14675a02SAndroid Build Coastguard Worker // this class performs just *one* session of the protocol.
47*14675a02SAndroid Build Coastguard Worker //
48*14675a02SAndroid Build Coastguard Worker // To create a new instance, use the public constructor. Once constructed, the
49*14675a02SAndroid Build Coastguard Worker // server is ready to receive messages from clients with the ReceiveMessage
50*14675a02SAndroid Build Coastguard Worker // method.
51*14675a02SAndroid Build Coastguard Worker //
52*14675a02SAndroid Build Coastguard Worker // When enough messages have been received (i.e. when ReceiveMessage or
53*14675a02SAndroid Build Coastguard Worker // ReadyForNextRound return true) or any time after that, proceed to the next
54*14675a02SAndroid Build Coastguard Worker // round by calling ProceedToNextRound.
55*14675a02SAndroid Build Coastguard Worker //
56*14675a02SAndroid Build Coastguard Worker // After all client interaction is done, the server needs to do some
57*14675a02SAndroid Build Coastguard Worker // multi-threaded computation using the supplied Scheduler. Call StartPrng to
58*14675a02SAndroid Build Coastguard Worker // begin this computation.
59*14675a02SAndroid Build Coastguard Worker //
60*14675a02SAndroid Build Coastguard Worker // When the computation is complete, call Result to get the final result.
61*14675a02SAndroid Build Coastguard Worker //
62*14675a02SAndroid Build Coastguard Worker // This class is not thread-safe.
63*14675a02SAndroid Build Coastguard Worker 
64*14675a02SAndroid Build Coastguard Worker class SecAggServer {
65*14675a02SAndroid Build Coastguard Worker  public:
66*14675a02SAndroid Build Coastguard Worker   // Constructs a new instance of the Secure Aggregation server.
67*14675a02SAndroid Build Coastguard Worker   //
68*14675a02SAndroid Build Coastguard Worker   // minimum_number_of_clients_to_proceed is the threshold lower bound on the
69*14675a02SAndroid Build Coastguard Worker   // total number of clients expected to complete the protocol. If there are
70*14675a02SAndroid Build Coastguard Worker   // ever fewer than this many clients still alive in the protocol, the server
71*14675a02SAndroid Build Coastguard Worker   // will abort (causing all clients to abort as well).
72*14675a02SAndroid Build Coastguard Worker   //
73*14675a02SAndroid Build Coastguard Worker   // total_number_of_clients is the number of clients selected to be in the
74*14675a02SAndroid Build Coastguard Worker   // cohort for this instance of Secure Aggregation.
75*14675a02SAndroid Build Coastguard Worker   //
76*14675a02SAndroid Build Coastguard Worker   // input_vector_specs must contain one InputVectorSpecification for each input
77*14675a02SAndroid Build Coastguard Worker   // vector which the protocol will aggregate.
78*14675a02SAndroid Build Coastguard Worker   //
79*14675a02SAndroid Build Coastguard Worker   // sender is used by the server to send messages to clients. The server will
80*14675a02SAndroid Build Coastguard Worker   // consume this object, taking ownership of it.
81*14675a02SAndroid Build Coastguard Worker   //
82*14675a02SAndroid Build Coastguard Worker   // sender may be called on a different thread than the thread used to call
83*14675a02SAndroid Build Coastguard Worker   // into SecAggServer, specifically in the PrngRunning state.
84*14675a02SAndroid Build Coastguard Worker   //
85*14675a02SAndroid Build Coastguard Worker   // prng_factory is a pointer to an instance of a subclass of AesPrngFactory.
86*14675a02SAndroid Build Coastguard Worker   // If this client will be communicating with the (C++) version of SecAggClient
87*14675a02SAndroid Build Coastguard Worker   // in this package, then the server and all clients should use
88*14675a02SAndroid Build Coastguard Worker   // AesCtrPrngFactory.
89*14675a02SAndroid Build Coastguard Worker   //
90*14675a02SAndroid Build Coastguard Worker   // metrics will be called over the course of the protocol to record message
91*14675a02SAndroid Build Coastguard Worker   // sizes and events. If it is null, no metrics will be recorded.
92*14675a02SAndroid Build Coastguard Worker   //
93*14675a02SAndroid Build Coastguard Worker   // threat_model includes the assumed maximum adversarial, maximum dropout
94*14675a02SAndroid Build Coastguard Worker   // rate, and adversary class.
95*14675a02SAndroid Build Coastguard Worker   //
96*14675a02SAndroid Build Coastguard Worker   //
97*14675a02SAndroid Build Coastguard Worker   // The protocol successfully
98*14675a02SAndroid Build Coastguard Worker   // completes and returns a sum if and only if no more than
99*14675a02SAndroid Build Coastguard Worker   // floor(total_number_of_clients * threat_model.estimated_dropout_rate())
100*14675a02SAndroid Build Coastguard Worker   // clients dropout before the end of the protocol execution. This ensure that
101*14675a02SAndroid Build Coastguard Worker   // at least ceil(total_number_of_clients
102*14675a02SAndroid Build Coastguard Worker   // *(1. - threat_model.estimated_dropout_rate() -
103*14675a02SAndroid Build Coastguard Worker   // threat_model.adversarial_client_rate)) values from honest clients are
104*14675a02SAndroid Build Coastguard Worker   // included in the final sum.
105*14675a02SAndroid Build Coastguard Worker   // The protocol allows to make that threshold larger by providing a larger
106*14675a02SAndroid Build Coastguard Worker   // value of minimum_number_of_clients_to_proceed, but
107*14675a02SAndroid Build Coastguard Worker   // never lower (if the provided minimum_number_of_clients_to_proceed is
108*14675a02SAndroid Build Coastguard Worker   // smaller than ceil(total_number_of_clients *(1. -
109*14675a02SAndroid Build Coastguard Worker   // threat_model.estimated_dropout_rate())), the protocol defaults to the
110*14675a02SAndroid Build Coastguard Worker   // latter value.
111*14675a02SAndroid Build Coastguard Worker   static StatusOr<std::unique_ptr<SecAggServer>> Create(
112*14675a02SAndroid Build Coastguard Worker       int minimum_number_of_clients_to_proceed, int total_number_of_clients,
113*14675a02SAndroid Build Coastguard Worker       const std::vector<InputVectorSpecification>& input_vector_specs,
114*14675a02SAndroid Build Coastguard Worker       SendToClientsInterface* sender,
115*14675a02SAndroid Build Coastguard Worker       std::unique_ptr<SecAggServerMetricsListener> metrics,
116*14675a02SAndroid Build Coastguard Worker       std::unique_ptr<SecAggScheduler> prng_runner,
117*14675a02SAndroid Build Coastguard Worker       std::unique_ptr<ExperimentsInterface> experiments,
118*14675a02SAndroid Build Coastguard Worker       const SecureAggregationRequirements& threat_model);
119*14675a02SAndroid Build Coastguard Worker 
120*14675a02SAndroid Build Coastguard Worker   ////////////////////////////// PROTOCOL METHODS //////////////////////////////
121*14675a02SAndroid Build Coastguard Worker 
122*14675a02SAndroid Build Coastguard Worker   // Makes the server abort the protocol, sending a message to all still-alive
123*14675a02SAndroid Build Coastguard Worker   // clients that the protocol has been aborted. Most of the state will be
124*14675a02SAndroid Build Coastguard Worker   // erased except for some diagnostic information. A new instance of
125*14675a02SAndroid Build Coastguard Worker   // SecAggServer will be needed to restart the protocol.
126*14675a02SAndroid Build Coastguard Worker   //
127*14675a02SAndroid Build Coastguard Worker   // If a reason string is provided, it will be stored by the server and sent to
128*14675a02SAndroid Build Coastguard Worker   // the clients as diagnostic information.
129*14675a02SAndroid Build Coastguard Worker   // An optional outcome can be provided for diagnostic purposes to be recorded
130*14675a02SAndroid Build Coastguard Worker   // via SecAggServerMetricsListener. By default, EXTERNAL_REQUEST outcome is
131*14675a02SAndroid Build Coastguard Worker   // assumed.
132*14675a02SAndroid Build Coastguard Worker   //
133*14675a02SAndroid Build Coastguard Worker   // The status will be OK unless the protocol was already completed or aborted.
134*14675a02SAndroid Build Coastguard Worker   Status Abort();
135*14675a02SAndroid Build Coastguard Worker   Status Abort(const std::string& reason, SecAggServerOutcome outcome);
136*14675a02SAndroid Build Coastguard Worker 
137*14675a02SAndroid Build Coastguard Worker   // Abort the specified client for the given reason.
138*14675a02SAndroid Build Coastguard Worker   //
139*14675a02SAndroid Build Coastguard Worker   // If the server is in a terminal state, returns a FAILED_PRECONDITION status.
140*14675a02SAndroid Build Coastguard Worker   Status AbortClient(uint32_t client_id, ClientAbortReason reason);
141*14675a02SAndroid Build Coastguard Worker 
142*14675a02SAndroid Build Coastguard Worker   // Proceeds to the next round, doing necessary computation and sending
143*14675a02SAndroid Build Coastguard Worker   // messages to clients as appropriate.
144*14675a02SAndroid Build Coastguard Worker   //
145*14675a02SAndroid Build Coastguard Worker   // If the server is not ready to proceed, this method will do nothing and
146*14675a02SAndroid Build Coastguard Worker   // return an UNAVAILABLE status. If the server is already in a terminal state,
147*14675a02SAndroid Build Coastguard Worker   // this method will do nothing and return a FAILED_PRECONDITION status.
148*14675a02SAndroid Build Coastguard Worker   //
149*14675a02SAndroid Build Coastguard Worker   // If the server is ready to proceed, but not all clients have yet sent in
150*14675a02SAndroid Build Coastguard Worker   // responses, any client that hasn't yet sent a response will be aborted (and
151*14675a02SAndroid Build Coastguard Worker   // a message informing them of this will be sent).
152*14675a02SAndroid Build Coastguard Worker   //
153*14675a02SAndroid Build Coastguard Worker   // After proceeding to the next round, the server is ready to receive more
154*14675a02SAndroid Build Coastguard Worker   // messages from clients in rounds 1, 2, and 3. In the PrngRunning round, it
155*14675a02SAndroid Build Coastguard Worker   // is instead ready to have StartPrng called.
156*14675a02SAndroid Build Coastguard Worker   //
157*14675a02SAndroid Build Coastguard Worker   // Returns OK as long as the server has actually executed the transition to
158*14675a02SAndroid Build Coastguard Worker   // the next state.
159*14675a02SAndroid Build Coastguard Worker   Status ProceedToNextRound();
160*14675a02SAndroid Build Coastguard Worker 
161*14675a02SAndroid Build Coastguard Worker   // Processes a message that has been received from a client with the given
162*14675a02SAndroid Build Coastguard Worker   // client_id.
163*14675a02SAndroid Build Coastguard Worker   //
164*14675a02SAndroid Build Coastguard Worker   // The boolean returned indicates whether the server is ready to proceed to
165*14675a02SAndroid Build Coastguard Worker   // the next round. This will be true when a number of clients equal to the
166*14675a02SAndroid Build Coastguard Worker   // minimum_number_of_clients_to_proceed threshold have sent in valid messages
167*14675a02SAndroid Build Coastguard Worker   // (and not subsequently aborted), including this one.
168*14675a02SAndroid Build Coastguard Worker   //
169*14675a02SAndroid Build Coastguard Worker   // If the message is invalid, the client who sent it will be aborted, and a
170*14675a02SAndroid Build Coastguard Worker   // message will be sent to them notifying them of the fact. A client may also
171*14675a02SAndroid Build Coastguard Worker   // send the server a message that it wishes to abort (in which case no further
172*14675a02SAndroid Build Coastguard Worker   // message to it is sent). This may cause a server that was previously ready
173*14675a02SAndroid Build Coastguard Worker   // for the next round to no longer be ready, or it may cause the server to
174*14675a02SAndroid Build Coastguard Worker   // abort if not enough clients remain alive.
175*14675a02SAndroid Build Coastguard Worker   //
176*14675a02SAndroid Build Coastguard Worker   // Returns a FAILED_PRECONDITION status if the server is in a terminal state
177*14675a02SAndroid Build Coastguard Worker   // or the PRNG_RUNNING state.
178*14675a02SAndroid Build Coastguard Worker   //
179*14675a02SAndroid Build Coastguard Worker   // Returns an ABORTED status to signify that the server has aborted after
180*14675a02SAndroid Build Coastguard Worker   // receiving this message. (This will cause all surviving clients to be
181*14675a02SAndroid Build Coastguard Worker   // notified as well.)
182*14675a02SAndroid Build Coastguard Worker   StatusOr<bool> ReceiveMessage(
183*14675a02SAndroid Build Coastguard Worker       uint32_t client_id,
184*14675a02SAndroid Build Coastguard Worker       std::unique_ptr<ClientToServerWrapperMessage> message);
185*14675a02SAndroid Build Coastguard Worker   // Sets up a callback to be invoked when any background asynchronous work
186*14675a02SAndroid Build Coastguard Worker   // has been done. The callback is guaranteed to invoked via the server's
187*14675a02SAndroid Build Coastguard Worker   // callback scheduler.
188*14675a02SAndroid Build Coastguard Worker   //
189*14675a02SAndroid Build Coastguard Worker   // Returns true if asynchronous processing is supported in the current
190*14675a02SAndroid Build Coastguard Worker   // server state and the callback has been setup successfully. Returns false
191*14675a02SAndroid Build Coastguard Worker   // if asynchronous processing isn't supported in the current server state or
192*14675a02SAndroid Build Coastguard Worker   // if no further asynchronous processing is possible. The callback argument
193*14675a02SAndroid Build Coastguard Worker   // is ignored in that case.
194*14675a02SAndroid Build Coastguard Worker   bool SetAsyncCallback(std::function<void()> async_callback);
195*14675a02SAndroid Build Coastguard Worker 
196*14675a02SAndroid Build Coastguard Worker   /////////////////////////////// STATUS METHODS ///////////////////////////////
197*14675a02SAndroid Build Coastguard Worker 
198*14675a02SAndroid Build Coastguard Worker   // Returns the set of clients that aborted the protocol. Can be used by the
199*14675a02SAndroid Build Coastguard Worker   // caller to close the relevant RPC connections or just start ignoring
200*14675a02SAndroid Build Coastguard Worker   // incoming messages from those clients for performance reasons.
201*14675a02SAndroid Build Coastguard Worker   absl::flat_hash_set<uint32_t> AbortedClientIds() const;
202*14675a02SAndroid Build Coastguard Worker 
203*14675a02SAndroid Build Coastguard Worker   // Returns a string describing the reason that the protocol was aborted.
204*14675a02SAndroid Build Coastguard Worker   // If the protocol has not actually been aborted, returns an error Status
205*14675a02SAndroid Build Coastguard Worker   // with code PRECONDITION_FAILED.
206*14675a02SAndroid Build Coastguard Worker   StatusOr<std::string> ErrorMessage() const;
207*14675a02SAndroid Build Coastguard Worker 
208*14675a02SAndroid Build Coastguard Worker   // Returns true if the protocol has been aborted, false else.
209*14675a02SAndroid Build Coastguard Worker   bool IsAborted() const;
210*14675a02SAndroid Build Coastguard Worker 
211*14675a02SAndroid Build Coastguard Worker   // Returns true if the protocol has been successfully completed, false else.
212*14675a02SAndroid Build Coastguard Worker   // The Result method can be called exactly when this method returns true.
213*14675a02SAndroid Build Coastguard Worker   bool IsCompletedSuccessfully() const;
214*14675a02SAndroid Build Coastguard Worker 
215*14675a02SAndroid Build Coastguard Worker   // Whether the set of inputs that will be included in the final aggregation
216*14675a02SAndroid Build Coastguard Worker   // has been fixed.
217*14675a02SAndroid Build Coastguard Worker   //
218*14675a02SAndroid Build Coastguard Worker   // If true, the value of NumberOfIncludedInputs will be fixed for the
219*14675a02SAndroid Build Coastguard Worker   // remainder of the protocol.
220*14675a02SAndroid Build Coastguard Worker   bool IsNumberOfIncludedInputsCommitted() const;
221*14675a02SAndroid Build Coastguard Worker 
222*14675a02SAndroid Build Coastguard Worker   // Indicates the minimum number of valid messages needed to be able to
223*14675a02SAndroid Build Coastguard Worker   // successfully move to the next round.
224*14675a02SAndroid Build Coastguard Worker   //
225*14675a02SAndroid Build Coastguard Worker   // Note that this value is not guaranteed to be monotonically decreasing.
226*14675a02SAndroid Build Coastguard Worker   // Client failures can cause this value to increase.
227*14675a02SAndroid Build Coastguard Worker   //
228*14675a02SAndroid Build Coastguard Worker   // Calling this in a terminal state results in an error.
229*14675a02SAndroid Build Coastguard Worker   StatusOr<int> MinimumMessagesNeededForNextRound() const;
230*14675a02SAndroid Build Coastguard Worker 
231*14675a02SAndroid Build Coastguard Worker   // Indicates the total number of clients that the server expects to receive
232*14675a02SAndroid Build Coastguard Worker   // a response from in this round (i.e. the ones that have not aborted). In
233*14675a02SAndroid Build Coastguard Worker   // the COMPLETED state, this returns the number of clients that survived to
234*14675a02SAndroid Build Coastguard Worker   // the final protocol message.
235*14675a02SAndroid Build Coastguard Worker   int NumberOfAliveClients() const;
236*14675a02SAndroid Build Coastguard Worker 
237*14675a02SAndroid Build Coastguard Worker   // Number of clients that failed after submitting their masked input. These
238*14675a02SAndroid Build Coastguard Worker   // clients' inputs will be included in the aggregate value, even though
239*14675a02SAndroid Build Coastguard Worker   // these clients did not complete the protocol.
240*14675a02SAndroid Build Coastguard Worker   int NumberOfClientsFailedAfterSendingMaskedInput() const;
241*14675a02SAndroid Build Coastguard Worker 
242*14675a02SAndroid Build Coastguard Worker   // Number of clients that failed before submitting their masked input. These
243*14675a02SAndroid Build Coastguard Worker   // clients' inputs won't be included in the aggregate value, even if the
244*14675a02SAndroid Build Coastguard Worker   // protocol succeeds.
245*14675a02SAndroid Build Coastguard Worker   int NumberOfClientsFailedBeforeSendingMaskedInput() const;
246*14675a02SAndroid Build Coastguard Worker 
247*14675a02SAndroid Build Coastguard Worker   // Number of clients that submitted a masked value, but didn't report their
248*14675a02SAndroid Build Coastguard Worker   // unmasking values fast enough to have them used in the final unmasking
249*14675a02SAndroid Build Coastguard Worker   // process. These clients' inputs will be included in the aggregate value.
250*14675a02SAndroid Build Coastguard Worker   int NumberOfClientsTerminatedWithoutUnmasking() const;
251*14675a02SAndroid Build Coastguard Worker 
252*14675a02SAndroid Build Coastguard Worker   // Returns the number of inputs that will appear in the final sum, if the
253*14675a02SAndroid Build Coastguard Worker   // protocol completes.
254*14675a02SAndroid Build Coastguard Worker   //
255*14675a02SAndroid Build Coastguard Worker   // Once IsNumberOfIncludedInputsCommitted is true, this value will be fixed
256*14675a02SAndroid Build Coastguard Worker   // for the remainder of the protocol.
257*14675a02SAndroid Build Coastguard Worker   //
258*14675a02SAndroid Build Coastguard Worker   // This will be 0 if the server is aborted. This will also be 0 if the
259*14675a02SAndroid Build Coastguard Worker   // server is in an early state, prior to receiving masked inputs. It is
260*14675a02SAndroid Build Coastguard Worker   // incremented only when the server receives a masked input from a client.
261*14675a02SAndroid Build Coastguard Worker   int NumberOfIncludedInputs() const;
262*14675a02SAndroid Build Coastguard Worker 
263*14675a02SAndroid Build Coastguard Worker   // Returns the number of live clients that have not yet submitted the
264*14675a02SAndroid Build Coastguard Worker   // expected response for the current round. In terminal states, this will be
265*14675a02SAndroid Build Coastguard Worker   // 0.
266*14675a02SAndroid Build Coastguard Worker   int NumberOfPendingClients() const;
267*14675a02SAndroid Build Coastguard Worker 
268*14675a02SAndroid Build Coastguard Worker   // Returns the number of clients that would still be alive if
269*14675a02SAndroid Build Coastguard Worker   // ProceedToNextRound were called immediately after. This value may be less
270*14675a02SAndroid Build Coastguard Worker   // than NumberOfMessagesReceivedInThisRound if a client fails after sending
271*14675a02SAndroid Build Coastguard Worker   // a message in this round.
272*14675a02SAndroid Build Coastguard Worker   //
273*14675a02SAndroid Build Coastguard Worker   // Note that this value is not guaranteed to be monotonically increasing,
274*14675a02SAndroid Build Coastguard Worker   // even within a round. Client failures can cause this value to decrease.
275*14675a02SAndroid Build Coastguard Worker   //
276*14675a02SAndroid Build Coastguard Worker   // Calling this in a terminal state results in an error.
277*14675a02SAndroid Build Coastguard Worker   StatusOr<int> NumberOfClientsReadyForNextRound() const;
278*14675a02SAndroid Build Coastguard Worker 
279*14675a02SAndroid Build Coastguard Worker   // Returns the number of valid messages received by clients this round.
280*14675a02SAndroid Build Coastguard Worker   // Unlike NumberOfClientsReadyForNextRound, this number is monotonically
281*14675a02SAndroid Build Coastguard Worker   // increasing until ProceedToNextRound is called, or the server aborts.
282*14675a02SAndroid Build Coastguard Worker   //
283*14675a02SAndroid Build Coastguard Worker   // Calling this in a terminal state results in an error.
284*14675a02SAndroid Build Coastguard Worker   StatusOr<int> NumberOfMessagesReceivedInThisRound() const;
285*14675a02SAndroid Build Coastguard Worker 
286*14675a02SAndroid Build Coastguard Worker   // Returns a boolean indicating if the server has received enough messages
287*14675a02SAndroid Build Coastguard Worker   // from clients (who have not subsequently aborted) to proceed to the next
288*14675a02SAndroid Build Coastguard Worker   // round. ProceedToNextRound will do nothing unless this returns true.
289*14675a02SAndroid Build Coastguard Worker   //
290*14675a02SAndroid Build Coastguard Worker   // Even after this method returns true, the server will remain in the
291*14675a02SAndroid Build Coastguard Worker   // current round until ProceedToNextRound is called.
292*14675a02SAndroid Build Coastguard Worker   //
293*14675a02SAndroid Build Coastguard Worker   // Calling this in a terminal state results in an error.
294*14675a02SAndroid Build Coastguard Worker   StatusOr<bool> ReadyForNextRound() const;
295*14675a02SAndroid Build Coastguard Worker 
296*14675a02SAndroid Build Coastguard Worker   // Transfers ownership of the result of the protocol to the caller. Requires
297*14675a02SAndroid Build Coastguard Worker   // the server to be in a completed state; returns UNAVAILABLE otherwise.
298*14675a02SAndroid Build Coastguard Worker   // Can be called only once; any consequitive calls result in an error.
299*14675a02SAndroid Build Coastguard Worker   StatusOr<std::unique_ptr<SecAggVectorMap>> Result();
300*14675a02SAndroid Build Coastguard Worker 
301*14675a02SAndroid Build Coastguard Worker   // Returns the number of neighbors of each client.
302*14675a02SAndroid Build Coastguard Worker   int NumberOfNeighbors() const;
303*14675a02SAndroid Build Coastguard Worker 
304*14675a02SAndroid Build Coastguard Worker   // Returns the minimum number of neighbors of a client that must not
305*14675a02SAndroid Build Coastguard Worker   // drop-out for that client's contribution to be included in the sum. This
306*14675a02SAndroid Build Coastguard Worker   // corresponds to the threshold in the shamir secret sharing of self and
307*14675a02SAndroid Build Coastguard Worker   // pairwise masks.
308*14675a02SAndroid Build Coastguard Worker   int MinimumSurvivingNeighborsForReconstruction() const;
309*14675a02SAndroid Build Coastguard Worker 
310*14675a02SAndroid Build Coastguard Worker   // Returns a value uniquely describing the current state of the client's
311*14675a02SAndroid Build Coastguard Worker   // FSM.
312*14675a02SAndroid Build Coastguard Worker   SecAggServerStateKind State() const;
313*14675a02SAndroid Build Coastguard Worker 
314*14675a02SAndroid Build Coastguard Worker  private:
315*14675a02SAndroid Build Coastguard Worker   // Constructs a new instance of the Secure Aggregation server.
316*14675a02SAndroid Build Coastguard Worker   explicit SecAggServer(std::unique_ptr<SecAggServerProtocolImpl> impl);
317*14675a02SAndroid Build Coastguard Worker 
318*14675a02SAndroid Build Coastguard Worker   // This causes the server to transition into a new state, and call the
319*14675a02SAndroid Build Coastguard Worker   // callback if one is provided.
320*14675a02SAndroid Build Coastguard Worker   void TransitionState(std::unique_ptr<SecAggServerState> new_state);
321*14675a02SAndroid Build Coastguard Worker 
322*14675a02SAndroid Build Coastguard Worker   // Validates if the client_id is within the expected bounds.
323*14675a02SAndroid Build Coastguard Worker   Status ValidateClientId(uint32_t client_id) const;
324*14675a02SAndroid Build Coastguard Worker 
325*14675a02SAndroid Build Coastguard Worker   // Returns an error if the server is in Aborted or Completed state.
326*14675a02SAndroid Build Coastguard Worker   Status ErrorIfAbortedOrCompleted() const;
327*14675a02SAndroid Build Coastguard Worker 
328*14675a02SAndroid Build Coastguard Worker   // The internal state object, containing details about the server's current
329*14675a02SAndroid Build Coastguard Worker   // state.
330*14675a02SAndroid Build Coastguard Worker   std::unique_ptr<SecAggServerState> state_;
331*14675a02SAndroid Build Coastguard Worker 
332*14675a02SAndroid Build Coastguard Worker   // Tracing span for this session of SecAggServer. This is bound to the
333*14675a02SAndroid Build Coastguard Worker   // lifetime of SecAggServer i.e. from the time the object is created till it
334*14675a02SAndroid Build Coastguard Worker   // is destroyed.
335*14675a02SAndroid Build Coastguard Worker   UnscopedTracingSpan<SecureAggServerSession> span_;
336*14675a02SAndroid Build Coastguard Worker 
337*14675a02SAndroid Build Coastguard Worker   // Holds pointer to a tracing span corresponding to the current active
338*14675a02SAndroid Build Coastguard Worker   // SecAggServerState.
339*14675a02SAndroid Build Coastguard Worker   std::unique_ptr<UnscopedTracingSpan<SecureAggServerState>> state_span_;
340*14675a02SAndroid Build Coastguard Worker };
341*14675a02SAndroid Build Coastguard Worker 
342*14675a02SAndroid Build Coastguard Worker }  // namespace secagg
343*14675a02SAndroid Build Coastguard Worker }  // namespace fcp
344*14675a02SAndroid Build Coastguard Worker #endif  // FCP_SECAGG_SERVER_SECAGG_SERVER_H_
345