Skip to content

Federated SC Learning

Privacy-preserving federated learning for SC networks. Supports secure aggregation, differential privacy (DP-SGD), and communication-efficient model compression.

Quick Start

Python
from sc_neurocore.federated.federated_sc import (
    FederatedCoordinator, SecureAggregator, DifferentialPrivacy,
)

sc_neurocore.federated.federated_sc

Federated stochastic computing learning with in-domain DP.

Gradient exchange as noisy SC bitstreams: differential privacy noise is injected directly in the stochastic domain (bit-flip mechanism) rather than post-hoc Gaussian/Laplace on real-valued gradients. This enables provable privacy budgets on deterministic LFSR-encoded bitstreams.

Secure aggregation uses SHA-256 commitment + Pedersen-style secret sharing (compatible with the existing ZKP verifier in security/zkp.py).

Key components: - DPMechanism: Bitstream-level DP noise (flip bits with calibrated probability) - PrivacyAccountant: Rényi-DP composition tracker with (ε,δ) conversion - SecretShare: Additive secret sharing over GF(2) for bitstream aggregation - CommitmentScheme: SHA-256 commitment for verifiable aggregation - FederatedClient: Local SC training step + DP gradient encoding - FederatedAggregator: Secure aggregation server - FederatedRound: Orchestrates one round of federated learning

DPMechanism dataclass

Bitstream-level differential privacy via calibrated bit-flipping.

Instead of adding Gaussian/Laplace noise to real-valued gradients, we flip bits in the SC bitstream with probability p. This achieves (ε,0)-differential privacy where ε = ln((1-p)/p) per bit.

For a bitstream of length L, the total privacy cost is ε_total via Rényi-DP composition (tighter than naive composition).

Source code in src/sc_neurocore/federated/federated_sc.py
Python
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
@dataclass
class DPMechanism:
    """Bitstream-level differential privacy via calibrated bit-flipping.

    Instead of adding Gaussian/Laplace noise to real-valued gradients,
    we flip bits in the SC bitstream with probability p. This achieves
    (ε,0)-differential privacy where ε = ln((1-p)/p) per bit.

    For a bitstream of length L, the total privacy cost is ε_total via
    Rényi-DP composition (tighter than naive composition).
    """

    epsilon: float = 1.0
    sensitivity: float = 1.0

    @property
    def flip_probability(self) -> float:
        """Calibrated bit-flip probability for the target ε."""
        e = self.epsilon / self.sensitivity
        return 1.0 / (1.0 + math.exp(e))

    def privatise(
        self, bitstream: np.ndarray[Any, Any], rng: np.random.Generator
    ) -> np.ndarray[Any, Any]:
        """Apply DP noise by flipping bits with calibrated probability."""
        p = self.flip_probability
        flip_mask = rng.random(len(bitstream)) < p
        noisy = bitstream.copy()
        noisy[flip_mask] = 1 - noisy[flip_mask]
        return noisy

    def per_bit_epsilon(self) -> float:
        """Privacy cost per individual bit."""
        p = self.flip_probability
        if p <= 0 or p >= 1:
            return float("inf")
        return abs(math.log((1 - p) / p))

    def total_epsilon(self, bitstream_length: int) -> float:
        """Total ε under advanced composition (Rényi-based bound)."""
        eps_1 = self.per_bit_epsilon()
        return eps_1 * math.sqrt(2 * bitstream_length * math.log(1 / 1e-5))

flip_probability property

Calibrated bit-flip probability for the target ε.

privatise(bitstream, rng)

Apply DP noise by flipping bits with calibrated probability.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
91
92
93
94
95
96
97
98
99
def privatise(
    self, bitstream: np.ndarray[Any, Any], rng: np.random.Generator
) -> np.ndarray[Any, Any]:
    """Apply DP noise by flipping bits with calibrated probability."""
    p = self.flip_probability
    flip_mask = rng.random(len(bitstream)) < p
    noisy = bitstream.copy()
    noisy[flip_mask] = 1 - noisy[flip_mask]
    return noisy

per_bit_epsilon()

Privacy cost per individual bit.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
101
102
103
104
105
106
def per_bit_epsilon(self) -> float:
    """Privacy cost per individual bit."""
    p = self.flip_probability
    if p <= 0 or p >= 1:
        return float("inf")
    return abs(math.log((1 - p) / p))

total_epsilon(bitstream_length)

Total ε under advanced composition (Rényi-based bound).

Source code in src/sc_neurocore/federated/federated_sc.py
Python
108
109
110
111
def total_epsilon(self, bitstream_length: int) -> float:
    """Total ε under advanced composition (Rényi-based bound)."""
    eps_1 = self.per_bit_epsilon()
    return eps_1 * math.sqrt(2 * bitstream_length * math.log(1 / 1e-5))

PrivacyAccountant dataclass

Rényi Differential Privacy (RDP) composition tracker.

Tracks cumulative privacy budget across federated rounds. Uses the moments accountant for tight composition.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
@dataclass
class PrivacyAccountant:
    """Rényi Differential Privacy (RDP) composition tracker.

    Tracks cumulative privacy budget across federated rounds.
    Uses the moments accountant for tight composition.
    """

    target_epsilon: float = 10.0
    target_delta: float = 1e-5
    alpha: float = 2.0
    rdp_budget: float = 0.0
    rounds_consumed: int = 0

    def consume_round(self, mechanism: DPMechanism, bitstream_length: int) -> bool:
        """Account for one round. Returns True if budget remains."""
        eps_1 = mechanism.per_bit_epsilon()
        # RDP of randomized response at order alpha
        rdp_step = (
            (self.alpha / (self.alpha - 1))
            * math.log(
                (1 - mechanism.flip_probability) ** self.alpha
                + mechanism.flip_probability**self.alpha
            )
            * bitstream_length
        )
        self.rdp_budget += abs(rdp_step)
        self.rounds_consumed += 1
        return not self.is_exhausted()

    def current_epsilon(self) -> float:
        """Convert accumulated RDP to (ε,δ)-DP."""
        if self.rdp_budget <= 0:
            return 0.0
        return self.rdp_budget + math.log(1 / self.target_delta) / (self.alpha - 1)

    def remaining_epsilon(self) -> float:
        """How much budget is left."""
        return max(0.0, self.target_epsilon - self.current_epsilon())

    def is_exhausted(self) -> bool:
        """Return whether the privacy budget is exhausted."""
        return self.current_epsilon() >= self.target_epsilon

consume_round(mechanism, bitstream_length)

Account for one round. Returns True if budget remains.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def consume_round(self, mechanism: DPMechanism, bitstream_length: int) -> bool:
    """Account for one round. Returns True if budget remains."""
    eps_1 = mechanism.per_bit_epsilon()
    # RDP of randomized response at order alpha
    rdp_step = (
        (self.alpha / (self.alpha - 1))
        * math.log(
            (1 - mechanism.flip_probability) ** self.alpha
            + mechanism.flip_probability**self.alpha
        )
        * bitstream_length
    )
    self.rdp_budget += abs(rdp_step)
    self.rounds_consumed += 1
    return not self.is_exhausted()

current_epsilon()

Convert accumulated RDP to (ε,δ)-DP.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
182
183
184
185
186
def current_epsilon(self) -> float:
    """Convert accumulated RDP to (ε,δ)-DP."""
    if self.rdp_budget <= 0:
        return 0.0
    return self.rdp_budget + math.log(1 / self.target_delta) / (self.alpha - 1)

remaining_epsilon()

How much budget is left.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
188
189
190
def remaining_epsilon(self) -> float:
    """How much budget is left."""
    return max(0.0, self.target_epsilon - self.current_epsilon())

is_exhausted()

Return whether the privacy budget is exhausted.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
192
193
194
def is_exhausted(self) -> bool:
    """Return whether the privacy budget is exhausted."""
    return self.current_epsilon() >= self.target_epsilon

SecretShare dataclass

Additive secret sharing over GF(2) for bitstream aggregation.

Splits a bitstream into N shares such that XOR of all shares recovers the original. Individual shares reveal nothing.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
@dataclass
class SecretShare:
    """Additive secret sharing over GF(2) for bitstream aggregation.

    Splits a bitstream into N shares such that XOR of all shares
    recovers the original. Individual shares reveal nothing.
    """

    num_parties: int = 3

    def split(
        self, bitstream: np.ndarray[Any, Any], rng: np.random.Generator
    ) -> List[np.ndarray[Any, Any]]:
        """Split bitstream into additive GF(2) shares."""
        bitstream_u8 = bitstream.astype(np.uint8, copy=False)
        shares: List[np.ndarray[Any, Any]] = []
        accumulated = np.zeros_like(bitstream_u8)
        for i in range(self.num_parties - 1):
            share = rng.integers(0, 2, size=len(bitstream_u8), dtype=np.uint8)
            shares.append(share)
            accumulated ^= share
        # Last share ensures XOR of all shares == original
        shares.append(bitstream_u8 ^ accumulated)
        return shares

    @staticmethod
    def reconstruct(shares: List[np.ndarray[Any, Any]]) -> np.ndarray[Any, Any]:
        """Reconstruct bitstream from all shares (XOR)."""
        result = np.zeros_like(shares[0])
        for share in shares:
            result ^= share
        return result

    @staticmethod
    def verify_reconstruction(
        original: np.ndarray[Any, Any], shares: List[np.ndarray[Any, Any]]
    ) -> bool:
        """Verify that shares reconstruct correctly."""
        return np.array_equal(original, SecretShare.reconstruct(shares))

split(bitstream, rng)

Split bitstream into additive GF(2) shares.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def split(
    self, bitstream: np.ndarray[Any, Any], rng: np.random.Generator
) -> List[np.ndarray[Any, Any]]:
    """Split bitstream into additive GF(2) shares."""
    bitstream_u8 = bitstream.astype(np.uint8, copy=False)
    shares: List[np.ndarray[Any, Any]] = []
    accumulated = np.zeros_like(bitstream_u8)
    for i in range(self.num_parties - 1):
        share = rng.integers(0, 2, size=len(bitstream_u8), dtype=np.uint8)
        shares.append(share)
        accumulated ^= share
    # Last share ensures XOR of all shares == original
    shares.append(bitstream_u8 ^ accumulated)
    return shares

reconstruct(shares) staticmethod

Reconstruct bitstream from all shares (XOR).

Source code in src/sc_neurocore/federated/federated_sc.py
Python
225
226
227
228
229
230
231
@staticmethod
def reconstruct(shares: List[np.ndarray[Any, Any]]) -> np.ndarray[Any, Any]:
    """Reconstruct bitstream from all shares (XOR)."""
    result = np.zeros_like(shares[0])
    for share in shares:
        result ^= share
    return result

verify_reconstruction(original, shares) staticmethod

Verify that shares reconstruct correctly.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
233
234
235
236
237
238
@staticmethod
def verify_reconstruction(
    original: np.ndarray[Any, Any], shares: List[np.ndarray[Any, Any]]
) -> bool:
    """Verify that shares reconstruct correctly."""
    return np.array_equal(original, SecretShare.reconstruct(shares))

CommitmentScheme dataclass

SHA-256 commitment for verifiable aggregation.

Compatible with the ZKPVerifier in security/zkp.py.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
@dataclass
class CommitmentScheme:
    """SHA-256 commitment for verifiable aggregation.

    Compatible with the ZKPVerifier in security/zkp.py.
    """

    @staticmethod
    def commit(data: np.ndarray[Any, Any], nonce: Optional[bytes] = None) -> str:
        """Create a binding commitment to data."""
        payload = data.tobytes()
        if nonce is not None:
            payload = nonce + payload
        return hashlib.sha256(payload).hexdigest()

    @staticmethod
    def verify(data: np.ndarray[Any, Any], commitment: str, nonce: Optional[bytes] = None) -> bool:
        """Verify a commitment."""
        return CommitmentScheme.commit(data, nonce) == commitment

    @staticmethod
    def generate_nonce(rng: np.random.Generator) -> bytes:
        """Generate a random 32-byte nonce."""
        return rng.bytes(32)

commit(data, nonce=None) staticmethod

Create a binding commitment to data.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
251
252
253
254
255
256
257
@staticmethod
def commit(data: np.ndarray[Any, Any], nonce: Optional[bytes] = None) -> str:
    """Create a binding commitment to data."""
    payload = data.tobytes()
    if nonce is not None:
        payload = nonce + payload
    return hashlib.sha256(payload).hexdigest()

verify(data, commitment, nonce=None) staticmethod

Verify a commitment.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
259
260
261
262
@staticmethod
def verify(data: np.ndarray[Any, Any], commitment: str, nonce: Optional[bytes] = None) -> bool:
    """Verify a commitment."""
    return CommitmentScheme.commit(data, nonce) == commitment

generate_nonce(rng) staticmethod

Generate a random 32-byte nonce.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
264
265
266
267
@staticmethod
def generate_nonce(rng: np.random.Generator) -> bytes:
    """Generate a random 32-byte nonce."""
    return rng.bytes(32)

SCGradientEncoder dataclass

Encodes real-valued gradients as SC bitstreams with DP noise.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
@dataclass
class SCGradientEncoder:
    """Encodes real-valued gradients as SC bitstreams with DP noise."""

    bitstream_length: int = 256
    dp: DPMechanism = field(default_factory=DPMechanism)

    def encode(
        self,
        gradients: np.ndarray[Any, Any],
        seeds: np.ndarray[Any, Any],
        rng: np.random.Generator,
    ) -> List[np.ndarray[Any, Any]]:
        """Encode gradient vector as privatised SC bitstreams.

        1. Normalise gradients to [0, 1]
        2. LFSR-encode each element
        3. Apply DP bit-flip noise
        """
        g_min, g_max = gradients.min(), gradients.max()
        span = g_max - g_min
        if span < 1e-12:
            normalised = np.full_like(gradients, 0.5)
        else:
            normalised = (gradients - g_min) / span

        bitstreams = []
        for i, val in enumerate(normalised):
            seed = int(seeds[i % len(seeds)]) & 0xFFFF
            if seed == 0:
                seed = 1
            bs = lfsr_encode(val, seed, self.bitstream_length)
            bs = self.dp.privatise(bs, rng)
            bitstreams.append(bs)
        return bitstreams

    def decode(
        self, bitstreams: List[np.ndarray[Any, Any]], g_min: float, g_max: float
    ) -> np.ndarray[Any, Any]:
        """Decode SC bitstreams back to gradient values."""
        probs = np.array([bitstream_probability(bs) for bs in bitstreams])
        span = g_max - g_min
        return probs * span + g_min

encode(gradients, seeds, rng)

Encode gradient vector as privatised SC bitstreams.

  1. Normalise gradients to [0, 1]
  2. LFSR-encode each element
  3. Apply DP bit-flip noise
Source code in src/sc_neurocore/federated/federated_sc.py
Python
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
def encode(
    self,
    gradients: np.ndarray[Any, Any],
    seeds: np.ndarray[Any, Any],
    rng: np.random.Generator,
) -> List[np.ndarray[Any, Any]]:
    """Encode gradient vector as privatised SC bitstreams.

    1. Normalise gradients to [0, 1]
    2. LFSR-encode each element
    3. Apply DP bit-flip noise
    """
    g_min, g_max = gradients.min(), gradients.max()
    span = g_max - g_min
    if span < 1e-12:
        normalised = np.full_like(gradients, 0.5)
    else:
        normalised = (gradients - g_min) / span

    bitstreams = []
    for i, val in enumerate(normalised):
        seed = int(seeds[i % len(seeds)]) & 0xFFFF
        if seed == 0:
            seed = 1
        bs = lfsr_encode(val, seed, self.bitstream_length)
        bs = self.dp.privatise(bs, rng)
        bitstreams.append(bs)
    return bitstreams

decode(bitstreams, g_min, g_max)

Decode SC bitstreams back to gradient values.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
309
310
311
312
313
314
315
def decode(
    self, bitstreams: List[np.ndarray[Any, Any]], g_min: float, g_max: float
) -> np.ndarray[Any, Any]:
    """Decode SC bitstreams back to gradient values."""
    probs = np.array([bitstream_probability(bs) for bs in bitstreams])
    span = g_max - g_min
    return probs * span + g_min

FederatedClient dataclass

One participant in federated SC learning.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
@dataclass
class FederatedClient:
    """One participant in federated SC learning."""

    client_id: int
    encoder: SCGradientEncoder
    rng: np.random.Generator = field(default_factory=lambda: np.random.default_rng(42))
    local_weights: Optional[np.ndarray[Any, Any]] = None
    commitment: Optional[str] = None

    def __post_init__(self) -> None:
        """Seed the client RNG deterministically from the client id."""
        self.rng = np.random.default_rng(self.client_id * 7919 + 1)

    def local_train(
        self, data: np.ndarray[Any, Any], labels: np.ndarray[Any, Any], lr: float = 0.01
    ) -> np.ndarray[Any, Any]:
        """Simulate one local training step (gradient computation).

        Uses a simple linear model for demonstration:
        loss = MSE(X @ w - y), grad = 2/n * X^T @ (X @ w - y)
        """
        if self.local_weights is None:
            weights = np.asarray(self.rng.standard_normal(data.shape[1]) * 0.01, dtype=float)
            self.local_weights = weights
        else:
            weights = self.local_weights

        predictions = data @ weights
        errors = predictions - labels
        gradients = 2.0 / len(labels) * (data.T @ errors)
        self.local_weights = weights - lr * gradients
        accumulated: np.ndarray[Any, Any] = gradients
        return accumulated

    def encode_gradients(
        self, gradients: np.ndarray[Any, Any]
    ) -> Tuple[List[np.ndarray[Any, Any]], str, float, float]:
        """Encode gradients as privatised SC bitstreams + commitment.

        Returns: (bitstreams, commitment_hash, g_min, g_max)
        """
        seeds = self.rng.integers(1, 65535, size=len(gradients), dtype=np.int64)
        bitstreams = self.encoder.encode(gradients, seeds, self.rng)

        # Commit to the privatised bitstreams
        concatenated = np.concatenate(bitstreams)
        nonce = CommitmentScheme.generate_nonce(self.rng)
        self.commitment = CommitmentScheme.commit(concatenated, nonce)

        return bitstreams, self.commitment, float(gradients.min()), float(gradients.max())

__post_init__()

Seed the client RNG deterministically from the client id.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
331
332
333
def __post_init__(self) -> None:
    """Seed the client RNG deterministically from the client id."""
    self.rng = np.random.default_rng(self.client_id * 7919 + 1)

local_train(data, labels, lr=0.01)

Simulate one local training step (gradient computation).

Uses a simple linear model for demonstration: loss = MSE(X @ w - y), grad = 2/n * X^T @ (X @ w - y)

Source code in src/sc_neurocore/federated/federated_sc.py
Python
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
def local_train(
    self, data: np.ndarray[Any, Any], labels: np.ndarray[Any, Any], lr: float = 0.01
) -> np.ndarray[Any, Any]:
    """Simulate one local training step (gradient computation).

    Uses a simple linear model for demonstration:
    loss = MSE(X @ w - y), grad = 2/n * X^T @ (X @ w - y)
    """
    if self.local_weights is None:
        weights = np.asarray(self.rng.standard_normal(data.shape[1]) * 0.01, dtype=float)
        self.local_weights = weights
    else:
        weights = self.local_weights

    predictions = data @ weights
    errors = predictions - labels
    gradients = 2.0 / len(labels) * (data.T @ errors)
    self.local_weights = weights - lr * gradients
    accumulated: np.ndarray[Any, Any] = gradients
    return accumulated

encode_gradients(gradients)

Encode gradients as privatised SC bitstreams + commitment.

Returns: (bitstreams, commitment_hash, g_min, g_max)

Source code in src/sc_neurocore/federated/federated_sc.py
Python
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
def encode_gradients(
    self, gradients: np.ndarray[Any, Any]
) -> Tuple[List[np.ndarray[Any, Any]], str, float, float]:
    """Encode gradients as privatised SC bitstreams + commitment.

    Returns: (bitstreams, commitment_hash, g_min, g_max)
    """
    seeds = self.rng.integers(1, 65535, size=len(gradients), dtype=np.int64)
    bitstreams = self.encoder.encode(gradients, seeds, self.rng)

    # Commit to the privatised bitstreams
    concatenated = np.concatenate(bitstreams)
    nonce = CommitmentScheme.generate_nonce(self.rng)
    self.commitment = CommitmentScheme.commit(concatenated, nonce)

    return bitstreams, self.commitment, float(gradients.min()), float(gradients.max())

FederatedAggregator dataclass

Secure aggregation server for federated SC bitstreams.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
@dataclass
class FederatedAggregator:
    """Secure aggregation server for federated SC bitstreams."""

    num_clients: int
    bitstream_length: int = 256

    def aggregate_bitstreams(
        self,
        client_bitstreams: List[List[np.ndarray[Any, Any]]],
        weights: Optional[List[float]] = None,
    ) -> List[np.ndarray[Any, Any]]:
        """Weighted majority-vote aggregation of SC bitstreams.

        For each gradient dimension, compute the weighted majority bit across
        all clients. If weights=None, uses uniform weights (standard FedAvg).
        """
        num_dims = len(client_bitstreams[0])
        n_clients = len(client_bitstreams)
        if weights is None:
            w = np.ones(n_clients) / n_clients
        else:
            w = np.array(weights)
            w = w / w.sum()

        aggregated = []
        for dim in range(num_dims):
            stacked = np.stack([c[dim] for c in client_bitstreams]).astype(np.float64)
            weighted_sum = w @ stacked
            agg_bs = (weighted_sum > 0.5).astype(np.uint8)
            aggregated.append(agg_bs)
        return aggregated

    def detect_outliers(
        self,
        client_bitstreams: List[List[np.ndarray[Any, Any]]],
        threshold: float = 0.3,
    ) -> List[bool]:
        """Detect malicious/outlier clients via cosine similarity.

        For each client, compute mean cosine similarity to all others.
        Clients with mean similarity < threshold are flagged.
        """
        n = len(client_bitstreams)
        if n < 2:
            return [False] * n

        # Flatten each client's update to a single vector
        flat = []
        for cbs in client_bitstreams:
            flat.append(np.concatenate(cbs).astype(np.float64))

        is_outlier = []
        for i in range(n):
            sims = []
            for j in range(n):
                if i == j:
                    continue
                dot = np.dot(flat[i], flat[j])
                na = np.linalg.norm(flat[i])
                nb = np.linalg.norm(flat[j])
                if na > 0 and nb > 0:
                    sims.append(dot / (na * nb))
                else:
                    sims.append(0.0)
            mean_sim = float(np.mean(sims))
            is_outlier.append(mean_sim < threshold)
        return is_outlier

    def verify_commitments(
        self,
        client_bitstreams: List[List[np.ndarray[Any, Any]]],
        commitments: List[str],
        nonces: Optional[List[bytes]] = None,
    ) -> List[bool]:
        """Verify that submitted bitstreams match commitments."""
        results = []
        for i, (bs_list, commitment) in enumerate(zip(client_bitstreams, commitments)):
            concatenated = np.concatenate(bs_list)
            nonce = nonces[i] if nonces else None
            results.append(CommitmentScheme.commit(concatenated, nonce) == commitment)
        return results

aggregate_bitstreams(client_bitstreams, weights=None)

Weighted majority-vote aggregation of SC bitstreams.

For each gradient dimension, compute the weighted majority bit across all clients. If weights=None, uses uniform weights (standard FedAvg).

Source code in src/sc_neurocore/federated/federated_sc.py
Python
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
def aggregate_bitstreams(
    self,
    client_bitstreams: List[List[np.ndarray[Any, Any]]],
    weights: Optional[List[float]] = None,
) -> List[np.ndarray[Any, Any]]:
    """Weighted majority-vote aggregation of SC bitstreams.

    For each gradient dimension, compute the weighted majority bit across
    all clients. If weights=None, uses uniform weights (standard FedAvg).
    """
    num_dims = len(client_bitstreams[0])
    n_clients = len(client_bitstreams)
    if weights is None:
        w = np.ones(n_clients) / n_clients
    else:
        w = np.array(weights)
        w = w / w.sum()

    aggregated = []
    for dim in range(num_dims):
        stacked = np.stack([c[dim] for c in client_bitstreams]).astype(np.float64)
        weighted_sum = w @ stacked
        agg_bs = (weighted_sum > 0.5).astype(np.uint8)
        aggregated.append(agg_bs)
    return aggregated

detect_outliers(client_bitstreams, threshold=0.3)

Detect malicious/outlier clients via cosine similarity.

For each client, compute mean cosine similarity to all others. Clients with mean similarity < threshold are flagged.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
def detect_outliers(
    self,
    client_bitstreams: List[List[np.ndarray[Any, Any]]],
    threshold: float = 0.3,
) -> List[bool]:
    """Detect malicious/outlier clients via cosine similarity.

    For each client, compute mean cosine similarity to all others.
    Clients with mean similarity < threshold are flagged.
    """
    n = len(client_bitstreams)
    if n < 2:
        return [False] * n

    # Flatten each client's update to a single vector
    flat = []
    for cbs in client_bitstreams:
        flat.append(np.concatenate(cbs).astype(np.float64))

    is_outlier = []
    for i in range(n):
        sims = []
        for j in range(n):
            if i == j:
                continue
            dot = np.dot(flat[i], flat[j])
            na = np.linalg.norm(flat[i])
            nb = np.linalg.norm(flat[j])
            if na > 0 and nb > 0:
                sims.append(dot / (na * nb))
            else:
                sims.append(0.0)
        mean_sim = float(np.mean(sims))
        is_outlier.append(mean_sim < threshold)
    return is_outlier

verify_commitments(client_bitstreams, commitments, nonces=None)

Verify that submitted bitstreams match commitments.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
446
447
448
449
450
451
452
453
454
455
456
457
458
def verify_commitments(
    self,
    client_bitstreams: List[List[np.ndarray[Any, Any]]],
    commitments: List[str],
    nonces: Optional[List[bytes]] = None,
) -> List[bool]:
    """Verify that submitted bitstreams match commitments."""
    results = []
    for i, (bs_list, commitment) in enumerate(zip(client_bitstreams, commitments)):
        concatenated = np.concatenate(bs_list)
        nonce = nonces[i] if nonces else None
        results.append(CommitmentScheme.commit(concatenated, nonce) == commitment)
    return results

ConvergenceTracker dataclass

Tracks loss/gradient-norm across federated rounds.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
@dataclass
class ConvergenceTracker:
    """Tracks loss/gradient-norm across federated rounds."""

    grad_norms: List[float] = field(default_factory=list)
    round_losses: List[float] = field(default_factory=list)

    def record(self, aggregated_gradient: np.ndarray[Any, Any]) -> None:
        """Record one round's gradient norm."""
        self.grad_norms.append(float(np.linalg.norm(aggregated_gradient)))

    def record_loss(self, loss: float) -> None:
        """Append a per-round loss value to the client history."""
        self.round_losses.append(loss)

    @property
    def converged(self) -> bool:
        """Simple heuristic: converged if last 5 grad norms < 0.01."""
        if len(self.grad_norms) < 5:
            return False
        return all(g < 0.01 for g in self.grad_norms[-5:])

    @property
    def trend(self) -> str:
        """Return trend direction."""
        if len(self.grad_norms) < 2:
            return "insufficient_data"
        if self.grad_norms[-1] < self.grad_norms[-2]:
            return "decreasing"
        elif self.grad_norms[-1] > self.grad_norms[-2]:
            return "increasing"
        return "stable"

converged property

Simple heuristic: converged if last 5 grad norms < 0.01.

trend property

Return trend direction.

record(aggregated_gradient)

Record one round's gradient norm.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
491
492
493
def record(self, aggregated_gradient: np.ndarray[Any, Any]) -> None:
    """Record one round's gradient norm."""
    self.grad_norms.append(float(np.linalg.norm(aggregated_gradient)))

record_loss(loss)

Append a per-round loss value to the client history.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
495
496
497
def record_loss(self, loss: float) -> None:
    """Append a per-round loss value to the client history."""
    self.round_losses.append(loss)

FederatedRound dataclass

Orchestrates one complete round of federated SC learning.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
@dataclass
class FederatedRound:
    """Orchestrates one complete round of federated SC learning."""

    clients: List[FederatedClient]
    aggregator: FederatedAggregator
    accountant: PrivacyAccountant
    round_number: int = 0
    convergence: ConvergenceTracker = field(default_factory=ConvergenceTracker)
    clip_norm: float = 0.0
    sampling_rate: float = 1.0
    audit_log: Any = None

    def run(
        self,
        data_per_client: List[np.ndarray[Any, Any]],
        labels_per_client: List[np.ndarray[Any, Any]],
        client_weights: Optional[List[float]] = None,
    ) -> Optional[np.ndarray[Any, Any]]:
        """Execute one federated round.

        Returns aggregated gradient if privacy budget allows, None otherwise.
        """
        if self.accountant.is_exhausted():
            return None

        self.round_number += 1

        # Client subsampling
        if self.sampling_rate < 1.0:
            rng = np.random.default_rng(self.round_number)
            active = poisson_subsample(self.clients, self.sampling_rate, rng)
            active_indices = [self.clients.index(c) for c in active]
        else:
            active = self.clients
            active_indices = list(range(len(self.clients)))

        all_bitstreams = []
        all_commitments = []
        g_mins, g_maxs = [], []

        for idx, client in zip(active_indices, active):
            gradients = client.local_train(data_per_client[idx], labels_per_client[idx])
            if self.clip_norm > 0:
                gradients = clip_gradients(gradients, self.clip_norm)
            bitstreams, commitment, g_min, g_max = client.encode_gradients(gradients)
            all_bitstreams.append(bitstreams)
            all_commitments.append(commitment)
            g_mins.append(g_min)
            g_maxs.append(g_max)

        # Track privacy budget
        dp_mech = active[0].encoder.dp
        bl = active[0].encoder.bitstream_length
        self.accountant.consume_round(dp_mech, bl)

        # Select weights for active clients
        if client_weights is not None:
            active_weights = [client_weights[i] for i in active_indices]
        else:
            active_weights = None

        # Aggregate
        aggregated_bs = self.aggregator.aggregate_bitstreams(all_bitstreams, weights=active_weights)

        # Decode using global min/max range
        global_min = min(g_mins)
        global_max = max(g_maxs)
        aggregated_grads = active[0].encoder.decode(aggregated_bs, global_min, global_max)

        # Track convergence
        self.convergence.record(aggregated_grads)

        # Audit log
        if self.audit_log is not None:
            self.audit_log.log_round(
                round_number=self.round_number,
                num_active=len(active),
                epsilon_consumed=self.accountant.current_epsilon(),
                grad_norm=float(np.linalg.norm(aggregated_grads)),
            )

        return aggregated_grads

    def status(self) -> Dict[str, Any]:
        """Return current federated learning status."""
        return {
            "round": self.round_number,
            "epsilon_consumed": self.accountant.current_epsilon(),
            "epsilon_remaining": self.accountant.remaining_epsilon(),
            "rounds_consumed": self.accountant.rounds_consumed,
            "budget_exhausted": self.accountant.is_exhausted(),
            "converged": self.convergence.converged,
            "trend": self.convergence.trend,
        }

run(data_per_client, labels_per_client, client_weights=None)

Execute one federated round.

Returns aggregated gradient if privacy budget allows, None otherwise.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
def run(
    self,
    data_per_client: List[np.ndarray[Any, Any]],
    labels_per_client: List[np.ndarray[Any, Any]],
    client_weights: Optional[List[float]] = None,
) -> Optional[np.ndarray[Any, Any]]:
    """Execute one federated round.

    Returns aggregated gradient if privacy budget allows, None otherwise.
    """
    if self.accountant.is_exhausted():
        return None

    self.round_number += 1

    # Client subsampling
    if self.sampling_rate < 1.0:
        rng = np.random.default_rng(self.round_number)
        active = poisson_subsample(self.clients, self.sampling_rate, rng)
        active_indices = [self.clients.index(c) for c in active]
    else:
        active = self.clients
        active_indices = list(range(len(self.clients)))

    all_bitstreams = []
    all_commitments = []
    g_mins, g_maxs = [], []

    for idx, client in zip(active_indices, active):
        gradients = client.local_train(data_per_client[idx], labels_per_client[idx])
        if self.clip_norm > 0:
            gradients = clip_gradients(gradients, self.clip_norm)
        bitstreams, commitment, g_min, g_max = client.encode_gradients(gradients)
        all_bitstreams.append(bitstreams)
        all_commitments.append(commitment)
        g_mins.append(g_min)
        g_maxs.append(g_max)

    # Track privacy budget
    dp_mech = active[0].encoder.dp
    bl = active[0].encoder.bitstream_length
    self.accountant.consume_round(dp_mech, bl)

    # Select weights for active clients
    if client_weights is not None:
        active_weights = [client_weights[i] for i in active_indices]
    else:
        active_weights = None

    # Aggregate
    aggregated_bs = self.aggregator.aggregate_bitstreams(all_bitstreams, weights=active_weights)

    # Decode using global min/max range
    global_min = min(g_mins)
    global_max = max(g_maxs)
    aggregated_grads = active[0].encoder.decode(aggregated_bs, global_min, global_max)

    # Track convergence
    self.convergence.record(aggregated_grads)

    # Audit log
    if self.audit_log is not None:
        self.audit_log.log_round(
            round_number=self.round_number,
            num_active=len(active),
            epsilon_consumed=self.accountant.current_epsilon(),
            grad_norm=float(np.linalg.norm(aggregated_grads)),
        )

    return aggregated_grads

status()

Return current federated learning status.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
605
606
607
608
609
610
611
612
613
614
615
def status(self) -> Dict[str, Any]:
    """Return current federated learning status."""
    return {
        "round": self.round_number,
        "epsilon_consumed": self.accountant.current_epsilon(),
        "epsilon_remaining": self.accountant.remaining_epsilon(),
        "rounds_consumed": self.accountant.rounds_consumed,
        "budget_exhausted": self.accountant.is_exhausted(),
        "converged": self.convergence.converged,
        "trend": self.convergence.trend,
    }

DPCertificate dataclass

Exportable privacy proof document for regulatory compliance.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
@dataclass
class DPCertificate:
    """Exportable privacy proof document for regulatory compliance."""

    mechanism: str
    epsilon: float
    delta: float
    rounds: int
    bitstream_length: int
    composition_method: str
    accountant_state: Dict[str, Any] = field(default_factory=dict)

    @classmethod
    def from_accountant(
        cls,
        accountant: PrivacyAccountant,
        mechanism: DPMechanism,
        bitstream_length: int,
    ) -> DPCertificate:
        """Build a differential-privacy certificate from an accountant."""
        return cls(
            mechanism="bitstream_flip_rr",
            epsilon=accountant.current_epsilon(),
            delta=accountant.target_delta,
            rounds=accountant.rounds_consumed,
            bitstream_length=bitstream_length,
            composition_method="renyi_dp",
            accountant_state={
                "rdp_budget": accountant.rdp_budget,
                "alpha": accountant.alpha,
                "target_epsilon": accountant.target_epsilon,
                "flip_probability": mechanism.flip_probability,
            },
        )

    def to_dict(self) -> Dict[str, Any]:
        """Return the certificate as a serialisable dictionary."""
        return {
            "mechanism": self.mechanism,
            "epsilon": self.epsilon,
            "delta": self.delta,
            "rounds": self.rounds,
            "bitstream_length": self.bitstream_length,
            "composition_method": self.composition_method,
            "accountant_state": self.accountant_state,
            "compliant": self.epsilon <= self.accountant_state.get("target_epsilon", float("inf")),
        }

    @property
    def is_compliant(self) -> bool:
        """Return whether the spent epsilon is within the target budget."""
        return bool(self.epsilon <= self.accountant_state.get("target_epsilon", float("inf")))

is_compliant property

Return whether the spent epsilon is within the target budget.

from_accountant(accountant, mechanism, bitstream_length) classmethod

Build a differential-privacy certificate from an accountant.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
@classmethod
def from_accountant(
    cls,
    accountant: PrivacyAccountant,
    mechanism: DPMechanism,
    bitstream_length: int,
) -> DPCertificate:
    """Build a differential-privacy certificate from an accountant."""
    return cls(
        mechanism="bitstream_flip_rr",
        epsilon=accountant.current_epsilon(),
        delta=accountant.target_delta,
        rounds=accountant.rounds_consumed,
        bitstream_length=bitstream_length,
        composition_method="renyi_dp",
        accountant_state={
            "rdp_budget": accountant.rdp_budget,
            "alpha": accountant.alpha,
            "target_epsilon": accountant.target_epsilon,
            "flip_probability": mechanism.flip_probability,
        },
    )

to_dict()

Return the certificate as a serialisable dictionary.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
656
657
658
659
660
661
662
663
664
665
666
667
def to_dict(self) -> Dict[str, Any]:
    """Return the certificate as a serialisable dictionary."""
    return {
        "mechanism": self.mechanism,
        "epsilon": self.epsilon,
        "delta": self.delta,
        "rounds": self.rounds,
        "bitstream_length": self.bitstream_length,
        "composition_method": self.composition_method,
        "accountant_state": self.accountant_state,
        "compliant": self.epsilon <= self.accountant_state.get("target_epsilon", float("inf")),
    }

AdaptiveEpsilonScheduler dataclass

Dynamically adjust ε per round based on convergence state.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
@dataclass
class AdaptiveEpsilonScheduler:
    """Dynamically adjust ε per round based on convergence state."""

    base_epsilon: float = 1.0
    decay_rate: float = 0.95
    min_epsilon: float = 0.1
    current_epsilon: float = 0.0

    def __post_init__(self) -> None:
        """Initialise the current epsilon to the base privacy budget."""
        self.current_epsilon = self.base_epsilon

    def step(self, converging: bool) -> float:
        """Compute ε for the next round.

        If converging: tighten privacy (reduce ε → more noise).
        If diverging: loosen privacy (increase ε → less noise).
        """
        if converging:
            self.current_epsilon = max(
                self.min_epsilon,
                self.current_epsilon * self.decay_rate,
            )
        else:
            self.current_epsilon = min(
                self.base_epsilon,
                self.current_epsilon / self.decay_rate,
            )
        return self.current_epsilon

__post_init__()

Initialise the current epsilon to the base privacy budget.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
713
714
715
def __post_init__(self) -> None:
    """Initialise the current epsilon to the base privacy budget."""
    self.current_epsilon = self.base_epsilon

step(converging)

Compute ε for the next round.

If converging: tighten privacy (reduce ε → more noise). If diverging: loosen privacy (increase ε → less noise).

Source code in src/sc_neurocore/federated/federated_sc.py
Python
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
def step(self, converging: bool) -> float:
    """Compute ε for the next round.

    If converging: tighten privacy (reduce ε → more noise).
    If diverging: loosen privacy (increase ε → less noise).
    """
    if converging:
        self.current_epsilon = max(
            self.min_epsilon,
            self.current_epsilon * self.decay_rate,
        )
    else:
        self.current_epsilon = min(
            self.base_epsilon,
            self.current_epsilon / self.decay_rate,
        )
    return self.current_epsilon

ErrorFeedback dataclass

Error feedback for gradient sparsification.

Accumulates the residual from sparsification to avoid information loss over rounds.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
@dataclass
class ErrorFeedback:
    """Error feedback for gradient sparsification.

    Accumulates the residual from sparsification to avoid information
    loss over rounds.
    """

    residual: Optional[np.ndarray[Any, Any]] = None

    def accumulate(self, gradients: np.ndarray[Any, Any]) -> np.ndarray[Any, Any]:
        """Add residual from previous round."""
        if self.residual is not None:
            compensated: np.ndarray[Any, Any] = gradients + self.residual
            return compensated
        return gradients.copy()

    def update(self, original: np.ndarray[Any, Any], sparse: np.ndarray[Any, Any]) -> None:
        """Store the residual (what was lost to sparsification)."""
        self.residual = original - sparse

accumulate(gradients)

Add residual from previous round.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
818
819
820
821
822
823
def accumulate(self, gradients: np.ndarray[Any, Any]) -> np.ndarray[Any, Any]:
    """Add residual from previous round."""
    if self.residual is not None:
        compensated: np.ndarray[Any, Any] = gradients + self.residual
        return compensated
    return gradients.copy()

update(original, sparse)

Store the residual (what was lost to sparsification).

Source code in src/sc_neurocore/federated/federated_sc.py
Python
825
826
827
def update(self, original: np.ndarray[Any, Any], sparse: np.ndarray[Any, Any]) -> None:
    """Store the residual (what was lost to sparsification)."""
    self.residual = original - sparse

AuditEntry dataclass

Single audit log entry for one federated round.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
@dataclass
class AuditEntry:
    """Single audit log entry for one federated round."""

    round_number: int
    num_active_clients: int
    epsilon_consumed: float
    grad_norm: float
    timestamp: float = 0.0

    def __post_init__(self) -> None:
        """Stamp the audit entry with the current wall-clock time."""
        import time

        if self.timestamp == 0.0:
            self.timestamp = time.time()

__post_init__()

Stamp the audit entry with the current wall-clock time.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
862
863
864
865
866
867
def __post_init__(self) -> None:
    """Stamp the audit entry with the current wall-clock time."""
    import time

    if self.timestamp == 0.0:
        self.timestamp = time.time()

AuditLog dataclass

Per-round provenance record for regulatory compliance.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
@dataclass
class AuditLog:
    """Per-round provenance record for regulatory compliance."""

    entries: List[AuditEntry] = field(default_factory=list)

    def log_round(
        self,
        round_number: int,
        num_active: int,
        epsilon_consumed: float,
        grad_norm: float,
    ) -> None:
        """Record one federated round in the audit log."""
        self.entries.append(
            AuditEntry(
                round_number=round_number,
                num_active_clients=num_active,
                epsilon_consumed=epsilon_consumed,
                grad_norm=grad_norm,
            )
        )

    def to_list(self) -> List[Dict[str, Any]]:
        """Return the audit log as a list of serialisable dictionaries."""
        return [
            {
                "round": e.round_number,
                "active_clients": e.num_active_clients,
                "epsilon": e.epsilon_consumed,
                "grad_norm": e.grad_norm,
                "timestamp": e.timestamp,
            }
            for e in self.entries
        ]

    @property
    def total_rounds(self) -> int:
        """Return the number of logged federated rounds."""
        return len(self.entries)

    @property
    def max_epsilon(self) -> float:
        """Return the maximum epsilon recorded across all rounds."""
        if not self.entries:
            return 0.0
        return max(e.epsilon_consumed for e in self.entries)

total_rounds property

Return the number of logged federated rounds.

max_epsilon property

Return the maximum epsilon recorded across all rounds.

log_round(round_number, num_active, epsilon_consumed, grad_norm)

Record one federated round in the audit log.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
def log_round(
    self,
    round_number: int,
    num_active: int,
    epsilon_consumed: float,
    grad_norm: float,
) -> None:
    """Record one federated round in the audit log."""
    self.entries.append(
        AuditEntry(
            round_number=round_number,
            num_active_clients=num_active,
            epsilon_consumed=epsilon_consumed,
            grad_norm=grad_norm,
        )
    )

to_list()

Return the audit log as a list of serialisable dictionaries.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
893
894
895
896
897
898
899
900
901
902
903
904
def to_list(self) -> List[Dict[str, Any]]:
    """Return the audit log as a list of serialisable dictionaries."""
    return [
        {
            "round": e.round_number,
            "active_clients": e.num_active_clients,
            "epsilon": e.epsilon_consumed,
            "grad_norm": e.grad_norm,
            "timestamp": e.timestamp,
        }
        for e in self.entries
    ]

lfsr_encode(value, seed, length)

Encode a probability [0,1] into a packed bitstream using LFSR-16.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
48
49
50
51
52
53
54
55
56
57
58
def lfsr_encode(value: float, seed: int, length: int) -> np.ndarray[Any, Any]:
    """Encode a probability [0,1] into a packed bitstream using LFSR-16."""
    threshold = int(np.clip(value, 0.0, 1.0) * 65535)
    reg = seed & 0xFFFF
    if reg == 0:
        reg = 1
    bits = np.zeros(length, dtype=np.uint8)
    for i in range(length):
        bits[i] = 1 if reg < threshold else 0
        reg = _lfsr16_step(reg)
    return bits

bitstream_probability(bits)

Estimate probability from a bitstream.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
61
62
63
64
def bitstream_probability(bits: np.ndarray[Any, Any]) -> float:
    """Estimate probability from a bitstream."""
    n = len(bits)
    return float(np.sum(bits)) / n if n > 0 else 0.0

clip_gradients(gradients, max_norm)

L2 gradient clipping for formal DP sensitivity bounds.

Clips the gradient vector so its L2 norm ≤ max_norm. This is required for provable (ε,δ)-DP guarantees.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
117
118
119
120
121
122
123
124
125
126
def clip_gradients(gradients: np.ndarray[Any, Any], max_norm: float) -> np.ndarray[Any, Any]:
    """L2 gradient clipping for formal DP sensitivity bounds.

    Clips the gradient vector so its L2 norm ≤ max_norm.
    This is required for provable (ε,δ)-DP guarantees.
    """
    l2 = float(np.linalg.norm(gradients))
    if l2 > max_norm and l2 > 0:
        return gradients * (max_norm / l2)
    return gradients.copy()

sparsify_topk(gradients, k)

Top-k gradient sparsification.

Returns (sparse_gradients, mask) where only k largest-magnitude entries are non-zero. Reduces communication cost proportionally.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def sparsify_topk(
    gradients: np.ndarray[Any, Any], k: int
) -> Tuple[np.ndarray[Any, Any], np.ndarray[Any, Any]]:
    """Top-k gradient sparsification.

    Returns (sparse_gradients, mask) where only k largest-magnitude
    entries are non-zero.  Reduces communication cost proportionally.
    """
    k = min(k, len(gradients))
    indices = np.argsort(np.abs(gradients))[-k:]
    mask = np.zeros(len(gradients), dtype=np.uint8)
    mask[indices] = 1
    sparse = np.zeros_like(gradients)
    sparse[indices] = gradients[indices]
    return sparse, mask

poisson_subsample(clients, sampling_rate, rng)

Poisson subsampling of clients for privacy amplification.

Each client is independently included with probability sampling_rate. This provides privacy amplification by a factor of O(sampling_rate).

Source code in src/sc_neurocore/federated/federated_sc.py
Python
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
def poisson_subsample(
    clients: List[FederatedClient],
    sampling_rate: float,
    rng: np.random.Generator,
) -> List[FederatedClient]:
    """Poisson subsampling of clients for privacy amplification.

    Each client is independently included with probability ``sampling_rate``.
    This provides privacy amplification by a factor of O(sampling_rate).
    """
    selected = []
    for c in clients:
        if rng.random() < sampling_rate:
            selected.append(c)
    return selected if selected else [clients[0]]  # at least one

stochastic_quantize(gradients, levels, rng)

Stochastic quantization to reduce communication bits.

Quantizes each gradient to one of levels levels with unbiased randomized rounding. E[Q(g)] = g.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
def stochastic_quantize(
    gradients: np.ndarray[Any, Any],
    levels: int,
    rng: np.random.Generator,
) -> np.ndarray[Any, Any]:
    """Stochastic quantization to reduce communication bits.

    Quantizes each gradient to one of ``levels`` levels with
    unbiased randomized rounding. E[Q(g)] = g.
    """
    g_min, g_max = gradients.min(), gradients.max()
    span = g_max - g_min
    if span < 1e-12:
        return gradients.copy()
    normalised = (gradients - g_min) / span * (levels - 1)
    lower = np.floor(normalised).astype(np.int32)
    prob = normalised - lower
    upper = lower + (rng.random(len(gradients)) < prob).astype(np.int32)
    upper = np.clip(upper, 0, levels - 1)
    dequantized: np.ndarray[Any, Any] = upper.astype(np.float64) / (levels - 1) * span + g_min
    return dequantized

krum_select(client_vectors, num_byzantine=1)

Multi-Krum selection: find the vector closest to most others.

Returns the index of the most "central" client update. Tolerates up to num_byzantine malicious clients.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
def krum_select(
    client_vectors: List[np.ndarray[Any, Any]],
    num_byzantine: int = 1,
) -> int:
    """Multi-Krum selection: find the vector closest to most others.

    Returns the index of the most "central" client update.
    Tolerates up to ``num_byzantine`` malicious clients.
    """
    n = len(client_vectors)
    k = n - num_byzantine - 2
    if k < 1:
        k = 1

    scores = []
    for i in range(n):
        dists = []
        for j in range(n):
            if i == j:
                continue
            dists.append(float(np.linalg.norm(client_vectors[i] - client_vectors[j]) ** 2))
        dists.sort()
        scores.append(sum(dists[:k]))

    return int(np.argmin(scores))

trimmed_mean(client_vectors, trim_fraction=0.1)

Coordinate-wise trimmed mean aggregation.

Removes the top and bottom trim_fraction of values per coordinate before averaging. Robust to Byzantine clients.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
def trimmed_mean(
    client_vectors: List[np.ndarray[Any, Any]],
    trim_fraction: float = 0.1,
) -> np.ndarray[Any, Any]:
    """Coordinate-wise trimmed mean aggregation.

    Removes the top and bottom ``trim_fraction`` of values per
    coordinate before averaging. Robust to Byzantine clients.
    """
    stacked = np.stack(client_vectors)
    n = stacked.shape[0]
    trim_count = max(1, int(n * trim_fraction))

    sorted_vals = np.sort(stacked, axis=0)
    trimmed = sorted_vals[trim_count : n - trim_count, :]
    if trimmed.shape[0] == 0:
        full_mean: np.ndarray[Any, Any] = np.mean(stacked, axis=0)
        return full_mean
    trimmed_mean: np.ndarray[Any, Any] = np.mean(trimmed, axis=0)
    return trimmed_mean

fedprox_gradient(gradients, local_weights, global_weights, mu=0.01)

Add FedProx proximal term for non-IID robustness.

grad_proximal = grad + μ * (w_local - w_global)

Source code in src/sc_neurocore/federated/federated_sc.py
Python
791
792
793
794
795
796
797
798
799
800
801
802
def fedprox_gradient(
    gradients: np.ndarray[Any, Any],
    local_weights: np.ndarray[Any, Any],
    global_weights: np.ndarray[Any, Any],
    mu: float = 0.01,
) -> np.ndarray[Any, Any]:
    """Add FedProx proximal term for non-IID robustness.

    grad_proximal = grad + μ * (w_local - w_global)
    """
    corrected: np.ndarray[Any, Any] = gradients + mu * (local_weights - global_weights)
    return corrected

amplified_epsilon(base_epsilon, sampling_rate)

Compute privacy amplification from Poisson subsampling.

Uses the Balle et al. (2018) bound: ε' ≈ log(1 + q(e^ε - 1)) where q is the sampling rate.

Source code in src/sc_neurocore/federated/federated_sc.py
Python
833
834
835
836
837
838
839
840
841
842
843
844
845
846
def amplified_epsilon(
    base_epsilon: float,
    sampling_rate: float,
) -> float:
    """Compute privacy amplification from Poisson subsampling.

    Uses the Balle et al. (2018) bound: ε' ≈ log(1 + q(e^ε - 1))
    where q is the sampling rate.
    """
    if sampling_rate >= 1.0:
        return base_epsilon
    if sampling_rate <= 0.0:
        return 0.0
    return math.log(1 + sampling_rate * (math.exp(base_epsilon) - 1))