Pack

Pack module

class fips.FIPS204.pack.PACK(parameter)[source]

Bases: object

These functions efficiently translate an element w belonging to R into a byte string and vica versa under the assumption that the coefficients of w are in a restricted range.

Parameters:

parameter (dict[str, int])

SimpleBitPack(w, b)[source]

Algorithm 16

Encodes a polynomial w into a byte string.

Parameters:
  • w (list[int]) – Polynomial coefficients, must be of length 256.

  • b (int) – Upper bound of coefficients. Must be 1.

Returns:

bytestring of length 32 * bitlen(b), representing packed bits.

Return type:

bytes

Raises:
  • TypeError – If types are invalid.

  • ValueError – If values/lengths are out of expected bounds.

BitPack(w, a, b)[source]

Algorithm 17

Encodes a polynomial w into a bytestring. :param w: List of 256 polynomial coefficients in [-a, b]. :type w: list[int] :param a: Lower bound (non-negative). :type a: int :param b: Upper bound (non-negative). :type b: int

Returns:

Packed byte string of length 32 * bitlen(a + b).

Return type:

bytes

Raises:
  • TypeError – If w is not a list of integers or a/b are not integers.

  • ValueError – If length of w 256, or any coefficient is out of range.

Parameters:
  • w (list[int])

  • a (int)

  • b (int)

SimpleBitUnpack(v, b)[source]

Algorithm 18

Reverses a procedure SimpleBitPack.

Parameters:
  • v (bytes) – Byte string of length 32 * bitlen(b), result of simple_bit_pack.

  • b (int) – Upper bound for the original coefficients (must be 1).

Returns:

List of 256 unpacked coefficients in range [0, b].

Return type:

list[int]

Raises:
  • TypeError – If v is not bytes or b is not integer.

  • ValueError – If v has invalid length or b is invalid.

BitUnpack(v, a, b)[source]

Algorithm 19

Reverses the procedure BitPack.

Parameters:
  • v (bytes) – Packed byte string (length = 32 * bitlen(a + b)).

  • a (int) – Non-negative integer (lower bound).

  • b (int) – Non-negative integer (upper bound).

Returns:

List of 256 coefficients in the range [-a, b].

Return type:

list[int]

Raises:
  • TypeError – If input types are incorrect.

  • ValueError – If v is invalid length or a/b are negative.

HintBitPack(h)[source]

Algorithm 20

Encodes a polynomial vector h with binary coefficients into a bytestring.

Parameters:

h (list[list[int]]) – A list of k polynomials, each of 256 binary coefficients.

Returns:

Byte string of length omega + k representing the packed hint vector.

Return type:

bytes

Raises:

ValueError – If h is malformed or contains more than omega number of 1s.

HintBitUnpack(y)[source]

Algorithm 21

Reverses the procedure HintBitPack.

Parameters:

y (bytes) – Bytestring of length omega + k that encodes h.

Returns:

h, a list of k polynomials, each being a list of w-bit coefficients.

Return type:

Matrix (list[list[int]])

Raises:

ValueError – If y is malformed or does not conform to expected structure.