Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

[KGA-14] [KGA-28] [KGA-74] [KGA-132] [KGA-92] fix: finalize dictionary in RIPEMD160 #1577

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cairo_zero/kakarot/precompiles/ripemd160.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,9 @@ func finish{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}(
dict_to_array{dict_ptr=x}(arr_x, 16);
let (buf, bufsize) = compress(buf, bufsize, arr_x, 16);
// reset dict to all 0.
default_dict_finalize(start, x, 0);
let (x) = default_dict_new(0);
tempvar start = x;

dict_write{dict_ptr=x}(14, val);
dict_write{dict_ptr=x}(15, val_15);
Expand Down
22 changes: 22 additions & 0 deletions cairo_zero/tests/src/kakarot/precompiles/test_ripemd160.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from hypothesis import example, given
from hypothesis.strategies import binary

from tests.utils.errors import cairo_error
from tests.utils.hints import insert_hint


@pytest.mark.asyncio
@pytest.mark.slow
Expand All @@ -20,3 +23,22 @@ async def test_ripemd160_should_return_correct_hash(self, cairo_run, msg_bytes):
expected_hash = ripemd160_crypto.hexdigest()

assert expected_hash.rjust(64, "0") == bytes(precompile_hash).hex()

# see https://github.com/code-423n4/2024-09-kakarot-findings/issues/54
# see https://github.com/code-423n4/2024-09-kakarot-findings/issues/21
async def test_finalized_dict_ripemd160(self, cairo_program, cairo_run):
msg_bytes = bytes([0x00] * 57)
with (
insert_hint(
cairo_program,
"ripemd160.cairo:161",
"try:\n"
" dict_tracker = __dict_manager.get_tracker(ids.dict_ptr)\n"
" dict_tracker.data[ids.index_4] = 1\n"
"except Exception: pass\n",
),
cairo_error(
message="An ASSERT_EQ instruction failed"
), # fails with an assertion error from default_dict_finalize_inner
):
cairo_run("test__ripemd160", msg=list(msg_bytes))
30 changes: 30 additions & 0 deletions tests/utils/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,33 @@ def patch_hint(program, hint, new_hint):
raise ValueError(f"Hint\n\n{hint}\n\nnot found in program hints.")
with patch.object(program, "hints", new=patched_hints):
yield


@contextmanager
def insert_hint(program, location: str, hint):
"""
Insert a hint at a given location in the program.

The location should be file_name:line_number.

"""
instructions = {
index: loc
for index, loc in program.debug_info.instruction_locations.items()
if location in str(loc.inst)
}
if not instructions:
raise ValueError(f"Location {location} not found in program.")
pc, instruction = list(instructions.items())[0]
hint = CairoHint(
accessible_scopes=instruction.accessible_scopes,
flow_tracking_data=instruction.flow_tracking_data,
code=hint,
)
new_hints = program.hints.copy()
new_hints[pc] = [*new_hints.get(pc, []), hint]
with (
patch.object(instruction, "hints", new=new_hints.get(pc, [])),
patch.object(program, "hints", new=new_hints),
):
yield
Loading