scalib.preprocessing.Quantizer#

class scalib.preprocessing.Quantizer(shift, scale)[source]#

Quantize a side channel traces given as an array of float into an array of int16.

The quantizer estimates a shift and scale that minimize the loss due to the rounding operation.

\[\mathrm{Quantize}( x) = \mathrm{Round}((x - \mathrm{Shift}) \cdot \mathrm{Scale})\]

The shift and scale parameter can be provided explicitly, or can be estimated based on a few traces.

Warning

The quantization procedure operates pointwise: each point is shifted and scaled by a different value. As a consequence the quantized version of the trace probably does not look like its non quantized version.

Parameters:
  • shift (npt.NDArray[np.floating]) – The value to shift every traces.

  • scale (npt.NDArray[np.floating]) – The value to scale every traces.

Examples

>>> from scalib.preprocessing import Quantizer
>>> import numpy as np
>>> # 500 traces of 200 points
>>> traces : npt.NDArray[np.floating] = np.random.randn(500,200)
>>> quantizer = Quantizer.fit(traces)
>>> quantized_traces : npt.NDArray[np.int16] = quantizer.quantize(traces)
>>> # Can be reused directly on 5000 new traces for instance
>>> traces : npt.NDArray[np.floating] = np.random.randn(5000,200)
>>> quantized_traces : npt.NDArray[np.int16] = quantizer.quantize(traces)

Methods

fit(traces[, method])

Compute the shift and scale estimation from sample of traces

quantize(traces[, clip])

Quantize the traces provide in traces

classmethod fit(traces, method=<scalib.preprocessing.quantization.QuantFitMethod object>)[source]#

Compute the shift and scale estimation from sample of traces

This class method returns an instance of Quantizer with the corresponding shift and scale.

Parameters:
  • traces (array_like, np.floating) – Array that contains the traces to estimate the shift and scale in the quantization. The array must be of dimension (n, ns)

  • method (QuantFitMethod) – A member of QuantFitMethod enum class that specifies how the minimum and maximum value of the trace to be quantized is estimated.

quantize(traces, clip=False)[source]#

Quantize the traces provide in traces

Parameters:
  • traces (array_like, np.floating) – Array that contains the traces to be quantized into int16. The array must be of dimension (n, ns)

  • clip (bool) – Boolean to bypass the overflow check prior to quantization and clip the overflowing values to the boundaries. By default it is set to False.

Return type:

numpy.typing.NDArray.numpy.int16