-
Notifications
You must be signed in to change notification settings - Fork 11
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
fix: bugs on border cases of udiv_mod #76
Open
Pablo-Dallegri
wants to merge
5
commits into
noir-lang:main
Choose a base branch
from
fatlabsxyz:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6353c9a
test: border cases of umod_div
Pablo-Dallegri ed8bc51
fix: underflow when numerator is shorter than divisor
Pablo-Dallegri 0d12343
chore: indent
Pablo-Dallegri 2fd30f9
fix: borrow flags miscalculation on lower limb limits
Pablo-Dallegri ecb9dec
chore: modify if condition to comparing msbs directly
Pablo-Dallegri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,34 +171,40 @@ pub(crate) unconstrained fn __udiv_mod<let N: u32>( | |
let mut divisor_u60: U60Repr<N, 2> = U60Repr::from(divisor); | ||
let b = divisor_u60; | ||
|
||
let mut bit_difference = remainder_u60.get_msb() - divisor_u60.get_msb(); | ||
let numerator_msb = remainder_u60.get_msb(); | ||
let divisor_msb = divisor_u60.get_msb(); | ||
if divisor_msb > numerator_msb { | ||
([0; N], numerator) | ||
} else { | ||
let mut bit_difference = numerator_msb - divisor_msb; | ||
|
||
let mut accumulator_u60: U60Repr<N, 2> = U60Repr::one(); | ||
divisor_u60 = divisor_u60.shl(bit_difference); | ||
accumulator_u60 = accumulator_u60.shl(bit_difference); | ||
let mut accumulator_u60: U60Repr<N, 2> = U60Repr::one(); | ||
divisor_u60 = divisor_u60.shl(bit_difference); | ||
accumulator_u60 = accumulator_u60.shl(bit_difference); | ||
|
||
if (divisor_u60.gte(remainder_u60 + U60Repr::one())) { | ||
divisor_u60.shr1(); | ||
accumulator_u60.shr1(); | ||
} | ||
for _ in 0..(N * 120) { | ||
if (remainder_u60.gte(b) == false) { | ||
break; | ||
if (divisor_u60.gte(remainder_u60 + U60Repr::one())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are these just formatter changes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They're from the if statement causing extra indentation. |
||
divisor_u60.shr1(); | ||
accumulator_u60.shr1(); | ||
} | ||
for _ in 0..(N * 120) { | ||
if (remainder_u60.gte(b) == false) { | ||
break; | ||
} | ||
|
||
// we've shunted 'divisor' up to have the same bit length as our remainder. | ||
// If remainder >= divisor, then a is at least '1 << bit_difference' multiples of b | ||
if (remainder_u60.gte(divisor_u60)) { | ||
remainder_u60 -= divisor_u60; | ||
// we can use OR here instead of +, as | ||
// accumulator is always a nice power of two | ||
quotient_u60 = quotient_u60 + accumulator_u60; | ||
// we've shunted 'divisor' up to have the same bit length as our remainder. | ||
// If remainder >= divisor, then a is at least '1 << bit_difference' multiples of b | ||
if (remainder_u60.gte(divisor_u60)) { | ||
remainder_u60 -= divisor_u60; | ||
// we can use OR here instead of +, as | ||
// accumulator is always a nice power of two | ||
quotient_u60 = quotient_u60 + accumulator_u60; | ||
} | ||
divisor_u60.shr1(); // >>= 1; | ||
accumulator_u60.shr1(); // >>= 1; | ||
} | ||
divisor_u60.shr1(); // >>= 1; | ||
accumulator_u60.shr1(); // >>= 1; | ||
} | ||
|
||
(U60Repr::into(quotient_u60), U60Repr::into(remainder_u60)) | ||
(U60Repr::into(quotient_u60), U60Repr::into(remainder_u60)) | ||
} | ||
} | ||
|
||
pub(crate) unconstrained fn __invmod<let N: u32, let MOD_BITS: u32>( | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see how this line can cause issues when the denominator has more bits in its u60 representation. Thanks for catching this!