Skip to content
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: get_next_power_exponent off by 1 #11169

Merged
merged 3 commits into from
Jan 13, 2025

Conversation

benesjan
Copy link
Contributor

@benesjan benesjan commented Jan 10, 2025

There was an issue with get_next_power_exponent func which resulted in it returning values off by 1. This didn't cause a bug but it was confusing.

Copy link
Contributor Author

benesjan commented Jan 10, 2025

@benesjan benesjan force-pushed the 01-10-fix_get_next_power_exponent_off_by_1 branch from 5490a82 to 6ece169 Compare January 10, 2025 21:08
@benesjan benesjan changed the base branch from 01-10-refactor_variablemerkletree_readability_improvements to 01-10-fix_underconstrained_bug January 10, 2025 21:08
@benesjan benesjan added the e2e-all CI: Enables this CI job. label Jan 10, 2025
@benesjan benesjan marked this pull request as ready for review January 10, 2025 21:15
@benesjan benesjan force-pushed the 01-10-fix_underconstrained_bug branch from a1eec2b to 2de30bd Compare January 10, 2025 21:16
@benesjan benesjan force-pushed the 01-10-fix_get_next_power_exponent_off_by_1 branch from 3985ec0 to 06e9cc2 Compare January 10, 2025 21:16
@@ -20,7 +20,8 @@ pub struct VariableMerkleTree {
/// The smallest exponent `n` where `2^n >= input`
unconstrained fn get_next_power_exponent(input: u32, start: u8) -> u8 {
let mut next_power_exponent = 0;
if input <= 2 << start {
// We check if input is less than or equal to 2^start.
if input <= (1 << start) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a crux of the bug because 2 << start is not equal to 2^start but to 2^(start+1).

@@ -49,8 +50,8 @@ fn get_prev_power_2(value: u32) -> u32 {
get_next_power_exponent(value, 0)
};

let next_power_2 = 2 << next_power_exponent;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here it didn't cause an issue because we were shifting 2 and not 1.

let next_power_2 = 2 << next_power_exponent;
let prev_power_2 = next_power_2 / 2;
let prev_power_2 = 1 << (next_power_exponent - 1);
let next_power_2 = prev_power_2 * 2;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I sneaked in an optimization. I compute prev_power_2 first and then I multiply by 2 to get next_power_2. This should be cheaper constraint-wise as division is more expensive than multiplication.

@benesjan benesjan force-pushed the 01-10-fix_underconstrained_bug branch from 2de30bd to 26c5817 Compare January 13, 2025 15:06
@benesjan benesjan force-pushed the 01-10-fix_get_next_power_exponent_off_by_1 branch from e56a20c to 6e9f6c5 Compare January 13, 2025 15:06
Copy link
Contributor Author

benesjan commented Jan 13, 2025

Merge activity

  • Jan 13, 11:24 AM EST: A user started a stack merge that includes this pull request via Graphite.
  • Jan 13, 12:07 PM EST: Graphite rebased this pull request as part of a merge.
  • Jan 13, 12:46 PM EST: A user merged this pull request with Graphite.

@benesjan benesjan changed the base branch from 01-10-fix_underconstrained_bug to graphite-base/11169 January 13, 2025 16:25
@benesjan benesjan changed the base branch from graphite-base/11169 to master January 13, 2025 17:04
@benesjan benesjan force-pushed the 01-10-fix_get_next_power_exponent_off_by_1 branch from 6e9f6c5 to d4b3da4 Compare January 13, 2025 17:06
@benesjan benesjan merged commit 80ec19e into master Jan 13, 2025
78 checks passed
@benesjan benesjan deleted the 01-10-fix_get_next_power_exponent_off_by_1 branch January 13, 2025 17:46
TomAFrench added a commit that referenced this pull request Jan 13, 2025
* master: (329 commits)
  fix(avm): mac build (#11195)
  fix: docs rebuild patterns (#11191)
  chore: refactor Solidity Transcript and improve error handling in  sol_honk flow (#11158)
  chore: move witness computation into class plus some other cleanup (#11140)
  fix: get_next_power_exponent off by 1 (#11169)
  chore(avm): vm2 followup cleanup (#11186)
  fix: underconstrained bug (#11174)
  refactor: VariableMerkleTree readability improvements (#11165)
  chore: removing noir bug workaround (#10535)
  chore(docs): Remove node pages  (#11161)
  git subrepo push --branch=master noir-projects/aztec-nr
  git_subrepo.sh: Fix parent in .gitrepo file. [skip ci]
  chore: replace relative paths to noir-protocol-circuits
  git subrepo push --branch=master barretenberg
  feat(avm2): avm redesign init (#10906)
  feat: Sync from noir (#11138)
  feat: simulator split (#11144)
  chore: rpc server cleanup & misc fixes (#11145)
  git subrepo push --branch=master noir-projects/aztec-nr
  git_subrepo.sh: Fix parent in .gitrepo file. [skip ci]
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e-all CI: Enables this CI job.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants