Skip to content

Commit

Permalink
Restrict maximum size of scalar types to 57 bit
Browse files Browse the repository at this point in the history
Ref. #238
  • Loading branch information
treiher committed May 29, 2020
1 parent 49562af commit ced4a91
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
12 changes: 6 additions & 6 deletions rflx/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def __init__(self, identifier: StrID, modulus: Expr) -> None:

modulus_int = int(modulus_num)

if modulus_int > 2 ** 62: # ISSUE: Componolit/RecordFlux#182
raise ModelError(f'modulus of "{self.name}" exceeds limit (2**62)')
if modulus_int > 2 ** 57: # ISSUE: Componolit/RecordFlux#238
raise ModelError(f'modulus of "{self.name}" exceeds limit (2**57)')
if modulus_int == 0 or (modulus_int & (modulus_int - 1)) != 0:
raise ModelError(f'modulus of "{self.name}" not power of two')

Expand Down Expand Up @@ -161,8 +161,8 @@ def __init__(self, identifier: StrID, first: Expr, last: Expr, size: Expr) -> No
raise ModelError(f'size of "{self.name}" contains variable')
if int(last_num).bit_length() > int(size_num):
raise ModelError(f'size for "{self.name}" too small')
if int(size_num) > 62: # ISSUE: Componolit/RecordFlux#182
raise ModelError(f'size of "{self.name}" exceeds limit (2**62)')
if int(size_num) > 57: # ISSUE: Componolit/RecordFlux#238
raise ModelError(f'size of "{self.name}" exceeds limit (2**57)')

self.__first = first
self.__last = last
Expand Down Expand Up @@ -211,8 +211,8 @@ def __init__(
raise ModelError(f'size of "{self.name}" contains variable')
if max(map(int, literals.values())).bit_length() > int(size_num):
raise ModelError(f'size for "{self.name}" too small')
if int(size_num) > 62: # ISSUE: Componolit/RecordFlux#182
raise ModelError(f'size of "{self.name}" exceeds limit (2**62)')
if int(size_num) > 57: # ISSUE: Componolit/RecordFlux#238
raise ModelError(f'size of "{self.name}" exceeds limit (2**57)')
if len(set(literals.values())) < len(literals.values()):
raise ModelError(f'"{self.name}" contains elements with same value')
for l in literals:
Expand Down
19 changes: 11 additions & 8 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ def test_type_name() -> None:


def test_modular_size() -> None:
assert ModularInteger("P.T", Pow(Number(2), Number(62))).size == Number(62)
assert ModularInteger("P.T", Pow(Number(2), Number(32))).size == Number(32)


def test_modular_first() -> None:
mod = ModularInteger("P.T", Pow(Number(2), Number(62)))
mod = ModularInteger("P.T", Pow(Number(2), Number(32)))
assert mod.first == Number(0)
assert mod.first.simplified() == Number(0)


def test_modular_last() -> None:
mod = ModularInteger("P.T", Pow(Number(2), Number(62)))
assert mod.last == Sub(Pow(Number(2), Number(62)), Number(1))
assert mod.last.simplified() == Number(2 ** 62 - 1)
mod = ModularInteger("P.T", Pow(Number(2), Number(32)))
assert mod.last == Sub(Pow(Number(2), Number(32)), Number(1))
assert mod.last.simplified() == Number(2 ** 32 - 1)


def test_modular_invalid_modulus_power_of_two() -> None:
Expand All @@ -174,7 +174,8 @@ def test_modular_invalid_modulus_variable() -> None:


def test_modular_invalid_modulus_limit() -> None:
with pytest.raises(ModelError, match=r'^modulus of "T" exceeds limit \(2\*\*62\)$'):
# ISSUE: Componolit/RecordFlux#238
with pytest.raises(ModelError, match=r'^modulus of "T" exceeds limit \(2\*\*57\)$'):
ModularInteger("P.T", Pow(Number(2), Number(128)))


Expand Down Expand Up @@ -216,7 +217,8 @@ def test_range_invalid_size_too_small() -> None:


def test_range_invalid_size_exceeds_limit() -> None:
with pytest.raises(ModelError, match=r'^size of "T" exceeds limit \(2\*\*62\)$'):
# ISSUE: Componolit/RecordFlux#238
with pytest.raises(ModelError, match=r'^size of "T" exceeds limit \(2\*\*57\)$'):
RangeInteger("P.T", Number(0), Number(256), Number(128))


Expand All @@ -231,7 +233,8 @@ def test_enumeration_invalid_size_too_small() -> None:


def test_enumeration_invalid_size_exceeds_limit() -> None:
with pytest.raises(ModelError, match=r'^size of "T" exceeds limit \(2\*\*62\)$'):
# ISSUE: Componolit/RecordFlux#238
with pytest.raises(ModelError, match=r'^size of "T" exceeds limit \(2\*\*57\)$'):
Enumeration("P.T", {"A": Number(256)}, Number(128), False)


Expand Down
3 changes: 2 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,14 @@ def test_illegal_redefinition() -> None:


def test_invalid_modular_type() -> None:
# ISSUE: Componolit/RecordFlux#238
assert_parse_exception_string(
"""
package Test is
type T is mod 2**128;
end Test;
""",
r'^modulus of "T" exceeds limit \(2\*\*62\)',
r'^modulus of "T" exceeds limit \(2\*\*57\)',
)


Expand Down

0 comments on commit ced4a91

Please sign in to comment.