Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

fix: brillig signed division #356

Merged
merged 2 commits into from
Jun 9, 2023
Merged

fix: brillig signed division #356

merged 2 commits into from
Jun 9, 2023

Conversation

sirasistant
Copy link
Contributor

@sirasistant sirasistant commented Jun 9, 2023

Description

Problem*

Signed division wasn't working with the bit sizes of the numbers involved.

The previous algorithm for to_signed was trying to check if the number was >= than 2.pow(NUM_BITS), when it should be 2.pow(NUM_BITS - 1)
for example, with 4 bits, -1 is 1111, which is 15 in unsigned form, which is less than 2**4 so it was treated as positive.

The same happened with to_unsigned, where -1 in 4 bits was transformed into -1 + 2.pow(NUM_BITS + 1) which is 31 instead of 15.

Summary*

Example

Before:


After:


Additional Context

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

BEGIN_COMMIT_OVERRIDE
fix(brillig): Correct signed division implementation (#356)
END_COMMIT_OVERRIDE

@sirasistant sirasistant requested a review from kevaundray June 9, 2023 12:43
@kevaundray kevaundray added this pull request to the merge queue Jun 9, 2023
Merged via the queue into master with commit 4eefda0 Jun 9, 2023
@kevaundray kevaundray mentioned this pull request Jun 9, 2023
kevaundray pushed a commit that referenced this pull request Jun 11, 2023
@TomAFrench TomAFrench deleted the arv/fix_signed_div branch June 14, 2023 07:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants