nebulaforge-offline-resilie.../nebulaforge/federated.py

30 lines
1.0 KiB
Python

from __future__ import annotations
from typing import Any, List
class SecureAggregator:
"""Very small, deterministic secure-aggregation surface.
This MVP aggregates a list of numeric vectors and optionally applies a
per-vector differential-privacy budget by clipping/noising if budgets are provided.
"""
def __init__(self, dp_budget: float | None = None) -> None:
self.dp_budget = dp_budget
def aggregate(self, updates: List[List[float]]) -> List[float]:
if not updates:
return []
# Simple elementwise average as a placeholder aggregation
n = len(updates)
m = max(len(u) for u in updates)
sums = [0.0] * m
for u in updates:
for i, v in enumerate(u):
sums[i] += v
agg = [s / n for s in sums]
if self.dp_budget is not None:
# Very naive DP: clip to +/- budget around 0 for each dimension
limit = float(self.dp_budget)
agg = [min(max(x, -limit), limit) for x in agg]
return agg