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

Add support for generic integer type conversions to Expression::Constant #333

Merged
merged 4 commits into from
Oct 9, 2024

Conversation

bgillesp
Copy link
Collaborator

@bgillesp bgillesp commented Oct 7, 2024

Current implementation only provides From<usize>, which requires explicit type conversions in various locations. This PR provides support for type conversions for arbitrary primitive integer types, specifically: u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize

In reference to this comment.

Copy link
Collaborator

@hero78119 hero78119 left a comment

Choose a reason for hiding this comment

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

LGTM! This is awesome for clean up any indirect type conversion 🔥

@hero78119 hero78119 enabled auto-merge (squash) October 9, 2024 04:32
@hero78119 hero78119 disabled auto-merge October 9, 2024 04:33
@hero78119 hero78119 self-requested a review October 9, 2024 04:42
Copy link
Collaborator

@hero78119 hero78119 left a comment

Choose a reason for hiding this comment

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

After approve and double check and left some comments :P

@hero78119 hero78119 enabled auto-merge (squash) October 9, 2024 15:47
@hero78119 hero78119 merged commit 992bc30 into master Oct 9, 2024
6 checks passed
@hero78119 hero78119 deleted the improve-expr-const-into branch October 9, 2024 15:56
impl_from_unsigned!(u8, u16, u32, u64, usize);

// Implement From trait for u128 separately since it requires explicit reduction
impl<F: SmallField, E: ExtensionField<BaseField = F>> From<u128> for Expression<E> {
Copy link
Collaborator

@naure naure Oct 11, 2024

Choose a reason for hiding this comment

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

Was this From<u128> really necessary? It is a lossy conversion therefore error-prone.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The existing conversion from usize prior to this PR was already slightly lossy in this sense because the value has to be renormalized if between p_goldilocks and 2^64 - 1. The way I interpret the syntax (and the way it's implemented here) is "integer primitive types represent actual integers, and any conversion to a BaseField constant is by the 'reduce mod p' map", which seems natural.

@naure
Copy link
Collaborator

naure commented Oct 11, 2024

Great move!

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.

4 participants