Skip to content

Process XOR/ROL optimisations #28

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
82 changes: 61 additions & 21 deletions kaitaistruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
__version__ = '0.8'


if PY2:
range = xrange
def integers2bytes(ints):
return bytes(bytearray(ints))
def bytes2integers(data):
return bytearray(data)
else:
def integers2bytes(ints):
return bytes(ints)
def bytes2integers(data):
return data


class KaitaiStruct(object):
def __init__(self, stream):
self._io = stream
Expand Down Expand Up @@ -335,30 +348,57 @@ def bytes_terminate(data, term, include_term):

@staticmethod
def process_xor_one(data, key):
if PY2:
return bytes(bytearray(v ^ key for v in bytearray(data)))
else:
return bytes(v ^ key for v in data)
if key == 0:
return data

return integers2bytes(v ^ key for v in bytes2integers(data))

@staticmethod
def process_xor_many(data, key):
if PY2:
return bytes(bytearray(a ^ b for a, b in zip(bytearray(data), itertools.cycle(bytearray(key)))))
else:
return bytes(a ^ b for a, b in zip(data, itertools.cycle(key)))
if len(key) == 1:
return KaitaiStream.process_xor_one(data, ord(key))
if len(key) <= 64 and key == b'\x00' * len(key):
Copy link
Contributor

@KOLANICH KOLANICH Apr 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 64? Why not, for example a quarter of message length?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its to make it O(1) time, in case both key and data was megabyte sized. If so then it skips the check and just does the xoring.

Copy link
Contributor

@KOLANICH KOLANICH Apr 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its to make it O(1) time, in case both key and data was megabyte sized. If so then it skips the check and just does the xoring.

Again, IMHO it is waste of resources, I assume xoring to zero string as improbable, in most cases it is waste of resources.

let we have the probability of the zero array is p, N is the bit-length of data, K is the bit-length of key.

the complexity in the case of zero is
O(K)
(in fact 2*K because we create an array and then compare it)

otherwise

O(K+N)

So the average complexity of zero-checking case (we check key for zero) is

c=K+(1-p)*N

And the average complexity of non-checking case is always N.

So we need to select by the clause
K+(1-p)*N<N

K<p*N

Now let's model p.
If each bit of a key is randomly chosen from {1, 0} then
p = 2^-K

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read the reply below and withdraw this one review.

return data

@staticmethod
def process_rotate_left(data, amount, group_size):
if group_size != 1:
raise Exception(
"unable to rotate group of %d bytes yet" %
(group_size,)
)
return integers2bytes(a ^ b for a, b in zip(bytes2integers(data), itertools.cycle(bytes2integers(key))))

mask = group_size * 8 - 1
anti_amount = -amount & mask
# formula taken from: http://stackoverflow.com/a/812039
precomputed_single_rotations = {amount: [(i << amount) & 0xff | (i >> (8-amount)) for i in range(256)] for amount in range(1,8)}

r = bytearray(data)
for i in range(len(r)):
r[i] = (r[i] << amount) & 0xff | (r[i] >> anti_amount)
return bytes(r)
@staticmethod
def process_rotate_left(data, amount, group_size):
if group_size < 1:
raise Exception("group size must be at least 1 to be valid")

amount = amount % (group_size * 8)
if amount == 0:
return data

amount_bytes = amount // 8
data_ints = bytes2integers(data)

if group_size == 1:
translate = KaitaiStream.precomputed_single_rotations[amount]
return integers2bytes(translate[a] for a in data_ints)

if len(data) % group_size != 0:
raise Exception("data length must be a multiple of group size")

if amount % 8 == 0:
indices = [(i + amount_bytes) % group_size for i in range(group_size)]
return integers2bytes(data_ints[i+k] for i in range(0,len(data),group_size) for k in indices)

amount1 = amount % 8
amount2 = 8 - amount1
indices_pairs = [ ((i+amount_bytes) % group_size, (i+1+amount_bytes) % group_size) for i in range(group_size)]
return integers2bytes((data_ints[i+k1] << amount1) & 0xff | (data_ints[i+k2] >> amount2) for i in range(0,len(data),group_size) for k1,k2 in indices_pairs)

# NOTE: unused implementation, left for reference
#
# cap = (1 << 8 * group_size) - 1
# anti_amount = -amount & (8 * group_size - 1)
# for i in range(0,len(data),group_size):
# group = bytes2combinedinteger(data[i:i+group_size])
# group = (group << amount) & cap | (group >> anti_amount)
# r.append(combinedinteger2bytes(group, group_size))
# return b''.join(r)