aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/sca/stacked_traces/combine.py
blob: cbdfe045d6dc2c0c38f481d60a73af626636e8ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
195
196
197
198
199
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
316
317
318
319
320
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from __future__ import annotations

from numba import cuda
from numba.cuda import devicearray
import numpy as np
from math import sqrt

from public import public
from typing import Callable, Union, Tuple, cast

from ...sca.trace.trace import CombinedTrace
from ...sca.stacked_traces import StackedTraces

TPB = Union[int, Tuple[int, ...]]
CudaCTX = Tuple[
    Tuple[devicearray.DeviceNDArray, ...],
    Union[int, Tuple[int, ...]]
]


@public
class BaseTraceManager:
    """Base class for trace managers"""

    traces: StackedTraces

    def __init__(self, traces: StackedTraces) -> None:
        self.traces = traces

    def average(self) -> CombinedTrace:
        """
        Average :paramref:`~.average.traces`, sample-wise.

        :param traces:
        :return:
        """
        raise NotImplementedError

    def conditional_average(self, cond: Callable[[np.ndarray], bool]) \
            -> CombinedTrace:
        """
        Average :paramref:`~.conditional_average.traces` for which the
        :paramref:`~.conditional_average.condition` is ``True``, sample-wise.

        :param traces:
        :param condition:
        :return:
        """
        raise NotImplementedError

    def standard_deviation(self) -> CombinedTrace:
        """
        Compute the sample standard-deviation of the
        :paramref:`~.standard_deviation.traces`, sample-wise.

        :param traces:
        :return:
        """
        raise NotImplementedError

    def variance(self) -> CombinedTrace:
        """
        Compute the sample variance of the
        :paramref:`~.variance.traces`, sample-wise.

        :param traces:
        :return:
        """
        raise NotImplementedError

    def average_and_variance(self) -> Tuple[CombinedTrace, CombinedTrace]:
        """
        Compute the sample average and variance of the
        :paramref:`~.average_and_variance.traces`, sample-wise.

        :param traces:
        :return:
        """
        raise NotImplementedError

    def add(self) -> CombinedTrace:
        """
        Add :paramref:`~.add.traces`, sample-wise.

        :param traces:
        :return:
        """
        raise NotImplementedError


@public
class GPUTraceManager(BaseTraceManager):
    """Manager for operations with stacked traces on GPU"""

    _tpb: TPB
    _samples_global: devicearray.DeviceNDArray

    def __init__(self, traces: StackedTraces, tpb: TPB = 128) -> None:
        if not cuda.is_available():
            raise RuntimeError("CUDA is not available, "
                               "use CPUTraceManager instead")
        if isinstance(tpb, int) and tpb % 32 != 0:
            raise ValueError('TPB should be a multiple of 32')
        if isinstance(tpb, tuple) and any(t % 32 != 0 for t in tpb):
            raise ValueError(
                'TPB should be a multiple of 32 in each dimension'
            )

        super().__init__(traces)
        self.tpb = tpb
        self._samples_global = cuda.to_device(self.traces.samples)

    def _setup1D(self, output_count: int) -> CudaCTX:
        """
        Creates context for 1D GPU CUDA functions

        :param traces: The input stacked traces.
        :param tpb: Threads per block to invoke the kernel with.
        :param output_count: Number of outputs expected from the GPU function.
        :return: Created context of input and output arrays and calculated
                 blocks per grid dimensions.
        """
        if not isinstance(self.tpb, int):
            raise TypeError("tpb is not an int for a 1D kernel")

        device_output = tuple((
            cuda.device_array(self.traces.samples.shape[1])
            for _ in range(output_count)
        ))
        bpg = (self.traces.samples.size + (self.tpb - 1)) // self.tpb

        return device_output, bpg

    def _gpu_combine1D(self, func, output_count: int = 1) \
            -> Union[CombinedTrace, Tuple[CombinedTrace, ...]]:
        """
        Runs GPU Cuda StackedTrace 1D combine function

        :param func: Function to run.
        :param traces: Stacked traces to provide as input to the function.
        :param tpb: Threads per block to invoke the kernel with
        :param output_count: Number of outputs expected from the GPU function.
        :return: Combined trace output from the GPU function
        """
        device_outputs, bpg = self._setup1D(output_count)

        func[bpg, self.tpb](self._samples_global, *device_outputs)

        if len(device_outputs) == 1:
            return CombinedTrace(
                device_outputs[0].copy_to_host(),
                self.traces.meta
            )
        return tuple(
            CombinedTrace(device_output.copy_to_host(), self.traces.meta)
            for device_output
            in device_outputs
        )

    def average(self) -> CombinedTrace:
        return cast(CombinedTrace, self._gpu_combine1D(gpu_average, 1))

    def conditional_average(self, cond: Callable[[np.ndarray], bool]) \
            -> CombinedTrace:
        raise NotImplementedError()

    def standard_deviation(self) -> CombinedTrace:
        return cast(CombinedTrace, self._gpu_combine1D(gpu_std_dev, 1))

    def variance(self) -> CombinedTrace:
        return cast(CombinedTrace, self._gpu_combine1D(gpu_variance, 1))

    def average_and_variance(self) -> Tuple[CombinedTrace, CombinedTrace]:
        averages, variances = self._gpu_combine1D(gpu_avg_var, 2)
        return averages, variances

    def add(self) -> CombinedTrace:
        return cast(CombinedTrace, self._gpu_combine1D(gpu_add, 1))


@cuda.jit(device=True)
def _gpu_average(col: int, samples: np.ndarray, result: np.ndarray):
    """
    Cuda device thread function computing the average of a sample of stacked traces.

    :param col: Index of the sample.
    :param samples: Shared array of the samples of stacked traces.
    :param result: Result output array.
    """
    acc = 0.
    for row in range(samples.shape[0]):
        acc += samples[row, col]
    result[col] = acc / samples.shape[0]


@cuda.jit
def gpu_average(samples: np.ndarray, result: np.ndarray):
    """
    Sample average of stacked traces, sample-wise.

    :param samples: Stacked traces' samples.
    :param result: Result output array.
    """
    col = cuda.grid(1)

    if col >= samples.shape[1]:
        return

    _gpu_average(col, samples, result)


@cuda.jit(device=True)
def _gpu_var_from_avg(col: int, samples: np.ndarray,
                      averages: np.ndarray, result: np.ndarray):
    """
    Cuda device thread function computing the variance from the average of a sample of stacked traces.

    :param col: Index of the sample.
    :param samples: Shared array of the samples of stacked traces.
    :param averages: Array of averages of samples.
    :param result: Result output array.
    """
    var = 0.
    for row in range(samples.shape[0]):
        current = samples[row, col] - averages[col]
        var += current * current
    result[col] = var / samples.shape[0]


@cuda.jit(device=True)
def _gpu_variance(col: int, samples: np.ndarray, result: np.ndarray):
    """
    Cuda device thread function computing the variance of a sample of stacked traces.

    :param col: Index of the sample.
    :param samples: Shared array of the samples of stacked traces.
    :param result: Result output array.
    """
    _gpu_average(col, samples, result)
    _gpu_var_from_avg(col, samples, result, result)


@cuda.jit
def gpu_std_dev(samples: np.ndarray, result: np.ndarray):
    """
    Sample standard deviation of stacked traces, sample-wise.

    :param samples: Stacked traces' samples.
    :param result: Result output array.
    """
    col = cuda.grid(1)

    if col >= samples.shape[1]:
        return

    _gpu_variance(col, samples, result)

    result[col] = sqrt(result[col])


@cuda.jit
def gpu_variance(samples: np.ndarray, result: np.ndarray):
    """
    Sample variance of stacked traces, sample-wise.

    :param samples: Stacked traces' samples.
    :param result: Result output array.
    """
    col = cuda.grid(1)

    if col >= samples.shape[1]:
        return

    _gpu_variance(col, samples, result)


@cuda.jit
def gpu_avg_var(samples: np.ndarray, result_avg: np.ndarray,
                result_var: np.ndarray):
    """
    Sample average and variance of stacked traces, sample-wise.

    :param samples: Stacked traces' samples.
    :param result_avg: Result average output array.
    :param result_var: Result variance output array.
    """
    col = cuda.grid(1)

    if col >= samples.shape[1]:
        return

    _gpu_average(col, samples, result_avg)
    _gpu_var_from_avg(col, samples, result_avg, result_var)


@cuda.jit
def gpu_add(samples: np.ndarray, result: np.ndarray):
    """
    Add samples of stacked traces, sample-wise.

    :param samples: Stacked traces' samples.
    :param result: Result output array.
    """
    col = cuda.grid(1)

    if col >= samples.shape[1]:
        return

    res = 0.
    for row in range(samples.shape[0]):
        res += samples[row, col]
    result[col] = res


@public
class CPUTraceManager:
    """Manager for operations on stacked traces on CPU."""

    traces: StackedTraces

    def __init__(self, traces: StackedTraces) -> None:
        self.traces = traces

    def average(self) -> CombinedTrace:
        """
        Compute the average of the :paramref:`~.average.traces`, sample-wise.

        :param traces:
        :return:
        """
        return CombinedTrace(
            np.average(self.traces.samples, 0),
            self.traces.meta
        )

    def conditional_average(self, condition: Callable[[np.ndarray], bool]) -> CombinedTrace:
        """
        Compute the conditional average of the :paramref:`~.conditional_average.traces`, sample-wise.

        :param traces:
        :return:
        """
        # TODO: Consider other ways to implement this
        return CombinedTrace(
            np.average(self.traces.samples[np.apply_along_axis(condition, 1, self.traces.samples)], 1),
            self.traces.meta
        )

    def standard_deviation(self) -> CombinedTrace:
        """
        Compute the sample standard-deviation of the :paramref:`~.standard_deviation.traces`, sample-wise.

        :param traces:
        :return:
        """
        return CombinedTrace(
            np.std(self.traces.samples, 0),
            self.traces.meta
        )

    def variance(self) -> CombinedTrace:
        """
        Compute the sample variance of the :paramref:`~.variance.traces`, sample-wise.

        :param traces:
        :return:
        """
        return CombinedTrace(
            np.var(self.traces.samples, 0),
            self.traces.meta
        )

    def average_and_variance(self) -> Tuple[CombinedTrace, CombinedTrace]:
        """
        Compute the average and sample variance of the :paramref:`~.average_and_variance.traces`, sample-wise.

        :param traces:
        :return:
        """
        return (
            self.average(),
            self.variance()
        )

    def add(self) -> CombinedTrace:
        """
        Add :paramref:`~.add.traces`, sample-wise.

        :param traces:
        :return:
        """
        return CombinedTrace(
            np.sum(self.traces.samples, 0),
            self.traces.meta
        )