Skip to content

perf: optimize range checks for small number of small field ops#1699

Merged
ivokub merged 11 commits intomasterfrom
perf/smallfield-width
Feb 4, 2026
Merged

perf: optimize range checks for small number of small field ops#1699
ivokub merged 11 commits intomasterfrom
perf/smallfield-width

Conversation

@ivokub
Copy link
Copy Markdown
Collaborator

@ivokub ivokub commented Feb 2, 2026

Description

Additionally, sets MulNoReduce as default when calling Field.Mul on small field parameter.

Fixes #1698

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How has this been tested?

  • Test A
  • Test B

How has this been benchmarked?

  • Benchmark A, on Macbook pro M1, 32GB RAM
  • Benchmark B, on x86 Intel xxx, 16GB RAM

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • golangci-lint does not output errors locally
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Note

Medium Risk
Changes Field.Mul semantics for single-limb emulated fields by defaulting to non-reducing multiplication, which can affect callers that assumed Mul always reduced (now must use MulMod for that guarantee). Adds stateful range-check deduplication and a threshold-triggered switch in range-checking behavior, which impacts constraint generation and overflow handling in hot paths.

Overview
Optimizes emulated-field range checks and small-field multiplication. Range checks now go through a new Field.rangeCheck helper that deduplicates checks per limb (tracking the widest enforced bit-width) and counts how many checks have been emitted.

For small-field emulation, range checking switches only after thresholdForInexactOverflow checks to a base-length (16-bit) range checker, and smallAdditionalOverflow() now returns non-zero only after that threshold to keep overflow accounting consistent.

Field.Mul now defaults to MulNoReduce when useSmallFieldOptimization() is enabled, falling back to reducing multiplication only when overflow would exceed limits; callers needing guaranteed modular reduction should use MulMod. Tests/benchmarks are adjusted accordingly (bench compile loop guard + suppress ns/op, and malicious-hint circuit uses MulMod).

Written by Cursor Bugbot for commit c994745. This will update automatically on new commits. Configure here.

Comment thread std/math/emulated/smallfield_test.go
Comment thread std/math/emulated/field.go Outdated
Comment thread std/math/emulated/field_assert.go
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Comment thread std/math/emulated/field.go
Comment thread std/math/emulated/field.go Outdated
@ivokub ivokub self-assigned this Feb 3, 2026
@ivokub ivokub added dep: linea Issues affecting Linea downstream feat: emulated labels Feb 3, 2026
@ivokub ivokub requested a review from YaoJGalteland February 3, 2026 16:13
Comment thread std/math/emulated/field.go Outdated
Comment thread std/math/emulated/field.go Outdated
Comment thread std/math/emulated/field_mul.go
@ivokub ivokub requested a review from YaoJGalteland February 4, 2026 05:59
Copy link
Copy Markdown

@YaoJGalteland YaoJGalteland left a comment

Choose a reason for hiding this comment

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

LGTM

@ivokub ivokub merged commit e45e0a7 into master Feb 4, 2026
13 checks passed
@ivokub ivokub deleted the perf/smallfield-width branch February 4, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dep: linea Issues affecting Linea downstream feat: emulated

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Perf: optimize small field range check optimizations for small circuits

2 participants