Auxilary

Auxilary module

class fips.FIPS204.auxilary.AUXILARY(parameter)[source]

Bases: object

This class provides subroutines utilized by MLDSA, including function for data-type converstions and arithmetic.

Parameters:

parameter (dict[str, int])

IntegerToBits(x, alpha)[source]

Algorithm 9

Computes the base-2 representation of x mod 2^alpha in little-endian order.

Parameters:
  • x (int) – A non-negative integer.

  • alpha (int) – Number of bits to represent.

Returns:

Bitstring of length alpha in little-endian order.

Return type:

str (bits)

Raises:
  • ValueError – If x is negative or alpha is not a positive integer.

  • ValueError – If x is too big to be represented in alpha bits.

  • TypeError – If x or alpha is not an integer.

BitsToInteger(y, alpha)[source]

Algorithm 10

Computes the integer value expressed by a bit string using little-endian order.

Parameters:
  • y (str) – Bitstring to convert to an integer.

  • alpha (int) – Number of bits to consider.

Returns:

The integer value represented by the bitstring.

Return type:

int

Raises:
  • ValueError – If the length of y does not match alpha or if y contains invalid characters.

  • TypeError – If y is not a string or alpha is not an integer.

IntegerToBytes(x, alpha)[source]

Algorithm 11

Computes a base-256 representation of x mod 256^alpha in little-endian order.

Parameters:
  • x (int) – A non-negative integer.

  • alpha (int) – Number of bytes in the output.

Returns:

Bytestring of length alpha in little-endian order.

Return type:

bytes

Raises:

ValueError – x = _ cannot be represented in _ bytes.

BitsToBytes(y)[source]

Algorithm 12

Converts a bitstring y into a bytestring using little-endian order.

Parameters:

y (str) – Bitstring consisting of 0 and 1.

Returns:

Bytestring of length ceil (len(y) / 8).

Return type:

bytes

Raises:
  • TypeError – If y is not a string.

  • ValueError – If y contains characters other than 0 or 1.

BytesToBits(z)[source]

Algorithm 13

Converts a bytestring z into a bitstring in little-endian order.

Parameters:

z (bytes) – A bytestring.

Returns:

A bitstring of length 8 * len(z), in little-endian order.

Return type:

str

Raises:

TypeError – If z is not a bytes object.

CoeffFromThreeBytes(b0, b1, b2)[source]

Algorithm 14

Generates an element of {0, 1, 2, … , q - 1} U { None }

Parameters:
  • b0 (int) – first byte

  • b1 (int) – second byte

  • b2 (int) – third byte

Returns:

sampled coefficient or None if rejected.

Return type:

int | None

Raises:

TypeError – if any of b0, b1, b2 is not an integer.

CoeffFromHalfByte(b)[source]

Algorithm 15

Let eta ∈ {2, 4}.

Generates an element of {-eta, -eta + 1, … , eta} U { None }

Parameters:

b (int) – an integer in the range 0 - 15.

Returns:

sampled coefficient or None if rejected.

Return type:

int | None

Raises:
  • TypeError – if b is not an integer.

  • ValueError – if b is not in the range 0 - 15.

CenteredModulus(z)[source]

Additional Helper Function 1

Computes the centered modulus z mod± q.

Maps each integer x to the unique r in

[-(q-1)/2, (q-1)/2]

such that

x ≡ r (mod q).
Parameters:

z (int) – An integer

Return type:

int

Returns:

CenteredModulus(int)

Raises:

TypeError – If z is not an integer.

CenteredModulusList(z)[source]

Additional Helper Function 2

Computes the centered modulus z mod± q for a list of Integers.

Maps each integer x to the unique r in

[-(q-1)/2, (q-1)/2]

such that

x ≡ r (mod q).
Parameters:

z (list[int]) – A list of integers.

Return type:

list[int]

Returns:

CenteredModulus(list[int])

Raises:

TypeError – If z is not a list[int].

CenteredModulusMatrix(z)[source]

Additional Helper Function 3

Computes the centered modulus z mod± q for a matrix of Integers.

Maps each integer x to the unique r in

[-(q-1)/2, (q-1)/2]

such that

x ≡ r (mod q).
Parameters:

z (list[list[int]]) – A matrix of integers.

Return type:

list[list[int]]

Returns:

CenteredModulus(list[list[int]])

Raises:

TypeError – If z is not a list[list[int]].

abs_for_list(z)[source]

Additional Helper Function 4

Computes the absolute values of a list of integers.

Parameters:

z (list[int]) – A list of integers.

Returns:

A list containing the absolute values of the input integers.

Return type:

list[int]

InfinityNorm(z)[source]

Additional Helper Function 5

Compute the L-Infinity Norm of a Matrix.

Parameters:

z (list[list[int]]) – A Matrix of integers.

Returns:

The infinity norm (maximum absolute value) among all lists.

Return type:

int

Raises:
  • TypeError – If z is not a list of lists of integers.

  • TypeError – If any element in the sublists is not an integer.

  • TypeError – If elements of z[x] are not lists.

CalcOnes(h)[source]

Additional Helper Function 6

Compute the number of 1 s inside a list[list[int]] .

Parameters:

h (list[list[int]]) – A matrix containing 0 s and 1 s.

Returns:

The count of 1 s in the matrix.

Return type:

int

Raises:
  • TypeError – If h is not a matrix of integers.

  • TypeError – If any element in the sublists is not an integer .

  • TypeError – If elements of h[x] are not 0 or 1.