Skip to content

Commit

Permalink
fix(crypt): Offset and size corrections.
Browse files Browse the repository at this point in the history
  • Loading branch information
wbrown committed Nov 3, 2023
1 parent 2aeade9 commit 15cb4a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
20 changes: 8 additions & 12 deletions tensorizer/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2570,37 +2570,35 @@ def encrypt_tensor() -> Tuple[Optional[bytes], Optional[bytes]]:
tensor_bytes = memoryview(tensor_memory.tobytes())
setup_end = time.monotonic()

chunk_repr = ""

for i in range(num_chunks):
# We XOR the nonce with the chunk index to avoid nonce reuse.
step_nonce = nonce_int ^ i
step_nonce_bytes = step_nonce.to_bytes(
nacl.secret.SecretBox.NONCE_SIZE, "big", signed=False
)
plaintext_begin = i * self._crypt_chunk_size
plaintext_begin = i * self._cleartext_chunk_size
plaintext_end = plaintext_begin + self._cleartext_chunk_size
if plaintext_end > tensor_memory.nbytes:
plaintext_end = tensor_memory.nbytes
to_encrypt = tensor_bytes[plaintext_begin:plaintext_end]
chunk = self._lockbox.encrypt(
to_encrypt,
step_nonce_bytes,
)
).ciphertext
cryptotext_begin = i * self._crypt_chunk_size
cryptotext_end = cryptotext_begin + len(chunk)
cryptotext[cryptotext_begin:cryptotext_end] = chunk
encryption_header_size = len(chunk) - len(to_encrypt)
if i == 0:
print(chunk)
end = time.monotonic()
duration_setup_ms = (setup_end - start) * 1000
duration_ms = (end - start) * 1000
print(
f"Salt: {self._salt} - Key: {self._crypto_key} - Nonce: {nonce}"
)
print(
f"Encryption time: {duration_ms:.2f}ms, "
f"setup: {duration_setup_ms:.2f}ms, "
f"{cryptotext_end} bytes, {encryption_header_size} header size"
f"Pos: {tensor_pos} - Size: {tensor_size} - Encryption time:"
f" {duration_ms:.2f}ms, setup: {duration_setup_ms:.2f}ms,"
f" {cryptotext_end} bytes, {encryption_header_size} header"
f" size, {chunk_repr}"
)

return nonce, cryptotext[:cryptotext_end]
Expand Down Expand Up @@ -2633,8 +2631,6 @@ def commit_header(
(encrypt_task, crc32_task, sha256_task, commit_header_task)
)

encrypt_task = self._computation_pool.submit(encrypt_tensor)

# This task is I/O-bound and has no prerequisites,
# so it goes into the regular writer pool.
def write_tensor_data():
Expand Down
9 changes: 3 additions & 6 deletions tensorizer/stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,7 @@ def __init__(
self._lockbox = nacl.secret.SecretBox(key)
self._nonce_int = int.from_bytes(nonce, "big", signed=False)
self._chunk_size = chunk_size
self._ciphertext_chunk_sz = (
chunk_size + self._lockbox.MACBYTES + self._lockbox.NONCE_SIZE
)
self._ciphertext_chunk_sz = chunk_size + self._lockbox.MACBYTES
self._ciphertext_buffer = bytearray(self._ciphertext_chunk_sz)

def __enter__(self):
Expand All @@ -233,9 +231,7 @@ def readinto(self, ba: bytearray) -> int:
num_chunks = goal // self._chunk_size
if goal % self._chunk_size:
num_chunks += 1
ciphertext_goal = goal + (
num_chunks * (self._lockbox.MACBYTES + self._lockbox.NONCE_SIZE)
)
ciphertext_goal = goal + (num_chunks * self._lockbox.MACBYTES)

step = 0
while ciphertext_offset < ciphertext_goal:
Expand All @@ -260,6 +256,7 @@ def readinto(self, ba: bytearray) -> int:
ba[plaintext_offset : plaintext_offset + plaintext_sz] = (
self._lockbox.decrypt(ciphertext, step_nonce_bytes)
)
step += 1
ciphertext_offset += ciphertext_read_sz
plaintext_offset += plaintext_sz

Expand Down

0 comments on commit 15cb4a1

Please sign in to comment.