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

feat: Implement hint on uint384_extension lib #983

Merged
merged 56 commits into from
Apr 18, 2023
Merged

Conversation

fmoletta
Copy link
Member

@fmoletta fmoletta commented Apr 13, 2023

Depends on #960
Implements the following hint:

           def split(num: int, num_bits_shift: int, length: int):
               a = []
               for _ in range(length):
                   a.append( num & ((1 << num_bits_shift) - 1) )
                   num = num >> num_bits_shift
               return tuple(a)

           def pack(z, num_bits_shift: int) -> int:
               limbs = (z.d0, z.d1, z.d2)
               return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

           def pack_extended(z, num_bits_shift: int) -> int:
               limbs = (z.d0, z.d1, z.d2, z.d3, z.d4, z.d5)
               return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

           a = pack_extended(ids.a, num_bits_shift = 128)
           div = pack(ids.div, num_bits_shift = 128)

           quotient, remainder = divmod(a, div)

           quotient_split = split(quotient, num_bits_shift=128, length=6)

           ids.quotient.d0 = quotient_split[0]
           ids.quotient.d1 = quotient_split[1]
           ids.quotient.d2 = quotient_split[2]
           ids.quotient.d3 = quotient_split[3]
           ids.quotient.d4 = quotient_split[4]
           ids.quotient.d5 = quotient_split[5]

           remainder_split = split(remainder, num_bits_shift=128, length=3)
           ids.remainder.d0 = remainder_split[0]
           ids.remainder.d1 = remainder_split[1]
           ids.remainder.d2 = remainder_split[2]

Checklist

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.
    • CHANGELOG has been updated.

@fmoletta fmoletta changed the base branch from main to uint348-hints April 13, 2023 18:56
@codecov
Copy link

codecov bot commented Apr 14, 2023

Codecov Report

Merging #983 (0d2e963) into main (a993318) will increase coverage by 0.00%.
The diff coverage is 98.39%.

@@           Coverage Diff            @@
##             main     #983    +/-   ##
========================================
  Coverage   98.01%   98.02%            
========================================
  Files          75       76     +1     
  Lines       31297    31606   +309     
========================================
+ Hits        30677    30981   +304     
- Misses        620      625     +5     
Impacted Files Coverage Δ
...c/hint_processor/builtin_hint_processor/uint384.rs 97.93% <96.00%> (-0.21%) ⬇️
...cessor/builtin_hint_processor/uint384_extension.rs 98.84% <98.84%> (ø)
...int_processor/builtin_hint_processor_definition.rs 98.78% <100.00%> (+<0.01%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Base automatically changed from uint348-hints to main April 14, 2023 16:31
@fmoletta fmoletta marked this pull request as ready for review April 17, 2023 19:12
Copy link
Member

@Oppen Oppen left a comment

Choose a reason for hiding this comment

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

LGTM, just one comment.

@github-actions
Copy link

github-actions bot commented Apr 17, 2023

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 2.776 ± 0.024 2.749 2.812 1.00
head blake2s_integration_benchmark 2.842 ± 0.021 2.825 2.892 1.02 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 3.396 ± 0.047 3.352 3.475 1.00
head compare_arrays_200000 3.514 ± 0.042 3.474 3.601 1.03 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 2.525 ± 0.008 2.519 2.545 1.00
head dict_integration_benchmark 2.676 ± 0.038 2.633 2.751 1.06 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base factorial_multirun 4.132 ± 0.083 4.087 4.359 1.00
head factorial_multirun 4.319 ± 0.036 4.272 4.388 1.05 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base fibonacci_1000_multirun 3.262 ± 0.011 3.249 3.285 1.00
head fibonacci_1000_multirun 3.429 ± 0.020 3.408 3.460 1.05 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 3.326 ± 0.023 3.313 3.383 1.00
head integration_builtins 3.441 ± 0.011 3.428 3.466 1.03 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 3.011 ± 0.011 3.001 3.029 1.00
head keccak_integration_benchmark 3.104 ± 0.009 3.095 3.121 1.03 ± 0.00
Command Mean [s] Min [s] Max [s] Relative
base linear_search 3.514 ± 0.017 3.501 3.549 1.00
head linear_search 3.661 ± 0.015 3.645 3.694 1.04 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 2.936 ± 0.008 2.921 2.951 1.00
head math_cmp_and_pow_integration_benchmark 3.046 ± 0.011 3.033 3.070 1.04 ± 0.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 2.666 ± 0.013 2.649 2.685 1.00
head math_integration_benchmark 2.767 ± 0.024 2.732 2.804 1.04 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 2.348 ± 0.012 2.334 2.367 1.00
head memory_integration_benchmark 2.434 ± 0.017 2.405 2.456 1.04 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 2.384 ± 0.030 2.363 2.460 1.00
head operations_with_data_structures_benchmarks 2.436 ± 0.015 2.419 2.464 1.02 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base pedersen 3.333 ± 0.005 3.325 3.339 1.00
head pedersen 3.349 ± 0.006 3.341 3.361 1.00 ± 0.00
Command Mean [s] Min [s] Max [s] Relative
base poseidon_integration_benchmark 1.482 ± 0.005 1.477 1.494 1.00
head poseidon_integration_benchmark 1.502 ± 0.006 1.495 1.515 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 2.632 ± 0.009 2.621 2.645 1.00
head secp_integration_benchmark 2.674 ± 0.006 2.666 2.683 1.02 ± 0.00
Command Mean [s] Min [s] Max [s] Relative
base set_integration_benchmark 1.843 ± 0.049 1.807 1.962 1.01 ± 0.03
head set_integration_benchmark 1.818 ± 0.015 1.799 1.842 1.00
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.126 ± 0.015 4.111 4.154 1.00
head uint256_integration_benchmark 4.247 ± 0.014 4.232 4.280 1.03 ± 0.01

@Oppen
Copy link
Member

Oppen commented Apr 17, 2023

There seems to be a significant, reproducible (I retried the workflow twice) slowdown in several benchmarks. I think we should figure out what causes it before merging.
I'll try to run the benchmarks by hand in the server tomorrow, if we're lucky it's just that by chance the memory layout became less efficient in this particular build. If we're not, it may be a fun ride.

EDIT: seems OK now.

@Oppen Oppen added this pull request to the merge queue Apr 18, 2023
Merged via the queue into main with commit 39cea94 Apr 18, 2023
15 checks passed
@Oppen Oppen deleted the uint384-hints-extended branch April 18, 2023 15:47
@pefontana pefontana added the whitelisted-hint Implementation of hint on whitelist directory label Apr 20, 2023
kariy pushed a commit to dojoengine/cairo-rs that referenced this pull request Jun 23, 2023
* Add hint code for UINT348_UNSIGNED_DIV_REM

* Add file for uint348 files

* Add pack & split for uint348

* Move comment

* Implement uint348_unsigned_div_rem hint

* Add integration test

* Add integration test

* Add unit tests

* Add hint on split_128

* Test split_128 hint

* Add add_no_uint384_hint

* Fix hint + add tests

* Add hint code for UINT348_UNSIGNED_DIV_REM_EXPAND

* Msc fixes

* Add integration test

* Reduce Uint384_expand representation to the 3 used limbs

* Add unit test

* Add hint code for UINT384_SQRT

* Add implementation for hint on sqrt

* Integration test

* Add unit tests

* Fix missing directive

* Run cairo-format

* Add changelog entry

* Spelling

* Add hint code + Uint768 type

* Implement hint unsigned_div_rem_uint768_by_uint384

* Update src/hint_processor/builtin_hint_processor/uint384.rs

Co-authored-by: Mario Rugiero <mario.rugiero@lambdaclass.com>

* Update src/hint_processor/builtin_hint_processor/uint384.rs

Co-authored-by: Mario Rugiero <mario.rugiero@lambdaclass.com>

* Update src/hint_processor/builtin_hint_processor/uint384.rs

Co-authored-by: Mario Rugiero <mario.rugiero@lambdaclass.com>

* Make hint code more readable

* Add integration test

* Add test

* Add unit test

* Add changelog entry + fmt

* Fix plural

* cargo fmt

* Fix test

* Fix syntax

* Fix test

* Add necessary lib fns

* fix fmt

* Fix test value

* Fix merge conflict

* Fix merge conflict

* Add misc tests

* Remove vec use

* Remove merge conflict from changelog

* Catch potential zero divison errors

---------

Co-authored-by: Mario Rugiero <mario.rugiero@lambdaclass.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
whitelisted-hint Implementation of hint on whitelist directory
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants