scalib.attacks.GenFactor#

class scalib.attacks.GenFactor(kind, factor)[source]#

Generic factor which can implement arbitrary functions between variables. A factor can be declared as MULTI if the factor changes between executions. For most use cases, where the implementation of the factor is static, SINGLE is sufficient. An example of a “butterfly” factor which computes the sum and difference of the inputs is shown below.

>>> # Describe and generate the SASCAGraph
>>> graph_desc = '''
... NC 13 # arbitrary prime number
... VAR MULTI a # inputs
... VAR MULTI b
... VAR MULTI c # outputs
... VAR MULTI d
... GENERIC SINGLE f # declare the factor
... PROPERTY F0: f(a,b,c,d) # declare the relationship c = a + b and d = a - b'''
>>> factor = []
>>> nc = 13
>>> # Construct the factor table
>>> for a in range(nc):
...     for b in range(nc):
...         factor.append([a, b, (a + b) % nc, (a - b) % nc])
>>> factor = GenFactor.sparse_functional(np.array(factor, dtype=np.uint32))
>>> graph = FactorGraph(graph_desc)
>>> bpstate = BPState(graph, nexec=1, gen_factors={"f": factor})

Notes

In general, the computational cost of sparse_functional factors is linear in the number of rows. For dense factors, the cost is exponential in the number of axes. Therefore, one should prefer to use built-in functions (^, &, !, etc) whenever possible to minimize the performance penalty of generic factors, and to keep the number entries in the factor small when they must be used.

Methods

dense(factor)

A dense factor.

sparse_functional(factor)

A sparse functional factor.

Attributes

Parameters:
class GenFactorKind(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Choices of generic factor types

DENSE = 0#
SPARSE_FUNCTIONAL = 1#
kind: GenFactorKind#
factor: numpy.typing.NDArray#
classmethod dense(factor)[source]#

A dense factor.

factor is a n-dimensional array, each axis corresponds to one variable, entries in the array are probabilities.

Parameters:

factor (numpy.typing.NDArray.numpy.float64) –

classmethod sparse_functional(factor)[source]#

A sparse functional factor.

factor is a 2D array, each row corresponding to an entry in the factor, and in each row, the values are the values of the variables. Each row corresponds to setting the factor entry to 1. Omitting a factor entry implicitly assigns that entry to 0. Sparse factors leads to better performance and lower memory usage if the number of rows is much smaller than the total number of entries in the factor.

Parameters:

factor (numpy.typing.NDArray.numpy.uint32) –