boolcrypt.modularaddition module

Auxiliary functions for the modular addition modulo a power of two.

boolcrypt.modularaddition.get_modadd_lut(wordsize, permuted=False)[source]

Get the LUT of the binary modular addition.

If permuted is True, returns (x, y) -> (x oxplus y, y).

>>> get_modadd_lut(2)
[0, 1, 2, 3, 1, 2, 3, 0, 2, 3, 0, 1, 3, 0, 1, 2]
>>> get_modadd_lut(2, permuted=True)
[0, 1, 2, 3, 5, 6, 7, 4, 10, 11, 8, 9, 15, 12, 13, 14]
boolcrypt.modularaddition.get_modadd_anf(wordsize, permuted=False, only_x_names=True)[source]

Return the anf of the modular addition.

If permuted is True, returns (x, y) -> (x oxplus y, y).

If only_x_names is True, the input variables are denoted by x0, x1, … Otherwise, the inputs are denoted by x0, x1,…,y0,y1,…

>>> for f in get_modadd_anf(2): print(f)
x0 + x2
x0*x2 + x1 + x3
>>> for f in get_modadd_anf(3, permuted=True, only_x_names=False): print(f)
x0 + y0
x0*y0 + x1 + y1
x0*x1*y0 + x0*y0*y1 + x1*y1 + x2 + y2
y0
y1
y2
boolcrypt.modularaddition.get_implicit_modadd_anf(wordsize, permuted=False, only_x_names=True)[source]

Return the quadratic anf of the implicit function of the modular addition.

Let F(x,y)=x oxplus y (F(x,y)=(x oxplus y,y) if permuted). The quadratic implicit function of oxplus is a quadratic function G such that Graph(F) = {(x, y) = G(x, y) = 0}.

For permuted=False, G(x, y, z) = (x ^ y ^ z ^ q(x ^ z, y ^ z)), where q(x, y) = (0, x_0 y_0, …, x_0 y_0 ^ … ^ x_{n-2} y_{n-2}). For permuted=True, G(x, y, z, y) same but with the extra component t ^ y.

>>> for f in get_implicit_modadd_anf(2): print(f)
x0 + x2 + x4
x0*x2 + x0*x4 + x1 + x2*x4 + x3 + x4 + x5
>>> for f in get_implicit_modadd_anf(3, permuted=True, only_x_names=False): print(f)
x0 + y0 + z0
x0*y0 + x0*z0 + x1 + y0*z0 + y1 + z0 + z1
x0*y0 + x0*z0 + x1*y1 + x1*z1 + x2 + y0*z0 + y1*z1 + y2 + z0 + z1 + z2
y0 + t0
y1 + t1
y2 + t2
boolcrypt.modularaddition.get_ccz_modadd_anf(wordsize, name, permuted=0, only_x_names=True, bpr=None, input_vars=None)[source]

Return a CCZ-equivalent function of the modular addition.

The argument name can take the values “q”, “Q”, “H” and “p” and the following quadratic CCZ functions are returned: - q(x, y) = (0, x_0 y_0, …, x_0 y_0 ^ … ^ x_{n-2} y_{n-2}) - Q(x, y) = x ^ y ^ q(x, y) - H(x, y) = x ^ q(x ^ y, y) - p(x, y) = (0, x_0 y_0, …, x_{n-2} y_{n-2})

If permuted=1 or permuted is True, appends the component ‘y’. If permuted=2, appends the component ‘0’.

If a BooleanPolynomialRing is given in bpr and a list of input variables (of x and y) of the same bpr are given in input_vars, then bpr is set as the parent of the CCZ-equivalent function.

>>> for f in get_ccz_modadd_anf(3, "q", only_x_names=False): print(f)
0
x0*y0
x0*y0 + x1*y1
>>> for f in get_ccz_modadd_anf(3, "H", only_x_names=False): print(f)
x0
x0*y0 + x1 + y0
x0*y0 + x1*y1 + x2 + y0 + y1
>>> for f in get_ccz_modadd_anf(3, "Q", only_x_names=False): print(f)
x0 + y0
x0*y0 + x1 + y1
x0*y0 + x1*y1 + x2 + y2
>>> for f in get_ccz_modadd_anf(3, "p", only_x_names=False): print(f)
0
x0*y0
x1*y1
>>> for f in get_ccz_modadd_anf(2, "q", permuted=2): print(f)
0
x0*x2
0
0
>>> for f in get_ccz_modadd_anf(2, "H", permuted=1): print(f)
x0
x0*x2 + x1 + x2
x2
x3
boolcrypt.modularaddition.get_admissible_mapping(wordsize, name, permuted=0, bpr=None)[source]

Return the admissible mapping L of G s.t. L(Graph(G))=Graph(modadd).

G is the function from get_ccz_modadd(wordsize, name, permuted),

>>> get_admissible_mapping(3, "q")
[1 0 0|0 0 0|1 0 0]
[0 1 0|0 0 0|0 1 0]
[0 0 1|0 0 0|0 0 1]
[-----+-----+-----]
[0 0 0|1 0 0|1 0 0]
[0 0 0|0 1 0|0 1 0]
[0 0 0|0 0 1|0 0 1]
[-----+-----+-----]
[1 0 0|1 0 0|1 0 0]
[0 1 0|0 1 0|0 1 0]
[0 0 1|0 0 1|0 0 1]
>>> get_admissible_mapping(3, "H")
[0 0 0|1 0 0|1 0 0]
[0 0 0|0 1 0|0 1 0]
[0 0 0|0 0 1|0 0 1]
[-----+-----+-----]
[1 0 0|1 0 0|1 0 0]
[0 1 0|0 1 0|0 1 0]
[0 0 1|0 0 1|0 0 1]
[-----+-----+-----]
[0 0 0|0 0 0|1 0 0]
[0 0 0|0 0 0|0 1 0]
[0 0 0|0 0 0|0 0 1]
>>> get_admissible_mapping(2, "q", permuted=2)
[0 0|1 0|1 0|0 0]
[0 0|0 1|0 1|0 0]
[---+---+---+---]
[1 0|0 0|1 0|0 0]
[0 1|0 0|0 1|0 0]
[---+---+---+---]
[1 0|1 0|1 0|0 0]
[0 1|0 1|0 1|0 0]
[---+---+---+---]
[1 0|0 0|1 0|1 0]
[0 1|0 0|0 1|0 1]
>>> get_admissible_mapping(2, "H", permuted=1)
[0 0|1 0|1 0|0 0]
[0 0|0 1|0 1|0 0]
[---+---+---+---]
[1 0|1 0|1 0|0 0]
[0 1|0 1|0 1|0 0]
[---+---+---+---]
[0 0|0 0|1 0|0 0]
[0 0|0 0|0 1|0 0]
[---+---+---+---]
[1 0|0 0|1 0|1 0]
[0 1|0 0|0 1|0 1]
boolcrypt.modularaddition.test_modadd_functions(wordsize_range=range(2, 5))[source]

Test the conversion functions

>>> test_modadd_functions()
True