def ecrecover_to_pub(rawhash, v, r, s): try: pk = coincurve.PublicKey.from_signature_and_message( zpad(concrete_int_to_bytes(r), 32) + zpad(concrete_int_to_bytes(s), 32) + ALL_BYTES[v - 27], rawhash, hasher=None, ) pub = pk.format(compressed=False)[1:] except BaseException: pub = b"\x00" * 64 return pub
def mstore_(self, global_state): state = global_state.mstate op0, value = state.stack.pop(), state.stack.pop() try: mstart = util.get_concrete_int(op0) except AttributeError: logging.debug("MSTORE to symbolic index. Not supported") return [global_state] try: state.mem_extend(mstart, 32) except Exception: logging.debug("Error extending memory, mstart = " + str(mstart) + ", size = 32") logging.debug("MSTORE to mem[" + str(mstart) + "]: " + str(value)) try: # Attempt to concretize value _bytes = util.concrete_int_to_bytes(value) i = 0 for b in _bytes: state.memory[mstart + i] = _bytes[i] i += 1 except: try: state.memory[mstart] = value except: logging.debug("Invalid memory access") return [global_state]
def write_word_at(self, index: int, value: Union[int, BitVec, bool, Bool]) -> None: """Writes a 32 byte word to memory at the specified index` :param index: index to write to :param value: the value to write to memory """ try: # Attempt to concretize value if isinstance(value, bool): _bytes = (int(1).to_bytes(32, byteorder="big") if value else int(0).to_bytes(32, byteorder="big")) else: _bytes = util.concrete_int_to_bytes(value) assert len(_bytes) == 32 self[index:index + 32] = list(bytearray(_bytes)) except (Z3Exception, AttributeError): # BitVector or BoolRef value = cast(Union[BitVec, Bool], value) if isinstance(value, Bool): value_to_write = If( value, symbol_factory.BitVecVal(1, 256), symbol_factory.BitVecVal(0, 256), ) else: value_to_write = value assert value_to_write.size() == 256 for i in range(0, value_to_write.size(), 8): self[index + 31 - (i // 8)] = Extract(i + 7, i, value_to_write)
def may_write_to(self, z3_index, z3_value, storage, constraint_list, consider_length): if consider_length: may_write_to, added_constraints = ConcretSlot.may_write_to(self, z3_index, z3_value, storage, constraint_list, consider_length) if may_write_to: return may_write_to, added_constraints slot_hash = utils.sha3(utils.bytearray_to_bytestr(util.concrete_int_to_bytes(self.slot_counter))) slot_hash = str(BitVecVal(util.concrete_int_from_bytes(slot_hash, 0), 256)) z3_index_str = str(z3_index).replace("\n", "") if z3_index_str.startswith(slot_hash): if z3_index_str.endswith(slot_hash) or z3_index_str[len(slot_hash):].startswith(" +"): return True, [] if z3_index_str in keccak_map: unresolved_index = keccak_map[str(z3_index).replace("\n" , "")] unresolved_index = str(unresolved_index).replace("\n", "") if unresolved_index.startswith(str(self.slot_counter)): if unresolved_index.endswith(str(self.slot_counter)) or unresolved_index[len(str(self.slot_counter)):].startswith(" +"): return True, [] return False, []