1 /* 2 * Copyright 2020 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 #ifndef FCP_SECAGG_SERVER_SECRET_SHARING_GRAPH_FACTORY_H_ 18 #define FCP_SECAGG_SERVER_SECRET_SHARING_GRAPH_FACTORY_H_ 19 20 #include <algorithm> 21 #include <cstddef> 22 #include <memory> 23 #include <utility> 24 25 #include "fcp/base/monitoring.h" 26 #include "fcp/secagg/server/secret_sharing_complete_graph.h" 27 #include "fcp/secagg/server/secret_sharing_graph.h" 28 #include "fcp/secagg/server/secret_sharing_harary_graph.h" 29 #include "fcp/secagg/server/ssl_bit_gen.h" 30 31 namespace fcp { 32 namespace secagg { 33 34 // Factory class that constructs non-copyable instances of children classes of 35 // SecretSharingGraph. 36 class SecretSharingGraphFactory { 37 public: 38 // Creates a SecretSharingCompleteGraph. CreateCompleteGraph(int num_nodes,int threshold)39 static std::unique_ptr<SecretSharingCompleteGraph> CreateCompleteGraph( 40 int num_nodes, int threshold) { 41 FCP_CHECK(num_nodes >= 1) 42 << "num_nodes must be >= 1, given value was " << num_nodes; 43 FCP_CHECK(threshold >= 1) 44 << "threshold must be >= 1, given value was " << threshold; 45 FCP_CHECK(threshold <= num_nodes) 46 << "threshold must be <= num_nodes, given values were " << threshold 47 << ", " << num_nodes; 48 return absl::WrapUnique( 49 new SecretSharingCompleteGraph(num_nodes, threshold)); 50 } 51 52 // Creates a SecretSharingHararyGraph. 53 static std::unique_ptr<SecretSharingHararyGraph> CreateHararyGraph( 54 int num_nodes, int degree, int threshold, bool is_random = true) { 55 FCP_CHECK(num_nodes >= 1) 56 << "num_nodes must be >= 1, given value was " << num_nodes; 57 FCP_CHECK(degree <= num_nodes) 58 << "degree must be <= num_nodes, given values were " << num_nodes 59 << ", " << degree; 60 FCP_CHECK(degree % 2 == 1) 61 << "degree must be odd, given value was " << degree; 62 FCP_CHECK(threshold >= 1) 63 << "threshold must be >= 1, given value was " << threshold; 64 FCP_CHECK(threshold <= degree) 65 << "threshold must be <= degree, given values were " << threshold 66 << ", " << degree; 67 auto permutation = std::vector<int>(num_nodes); 68 for (int i = 0; i < num_nodes; ++i) { 69 permutation[i] = i; 70 } 71 if (is_random) { 72 std::shuffle(permutation.begin(), permutation.end(), SslBitGen()); 73 } 74 return absl::WrapUnique(new SecretSharingHararyGraph( 75 degree, threshold, std::move(permutation))); 76 } 77 }; 78 79 } // namespace secagg 80 } // namespace fcp 81 82 #endif // FCP_SECAGG_SERVER_SECRET_SHARING_GRAPH_FACTORY_H_ 83