Skip to content

Commit 40fe815

Browse files
committed
Docstrings and comments added to secure history aggregator
1 parent 42f2e3b commit 40fe815

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

discojs/src/aggregator/secure_history.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe("Secure history aggregator", function () {
9292
const avgPartialSum = aggregation.avg(partialSums);
9393
expect(sumRound1.equals(avgPartialSum, epsilon)).to.be.true;
9494

95+
// Now we simulate a second round of aggregation with momentum smoothing
9596
const dummyPromise = aggregator.getPromiseForAggregation();
9697
partialSums.forEach((partialSum, idx) => {
9798
const nodeId = idx.toString();
@@ -105,7 +106,7 @@ describe("Secure history aggregator", function () {
105106
ws.map((tensor) => tf.mul(tensor, 1.1))
106107
);
107108

108-
// Step 3: Add new partial sums to aggregator
109+
// Add the modified partial sums to the aggregator
109110
partialSums2.forEach((partialSum, idx) => {
110111
const nodeId = idx.toString();
111112
aggregator.add(nodeId, partialSum, 1);
@@ -117,7 +118,7 @@ describe("Secure history aggregator", function () {
117118
prev.mul(0.8).add(curr.mul(0.2)) // 0.8 = beta, 0.2 = (1 - beta)
118119
);
119120

120-
// Compare the actual result to the expected smoothed result
121+
// Compare the actual result to the expected smoothed result using momentum
121122
expect(sumRound2.equals(expectedSumRound2, 1e-3)).to.be.true;
122123
});
123124

discojs/src/aggregator/secure_history.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,30 @@ import { SecureAggregator } from "./secure.js";
33
import * as tf from "@tensorflow/tfjs";
44
import { aggregation } from "../index.js";
55

6+
/**
7+
* Aggregator that implements secure multi-party computation with history-based momentum smoothing.
8+
* It aggregates contributions in two communication rounds:
9+
* - In the first round, nodes send their secret shares to each other.
10+
* - In the second round, they sum their received shares and communicate the result.
11+
* Finally, nodes average the received partial sums to establish the aggregation result.
12+
* This aggregator also applies momentum smoothing based on the previous aggregation result.
13+
* It uses a beta parameter to control the smoothing effect.
14+
* The first aggregation round uses the average of contributions, while subsequent rounds apply momentum smoothing.
15+
* This allows for a more stable aggregation result over time, reducing the impact of outliers.
16+
* * @extends SecureAggregator
17+
* * @example
18+
* const aggregator = new SecureHistoryAggregator(100, 0.9);
19+
*/
20+
621
export class SecureHistoryAggregator extends SecureAggregator {
722
private prevAggregate: WeightsContainer | null = null;
823
private readonly beta: number;
924

25+
/**
26+
* @param maxShareValue - The maximum value for each share.
27+
* @param beta - The momentum smoothing factor (0 < beta < 1).
28+
*/
29+
1030
constructor(maxShareValue = 100, beta = 0.9) {
1131
super(maxShareValue);
1232
this.beta = beta;

0 commit comments

Comments
 (0)