kkrt-labs / kakarot-ssj

Kakarot zkEVM - rewrite in the latest version of Cairo
https://www.kakarot.org
MIT License
137 stars 83 forks source link

dev: optimized bitshifts by using a lookup table for powers of two #984

Open augustin-v opened 2 months ago

augustin-v commented 2 months ago

Pull Request type

Please check the type of change your PR introduces:

What is the current behavior?

The bit shifting operations in the existing implementation are not optimized, leading to potential inefficiencies.

Resolves: #905

What is the new behavior?

Does this introduce a breaking change?


This change is Reviewable

enitrat commented 2 months ago

The easiest way would be to use python to generate cairo code, like so:

def generate_powers_of_two():
    print("pub const POW_2_256: [")
    print("    u256")
    print("    ; 256] = [")

    for i in range(256):
        value = 2 ** i
        hex_value = f"0x{value:x}"
        print(f"    {hex_value},")

    print("];")

if __name__ == "__main__":
    generate_powers_of_two()
augustin-v commented 2 months ago

Thank you for the review, I'll add a new constant as instructed and try profiling as well!

enitrat commented 1 month ago

hi @augustin-v it seems that the changes brought in scope of these PR highlight a memory-management related issue in Cairo Native which crashes our CI runner, thus I will have to keep it open until it is fixed

augustin-v commented 1 month ago

Hi @enitrat, understood. Thank you for the update, and please let me know if there's anything I can do to assist with resolving the issue

enitrat commented 1 month ago

hey @augustin-v i ran some profiling on test_execute_from_outside_eip1559 - which is just an empty execution, so it only performs validation steps:

Before:

image

After:image

It's nearly 10x better :)

augustin-v commented 1 month ago

@enitrat wow it improved much more than I thought it would, bitshift operations looking good