Skip to content

fix: handle uints8 Long addition even in small field#1674

Merged
ivokub merged 35 commits intomasterfrom
fix/sha2-smallfield
Jan 19, 2026
Merged

fix: handle uints8 Long addition even in small field#1674
ivokub merged 35 commits intomasterfrom
fix/sha2-smallfield

Conversation

@ivokub
Copy link
Copy Markdown
Collaborator

@ivokub ivokub commented Jan 8, 2026

Description

For small fields our long (U32, U64) addition overflowed the native field. Modify the implementation to operate on bytes only to avoid overflows.

NB! Merge only after #1673 is merged.

Type of change

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

How has this been tested?

  • Test addition also with small fields.
  • added more edge case tests

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

Improves long-integer arithmetic and adds byte packing utilities.

  • Refactors BinaryField.Add to accept variable inputs (including 0/1) and to switch between native add+partition and bytewise add-with-carry when maxBitlen exceeds the field size, preventing overflow on small fields
  • Adds PackMSB, PackLSB, UnpackMSB, UnpackLSB for constructing/deconstructing T from bytes; adds zero() helper
  • Documents bitwise ops and shifts; no functional change to And/Or/Xor/Not, Lrot, Rshift besides comments
  • Tests: rewrite add circuit to slice-based and add cases for 0, 1, 2, and many inputs with WithSmallfieldCheck; add pack/unpack circuit tests

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical bug in the Add method for handling long integer (U32/U64) addition in circuits with small field sizes. When adding multiple long integers, the previous implementation could overflow the native field, leading to incorrect results. The fix adds conditional logic to detect when the result might overflow and switches to a bytewise addition approach with proper carry propagation.

Key changes:

  • Added early-return optimizations for empty and single-element additions
  • Split the addition logic into two branches based on field capacity
  • Implemented bytewise addition with carry propagation for small fields

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread std/math/uints/uint8.go
Comment thread std/math/uints/uint8.go
@ivokub ivokub requested a review from yelhousni January 9, 2026 11:15
@ivokub ivokub self-assigned this Jan 9, 2026
@ivokub ivokub added the dep: linea Issues affecting Linea downstream label Jan 9, 2026
@ivokub ivokub marked this pull request as ready for review January 9, 2026 11:31
Base automatically changed from feat/logderivative-smallfield to master January 12, 2026 13:49
Comment thread std/math/uints/uint8.go
Comment thread std/math/uints/uint8.go
Comment thread std/math/uints/uint8.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 1 potential issue.

Comment thread std/math/uints/uint8.go Outdated
This reverts commit a229fa0.
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 1 potential issue.

Comment thread std/math/uints/uint8.go
@ivokub ivokub merged commit 64bc6af into master Jan 19, 2026
13 checks passed
@ivokub ivokub deleted the fix/sha2-smallfield branch January 19, 2026 23: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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants