fix: handle uints8 Long addition even in small field#1674
Merged
Conversation
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
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.
This reverts commit a229fa0.
YaoJGalteland
approved these changes
Jan 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
How has this been tested?
Checklist:
golangci-lintdoes not output errors locallyNote
Improves long-integer arithmetic and adds byte packing utilities.
BinaryField.Addto accept variable inputs (including 0/1) and to switch between native add+partition and bytewise add-with-carry whenmaxBitlenexceeds the field size, preventing overflow on small fieldsPackMSB,PackLSB,UnpackMSB,UnpackLSBfor constructing/deconstructingTfrom bytes; addszero()helperAnd/Or/Xor/Not,Lrot,Rshiftbesides commentsWithSmallfieldCheck; add pack/unpack circuit testsWritten by Cursor Bugbot for commit 866e41c. This will update automatically on new commits. Configure here.