Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sha512 optimisation #305

Merged
merged 2 commits into from
May 16, 2024
Merged

sha512 optimisation #305

merged 2 commits into from
May 16, 2024

Conversation

shramee
Copy link
Contributor

@shramee shramee commented May 16, 2024

Pull Request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Build-related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Collected 3 test(s) from alexandria_math package
Running 3 test(s) from src/
[PASS] alexandria_math::tests::sha512_test::test_size_zero (gas: ~20074)
-        steps: 2165513
+        steps: 735949
        memory holes: 353718
        builtins: ("range_check_builtin": 501826, "bitwise_builtin": 2025)
        syscalls: ()
        
[PASS] alexandria_math::tests::sha512_test::test_sha512_size_one (gas: ~20073)
-        steps: 2165490
+        steps: 735926
        memory holes: 353718
        builtins: ("range_check_builtin": 501823, "bitwise_builtin": 2025)
        syscalls: ()
        
[PASS] alexandria_math::tests::sha512_test::test_sha512_lorem_ipsum (gas: ~59599)
-        steps: 6427014
+        steps: 2149930
        memory holes: 1055226
        builtins: ("range_check_builtin": 1489975, "bitwise_builtin": 5929)
        syscalls: ()
        
Tests: 3 passed, 0 failed, 0 skipped, 0 ignored, 145 filtered out

Does this introduce a breaking change?

  • Yes
  • No

@shramee shramee requested a review from 0xLucqs as a code owner May 16, 2024 02:21
@shramee shramee mentioned this pull request May 16, 2024
9 tasks
Copy link
Contributor

@tdelabro tdelabro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you:

  • add doc to the functions you use/create
  • add an explanation of the optimisation you are doing in your PR description (I have no clue what is going on)
  • add comments in your code explaining the algorithm you are using, a maybe a link to the Wikipedia page of the algo, or the paper describing this optimization

src/math/src/sha512.cairo Show resolved Hide resolved
src/math/src/sha512.cairo Show resolved Hide resolved
@shramee
Copy link
Contributor Author

shramee commented May 16, 2024

Apologies for not adding the details,

I mostly did common dev sense optimisations on this one, I noticed a loooot of power ops.

fpow(base: u128, power: u128) -> u128

This one does a modulo op and a divide op, behind the scenes both these ops use a div_mod op. So we can just do it once saving one div_mod call on each iteration.

fn two_pow(power: u128) -> u128

Among pow ops, most were pow of 2. There are only so many powers of 2 you can have in a u128. So I wrote two_pow with cached 2 pow squarings saving a square op in each iteration.
I'll add some docs on this one.

These two things make it 3x faster to do SHA512.

About renaming single letter variables, as an outside optimiser, I'd refrain from changing var names coz they might hold significance beyond my understanding.

@tdelabro
Copy link
Contributor

About renaming single letter variables, as an outside optimiser, I'd refrain from changing var names coz they might hold significance beyond my understanding.

Ok no worry then, I have been asked to do a cleanup of the codebase before audit. Renaming to more explicit var name will be part of it

@tdelabro tdelabro merged commit a9caf69 into keep-starknet-strange:main May 16, 2024
3 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants