def _create_keys(key_bits: bitarray) -> list: while len(key_bits) < 56: key_bits.append(0) key_bits.insert(7, 1) key_bits.insert(15, 1) key_bits.insert(23, 1) key_bits.insert(31, 1) key_bits.insert(39, 1) key_bits.insert(47, 1) key_bits.insert(55, 1) key_bits.insert(63, 1) round_keys = list() c0 = bitarray(28) c0.setall(False) d0 = bitarray(28) d0.setall(False) for ind in range(28): c0[ind] = key_bits[tables.extend_key_permutation_c[ind]] d0[ind] = key_bits[tables.extend_key_permutation_d[ind]] for shift_ind in range(16): ci = _shift_left(c0, tables.cyclic_shift[shift_ind]) di = _shift_left(d0, tables.cyclic_shift[shift_ind]) c0 = ci.copy() d0 = di.copy() ci.extend(di) ki = bitarray(48) for ind in range(0, 48): ki[ind] = ci[tables.key_bits_positions[ind]] round_keys.append(ki) return round_keys
def __calculateAdjacent(bits: bitarray, type): positive = bits.copy() negative = bits.copy() length = bits.length() index = -1 count = bits.count() carrySubtract = True carryAdd = True if count == length and type == 'lat': carryAdd = False elif count == 0 and type == 'lat': carrySubtract = False while (carrySubtract or carryAdd) and abs(index) <= length: value = bits[index] if carrySubtract: if value: negative[index] = False carrySubtract = False else: negative[index] = True if carryAdd: if value: positive[index] = False else: positive[index] = True carryAdd = False index += -1 return negative, positive
def shr(data: bitarray.bitarray, dist: int): # shift right data = data.copy() for a in range(dist): data.insert(0, 0) data.pop() return data
def apply_bitmask_to_list(reqIdrs: List, mask: bitarray): if mask.length() == 0: return reqIdrs, [] if len(reqIdrs) != mask.length(): raise LogicError( "Length of reqIdr list and bitmask is not the same") return BitmaskHelper.get_valid_reqs( reqIdrs, mask), BitmaskHelper.get_invalid_reqs(reqIdrs, mask)
def decode(binary_data, symbol_to_encoding_dict): padding = 0 bit_array = BitArray() bit_array.frombytes(binary_data) while bool(bit_array[padding]): padding += 1 decoded_chars = bit_array[padding+1:].decode(symbol_to_encoding_dict) return ''.join(decoded_chars)
def encode(string, symbol_to_encoding_dict): string_len = 0 for symbol in string: string_len += len(symbol_to_encoding_dict[symbol]) padding = 8 - (string_len % 8) bit_array = BitArray((padding - 1) * '1' + '0') for symbol in string: bit_array += BitArray(symbol_to_encoding_dict[symbol]) return bit_array.tobytes()
def bitarray_to_uint(array: bitarray, endian: str = "little") -> int: if endian == "little": bits = array.to01() return int(bits[::-1], 2) elif endian == "big": return int(array.to01(), 2) else: raise UnexpectedException( "Endian can only be 'little' or 'big'. Was: {}".format(endian) )
def store(self, address: int, size: str, value: bitarray): points = self._get_pixels_by_address(address) tmp = value[value.length() - 8:value.length()] tmp_pos = 0 for point in points: self._image.setPixel( point[0], point[1], int(tmp[tmp_pos:tmp_pos + self._mode.depth].to01(), 2)) tmp_pos += self._mode.depth if size == 'word': self.store(address=address + 1, size="byte", value=value[0:8])
def __zipBits(lng: bitarray, lat: bitarray): latindex = 0 lngindex = 0 length = lng.length() + lat.length() bits = bitarray(length) for index in range(length): if index % 2 == 0: bits[index] = lng[lngindex] lngindex += 1 else: bits[index] = lat[latindex] latindex += 1 return bits
def __init__(self, block: Block, flags: bitarray): hashes = [t.hash() for t in block.transactions] tree = crypto.MerkleTree(hashes) self.flags = flags.tobytes() self.tx_count = len(hashes) self.hashes = tree.to_hash_array() self.header = block.header
def count_one_until_zero(bits : bitarray) -> int: """ 从左往右数一个比特串中1的数量,数到第一个不为0的数时停止计数 """ bits_list = bits.tolist() one_count = 0 while (one_count < 32 and bits_list[one_count] == 1): one_count += 1 return one_count
def create(cls, block: Block, flags: bitarray) -> MerkleBlockPayload: """ Create payload. Should be used instead of directly calling the initializer. Args: block: flags: """ hashes = [block.consensus_data.hash() ] + [t.hash() for t in block.transactions] tree = crypto.MerkleTree(hashes) flag_bytes = flags.tobytes() return cls(version=block.version, prev_hash=block.prev_hash, merkle_root=block.merkle_root, timestamp=block.timestamp, index=block.index, next_consensus=block.next_consensus, witness=block.witness, content_count=len(hashes), hashes=tree.to_hash_array(), flags=flag_bytes)
def make_frame(sound_sample: bitarray, sample_index): """ Makes a frame given sample bits and user index :param sound_sample: bit array containing the sample :param sample_index: Index of the current sample in the frame :return: the frame containing sound sample and framing bit """ return sound_sample.insert(FRAMING_BITS[sample_index])
def __splitBits(bits: bitarray): lats = bitarray() lngs = bitarray() for index in range(bits.length()): if index % 2 == 0: lngs.append(bits[index]) else: lats.append(bits[index]) return (lngs, lats)
def __format_bitarray_by_block(bits: bitarray, block_size: int = 8) -> str: """Separates bits by blocks of length specified by **block_size** parameter. :param bits: Bitarray object that will be formatted. :param block_size: Block length. :return: String containing the blocks separated by space character. """ bits_str = bits.to01() return ' '.join(bits_str[idx * block_size: (idx + 1) * block_size] for idx in range(len(bits) // block_size))
def generate_delta_inv(element: bitarray): if len(element) == 1: return 0 zero_counter = 0 while not element[0]: element.pop(0) zero_counter += 1 element.pop(0) for i in range(zero_counter): element.pop(0) element.insert(0, True) return ba2int(element) - 1
def printRecur(self, node: Node, path: bitarray = bitarray(), length=0, point: bool = None): if not (node is None): if not (point is None): if len(path) <= length: path.append(point) else: path[length] = point length += 1 if len(node.children) == 0: print(node.data['l']) print(path[:length]) else: for i in range(len(node.children)): self.printRecur(node.children[i], path, length, bool(i))
def __init__(self, block: Block, flags: bitarray): super(MerkleBlockPayload, self).__init__(block.version, block.prev_hash, block.timestamp, block.index, block.next_consensus, block.witness, block.merkle_root) hashes = [block.consensus_data.hash() ] + [t.hash() for t in block.transactions] tree = crypto.MerkleTree(hashes) self.flags = flags.tobytes() self.content_count = len(hashes) self.hashes = tree.to_hash_array()
def _encode(self, x: bitarray) -> bitarray: x = int(x.to01(), 2) x_pow3 = self.F.Multiply(self.F.Multiply(x, x), x) x_pow3 = int2bitarray(x_pow3, self.k) redundancy = bitarray('0' * self.r) red_idx = 0 for idx, val in enumerate(x_pow3): if self.PV[idx] == 1: redundancy[red_idx] = val red_idx += 1 return redundancy
def __get_locations(self, serialized_item: bytes, fingerprint: bitarray): """ Applies a partial-key hashing scheme, which applies a clever, reversible transformation to the first computed location. l2 = l1 xor fingerprint -> l1 = l2 xor fingerprint. Therefore we can calculate alternative locations of cuckoo items even when storing only short fingerprints """ location1 = self.__hasher.hash( serialized_item) % self.__number_of_buckets hashed_fingerprint = self.__hasher.hash(fingerprint.tobytes()) location2 = (location1 ^ hashed_fingerprint) % self.__number_of_buckets return [location1, location2]
def __init__(self, k: int, r: int, pv: bitarray = None, gen: list = None): """ pv: Punching vector. len(P)=k, w(P)=r gen: generating polynomial in binary vector form. """ super().__init__(k=k, r=r, name='PC') if pv is None: pv = bitarray([0 for _ in range(k - r)] + [1 for _ in range(r)]) if pv.count(1) != r or pv.count(0) != k - r: raise Exception( '{pv} can not be used as punching vector (k={k}, r={r})'. format(pv=pv, k=k, r=r)) self.PV = pv if gen is None: self.F = ffield.FField(k) else: if not isinstance(gen, list) or len( gen) != k + 1 or gen.count(0) + gen.count(1) != len(gen): raise Exception('generator polynom error') gen = binlist2int(gen) self.F = ffield.FField(k, gen, useLUT=0) # create GF(2^k)
def pad(message: bitarray.bitarray): # This assumes all fits into only 1 message of 512 base = message.copy() ks = 448 - 1 - len(message) while ks < 0: ks += block_size base.append(1) for _ in range(ks): base.append(0) extra = "{0:b}".format(len(message)) extra = "0" * (64 - len(extra)) + extra return base + bitarray.bitarray(extra)
def decompress(cmpr_text: bitarray, tree: HuffmanTree): cmpr_text = cmpr_text.to01() root = tree.root itr = root text = "" for bit in cmpr_text: itr = itr.leftChild if bit == '0' else itr.rightChild value = itr.symbol.value if value is not None: text += value itr = root return text
def _pack_data(encoded_data: bitarray, checksum: bytes, codes_table: dict): packed_data = bytearray() packed_data.extend(checksum) serialized_table = json.dumps({int(i): codes_table[i].to01() for i in codes_table}).encode() packed_data.extend(struct.pack('I', len(serialized_table))) packed_data.extend(serialized_table) # это и есть по сути длина блока packed_data.extend(struct.pack('I', len(encoded_data))) packed_data.extend(encoded_data.tobytes()) return packed_data
def bits_ip_to_str(bit_ip : bitarray) -> str: """ 将比特形式的ip转成字符串形式,形如 '192.168.3.5' """ bytes_ip = bit_ip.tobytes() ip_num_list = struct.unpack('!BBBB', bytes_ip) str_ip = '' # 首次循环标志,如果不是首次循环的话就加一个'.' first_flag = 1 for i in range(0,4): if(1-first_flag): str_ip += '.' str_ip += str(ip_num_list[i]) first_flag = 0 return str_ip
def decode(self, coded_text: ba.bitarray) -> str: """ docstring """ reversed_dict = {v.to01(): k for k, v in self.code.items()} result = [] for i in range(0, coded_text.length(), self.code_length): try: char_code = coded_text[i:i + self.code_length].to01() result.append(reversed_dict[char_code]) except KeyError: continue return ''.join(result)
def addToCanonicalTree(self, node: Node, label, length, depth, maxWidth, codeword: bitarray) -> bool: if depth >= 0: if depth < length - 1: if len(node.children) == 0: node.children.append(Node()) codeword.append(bool(len(node.children) - 1)) added = self.addToCanonicalTree(node.children[-1], label, length, depth + 1, maxWidth, codeword) while not added: if len(node.children) >= maxWidth: if len(codeword) > 0: codeword.pop() return False node.children.append(Node()) codeword.append(bool(len(node.children) - 1)) added = self.addToCanonicalTree(node.children[-1], label, length, depth + 1, maxWidth, codeword) else: if len(node.children) >= maxWidth: if len(codeword) > 0: codeword.pop() return False node.children.append(Node(l=label)) codeword.append(bool(len(node.children) - 1)) return True return False
def __init__(self, g: ba.bitarray, n: int = 7, k: int = 4, t: int = 1, L: int = 0): if n - k + 1 != len(g): raise AttributeError(f"Polynomial g must have lenght n - k: {n} - {k} = {n - k}") self.g = g self.list_g = g.tolist() self.np_g = np.asarray(self.list_g, dtype=np.int8) self.k = k self.n = n self.t = t self.L = L self.bone = dict() self.n_k = n - k # n minus k self.extended_g = self.list_g.copy() self.extended_g.extend([0] * (n - k))
def _encrypt(data_bits: bitarray, key_bits: bitarray) -> list: while len(data_bits) < 64: data_bits.append(0) permutation_data = bitarray() for ind in range(0, len(data_bits)): permutation_data.append(data_bits[tables.initial_permutation[ind]]) entropies = list() keys = _create_keys(key_bits) part_l = permutation_data[:32] part_r = permutation_data[32:] entropies.append(_find_entropy(part_l, part_r)) for counter in range(16): li = part_r ri = part_l ^ _do_func_feistel(part_r, keys[counter]) part_l, part_r = li, ri entropies.append(_find_entropy(part_l, part_r)) one_part = part_l one_part.extend(part_r) res = _do_last_permutation(one_part) return [res, entropies]
def _pack_data(encoded_data: bitarray, checksum: bytes, codes_table: dict) -> bytes: """ Pack encoded data to write in file. Needs because in LZ77 we use Codewords instead of bytes. """ packed_data = bytearray() packed_data.extend(checksum) serialized_table = json.dumps({int(i): codes_table[i].to01() for i in codes_table}).encode() packed_data.extend(struct.pack('I', len(serialized_table))) packed_data.extend(serialized_table) packed_data.extend(struct.pack('I', len(encoded_data))) packed_data.extend(encoded_data.tobytes()) return bytes(packed_data)