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

dev: Exponentiation & Bitshift generics #410

Merged
merged 12 commits into from
Oct 11, 2023

Conversation

akhercha
Copy link
Contributor

@akhercha akhercha commented Oct 6, 2023

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?

Resolves: #408

What is the new behavior?

  • Added ExponentiationImpl<T, ...> of Exponentiation<T>

    • then removed U256ExpImpl & U128ExpImpl
  • Added BitshiftImpl<T, ...> of Bitshift<T>

    • then removed U256BitshiftImpl & U128BitshiftImpl

Does this introduce a breaking change?

  • Yes
  • No

crates/utils/src/math.cairo Outdated Show resolved Hide resolved
crates/utils/src/math.cairo Outdated Show resolved Hide resolved
Copy link
Member

@Eikix Eikix left a comment

Choose a reason for hiding this comment

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

Nice PR overall,
Good catch that wrappingbitshift and wrappingpow are not able to be generic over T

@akhercha
Copy link
Contributor Author

Saw that the original implementation of shr has 2 different behavior depending on the type:

fn shr(self: u128, shift: u128) -> u128 {
        if shift > 127 {
            return 0;
        }
        self / 2.pow(shift)
    }
// or
fn shr(self: u256, shift: u256) -> u256 {
        if shift > 255 {
            // 2.pow(shift) for shift > 255 will panic with 'u256_mul Overflow'
            panic_with_felt252('u256_mul Overflow');
        }
        self / 2.pow(shift)
    }

Not sure of which we prefer for the T type. I'd say panic, what do you think @Eikix ?

@enitrat
Copy link
Collaborator

enitrat commented Oct 10, 2023

Regular shr is supposed to panic indeed

@akhercha
Copy link
Contributor Author

akhercha commented Oct 10, 2023

Should be good then!
Let me know if I missed something. 🙏

I also quickly updated the organization of the code, added a mem module & updated the names of the functions.

Copy link
Collaborator

@enitrat enitrat left a comment

Choose a reason for hiding this comment

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

Great work

crates/utils/src/math.cairo Outdated Show resolved Hide resolved
crates/utils/src/mem.cairo Outdated Show resolved Hide resolved
@enitrat
Copy link
Collaborator

enitrat commented Oct 11, 2023

Also @akhercha I think your local git is not synced to your GH account - not sure if it's on purpose or not

@akhercha
Copy link
Contributor Author

Ah no indeed, something exploded in my config I guess, gonna check that thanks!

@enitrat
Copy link
Collaborator

enitrat commented Oct 11, 2023

@akhercha I just pushed a change with more optimized gas usage - basically since Oneable and Zeroable fns are not inlined there's a small overhead in gas consumption if we rely on them for genericity. Therefore I changed my mind about my previous comment, and explicitly implemented Zero and One for u128, u256, and felt252 ( we won't need other impls, at least for now).

This nullifies any increase in gas consumption for pow-dependent functions; however there's still an increase for shr. and shl, I'm looking for the cause

@enitrat enitrat force-pushed the dev/exponentiation_bitshift_generics branch from 4924e76 to b6a84b4 Compare October 11, 2023 05:24
Copy link
Collaborator

@enitrat enitrat left a comment

Choose a reason for hiding this comment

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

LGTM!
@akhercha do you want to open an issue to implement SizeOf in the corelib https://github.com/starkware-libs/cairo?

@enitrat enitrat added this pull request to the merge queue Oct 11, 2023
Copy link
Member

@Eikix Eikix left a comment

Choose a reason for hiding this comment

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

lgtm 🔥

Merged via the queue into kkrt-labs:main with commit 11adddc Oct 11, 2023
2 of 3 checks passed
@akhercha akhercha deleted the dev/exponentiation_bitshift_generics branch October 11, 2023 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dev: make Exponentiation and Bitshift in math.cairo completely generic
3 participants