Submitted by RadiantLabs
When ripemd160.finish() does not enter the if (next_block == FALSE) condition at L456, the dictionary x initialized at the beginning of the function is not finalized. Instead, x is reassigned to reference a new dictionary at L470:
Because the old dictionary is never finalized, it is possible to insert incorrect values for read operations on the old dictionary, which allows proving an incorrect output for any input of size > 55 (56 with the fix to our separate vulnerability on this value).
Read operations on the old dictionary are performed in ripemd160::absorb_data:
The below test case can be added to test_ripemd160.py to demonstrate the vulnerability:
The test requires adding the following noop hints below ripemd160.cairo#L160 so we can insert the malicious hint:
The patched hint modifies the in-memory dictionary of the prover, which results in the dict_read operation in the following line returning an incorrect fixed value. Because a dict_read call is really an append operation, and the new values arent checked against the previous ones if the dictionary is not squashed, this results in a forged output that can be proven to be correct.
Call default_dict_finalize(start, x, 0); before let (x) = default_dict_new(0);.
ClementWalter (Kakarot) confirmed and commented:
Kakarot mitigated:
Status: Mitigation confirmed.
