Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions willow/api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,72 @@ cc_test(
"//willow/testing_utils:testing_utils_cc",
],
)

rust_library(
name = "coordinator_rust",
srcs = ["coordinator.rs"],
deps = [
":aggregation_config",
":proto_serialization_traits",
"@protobuf//rust:protobuf",
"@cxx.rs//:cxx",
"//ffi_utils:status",
"//willow/crypto:ahe_traits",
"//willow/crypto:kahe_traits",
"//willow/crypto:shell_kahe",
"//willow/crypto:shell_parameters",
"//willow/crypto:shell_vahe",
"//willow/crypto:vahe_traits",
"//willow/proto/shell:shell_ciphertexts_rust_proto",
"//willow/proto/willow:aggregation_config_rust_proto",
"//willow/proto/willow:messages_rust_proto",
"//willow/protocol:decryptor_traits",
"//willow/protocol:messages",
"//willow/protocol:willow_v1_coordinator",
],
)

rust_cxx_bridge(
name = "coordinator_cxx",
src = "coordinator.rs",
deps = [
":coordinator_rust",
"//ffi_utils:status_cxx",
],
)

cc_library(
name = "coordinator",
srcs = ["coordinator.cc"],
hdrs = ["coordinator.h"],
deps = [
":coordinator_cxx",
":coordinator_cxx/include", # fixdeps: keep
":coordinator_rust", # fixdeps: keep
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/status:statusor",
"@abseil-cpp//absl/types:span",
"@cxx.rs//:core",
"//ffi_utils:cxx_utils",
"//ffi_utils:status_macros",
"//willow/proto/shell:shell_ciphertexts_cc_proto",
"//willow/proto/willow:aggregation_config_cc_proto",
"//willow/proto/willow:messages_cc_proto",
],
)

cc_test(
name = "coordinator_test",
srcs = ["coordinator_test.cc"],
deps = [
":coordinator",
":coordinator_cxx", # fixdeps: keep
"@googletest//:gtest_main",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/status:statusor",
"//ffi_utils:status_matchers",
"//willow/proto/shell:shell_ciphertexts_cc_proto",
"//willow/proto/willow:aggregation_config_cc_proto",
"//willow/proto/willow:messages_cc_proto",
],
)
130 changes: 130 additions & 0 deletions willow/api/coordinator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "willow/api/coordinator.h"

#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "ffi_utils/cxx_utils.h"
#include "ffi_utils/status_macros.h"
#include "include/cxx.h"
#include "willow/api/coordinator.rs.h"
#include "willow/proto/shell/ciphertexts.pb.h"
#include "willow/proto/willow/aggregation_config.pb.h"
#include "willow/proto/willow/messages.pb.h"

namespace secure_aggregation {
namespace willow {
namespace {

// Helper to serialize an array of C++ protobufs and pass them
// across the FFI boundary as a slice of byte views.
template <typename T>
struct SerializedProtoSpan {
std::vector<std::string> strs;
std::vector<SerializedProtoView> views;
rust::Slice<const SerializedProtoView> slice;

explicit SerializedProtoSpan(absl::Span<const T> protos) {
strs.reserve(protos.size());
views.reserve(protos.size());
for (const auto& proto : protos) {
strs.push_back(proto.SerializeAsString());
views.push_back(SerializedProtoView{ToRustSlice(strs.back())});
}
slice = rust::Slice<const SerializedProtoView>(views.data(), views.size());
}
};

} // namespace

absl::StatusOr<std::unique_ptr<Coordinator>> Coordinator::Create(
const AggregationConfigProto& aggregation_config) {
std::string config_str = aggregation_config.SerializeAsString();
secure_aggregation::WillowShellCoordinator* coord_ptr = nullptr;
SECAGG_RETURN_IF_FFI_ERROR(NewWillowShellCoordinatorFromSerializedConfig(
ToRustSlice(config_str), &coord_ptr));
auto coord_box = WillowShellCoordinatorIntoBox(coord_ptr);
return std::unique_ptr<Coordinator>(new Coordinator(std::move(coord_box)));
}

absl::StatusOr<VerifyKeyContributionsRequest>
Coordinator::HandleSetupSubmissions(
absl::Span<const SetupContribution> non_reputable_contributions,
absl::Span<const SetupContribution> reputable_contributions) {
SerializedProtoSpan<SetupContribution> non_reputable_helper(
non_reputable_contributions);
SerializedProtoSpan<SetupContribution> reputable_helper(
reputable_contributions);

rust::Vec<uint8_t> result_bytes;
// We only pass slices to Rust, which point to views of strings owned by
// SerializedProtoSpan
SECAGG_RETURN_IF_FFI_ERROR(coordinator_->HandleSetupSubmissions(
non_reputable_helper.slice, reputable_helper.slice, result_bytes));

VerifyKeyContributionsRequest verify_request;
if (!verify_request.ParseFromArray(result_bytes.data(),
result_bytes.size())) {
return absl::InternalError(
"Failed to parse VerifyKeyContributionsRequest from serialized FFI "
"bytes.");
}
return verify_request;
}

absl::StatusOr<PartialDecryptionRequest> Coordinator::PrepareDecryptionRequest(
const ShellAhePartialDecCiphertext& verifier_ciphertext) {
std::string ct_str = verifier_ciphertext.SerializeAsString();
rust::Vec<uint8_t> result_bytes;
SECAGG_RETURN_IF_FFI_ERROR(coordinator_->PrepareDecryptionRequest(
ToRustSlice(ct_str), result_bytes));

PartialDecryptionRequest dec_request;
if (!dec_request.ParseFromArray(result_bytes.data(), result_bytes.size())) {
return absl::InternalError(
"Failed to parse PartialDecryptionRequest from serialized FFI bytes.");
}
return dec_request;
}

absl::StatusOr<FinalizedPartialDecryption>
Coordinator::AggregateAndFinalizePartialDecryptions(
absl::Span<const PartialDecryptionResponse> partial_responses) {
SerializedProtoSpan<PartialDecryptionResponse> responses_helper(
partial_responses);

rust::Vec<uint8_t> result_bytes;
SECAGG_RETURN_IF_FFI_ERROR(
coordinator_->AggregateAndFinalizePartialDecryptions(
responses_helper.slice, result_bytes));

FinalizedPartialDecryption finalized_pd;
if (!finalized_pd.ParseFromArray(result_bytes.data(), result_bytes.size())) {
return absl::InternalError(
"Failed to parse FinalizedPartialDecryption from serialized FFI "
"bytes.");
}
return finalized_pd;
}

} // namespace willow
} // namespace secure_aggregation
69 changes: 69 additions & 0 deletions willow/api/coordinator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SECURE_AGGREGATION_WILLOW_API_COORDINATOR_H_
#define SECURE_AGGREGATION_WILLOW_API_COORDINATOR_H_

#include <memory>
#include <utility>

#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "include/cxx.h"
#include "willow/api/coordinator.rs.h"
#include "willow/proto/shell/ciphertexts.pb.h"
#include "willow/proto/willow/aggregation_config.pb.h"
#include "willow/proto/willow/messages.pb.h"

namespace secure_aggregation {
namespace willow {

// Implements a C++ wrapper around the untrusted multi-decryptor Coordinator.
// Manages protocol flow and aggregates messages from all decryptors.
class Coordinator {
public:
// Creates a new coordinator with the given aggregation_config.
static absl::StatusOr<std::unique_ptr<Coordinator>> Create(
const AggregationConfigProto& aggregation_config);

// Stores setup contributions from all decryptors during key generation and
// creates a request for reputable decryptors to verify and aggregate key
// contributions into a public key.
absl::StatusOr<VerifyKeyContributionsRequest> HandleSetupSubmissions(
absl::Span<const SetupContribution> non_reputable_contributions,
absl::Span<const SetupContribution> reputable_contributions);

// Combines the verifier ciphertext half (obtained from the accumulation
// pipeline) with the AHE DP noise component sum received at setup, to prepare
// a decryption request for decryptors.
absl::StatusOr<PartialDecryptionRequest> PrepareDecryptionRequest(
const ShellAhePartialDecCiphertext& verifier_ciphertext);

// Accumulates partial decryptions and finalizes the partial decryption sum.
absl::StatusOr<FinalizedPartialDecryption>
AggregateAndFinalizePartialDecryptions(
absl::Span<const PartialDecryptionResponse> partial_responses);

private:
explicit Coordinator(
rust::Box<secure_aggregation::WillowShellCoordinator> coordinator)
: coordinator_(std::move(coordinator)) {}

rust::Box<secure_aggregation::WillowShellCoordinator> coordinator_;
};

} // namespace willow
} // namespace secure_aggregation

#endif // SECURE_AGGREGATION_WILLOW_API_COORDINATOR_H_
Loading
Loading