Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify decode vector #61

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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