scalib.metrics.SNR#

class scalib.metrics.SNR(nc, use_64bit=False)[source]#

Computes the Signal-to-Noise Ratio (SNR) between the traces and the intermediate values. Informally, SNR allows to quantify the amount of information about a random variable \(X\) contained in the mean of the leakage \(L_X\). High SNR means more information contained in the mean. The SNR metric is defined with the following equation [Man04]:

\[\mathrm{SNR} = \frac{\mathrm{Var}_{x\leftarrow X}(\mathrm{E}[L_x])} {\mathrm{E}_{x\leftarrow X}(\mathrm{Var}[L_x])}\]

The SNR is estimated from leakage samples \(L_x\) and the value x of the random variable.

Parameters:
  • nc (int) – Number of possible values for the random variable \(X\) (e.g., 256 for 8-bit target). nc must be between \(2\) and \(2^{16}\) (included).

  • use_64bit (bool (default False)) – Use 64 bits for intermediate sums instead of 32 bits. When using 64-bit sums, SNR can accumulate up to \(2^{32}\) traces, while when 32-bit sums are used, the bound is \(n_i < 2^{32}/b\), where b is the maximum absolute value of a sample rounded to the next power of 2, and \(n_i\) is the maximum number of times a variable can take a given value. Concretely, the total number of traces n should be at most \((nc \cdot 2^{32}/b) - k `, where :math:`k = O(\sqrt{n})\), typ. \(k>=3*\sqrt{n}\) (see https://mathoverflow.net/a/273060).

Examples

>>> from scalib.metrics import SNR
>>> import numpy as np
>>> # 500 traces of 200 points, 8-bit samples
>>> traces = np.random.randint(0,256,(500,200),dtype=np.int16)
>>> # 10 variables on 4 bit (16 classes = 2^4)
>>> x = np.random.randint(0,16,(500,10),dtype=np.uint16)
>>> snr = SNR(nc=16)
>>> snr.fit_u(traces,x)
>>> snr_val = snr.get_snr()

Notes

[Man04]

“Hardware Countermeasures against DPA ? A Statistical Analysis of Their Effectiveness”, Stefan Mangard, CT-RSA 2004: 222-235

Methods

fit_u(traces, x)

Updates the SNR estimation with samples of traces for the classes x.

get_snr()

Return the current SNR estimation with an array of shape (np,ns).

fit_u(traces, x)[source]#

Updates the SNR estimation with samples of traces for the classes x. This method may be called multiple times.

Parameters:
  • traces (numpy.typing.NDArray.numpy.int16) – Array that contains the leakage traces. The array must be of dimension (n, ns).

  • x (numpy.typing.NDArray.numpy.uint16) – Labels for each trace. Must be of shape (n, nv).

get_snr()[source]#

Return the current SNR estimation with an array of shape (np,ns).

Return type:

numpy.typing.NDArray.numpy.float64