def decode_from_reader(self, reader: serialization.BinaryReader) -> EllipticCurve.ECPoint: """ Raises: NotImplementedError: if an unsupported point encoding is used TypeError: if unexpected encoding is read """ try: f = reader.read_byte() except ValueError: return self.Infinity f = int.from_bytes(f, "little") if f == 0: return self.Infinity # these are compressed if f == 2 or f == 3: yTilde = f & 1 data = bytearray(reader.read_bytes(32)) data.reverse() data.append(0) X1 = int.from_bytes(data, 'little') return self.decompress_from_curve(X1, yTilde) # uncompressed or hybrid elif f == 4 or f == 6 or f == 7: raise NotImplementedError() raise ValueError(f"Invalid point encoding: {f}")
def deserialize_binary(reader: BinaryReader) -> Any: deserialized_items = [] underserialized = 1 while underserialized > 0: stack_type = reader.read_byte() if stack_type == StackItemType.Any: deserialized_items.append(None) elif stack_type == StackItemType.Boolean: deserialized_items.append(reader.read_bool()) elif stack_type == StackItemType.Integer: value_in_bytes = reader.read_var_bytes() deserialized_items.append(Integer.from_bytes(value_in_bytes)) elif stack_type in (StackItemType.ByteString, StackItemType.Buffer): deserialized_items.append(reader.read_var_bytes()) elif stack_type in (StackItemType.Array, StackItemType.Struct): count = reader.read_var_int() deserialized_items.append(list(range(count))) underserialized += count elif stack_type == StackItemType.Map: count = reader.read_var_int() deserialized_items.append({key: None for key in range(count)}) underserialized += count * 2 else: raise ValueError underserialized -= 1 stack_temp = [] while len(deserialized_items) > 0: item = deserialized_items.pop() if isinstance(item, list): new_item = [] for _ in range(len(item)): new_item.append(stack_temp.pop()) item = new_item elif isinstance(item, dict): new_item = {} for _ in range(0, len(item), 2): key = stack_temp.pop() value = stack_temp.pop() new_item[key] = value item = new_item stack_temp.append(item) return stack_temp.pop()