Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def ecmul(expr, args, kwargs, context):
placeholder_node = LLLnode.from_list(
context.new_placeholder(ByteArrayType(128)), typ=ByteArrayType(128), location='memory'
)
pos = getpos(expr)
o = LLLnode.from_list([
'seq',
['mstore', placeholder_node, avo(args[0], 0, pos)],
['mstore', ['add', placeholder_node, 32], avo(args[0], 1, pos)],
['mstore', ['add', placeholder_node, 64], args[1]],
['assert', ['call', ['gas'], 7, 0, placeholder_node, 96, placeholder_node, 64]],
placeholder_node,
], typ=ListType(BaseType('uint256'), 2), pos=pos, location='memory')
return o
def ecrecover(expr, args, kwargs, context):
placeholder_node = LLLnode.from_list(
context.new_placeholder(ByteArrayType(128)), typ=ByteArrayType(128), location='memory'
)
return LLLnode.from_list([
'seq',
['mstore', placeholder_node, args[0]],
['mstore', ['add', placeholder_node, 32], args[1]],
['mstore', ['add', placeholder_node, 64], args[2]],
['mstore', ['add', placeholder_node, 96], args[3]],
['pop', ['call', ['gas'], 1, 0, placeholder_node, 128, MemoryPositions.FREE_VAR_SPACE, 32]],
['mload', MemoryPositions.FREE_VAR_SPACE],
], typ=BaseType('address'), pos=getpos(expr))
return ABI_GIntM(128, True)
elif 'address' == t:
return ABI_Address()
elif 'bytes32' == t:
return ABI_BytesM(32)
elif 'bool' == t:
return ABI_Bool()
elif 'decimal' == t:
return ABI_FixedMxN(168, 10, True)
else:
raise CompilerPanic(f'Unrecognized type {t}')
elif isinstance(lll_typ, TupleLike):
return ABI_Tuple([abi_type_of(t) for t in lll_typ.tuple_members()])
elif isinstance(lll_typ, ListType):
return ABI_StaticArray(abi_type_of(lll_typ.subtype), lll_typ.count)
elif isinstance(lll_typ, ByteArrayType):
return ABI_Bytes(lll_typ.maxlen)
elif isinstance(lll_typ, StringType):
return ABI_String(lll_typ.maxlen)
else:
raise CompilerPanic(f'Unrecognized type {lll_typ}')
if len(args[1].elts) > 32:
raise TypeMismatchException("RLP list must have at most 32 items", expr)
# Get the output format
_format = []
for arg in args[1].elts:
if isinstance(arg, ast.Name) and arg.id == "bytes":
subtyp = ByteArrayType(args[0].typ.maxlen)
else:
subtyp = context.parse_type(arg, 'memory')
if not isinstance(subtyp, BaseType):
raise TypeMismatchException("RLP lists only accept BaseTypes and byte arrays", arg)
if not is_base_type(subtyp, ('int128', 'uint256', 'bytes32', 'address', 'bool')):
raise TypeMismatchException(f"Unsupported base type: {subtyp.typ}", arg)
_format.append(subtyp)
output_type = TupleType(_format)
output_placeholder_type = ByteArrayType(
(2 * len(_format) + 1 + get_size_of_type(output_type)) * 32,
)
output_placeholder = context.new_placeholder(output_placeholder_type)
output_node = LLLnode.from_list(
output_placeholder,
typ=output_placeholder_type,
location='memory',
)
# Create a decoder for each element in the tuple
decoder = []
for i, typ in enumerate(_format):
# Decoder for bytes32
if is_base_type(typ, 'bytes32'):
decoder.append(LLLnode.from_list(
[
'seq',
self.stmt)
sub = Expr(self.stmt.value, self.context).lll_node
# Disallow assignment to None
if isinstance(sub.typ, NullType):
raise InvalidLiteralException(
(
'Assignment to None is not allowed, use a default '
'value or built-in `clear()`.'
),
self.stmt
)
is_literal_bytes32_assign = (
isinstance(sub.typ, ByteArrayType)
and sub.typ.maxlen == 32
and isinstance(typ, BaseType)
and typ.typ == 'bytes32'
and sub.typ.is_literal
)
# If bytes[32] to bytes32 assignment rewrite sub as bytes32.
if is_literal_bytes32_assign:
sub = LLLnode(
bytes_to_int(self.stmt.value.s),
typ=BaseType('bytes32'),
pos=getpos(self.stmt),
)
self._check_valid_assign(sub)
self._check_same_variable_assign(sub)
def method_id(expr, args, kwargs, context):
if b' ' in args[0]:
raise TypeMismatchException('Invalid function signature no spaces allowed.')
method_id = fourbytes_to_int(keccak256(args[0])[:4])
if args[1] == 'bytes32':
return LLLnode(method_id, typ=BaseType('bytes32'), pos=getpos(expr))
elif args[1] == 'bytes[4]':
placeholder = LLLnode.from_list(context.new_placeholder(ByteArrayType(4)))
return LLLnode.from_list(
['seq',
['mstore', ['add', placeholder, 4], method_id],
['mstore', placeholder, 4], placeholder],
typ=ByteArrayType(4), location='memory', pos=getpos(expr))
else:
raise StructureException('Can only produce bytes32 or bytes[4] as outputs')
def _mk_getter_helper(typ, depth=0):
# Base type and byte array type: do not extend the getter function
# name, add no input arguments, add nothing to the return statement,
# output type is the base type
if isinstance(typ, BaseType):
return [("", "", "", repr(typ))]
elif isinstance(typ, ByteArrayType):
return [("", "", "", repr(typ))]
# List type: do not extend the getter name, add an input argument for
# the index in the list, add an item access to the return statement
elif isinstance(typ, ListType):
o = []
for funname, head, tail, base in _mk_getter_helper(typ.subtype, depth + 1):
o.append((funname, ("arg%d: int128, " % depth) + head, ("[arg%d]" % depth) + tail, base))
return o
# Mapping type: do not extend the getter name, add an input argument for
# the key in the map, add a value access to the return statement
elif isinstance(typ, MappingType):
o = []
for funname, head, tail, base in _mk_getter_helper(typ.valuetype, depth + 1):
o.append((funname, ("arg%d: %r, " % (depth, typ.keytype)) + head, ("[arg%d]" % depth) + tail, base))
return o
# Struct type: for each member variable, make a separate getter, extend
if not is_varname_valid(name, custom_units=custom_units):
raise FunctionDeclarationException("Function name invalid: " + name)
# Determine the arguments, expects something of the form def foo(arg1: int128, arg2: int128 ...
args = []
for arg in code.args.args:
typ = arg.annotation
if not typ:
raise InvalidTypeException("Argument must have type", arg)
if not is_varname_valid(arg.arg, custom_units=custom_units):
raise FunctionDeclarationException("Argument name invalid or reserved: " + arg.arg, arg)
if arg.arg in (x.name for x in args):
raise FunctionDeclarationException("Duplicate function argument name: " + arg.arg, arg)
parsed_type = parse_type(typ, None, sigs, custom_units=custom_units)
args.append(VariableRecord(arg.arg, pos, parsed_type, False))
if isinstance(parsed_type, ByteArrayType):
pos += 32
else:
pos += get_size_of_type(parsed_type) * 32
# Apply decorators
const, payable, private, public = False, False, False, False
for dec in code.decorator_list:
if isinstance(dec, ast.Name) and dec.id == "constant":
const = True
elif isinstance(dec, ast.Name) and dec.id == "payable":
payable = True
elif isinstance(dec, ast.Name) and dec.id == "private":
private = True
elif isinstance(dec, ast.Name) and dec.id == "public":
public = True
else:
def ecadd(expr, args, kwargs, context):
placeholder_node = LLLnode.from_list(
context.new_placeholder(ByteArrayType(128)), typ=ByteArrayType(128), location='memory'
)
pos = getpos(expr)
o = LLLnode.from_list([
'seq',
['mstore', placeholder_node, avo(args[0], 0, pos)],
['mstore', ['add', placeholder_node, 32], avo(args[0], 1, pos)],
['mstore', ['add', placeholder_node, 64], avo(args[1], 0, pos)],
['mstore', ['add', placeholder_node, 96], avo(args[1], 1, pos)],
['assert', ['call', ['gas'], 6, 0, placeholder_node, 128, placeholder_node, 64]],
placeholder_node,
], typ=ListType(BaseType('uint256'), 2), pos=getpos(expr), location='memory')
return o
def _RLPlist(expr, args, kwargs, context):
# Second argument must be a list of types
if not isinstance(args[1], ast.List):
raise TypeMismatchException("Expecting list of types for second argument", args[1])
if len(args[1].elts) == 0:
raise TypeMismatchException("RLP list must have at least one item", expr)
if len(args[1].elts) > 32:
raise TypeMismatchException("RLP list must have at most 32 items", expr)
# Get the output format
_format = []
for arg in args[1].elts:
if isinstance(arg, ast.Name) and arg.id == "bytes":
subtyp = ByteArrayType(args[0].typ.maxlen)
else:
subtyp = context.parse_type(arg, 'memory')
if not isinstance(subtyp, BaseType):
raise TypeMismatchException("RLP lists only accept BaseTypes and byte arrays", arg)
if not is_base_type(subtyp, ('int128', 'uint256', 'bytes32', 'address', 'bool')):
raise TypeMismatchException(f"Unsupported base type: {subtyp.typ}", arg)
_format.append(subtyp)
output_type = TupleType(_format)
output_placeholder_type = ByteArrayType(
(2 * len(_format) + 1 + get_size_of_type(output_type)) * 32,
)
output_placeholder = context.new_placeholder(output_placeholder_type)
output_node = LLLnode.from_list(
output_placeholder,
typ=output_placeholder_type,
location='memory',