Skip to content

Const initialization helper for SmallFp#1084

Merged
weikengchen merged 11 commits intomasterfrom
z-tech/smallfp-support-const-vars
Mar 29, 2026
Merged

Const initialization helper for SmallFp#1084
weikengchen merged 11 commits intomasterfrom
z-tech/smallfp-support-const-vars

Conversation

@z-tech
Copy link
Copy Markdown
Contributor

@z-tech z-tech commented Mar 29, 2026

There was no equivalent const helper for MontFp! so we create NAME::from_u128(1) that is const compatible. This isn't strictly needed but it's far more convenient for users especially when writing extension field config.

Example use: https://github.com/WizardOfMenlo/whir/pull/242/changes#diff-a0fce05055298d8ce81ce601933b6e9349617338f89f53db251b60d5ff884182R95

Small Items:

  • also addresses clippy warnings
  • also addresses proc macro warnings
  • also addresses mapping of modulus to zero when this should probably map to none

closes: #1085


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • [ x] Targeted PR against correct branch (master)
  • [ x] Linked to GitHub issue with discussion and accepted design OR have an explanation in the PR that describes this work.
  • [x ] Wrote unit tests
  • [x ] Updated relevant documentation in the code
  • [ x] Added a relevant changelog entry to the Pending section in CHANGELOG.md
  • [ x] Re-reviewed Files changed in the GitHub PR explorer

fn from_bigint(a: ark_ff::BigInt<2>) -> Option<SmallFp<Self>> {
let val = (a.0[0] as u128) + ((a.0[1] as u128) << 64);
if val > Self::MODULUS_U128 {
if val >= Self::MODULUS_U128 {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Both zero and the modulus would be mapped to zero if not changed to >=, which came up in serialization for spongefish.

let (mut val, overflow) = a.value.overflowing_add(b.value);
if overflow {
val = Self::T::MAX - Self::MODULUS + 1 + val
val += Self::T::MAX - Self::MODULUS + 1
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixes downstream clippy warnings


let field_ty = if is_small_modulus {
quote::quote! { ark_ff::SmallFp<#config_name> }
if is_small_modulus {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These changes fix the proc macro error given by rust analyzer

@z-tech z-tech changed the title smallfp support const vars Const initialization helper for SmallFp Mar 29, 2026
@z-tech z-tech marked this pull request as ready for review March 29, 2026 11:05
@z-tech z-tech requested review from a team as code owners March 29, 2026 11:05
@z-tech z-tech requested review from Pratyush, mmagician and weikengchen and removed request for a team March 29, 2026 11:05
@weikengchen weikengchen added this pull request to the merge queue Mar 29, 2026
Merged via the queue into master with commit 285dac2 Mar 29, 2026
42 checks passed
@weikengchen weikengchen deleted the z-tech/smallfp-support-const-vars branch March 29, 2026 12:49
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.

Support Const initializations of SmallFp

2 participants