Skip to content

Commit

Permalink
Fix ex mem computation for integers
Browse files Browse the repository at this point in the history
  • Loading branch information
nielstron committed Dec 30, 2024
1 parent 3183e97 commit 883197e
Show file tree
Hide file tree
Showing 3 changed files with 658 additions and 604 deletions.
2 changes: 1 addition & 1 deletion uplc/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def valuestring(self, dialect=UPLCDialect.Plutus):
def ex_mem(self) -> int:
if self.value == 0:
return 1
return (math.ceil(math.log2(abs(self.value))) // 64) + 1
return ((abs(self.value).bit_length() - 1) // 64) + 1

def __add__(self, other):
assert isinstance(
Expand Down
16 changes: 12 additions & 4 deletions uplc/cost_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ def update_from_network_config(
self.c1 = network_config.get(f"{prefix}-arguments-c1", DEFAULT_COST_COEFF)
self.c2 = network_config.get(f"{prefix}-arguments-c2", DEFAULT_COST_COEFF)


@dataclasses.dataclass
class QuadraticInZ(CostingFun):
c0: int = 0
Expand Down Expand Up @@ -380,11 +379,19 @@ class LiteralInYOrLinearInZ(CostingFun):
slope: int = 0

def cost(self, *memories: int, values=[]) -> int:
# LOL good for you having implemented this extra step to pass actual values to the costing function
# because the specs say you need it
# (correct implementation based on official specifications below)
y = values[1].value
if y == 0:
return self.intercept + self.slope * memories[2]
# "+ 7" for ceil
return ((abs(y) - 1 + 7) // 8) + 1
return self.intercept + self.slope * memories[2]
return int(math.ceil((abs(y) - 1) / 8))
# NO FCK YOU and instead use this completely broken implementation because possibly some IOHK engineer
# was too lazy to implement the specs or they realized that the specs were a terrible idea
# y = memories[1]
# if y == 0:
# return self.intercept + self.slope * memories[2]
# return y

@classmethod
def from_arguments(cls, arguments: Dict[str, int]):
Expand Down Expand Up @@ -516,6 +523,7 @@ class PlutusVersion(enum.Enum):

@functools.lru_cache()
def default_builtin_cost_model_base():
# TODO choose different base cost model based on Plutus Version
builtinCostModel = (
Path(__file__)
.parent.joinpath("cost_model_files")
Expand Down
Loading

0 comments on commit 883197e

Please sign in to comment.