Skip to content

Commit

Permalink
Merge pull request #61 from GiacomoPope/simplify_decode_vector
Browse files Browse the repository at this point in the history
simplify decode vector
  • Loading branch information
GiacomoPope authored Jul 23, 2024
2 parents 77d6e48 + c1bbf05 commit ffb428d
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/kyber_py/modules/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ def __init__(self):
self.matrix = MatrixKyber

def decode_vector(self, input_bytes, k, d, is_ntt=False):

if self.ring.n * d * k > len(input_bytes) * 8:
raise ValueError("Byte length is too short for given l")
# Ensure the input bytes are the correct length to create k elements with
# d bits used for each coefficient
if self.ring.n * d * k != len(input_bytes) * 8:
raise ValueError(
"Byte length is the wrong length for given k, d values"
)

# Bytes needed to decode a polynomial
chunk_length = 32 * d

# Break input_bytes into blocks of length chunk_length
poly_bytes = [
input_bytes[i : i + chunk_length]
for i in range(0, len(input_bytes), chunk_length)
]
n = 32 * d

# Encode each chunk of bytes as a polynomial, we iterate only the first k elements in case we've
# been sent too many bytes to decode for the vector
# Encode each chunk of bytes as a polynomial and create the vector
elements = [
self.ring.decode(poly_bytes[i], d, is_ntt=is_ntt) for i in range(k)
self.ring.decode(input_bytes[i : i + n], d, is_ntt=is_ntt)
for i in range(0, len(input_bytes), n)
]

return self.vector(elements)
Expand Down

0 comments on commit ffb428d

Please sign in to comment.