Skip to content

Commit

Permalink
Merge pull request #569 from DavidKnott/add-modulo-math-checks
Browse files Browse the repository at this point in the history
Add num256 modulo math checks
  • Loading branch information
DavidKnott authored Dec 25, 2017
2 parents eb4b1dd + 441e41b commit c0c50da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/parser/types/numbers/test_num256.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def _num256_le(x: num256, y: num256) -> bool:
assert c._num256_div(x, y) == x // y
assert_tx_failed(lambda: c._num256_div(NUM256_MAX, 0))
assert c._num256_div(y, x) == 0
assert_tx_failed(lambda: c._num256_div(x, 0))
assert c._num256_gt(x, y) is True
assert c._num256_ge(x, y) is True
assert c._num256_le(x, y) is False
Expand Down Expand Up @@ -92,14 +93,17 @@ def _num256_mulmod(x: num256, y: num256, z: num256) -> num256:

assert c._num256_mod(3, 2) == 1
assert c._num256_mod(34, 32) == 2
assert_tx_failed(lambda: c._num256_mod(3, 0))
assert c._num256_addmod(1, 2, 2) == 1
assert c._num256_addmod(32, 2, 32) == 2
assert c._num256_addmod((2**256) - 1, 0, 2) == 1
assert_tx_failed(lambda: c._num256_addmod((2**256) - 1, 1, 1))
assert_tx_failed(lambda: c._num256_addmod(1, 2, 0))
assert c._num256_mulmod(3, 1, 2) == 1
assert c._num256_mulmod(200, 3, 601) == 600
assert c._num256_mulmod(2**255, 1, 3) == 2
assert_tx_failed(lambda: c._num256_mulmod(2**255, 2, 1))
assert_tx_failed(lambda: c._num256_mulmod(2, 2, 0))


def test_num256_with_exponents(t, chain, assert_tx_failed, get_contract_with_gas_estimation):
Expand Down
6 changes: 5 additions & 1 deletion viper/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,19 +691,23 @@ def num256_exp(expr, args, kwargs, context):

@signature('num256', 'num256')
def num256_mod(expr, args, kwargs, context):
return LLLnode.from_list(['mod', args[0], args[1]], typ=BaseType('num256'), pos=getpos(expr))
return LLLnode.from_list(['seq',
['assert', args[1]],
['mod', args[0], args[1]]], typ=BaseType('num256'), pos=getpos(expr))


@signature('num256', 'num256', 'num256')
def num256_addmod(expr, args, kwargs, context):
return LLLnode.from_list(['seq',
['assert', args[2]],
['assert', ['or', ['iszero', args[1]], ['gt', ['add', args[0], args[1]], args[0]]]],
['addmod', args[0], args[1], args[2]]], typ=BaseType('num256'), pos=getpos(expr))


@signature('num256', 'num256', 'num256')
def num256_mulmod(expr, args, kwargs, context):
return LLLnode.from_list(['seq',
['assert', args[2]],
['assert', ['or', ['iszero', args[0]],
['eq', ['div', ['mul', args[0], args[1]], args[0]], args[1]]]],
['mulmod', args[0], args[1], args[2]]], typ=BaseType('num256'), pos=getpos(expr))
Expand Down

0 comments on commit c0c50da

Please sign in to comment.