flwr-nightly 1.8.0.dev20240314__py3-none-any.whl → 1.15.0.dev20250114__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- flwr/cli/app.py +16 -2
- flwr/cli/build.py +181 -0
- flwr/cli/cli_user_auth_interceptor.py +90 -0
- flwr/cli/config_utils.py +343 -0
- flwr/cli/example.py +4 -1
- flwr/cli/install.py +253 -0
- flwr/cli/log.py +182 -0
- flwr/{server/superlink/state → cli/login}/__init__.py +4 -10
- flwr/cli/login/login.py +88 -0
- flwr/cli/ls.py +327 -0
- flwr/cli/new/__init__.py +1 -0
- flwr/cli/new/new.py +210 -66
- flwr/cli/new/templates/app/.gitignore.tpl +163 -0
- flwr/cli/new/templates/app/LICENSE.tpl +202 -0
- flwr/cli/new/templates/app/README.baseline.md.tpl +127 -0
- flwr/cli/new/templates/app/README.flowertune.md.tpl +66 -0
- flwr/cli/new/templates/app/README.md.tpl +16 -32
- flwr/cli/new/templates/app/code/__init__.baseline.py.tpl +1 -0
- flwr/cli/new/templates/app/code/__init__.py.tpl +1 -1
- flwr/cli/new/templates/app/code/client.baseline.py.tpl +58 -0
- flwr/cli/new/templates/app/code/client.huggingface.py.tpl +55 -0
- flwr/cli/new/templates/app/code/client.jax.py.tpl +50 -0
- flwr/cli/new/templates/app/code/client.mlx.py.tpl +73 -0
- flwr/cli/new/templates/app/code/client.numpy.py.tpl +7 -7
- flwr/cli/new/templates/app/code/client.pytorch.py.tpl +30 -21
- flwr/cli/new/templates/app/code/client.sklearn.py.tpl +63 -0
- flwr/cli/new/templates/app/code/client.tensorflow.py.tpl +57 -1
- flwr/cli/new/templates/app/code/dataset.baseline.py.tpl +36 -0
- flwr/cli/new/templates/app/code/flwr_tune/__init__.py +15 -0
- flwr/cli/new/templates/app/code/flwr_tune/client_app.py.tpl +126 -0
- flwr/cli/new/templates/app/code/flwr_tune/dataset.py.tpl +87 -0
- flwr/cli/new/templates/app/code/flwr_tune/models.py.tpl +78 -0
- flwr/cli/new/templates/app/code/flwr_tune/server_app.py.tpl +94 -0
- flwr/cli/new/templates/app/code/flwr_tune/strategy.py.tpl +83 -0
- flwr/cli/new/templates/app/code/model.baseline.py.tpl +80 -0
- flwr/cli/new/templates/app/code/server.baseline.py.tpl +46 -0
- flwr/cli/new/templates/app/code/server.huggingface.py.tpl +38 -0
- flwr/cli/new/templates/app/code/server.jax.py.tpl +26 -0
- flwr/cli/new/templates/app/code/server.mlx.py.tpl +31 -0
- flwr/cli/new/templates/app/code/server.numpy.py.tpl +22 -9
- flwr/cli/new/templates/app/code/server.pytorch.py.tpl +21 -18
- flwr/cli/new/templates/app/code/server.sklearn.py.tpl +36 -0
- flwr/cli/new/templates/app/code/server.tensorflow.py.tpl +29 -1
- flwr/cli/new/templates/app/code/strategy.baseline.py.tpl +1 -0
- flwr/cli/new/templates/app/code/task.huggingface.py.tpl +102 -0
- flwr/cli/new/templates/app/code/task.jax.py.tpl +57 -0
- flwr/cli/new/templates/app/code/task.mlx.py.tpl +102 -0
- flwr/cli/new/templates/app/code/task.numpy.py.tpl +7 -0
- flwr/cli/new/templates/app/code/task.pytorch.py.tpl +29 -24
- flwr/cli/new/templates/app/code/task.sklearn.py.tpl +67 -0
- flwr/cli/new/templates/app/code/task.tensorflow.py.tpl +53 -0
- flwr/cli/new/templates/app/code/utils.baseline.py.tpl +1 -0
- flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +138 -0
- flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +68 -0
- flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +46 -0
- flwr/cli/new/templates/app/pyproject.jax.toml.tpl +35 -0
- flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +39 -0
- flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +25 -12
- flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +29 -14
- flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +35 -0
- flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +29 -14
- flwr/cli/run/__init__.py +1 -0
- flwr/cli/run/run.py +212 -34
- flwr/cli/stop.py +130 -0
- flwr/cli/utils.py +240 -5
- flwr/client/__init__.py +3 -2
- flwr/client/app.py +432 -255
- flwr/client/client.py +1 -11
- flwr/client/client_app.py +74 -13
- flwr/client/clientapp/__init__.py +22 -0
- flwr/client/clientapp/app.py +259 -0
- flwr/client/clientapp/clientappio_servicer.py +244 -0
- flwr/client/clientapp/utils.py +115 -0
- flwr/client/dpfedavg_numpy_client.py +7 -8
- flwr/client/grpc_adapter_client/__init__.py +15 -0
- flwr/client/grpc_adapter_client/connection.py +98 -0
- flwr/client/grpc_client/connection.py +21 -7
- flwr/client/grpc_rere_client/__init__.py +1 -1
- flwr/client/grpc_rere_client/client_interceptor.py +176 -0
- flwr/client/grpc_rere_client/connection.py +163 -56
- flwr/client/grpc_rere_client/grpc_adapter.py +167 -0
- flwr/client/heartbeat.py +74 -0
- flwr/client/message_handler/__init__.py +1 -1
- flwr/client/message_handler/message_handler.py +10 -11
- flwr/client/mod/__init__.py +5 -5
- flwr/client/mod/centraldp_mods.py +4 -2
- flwr/client/mod/comms_mods.py +5 -4
- flwr/client/mod/localdp_mod.py +10 -5
- flwr/client/mod/secure_aggregation/__init__.py +1 -1
- flwr/client/mod/secure_aggregation/secaggplus_mod.py +26 -26
- flwr/client/mod/utils.py +2 -4
- flwr/client/nodestate/__init__.py +26 -0
- flwr/client/nodestate/in_memory_nodestate.py +38 -0
- flwr/client/nodestate/nodestate.py +31 -0
- flwr/client/nodestate/nodestate_factory.py +38 -0
- flwr/client/numpy_client.py +8 -31
- flwr/client/rest_client/__init__.py +1 -1
- flwr/client/rest_client/connection.py +199 -176
- flwr/client/run_info_store.py +112 -0
- flwr/client/supernode/__init__.py +24 -0
- flwr/client/supernode/app.py +321 -0
- flwr/client/typing.py +1 -0
- flwr/common/__init__.py +17 -11
- flwr/common/address.py +47 -3
- flwr/common/args.py +153 -0
- flwr/common/auth_plugin/__init__.py +24 -0
- flwr/common/auth_plugin/auth_plugin.py +121 -0
- flwr/common/config.py +243 -0
- flwr/common/constant.py +132 -1
- flwr/common/context.py +32 -2
- flwr/common/date.py +22 -4
- flwr/common/differential_privacy.py +2 -2
- flwr/common/dp.py +2 -4
- flwr/common/exit_handlers.py +3 -3
- flwr/common/grpc.py +164 -5
- flwr/common/logger.py +230 -12
- flwr/common/message.py +191 -106
- flwr/common/object_ref.py +179 -44
- flwr/common/pyproject.py +1 -0
- flwr/common/record/__init__.py +2 -1
- flwr/common/record/configsrecord.py +58 -18
- flwr/common/record/metricsrecord.py +57 -17
- flwr/common/record/parametersrecord.py +88 -20
- flwr/common/record/recordset.py +153 -30
- flwr/common/record/typeddict.py +30 -55
- flwr/common/recordset_compat.py +31 -12
- flwr/common/retry_invoker.py +123 -30
- flwr/common/secure_aggregation/__init__.py +1 -1
- flwr/common/secure_aggregation/crypto/__init__.py +1 -1
- flwr/common/secure_aggregation/crypto/shamir.py +11 -11
- flwr/common/secure_aggregation/crypto/symmetric_encryption.py +68 -4
- flwr/common/secure_aggregation/ndarrays_arithmetic.py +17 -17
- flwr/common/secure_aggregation/quantization.py +8 -8
- flwr/common/secure_aggregation/secaggplus_constants.py +1 -1
- flwr/common/secure_aggregation/secaggplus_utils.py +10 -12
- flwr/common/serde.py +298 -19
- flwr/common/telemetry.py +65 -29
- flwr/common/typing.py +120 -19
- flwr/common/version.py +17 -3
- flwr/proto/clientappio_pb2.py +45 -0
- flwr/proto/clientappio_pb2.pyi +132 -0
- flwr/proto/clientappio_pb2_grpc.py +135 -0
- flwr/proto/clientappio_pb2_grpc.pyi +53 -0
- flwr/proto/exec_pb2.py +62 -0
- flwr/proto/exec_pb2.pyi +212 -0
- flwr/proto/exec_pb2_grpc.py +237 -0
- flwr/proto/exec_pb2_grpc.pyi +93 -0
- flwr/proto/fab_pb2.py +31 -0
- flwr/proto/fab_pb2.pyi +65 -0
- flwr/proto/fab_pb2_grpc.py +4 -0
- flwr/proto/fab_pb2_grpc.pyi +4 -0
- flwr/proto/fleet_pb2.py +42 -23
- flwr/proto/fleet_pb2.pyi +123 -1
- flwr/proto/fleet_pb2_grpc.py +170 -0
- flwr/proto/fleet_pb2_grpc.pyi +61 -0
- flwr/proto/grpcadapter_pb2.py +32 -0
- flwr/proto/grpcadapter_pb2.pyi +43 -0
- flwr/proto/grpcadapter_pb2_grpc.py +66 -0
- flwr/proto/grpcadapter_pb2_grpc.pyi +24 -0
- flwr/proto/log_pb2.py +29 -0
- flwr/proto/log_pb2.pyi +39 -0
- flwr/proto/log_pb2_grpc.py +4 -0
- flwr/proto/log_pb2_grpc.pyi +4 -0
- flwr/proto/message_pb2.py +41 -0
- flwr/proto/message_pb2.pyi +128 -0
- flwr/proto/message_pb2_grpc.py +4 -0
- flwr/proto/message_pb2_grpc.pyi +4 -0
- flwr/proto/node_pb2.py +1 -1
- flwr/proto/recordset_pb2.py +35 -33
- flwr/proto/recordset_pb2.pyi +40 -14
- flwr/proto/run_pb2.py +64 -0
- flwr/proto/run_pb2.pyi +268 -0
- flwr/proto/run_pb2_grpc.py +4 -0
- flwr/proto/run_pb2_grpc.pyi +4 -0
- flwr/proto/serverappio_pb2.py +52 -0
- flwr/proto/{driver_pb2.pyi → serverappio_pb2.pyi} +62 -20
- flwr/proto/serverappio_pb2_grpc.py +410 -0
- flwr/proto/serverappio_pb2_grpc.pyi +160 -0
- flwr/proto/simulationio_pb2.py +38 -0
- flwr/proto/simulationio_pb2.pyi +65 -0
- flwr/proto/simulationio_pb2_grpc.py +239 -0
- flwr/proto/simulationio_pb2_grpc.pyi +94 -0
- flwr/proto/task_pb2.py +7 -8
- flwr/proto/task_pb2.pyi +8 -5
- flwr/proto/transport_pb2.py +8 -8
- flwr/proto/transport_pb2.pyi +9 -6
- flwr/server/__init__.py +2 -10
- flwr/server/app.py +579 -402
- flwr/server/client_manager.py +8 -6
- flwr/server/compat/app.py +6 -62
- flwr/server/compat/app_utils.py +14 -8
- flwr/server/compat/driver_client_proxy.py +25 -58
- flwr/server/compat/legacy_context.py +5 -4
- flwr/server/driver/__init__.py +2 -0
- flwr/server/driver/driver.py +36 -131
- flwr/server/driver/grpc_driver.py +217 -81
- flwr/server/driver/inmemory_driver.py +182 -0
- flwr/server/history.py +28 -29
- flwr/server/run_serverapp.py +15 -126
- flwr/server/server.py +50 -44
- flwr/server/server_app.py +59 -10
- flwr/server/serverapp/__init__.py +22 -0
- flwr/server/serverapp/app.py +256 -0
- flwr/server/serverapp_components.py +52 -0
- flwr/server/strategy/__init__.py +2 -2
- flwr/server/strategy/aggregate.py +37 -23
- flwr/server/strategy/bulyan.py +9 -9
- flwr/server/strategy/dp_adaptive_clipping.py +25 -25
- flwr/server/strategy/dp_fixed_clipping.py +23 -22
- flwr/server/strategy/dpfedavg_adaptive.py +8 -8
- flwr/server/strategy/dpfedavg_fixed.py +13 -12
- flwr/server/strategy/fault_tolerant_fedavg.py +11 -11
- flwr/server/strategy/fedadagrad.py +9 -9
- flwr/server/strategy/fedadam.py +20 -10
- flwr/server/strategy/fedavg.py +16 -16
- flwr/server/strategy/fedavg_android.py +17 -17
- flwr/server/strategy/fedavgm.py +9 -9
- flwr/server/strategy/fedmedian.py +5 -5
- flwr/server/strategy/fedopt.py +6 -6
- flwr/server/strategy/fedprox.py +7 -7
- flwr/server/strategy/fedtrimmedavg.py +8 -8
- flwr/server/strategy/fedxgb_bagging.py +12 -12
- flwr/server/strategy/fedxgb_cyclic.py +10 -10
- flwr/server/strategy/fedxgb_nn_avg.py +6 -6
- flwr/server/strategy/fedyogi.py +9 -9
- flwr/server/strategy/krum.py +9 -9
- flwr/server/strategy/qfedavg.py +16 -16
- flwr/server/strategy/strategy.py +10 -10
- flwr/server/superlink/driver/__init__.py +2 -2
- flwr/server/superlink/driver/serverappio_grpc.py +61 -0
- flwr/server/superlink/driver/serverappio_servicer.py +363 -0
- flwr/server/superlink/ffs/__init__.py +24 -0
- flwr/server/superlink/ffs/disk_ffs.py +108 -0
- flwr/server/superlink/ffs/ffs.py +79 -0
- flwr/server/superlink/ffs/ffs_factory.py +47 -0
- flwr/server/superlink/fleet/__init__.py +1 -1
- flwr/server/superlink/fleet/grpc_adapter/__init__.py +15 -0
- flwr/server/superlink/fleet/grpc_adapter/grpc_adapter_servicer.py +162 -0
- flwr/server/superlink/fleet/grpc_bidi/__init__.py +1 -1
- flwr/server/superlink/fleet/grpc_bidi/flower_service_servicer.py +4 -2
- flwr/server/superlink/fleet/grpc_bidi/grpc_bridge.py +3 -2
- flwr/server/superlink/fleet/grpc_bidi/grpc_client_proxy.py +1 -1
- flwr/server/superlink/fleet/grpc_bidi/grpc_server.py +5 -154
- flwr/server/superlink/fleet/grpc_rere/__init__.py +1 -1
- flwr/server/superlink/fleet/grpc_rere/fleet_servicer.py +120 -13
- flwr/server/superlink/fleet/grpc_rere/server_interceptor.py +228 -0
- flwr/server/superlink/fleet/message_handler/__init__.py +1 -1
- flwr/server/superlink/fleet/message_handler/message_handler.py +153 -9
- flwr/server/superlink/fleet/rest_rere/__init__.py +1 -1
- flwr/server/superlink/fleet/rest_rere/rest_api.py +119 -81
- flwr/server/superlink/fleet/vce/__init__.py +1 -0
- flwr/server/superlink/fleet/vce/backend/__init__.py +4 -4
- flwr/server/superlink/fleet/vce/backend/backend.py +8 -9
- flwr/server/superlink/fleet/vce/backend/raybackend.py +87 -68
- flwr/server/superlink/fleet/vce/vce_api.py +208 -146
- flwr/server/superlink/linkstate/__init__.py +28 -0
- flwr/server/superlink/linkstate/in_memory_linkstate.py +581 -0
- flwr/server/superlink/linkstate/linkstate.py +389 -0
- flwr/server/superlink/{state/state_factory.py → linkstate/linkstate_factory.py} +19 -10
- flwr/server/superlink/linkstate/sqlite_linkstate.py +1236 -0
- flwr/server/superlink/linkstate/utils.py +389 -0
- flwr/server/superlink/simulation/__init__.py +15 -0
- flwr/server/superlink/simulation/simulationio_grpc.py +65 -0
- flwr/server/superlink/simulation/simulationio_servicer.py +186 -0
- flwr/server/superlink/utils.py +65 -0
- flwr/server/typing.py +2 -0
- flwr/server/utils/__init__.py +1 -1
- flwr/server/utils/tensorboard.py +5 -5
- flwr/server/utils/validator.py +31 -11
- flwr/server/workflow/default_workflows.py +70 -26
- flwr/server/workflow/secure_aggregation/secagg_workflow.py +1 -0
- flwr/server/workflow/secure_aggregation/secaggplus_workflow.py +40 -27
- flwr/simulation/__init__.py +12 -5
- flwr/simulation/app.py +247 -315
- flwr/simulation/legacy_app.py +402 -0
- flwr/simulation/ray_transport/__init__.py +1 -1
- flwr/simulation/ray_transport/ray_actor.py +42 -67
- flwr/simulation/ray_transport/ray_client_proxy.py +37 -17
- flwr/simulation/ray_transport/utils.py +1 -0
- flwr/simulation/run_simulation.py +306 -163
- flwr/simulation/simulationio_connection.py +89 -0
- flwr/superexec/__init__.py +15 -0
- flwr/superexec/app.py +59 -0
- flwr/superexec/deployment.py +188 -0
- flwr/superexec/exec_grpc.py +80 -0
- flwr/superexec/exec_servicer.py +231 -0
- flwr/superexec/exec_user_auth_interceptor.py +101 -0
- flwr/superexec/executor.py +96 -0
- flwr/superexec/simulation.py +124 -0
- {flwr_nightly-1.8.0.dev20240314.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/METADATA +33 -26
- flwr_nightly-1.15.0.dev20250114.dist-info/RECORD +328 -0
- flwr_nightly-1.15.0.dev20250114.dist-info/entry_points.txt +12 -0
- flwr/cli/flower_toml.py +0 -140
- flwr/cli/new/templates/app/flower.toml.tpl +0 -13
- flwr/cli/new/templates/app/requirements.numpy.txt.tpl +0 -2
- flwr/cli/new/templates/app/requirements.pytorch.txt.tpl +0 -4
- flwr/cli/new/templates/app/requirements.tensorflow.txt.tpl +0 -4
- flwr/client/node_state.py +0 -48
- flwr/client/node_state_tests.py +0 -65
- flwr/proto/driver_pb2.py +0 -44
- flwr/proto/driver_pb2_grpc.py +0 -169
- flwr/proto/driver_pb2_grpc.pyi +0 -66
- flwr/server/superlink/driver/driver_grpc.py +0 -54
- flwr/server/superlink/driver/driver_servicer.py +0 -129
- flwr/server/superlink/state/in_memory_state.py +0 -230
- flwr/server/superlink/state/sqlite_state.py +0 -630
- flwr/server/superlink/state/state.py +0 -154
- flwr_nightly-1.8.0.dev20240314.dist-info/RECORD +0 -211
- flwr_nightly-1.8.0.dev20240314.dist-info/entry_points.txt +0 -9
- {flwr_nightly-1.8.0.dev20240314.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/LICENSE +0 -0
- {flwr_nightly-1.8.0.dev20240314.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/WHEEL +0 -0
flwr/common/retry_invoker.py
CHANGED
@@ -18,20 +18,19 @@
|
|
18
18
|
import itertools
|
19
19
|
import random
|
20
20
|
import time
|
21
|
+
from collections.abc import Generator, Iterable
|
21
22
|
from dataclasses import dataclass
|
22
|
-
from
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
cast,
|
34
|
-
)
|
23
|
+
from logging import INFO, WARN
|
24
|
+
from typing import Any, Callable, Optional, Union, cast
|
25
|
+
|
26
|
+
import grpc
|
27
|
+
|
28
|
+
from flwr.common.constant import MAX_RETRY_DELAY
|
29
|
+
from flwr.common.logger import log
|
30
|
+
from flwr.common.typing import RunNotRunningException
|
31
|
+
from flwr.proto.clientappio_pb2_grpc import ClientAppIoStub
|
32
|
+
from flwr.proto.serverappio_pb2_grpc import ServerAppIoStub
|
33
|
+
from flwr.proto.simulationio_pb2_grpc import SimulationIoStub
|
35
34
|
|
36
35
|
|
37
36
|
def exponential(
|
@@ -49,6 +48,11 @@ def exponential(
|
|
49
48
|
Factor by which the delay is multiplied after each retry.
|
50
49
|
max_delay: Optional[float] (default: None)
|
51
50
|
The maximum delay duration between two consecutive retries.
|
51
|
+
|
52
|
+
Returns
|
53
|
+
-------
|
54
|
+
Generator[float, None, None]
|
55
|
+
A generator for the delay between 2 retries.
|
52
56
|
"""
|
53
57
|
delay = base_delay if max_delay is None else min(base_delay, max_delay)
|
54
58
|
while True:
|
@@ -67,6 +71,11 @@ def constant(
|
|
67
71
|
----------
|
68
72
|
interval: Union[float, Iterable[float]] (default: 1)
|
69
73
|
A constant value to yield or an iterable of such values.
|
74
|
+
|
75
|
+
Returns
|
76
|
+
-------
|
77
|
+
Generator[float, None, None]
|
78
|
+
A generator for the delay between 2 retries.
|
70
79
|
"""
|
71
80
|
if not isinstance(interval, Iterable):
|
72
81
|
interval = itertools.repeat(interval)
|
@@ -84,6 +93,11 @@ def full_jitter(max_value: float) -> float:
|
|
84
93
|
----------
|
85
94
|
max_value : float
|
86
95
|
The upper limit for the randomized value.
|
96
|
+
|
97
|
+
Returns
|
98
|
+
-------
|
99
|
+
float
|
100
|
+
A random float that is less than max_value.
|
87
101
|
"""
|
88
102
|
return random.uniform(0, max_value)
|
89
103
|
|
@@ -93,8 +107,8 @@ class RetryState:
|
|
93
107
|
"""State for callbacks in RetryInvoker."""
|
94
108
|
|
95
109
|
target: Callable[..., Any]
|
96
|
-
args:
|
97
|
-
kwargs:
|
110
|
+
args: tuple[Any, ...]
|
111
|
+
kwargs: dict[str, Any]
|
98
112
|
tries: int
|
99
113
|
elapsed_time: float
|
100
114
|
exception: Optional[Exception] = None
|
@@ -107,7 +121,7 @@ class RetryInvoker:
|
|
107
121
|
|
108
122
|
Parameters
|
109
123
|
----------
|
110
|
-
|
124
|
+
wait_gen_factory: Callable[[], Generator[float, None, None]]
|
111
125
|
A generator yielding successive wait times in seconds. If the generator
|
112
126
|
is finite, the giveup event will be triggered when the generator raises
|
113
127
|
`StopIteration`.
|
@@ -129,12 +143,12 @@ class RetryInvoker:
|
|
129
143
|
data class object detailing the invocation.
|
130
144
|
on_giveup: Optional[Callable[[RetryState], None]] (default: None)
|
131
145
|
A callable to be executed in the event that `max_tries` or `max_time` is
|
132
|
-
exceeded, `should_giveup` returns True, or `
|
146
|
+
exceeded, `should_giveup` returns True, or `wait_gen_factory()` generator raises
|
133
147
|
`StopInteration`. The parameter is a data class object detailing the
|
134
148
|
invocation.
|
135
149
|
jitter: Optional[Callable[[float], float]] (default: full_jitter)
|
136
|
-
A function of the value yielded by `
|
137
|
-
to wait. This function helps distribute wait times stochastically to avoid
|
150
|
+
A function of the value yielded by `wait_gen_factory()` returning the actual
|
151
|
+
time to wait. This function helps distribute wait times stochastically to avoid
|
138
152
|
timing collisions across concurrent clients. Wait times are jittered by
|
139
153
|
default using the `full_jitter` function. To disable jittering, pass
|
140
154
|
`jitter=None`.
|
@@ -142,6 +156,13 @@ class RetryInvoker:
|
|
142
156
|
A function accepting an exception instance, returning whether or not
|
143
157
|
to give up prematurely before other give-up conditions are evaluated.
|
144
158
|
If set to None, the strategy is to never give up prematurely.
|
159
|
+
wait_function: Optional[Callable[[float], None]] (default: None)
|
160
|
+
A function that defines how to wait between retry attempts. It accepts
|
161
|
+
one argument, the wait time in seconds, allowing the use of various waiting
|
162
|
+
mechanisms (e.g., asynchronous waits or event-based synchronization) suitable
|
163
|
+
for different execution environments. If set to `None`, the `wait_function`
|
164
|
+
defaults to `time.sleep`, which is ideal for synchronous operations. Custom
|
165
|
+
functions should manage execution flow to prevent blocking or interference.
|
145
166
|
|
146
167
|
Examples
|
147
168
|
--------
|
@@ -159,8 +180,8 @@ class RetryInvoker:
|
|
159
180
|
# pylint: disable-next=too-many-arguments
|
160
181
|
def __init__(
|
161
182
|
self,
|
162
|
-
|
163
|
-
recoverable_exceptions: Union[
|
183
|
+
wait_gen_factory: Callable[[], Generator[float, None, None]],
|
184
|
+
recoverable_exceptions: Union[type[Exception], tuple[type[Exception], ...]],
|
164
185
|
max_tries: Optional[int],
|
165
186
|
max_time: Optional[float],
|
166
187
|
*,
|
@@ -169,8 +190,9 @@ class RetryInvoker:
|
|
169
190
|
on_giveup: Optional[Callable[[RetryState], None]] = None,
|
170
191
|
jitter: Optional[Callable[[float], float]] = full_jitter,
|
171
192
|
should_giveup: Optional[Callable[[Exception], bool]] = None,
|
193
|
+
wait_function: Optional[Callable[[float], None]] = None,
|
172
194
|
) -> None:
|
173
|
-
self.
|
195
|
+
self.wait_gen_factory = wait_gen_factory
|
174
196
|
self.recoverable_exceptions = recoverable_exceptions
|
175
197
|
self.max_tries = max_tries
|
176
198
|
self.max_time = max_time
|
@@ -179,6 +201,9 @@ class RetryInvoker:
|
|
179
201
|
self.on_giveup = on_giveup
|
180
202
|
self.jitter = jitter
|
181
203
|
self.should_giveup = should_giveup
|
204
|
+
if wait_function is None:
|
205
|
+
wait_function = time.sleep
|
206
|
+
self.wait_function = wait_function
|
182
207
|
|
183
208
|
# pylint: disable-next=too-many-locals
|
184
209
|
def invoke(
|
@@ -212,13 +237,13 @@ class RetryInvoker:
|
|
212
237
|
Raises
|
213
238
|
------
|
214
239
|
Exception
|
215
|
-
If the number of tries exceeds `max_tries`, if the total time
|
216
|
-
|
240
|
+
If the number of tries exceeds `max_tries`, if the total time exceeds
|
241
|
+
`max_time`, if `wait_gen_factory()` generator raises `StopInteration`,
|
217
242
|
or if the `should_giveup` returns True for a raised exception.
|
218
243
|
|
219
244
|
Notes
|
220
245
|
-----
|
221
|
-
The time between retries is determined by the provided `
|
246
|
+
The time between retries is determined by the provided `wait_gen_factory()`
|
222
247
|
generator and can optionally be jittered using the `jitter` function.
|
223
248
|
The recoverable exceptions that trigger a retry, as well as conditions to
|
224
249
|
stop retries, are also determined by the class's initialization parameters.
|
@@ -231,13 +256,13 @@ class RetryInvoker:
|
|
231
256
|
handler(cast(RetryState, ref_state[0]))
|
232
257
|
|
233
258
|
try_cnt = 0
|
234
|
-
wait_generator = self.
|
235
|
-
start = time.
|
236
|
-
ref_state:
|
259
|
+
wait_generator = self.wait_gen_factory()
|
260
|
+
start = time.monotonic()
|
261
|
+
ref_state: list[Optional[RetryState]] = [None]
|
237
262
|
|
238
263
|
while True:
|
239
264
|
try_cnt += 1
|
240
|
-
elapsed_time = time.
|
265
|
+
elapsed_time = time.monotonic() - start
|
241
266
|
state = RetryState(
|
242
267
|
target=target,
|
243
268
|
args=args,
|
@@ -250,6 +275,7 @@ class RetryInvoker:
|
|
250
275
|
try:
|
251
276
|
ret = target(*args, **kwargs)
|
252
277
|
except self.recoverable_exceptions as err:
|
278
|
+
state.exception = err
|
253
279
|
# Check if giveup event should be triggered
|
254
280
|
max_tries_exceeded = try_cnt == self.max_tries
|
255
281
|
max_time_exceeded = (
|
@@ -282,8 +308,75 @@ class RetryInvoker:
|
|
282
308
|
try_call_event_handler(self.on_backoff)
|
283
309
|
|
284
310
|
# Sleep
|
285
|
-
|
311
|
+
self.wait_function(state.actual_wait)
|
286
312
|
else:
|
287
313
|
# Trigger success event
|
288
314
|
try_call_event_handler(self.on_success)
|
289
315
|
return ret
|
316
|
+
|
317
|
+
|
318
|
+
def _make_simple_grpc_retry_invoker() -> RetryInvoker:
|
319
|
+
"""Create a simple gRPC retry invoker."""
|
320
|
+
|
321
|
+
def _on_sucess(retry_state: RetryState) -> None:
|
322
|
+
if retry_state.tries > 1:
|
323
|
+
log(
|
324
|
+
INFO,
|
325
|
+
"Connection successful after %.2f seconds and %s tries.",
|
326
|
+
retry_state.elapsed_time,
|
327
|
+
retry_state.tries,
|
328
|
+
)
|
329
|
+
|
330
|
+
def _on_backoff(retry_state: RetryState) -> None:
|
331
|
+
if retry_state.tries == 1:
|
332
|
+
log(WARN, "Connection attempt failed, retrying...")
|
333
|
+
else:
|
334
|
+
log(
|
335
|
+
WARN,
|
336
|
+
"Connection attempt failed, retrying in %.2f seconds",
|
337
|
+
retry_state.actual_wait,
|
338
|
+
)
|
339
|
+
|
340
|
+
def _on_giveup(retry_state: RetryState) -> None:
|
341
|
+
if retry_state.tries > 1:
|
342
|
+
log(
|
343
|
+
WARN,
|
344
|
+
"Giving up reconnection after %.2f seconds and %s tries.",
|
345
|
+
retry_state.elapsed_time,
|
346
|
+
retry_state.tries,
|
347
|
+
)
|
348
|
+
|
349
|
+
def _should_giveup_fn(e: Exception) -> bool:
|
350
|
+
if e.code() == grpc.StatusCode.PERMISSION_DENIED: # type: ignore
|
351
|
+
raise RunNotRunningException
|
352
|
+
if e.code() == grpc.StatusCode.UNAVAILABLE: # type: ignore
|
353
|
+
return False
|
354
|
+
return True
|
355
|
+
|
356
|
+
return RetryInvoker(
|
357
|
+
wait_gen_factory=lambda: exponential(max_delay=MAX_RETRY_DELAY),
|
358
|
+
recoverable_exceptions=grpc.RpcError,
|
359
|
+
max_tries=None,
|
360
|
+
max_time=None,
|
361
|
+
on_success=_on_sucess,
|
362
|
+
on_backoff=_on_backoff,
|
363
|
+
on_giveup=_on_giveup,
|
364
|
+
should_giveup=_should_giveup_fn,
|
365
|
+
)
|
366
|
+
|
367
|
+
|
368
|
+
def _wrap_stub(
|
369
|
+
stub: Union[ServerAppIoStub, ClientAppIoStub, SimulationIoStub],
|
370
|
+
retry_invoker: RetryInvoker,
|
371
|
+
) -> None:
|
372
|
+
"""Wrap a gRPC stub with a retry invoker."""
|
373
|
+
|
374
|
+
def make_lambda(original_method: Any) -> Any:
|
375
|
+
return lambda *args, **kwargs: retry_invoker.invoke(
|
376
|
+
original_method, *args, **kwargs
|
377
|
+
)
|
378
|
+
|
379
|
+
for method_name in vars(stub):
|
380
|
+
method = getattr(stub, method_name)
|
381
|
+
if callable(method):
|
382
|
+
setattr(stub, method_name, make_lambda(method))
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright
|
1
|
+
# Copyright 2023 Flower Labs GmbH. All Rights Reserved.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -17,20 +17,20 @@
|
|
17
17
|
|
18
18
|
import pickle
|
19
19
|
from concurrent.futures import ThreadPoolExecutor
|
20
|
-
from typing import
|
20
|
+
from typing import cast
|
21
21
|
|
22
22
|
from Crypto.Protocol.SecretSharing import Shamir
|
23
23
|
from Crypto.Util.Padding import pad, unpad
|
24
24
|
|
25
25
|
|
26
|
-
def create_shares(secret: bytes, threshold: int, num: int) ->
|
26
|
+
def create_shares(secret: bytes, threshold: int, num: int) -> list[bytes]:
|
27
27
|
"""Return list of shares (bytes)."""
|
28
28
|
secret_padded = pad(secret, 16)
|
29
29
|
secret_padded_chunk = [
|
30
30
|
(threshold, num, secret_padded[i : i + 16])
|
31
31
|
for i in range(0, len(secret_padded), 16)
|
32
32
|
]
|
33
|
-
share_list:
|
33
|
+
share_list: list[list[tuple[int, bytes]]] = [[] for _ in range(num)]
|
34
34
|
|
35
35
|
with ThreadPoolExecutor(max_workers=10) as executor:
|
36
36
|
for chunk_shares in executor.map(
|
@@ -43,22 +43,22 @@ def create_shares(secret: bytes, threshold: int, num: int) -> List[bytes]:
|
|
43
43
|
return [pickle.dumps(shares) for shares in share_list]
|
44
44
|
|
45
45
|
|
46
|
-
def _shamir_split(threshold: int, num: int, chunk: bytes) ->
|
46
|
+
def _shamir_split(threshold: int, num: int, chunk: bytes) -> list[tuple[int, bytes]]:
|
47
47
|
return Shamir.split(threshold, num, chunk, ssss=False)
|
48
48
|
|
49
49
|
|
50
50
|
# Reconstructing secret with PyCryptodome
|
51
|
-
def combine_shares(share_list:
|
51
|
+
def combine_shares(share_list: list[bytes]) -> bytes:
|
52
52
|
"""Reconstruct secret from shares."""
|
53
|
-
unpickled_share_list:
|
54
|
-
cast(
|
53
|
+
unpickled_share_list: list[list[tuple[int, bytes]]] = [
|
54
|
+
cast(list[tuple[int, bytes]], pickle.loads(share)) for share in share_list
|
55
55
|
]
|
56
56
|
|
57
57
|
chunk_num = len(unpickled_share_list[0])
|
58
58
|
secret_padded = bytearray(0)
|
59
|
-
chunk_shares_list:
|
59
|
+
chunk_shares_list: list[list[tuple[int, bytes]]] = []
|
60
60
|
for i in range(chunk_num):
|
61
|
-
chunk_shares:
|
61
|
+
chunk_shares: list[tuple[int, bytes]] = []
|
62
62
|
for share in unpickled_share_list:
|
63
63
|
chunk_shares.append(share[i])
|
64
64
|
chunk_shares_list.append(chunk_shares)
|
@@ -71,5 +71,5 @@ def combine_shares(share_list: List[bytes]) -> bytes:
|
|
71
71
|
return bytes(secret)
|
72
72
|
|
73
73
|
|
74
|
-
def _shamir_combine(shares:
|
74
|
+
def _shamir_combine(shares: list[tuple[int, bytes]]) -> bytes:
|
75
75
|
return Shamir.combine(shares, ssss=False)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright
|
1
|
+
# Copyright 2023 Flower Labs GmbH. All Rights Reserved.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -16,16 +16,17 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
import base64
|
19
|
-
from typing import
|
19
|
+
from typing import cast
|
20
20
|
|
21
|
+
from cryptography.exceptions import InvalidSignature
|
21
22
|
from cryptography.fernet import Fernet
|
22
|
-
from cryptography.hazmat.primitives import hashes, serialization
|
23
|
+
from cryptography.hazmat.primitives import hashes, hmac, serialization
|
23
24
|
from cryptography.hazmat.primitives.asymmetric import ec
|
24
25
|
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
25
26
|
|
26
27
|
|
27
28
|
def generate_key_pairs() -> (
|
28
|
-
|
29
|
+
tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]
|
29
30
|
):
|
30
31
|
"""Generate private and public key pairs with Cryptography."""
|
31
32
|
private_key = ec.generate_private_key(ec.SECP384R1())
|
@@ -98,3 +99,66 @@ def decrypt(key: bytes, ciphertext: bytes) -> bytes:
|
|
98
99
|
# The input key must be url safe
|
99
100
|
fernet = Fernet(key)
|
100
101
|
return fernet.decrypt(ciphertext)
|
102
|
+
|
103
|
+
|
104
|
+
def compute_hmac(key: bytes, message: bytes) -> bytes:
|
105
|
+
"""Compute hmac of a message using key as hash."""
|
106
|
+
computed_hmac = hmac.HMAC(key, hashes.SHA256())
|
107
|
+
computed_hmac.update(message)
|
108
|
+
return computed_hmac.finalize()
|
109
|
+
|
110
|
+
|
111
|
+
def verify_hmac(key: bytes, message: bytes, hmac_value: bytes) -> bool:
|
112
|
+
"""Verify hmac of a message using key as hash."""
|
113
|
+
computed_hmac = hmac.HMAC(key, hashes.SHA256())
|
114
|
+
computed_hmac.update(message)
|
115
|
+
try:
|
116
|
+
computed_hmac.verify(hmac_value)
|
117
|
+
return True
|
118
|
+
except InvalidSignature:
|
119
|
+
return False
|
120
|
+
|
121
|
+
|
122
|
+
def sign_message(private_key: ec.EllipticCurvePrivateKey, message: bytes) -> bytes:
|
123
|
+
"""Sign a message using the provided EC private key.
|
124
|
+
|
125
|
+
Parameters
|
126
|
+
----------
|
127
|
+
private_key : ec.EllipticCurvePrivateKey
|
128
|
+
The EC private key to sign the message with.
|
129
|
+
message : bytes
|
130
|
+
The message to be signed.
|
131
|
+
|
132
|
+
Returns
|
133
|
+
-------
|
134
|
+
bytes
|
135
|
+
The signature of the message.
|
136
|
+
"""
|
137
|
+
signature = private_key.sign(message, ec.ECDSA(hashes.SHA256()))
|
138
|
+
return signature
|
139
|
+
|
140
|
+
|
141
|
+
def verify_signature(
|
142
|
+
public_key: ec.EllipticCurvePublicKey, message: bytes, signature: bytes
|
143
|
+
) -> bool:
|
144
|
+
"""Verify a signature against a message using the provided EC public key.
|
145
|
+
|
146
|
+
Parameters
|
147
|
+
----------
|
148
|
+
public_key : ec.EllipticCurvePublicKey
|
149
|
+
The EC public key to verify the signature.
|
150
|
+
message : bytes
|
151
|
+
The original message.
|
152
|
+
signature : bytes
|
153
|
+
The signature to verify.
|
154
|
+
|
155
|
+
Returns
|
156
|
+
-------
|
157
|
+
bool
|
158
|
+
True if the signature is valid, False otherwise.
|
159
|
+
"""
|
160
|
+
try:
|
161
|
+
public_key.verify(signature, message, ec.ECDSA(hashes.SHA256()))
|
162
|
+
return True
|
163
|
+
except InvalidSignature:
|
164
|
+
return False
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright
|
1
|
+
# Copyright 2023 Flower Labs GmbH. All Rights Reserved.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -15,51 +15,51 @@
|
|
15
15
|
"""Utility functions for performing operations on Numpy NDArrays."""
|
16
16
|
|
17
17
|
|
18
|
-
from typing import Any,
|
18
|
+
from typing import Any, Union
|
19
19
|
|
20
20
|
import numpy as np
|
21
21
|
from numpy.typing import DTypeLike, NDArray
|
22
22
|
|
23
23
|
|
24
|
-
def factor_combine(factor: int, parameters:
|
24
|
+
def factor_combine(factor: int, parameters: list[NDArray[Any]]) -> list[NDArray[Any]]:
|
25
25
|
"""Combine factor with parameters."""
|
26
26
|
return [np.array([factor])] + parameters
|
27
27
|
|
28
28
|
|
29
29
|
def factor_extract(
|
30
|
-
parameters:
|
31
|
-
) ->
|
30
|
+
parameters: list[NDArray[Any]],
|
31
|
+
) -> tuple[int, list[NDArray[Any]]]:
|
32
32
|
"""Extract factor from parameters."""
|
33
33
|
return parameters[0][0], parameters[1:]
|
34
34
|
|
35
35
|
|
36
|
-
def get_parameters_shape(parameters:
|
36
|
+
def get_parameters_shape(parameters: list[NDArray[Any]]) -> list[tuple[int, ...]]:
|
37
37
|
"""Get dimensions of each NDArray in parameters."""
|
38
38
|
return [arr.shape for arr in parameters]
|
39
39
|
|
40
40
|
|
41
41
|
def get_zero_parameters(
|
42
|
-
dimensions_list:
|
43
|
-
) ->
|
42
|
+
dimensions_list: list[tuple[int, ...]], dtype: DTypeLike = np.int64
|
43
|
+
) -> list[NDArray[Any]]:
|
44
44
|
"""Generate zero parameters based on the dimensions list."""
|
45
45
|
return [np.zeros(dimensions, dtype=dtype) for dimensions in dimensions_list]
|
46
46
|
|
47
47
|
|
48
48
|
def parameters_addition(
|
49
|
-
parameters1:
|
50
|
-
) ->
|
49
|
+
parameters1: list[NDArray[Any]], parameters2: list[NDArray[Any]]
|
50
|
+
) -> list[NDArray[Any]]:
|
51
51
|
"""Add two parameters."""
|
52
52
|
return [parameters1[idx] + parameters2[idx] for idx in range(len(parameters1))]
|
53
53
|
|
54
54
|
|
55
55
|
def parameters_subtraction(
|
56
|
-
parameters1:
|
57
|
-
) ->
|
56
|
+
parameters1: list[NDArray[Any]], parameters2: list[NDArray[Any]]
|
57
|
+
) -> list[NDArray[Any]]:
|
58
58
|
"""Subtract parameters from the other parameters."""
|
59
59
|
return [parameters1[idx] - parameters2[idx] for idx in range(len(parameters1))]
|
60
60
|
|
61
61
|
|
62
|
-
def parameters_mod(parameters:
|
62
|
+
def parameters_mod(parameters: list[NDArray[Any]], divisor: int) -> list[NDArray[Any]]:
|
63
63
|
"""Take mod of parameters with an integer divisor."""
|
64
64
|
if bin(divisor).count("1") == 1:
|
65
65
|
msk = divisor - 1
|
@@ -68,14 +68,14 @@ def parameters_mod(parameters: List[NDArray[Any]], divisor: int) -> List[NDArray
|
|
68
68
|
|
69
69
|
|
70
70
|
def parameters_multiply(
|
71
|
-
parameters:
|
72
|
-
) ->
|
71
|
+
parameters: list[NDArray[Any]], multiplier: Union[int, float]
|
72
|
+
) -> list[NDArray[Any]]:
|
73
73
|
"""Multiply parameters by an integer/float multiplier."""
|
74
74
|
return [parameters[idx] * multiplier for idx in range(len(parameters))]
|
75
75
|
|
76
76
|
|
77
77
|
def parameters_divide(
|
78
|
-
parameters:
|
79
|
-
) ->
|
78
|
+
parameters: list[NDArray[Any]], divisor: Union[int, float]
|
79
|
+
) -> list[NDArray[Any]]:
|
80
80
|
"""Divide weight by an integer/float divisor."""
|
81
81
|
return [parameters[idx] / divisor for idx in range(len(parameters))]
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright
|
1
|
+
# Copyright 2023 Flower Labs GmbH. All Rights Reserved.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -15,7 +15,7 @@
|
|
15
15
|
"""Utility functions for model quantization."""
|
16
16
|
|
17
17
|
|
18
|
-
from typing import
|
18
|
+
from typing import cast
|
19
19
|
|
20
20
|
import numpy as np
|
21
21
|
|
@@ -30,10 +30,10 @@ def _stochastic_round(arr: NDArrayFloat) -> NDArrayInt:
|
|
30
30
|
|
31
31
|
|
32
32
|
def quantize(
|
33
|
-
parameters:
|
34
|
-
) ->
|
33
|
+
parameters: list[NDArrayFloat], clipping_range: float, target_range: int
|
34
|
+
) -> list[NDArrayInt]:
|
35
35
|
"""Quantize float Numpy arrays to integer Numpy arrays."""
|
36
|
-
quantized_list:
|
36
|
+
quantized_list: list[NDArrayInt] = []
|
37
37
|
quantizer = target_range / (2 * clipping_range)
|
38
38
|
for arr in parameters:
|
39
39
|
# Stochastic quantization
|
@@ -49,12 +49,12 @@ def quantize(
|
|
49
49
|
|
50
50
|
# Dequantize parameters to range [-clipping_range, clipping_range]
|
51
51
|
def dequantize(
|
52
|
-
quantized_parameters:
|
52
|
+
quantized_parameters: list[NDArrayInt],
|
53
53
|
clipping_range: float,
|
54
54
|
target_range: int,
|
55
|
-
) ->
|
55
|
+
) -> list[NDArrayFloat]:
|
56
56
|
"""Dequantize integer Numpy arrays to float Numpy arrays."""
|
57
|
-
reverse_quantized_list:
|
57
|
+
reverse_quantized_list: list[NDArrayFloat] = []
|
58
58
|
quantizer = (2 * clipping_range) / target_range
|
59
59
|
shift = -clipping_range
|
60
60
|
for arr in quantized_parameters:
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright
|
1
|
+
# Copyright 2023 Flower Labs GmbH. All Rights Reserved.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -15,8 +15,6 @@
|
|
15
15
|
"""Utility functions for the SecAgg/SecAgg+ protocol."""
|
16
16
|
|
17
17
|
|
18
|
-
from typing import List, Tuple
|
19
|
-
|
20
18
|
import numpy as np
|
21
19
|
|
22
20
|
from flwr.common.typing import NDArrayInt
|
@@ -45,8 +43,8 @@ def share_keys_plaintext_concat(
|
|
45
43
|
"""
|
46
44
|
return b"".join(
|
47
45
|
[
|
48
|
-
int.to_bytes(src_node_id, 8, "little", signed=
|
49
|
-
int.to_bytes(dst_node_id, 8, "little", signed=
|
46
|
+
int.to_bytes(src_node_id, 8, "little", signed=False),
|
47
|
+
int.to_bytes(dst_node_id, 8, "little", signed=False),
|
50
48
|
int.to_bytes(len(b_share), 4, "little"),
|
51
49
|
b_share,
|
52
50
|
sk_share,
|
@@ -54,7 +52,7 @@ def share_keys_plaintext_concat(
|
|
54
52
|
)
|
55
53
|
|
56
54
|
|
57
|
-
def share_keys_plaintext_separate(plaintext: bytes) ->
|
55
|
+
def share_keys_plaintext_separate(plaintext: bytes) -> tuple[int, int, bytes, bytes]:
|
58
56
|
"""Retrieve arguments from bytes.
|
59
57
|
|
60
58
|
Parameters
|
@@ -74,8 +72,8 @@ def share_keys_plaintext_separate(plaintext: bytes) -> Tuple[int, int, bytes, by
|
|
74
72
|
the secret key share of the source sent to the destination.
|
75
73
|
"""
|
76
74
|
src, dst, mark = (
|
77
|
-
int.from_bytes(plaintext[:8], "little", signed=
|
78
|
-
int.from_bytes(plaintext[8:16], "little", signed=
|
75
|
+
int.from_bytes(plaintext[:8], "little", signed=False),
|
76
|
+
int.from_bytes(plaintext[8:16], "little", signed=False),
|
79
77
|
int.from_bytes(plaintext[16:20], "little"),
|
80
78
|
)
|
81
79
|
ret = (src, dst, plaintext[20 : 20 + mark], plaintext[20 + mark :])
|
@@ -83,8 +81,8 @@ def share_keys_plaintext_separate(plaintext: bytes) -> Tuple[int, int, bytes, by
|
|
83
81
|
|
84
82
|
|
85
83
|
def pseudo_rand_gen(
|
86
|
-
seed: bytes, num_range: int, dimensions_list:
|
87
|
-
) ->
|
84
|
+
seed: bytes, num_range: int, dimensions_list: list[tuple[int, ...]]
|
85
|
+
) -> list[NDArrayInt]:
|
88
86
|
"""Seeded pseudo-random number generator for noise generation with Numpy."""
|
89
87
|
assert len(seed) & 0x3 == 0
|
90
88
|
seed32 = 0
|
@@ -95,8 +93,8 @@ def pseudo_rand_gen(
|
|
95
93
|
output = []
|
96
94
|
for dimension in dimensions_list:
|
97
95
|
if len(dimension) == 0:
|
98
|
-
arr = np.array(gen.randint(0, num_range - 1), dtype=
|
96
|
+
arr = np.array(gen.randint(0, num_range - 1), dtype=np.int64)
|
99
97
|
else:
|
100
|
-
arr = gen.randint(0, num_range - 1, dimension)
|
98
|
+
arr = gen.randint(0, num_range - 1, dimension, dtype=np.int64)
|
101
99
|
output.append(arr)
|
102
100
|
return output
|