xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/map_of_masks.h (revision 14675a029014e728ec732f129a32e299b2da0601)
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  *     https://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 #ifndef FCP_SECAGG_SHARED_MAP_OF_MASKS_H_
17 #define FCP_SECAGG_SHARED_MAP_OF_MASKS_H_
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "absl/container/flat_hash_map.h"
24 #include "fcp/secagg/shared/aes_key.h"
25 #include "fcp/secagg/shared/aes_prng_factory.h"
26 #include "fcp/secagg/shared/async_abort.h"
27 #include "fcp/secagg/shared/compute_session_id.h"
28 #include "fcp/secagg/shared/input_vector_specification.h"
29 #include "fcp/secagg/shared/secagg_vector.h"
30 
31 // This file contains two unbound functions for generating and adding maps of
32 // mask vectors.
33 
34 namespace fcp {
35 namespace secagg {
36 
37 // Generates and returns a map of masks for all the vectors that need to be
38 // masked, given all the keys that need to be used to mask (or unmask) those
39 // vectors.
40 //
41 // prng_factory is an instance of a subclass of AesPrngFactory.
42 // For clients communicating with the (C++) version of SecAggServer in this
43 // package, or the SecAggServer itself, this must be an instance of
44 // AesCtrPrngFactory.
45 //
46 // Returns a nullptr value if the operation was aborted, as detected via the
47 // optional async_abort parameter.
48 std::unique_ptr<SecAggVectorMap> MapOfMasks(
49     const std::vector<AesKey>& prng_keys_to_add,
50     const std::vector<AesKey>& prng_keys_to_subtract,
51     const std::vector<InputVectorSpecification>& input_vector_specs,
52     const SessionId& session_id, const AesPrngFactory& prng_factory,
53     AsyncAbort* async_abort = nullptr);
54 
55 // Optimized version of MapOfMasks that uses optimized AddModOpt and
56 // SubtractModOpt modulus operations.
57 std::unique_ptr<SecAggVectorMap> MapOfMasksV3(
58     const std::vector<AesKey>& prng_keys_to_add,
59     const std::vector<AesKey>& prng_keys_to_subtract,
60     const std::vector<InputVectorSpecification>& input_vector_specs,
61     const SessionId& session_id, const AesPrngFactory& prng_factory,
62     AsyncAbort* async_abort = nullptr);
63 
64 // Optimized version of MapOfMasks that uses optimized AddMapOpt and
65 // SubtractMapOpt modululs operations and produces map of unpacked vectors.
66 std::unique_ptr<SecAggUnpackedVectorMap> UnpackedMapOfMasks(
67     const std::vector<AesKey>& prng_keys_to_add,
68     const std::vector<AesKey>& prng_keys_to_subtract,
69     const std::vector<InputVectorSpecification>& input_vector_specs,
70     const SessionId& session_id, const AesPrngFactory& prng_factory,
71     AsyncAbort* async_abort = nullptr);
72 
73 // Adds two vectors together and returns a new sum vector.
74 SecAggVector AddVectors(const SecAggVector& a, const SecAggVector& b);
75 
76 // Takes two maps of masks/masked vectors, and adds them together, returning the
77 // sum.
78 std::unique_ptr<SecAggVectorMap> AddMaps(const SecAggVectorMap& a,
79                                          const SecAggVectorMap& b);
80 
81 }  // namespace secagg
82 }  // namespace fcp
83 
84 #endif  // FCP_SECAGG_SHARED_MAP_OF_MASKS_H_
85