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: underconstrained bug #11174

Merged
merged 3 commits into from
Jan 13, 2025
Merged

fix: underconstrained bug #11174

merged 3 commits into from
Jan 13, 2025

Conversation

benesjan
Copy link
Contributor

@benesjan benesjan commented Jan 10, 2025

Fixes underconstrained bug Nico spotted here.

Note on severity

The bug could not be exploited because the VariableMerkleTree is only used when computing an out_hash here and out_hash is checked on L1 against a hash computed on L1. For this reason the severity of the bug is low. But it makes sense to fix because it could become an issue in case the implementation would get used somewhere else later on.

The bug is explained in comments.

Copy link
Contributor Author

benesjan commented Jan 10, 2025

@benesjan benesjan force-pushed the 01-10-refactor_variablemerkletree_readability_improvements branch from 4541806 to 6c8ecfd Compare 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 marked this pull request as ready for review January 10, 2025 21:16
@@ -50,17 +51,15 @@ fn get_prev_power_2(value: u32) -> u32 {

let next_power_2 = 2 << next_power_exponent;
let prev_power_2 = next_power_2 / 2;
assert((value == 0) | (value == 1) | (value > prev_power_2));
assert(prev_power_2 < value);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If value equals 0 or 1, the previous power of 2 is completely unconstrained and we could craft a response from get_next_power_exponent such that prev_power_2 is anything in [1, 2, 4, 8, 16, ...].


// hash base layer
// If we have no num_non_empty_leaves, we return 0
let mut stop = num_non_empty_leaves == 0;
Copy link
Contributor Author

@benesjan benesjan Jan 10, 2025

Choose a reason for hiding this comment

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

Before the code changes we could do the following when hashing base layer.

For num_non_empty_leaves = 0, there is no damage because stop on the line above would be set to true.
For num_non_empty_leaves = 1, this could allow us to hash also the empty leaves getting a different root.

nodes[i] = accumulate_sha256([leaves[2 * i], leaves[2 * i + 1]]);
}
}

// hash the other layers
stop = prev_power_2 == 1;
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 we can cause damage even for num_non_empty_leaves = 0, because we decide whether to hash just based on prev_power_2.

let mut next_layer_size = next_layer_end;
let mut root = nodes[0];
for i in 0..(N - 1 - N / 2) {
if !stop {
nodes[prev_power_2 + i] = accumulate_sha256([nodes[2 * i], nodes[2 * i + 1]]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For num_non_empty_leaves = 0 and 1 we could cause havoc here as prev_power_2 is directly used in index here and on line 108.

} else {
// For more than 1 leaf, we dynamically compute num of nodes in layer 1 by finding the previous power of 2
get_prev_power_2(num_non_empty_leaves)
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These ^ ifs are the fix --> now we don't enter get_prev_power_2 for num_non_empty_leaves < 2 and instead in those situations we explicitly set the values.

// If we have no num_non_empty_leaves, we return 0
let mut stop = num_non_empty_leaves == 0;

let num_nodes_layer_1 = if (num_non_empty_leaves == 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed prev_power_2 to num_nodes_layer_1 as I found it hard to reason about prev_power_2.

@benesjan benesjan force-pushed the 01-10-refactor_variablemerkletree_readability_improvements branch from 6c8ecfd to 3bcccd6 Compare January 13, 2025 15:06
@benesjan benesjan force-pushed the 01-10-fix_underconstrained_bug branch from 2de30bd to 26c5817 Compare January 13, 2025 15:06
Copy link
Contributor

github-actions bot commented Jan 13, 2025

Changes to circuit sizes

Generated at commit: 1c4fcda1378cd1927d869a02bf32e862a51c76a7, compared to commit: 8be882f3f00a0652abe1a70709eeb9374a768b22

🧾 Summary (100% most significant diffs)

Program ACIR opcodes (+/-) % Circuit size (+/-) %
rollup_base_private +2 ❌ +0.00% +6 ❌ +0.00%
rollup_base_public +2 ❌ +0.00% +6 ❌ +0.00%

Full diff report 👇
Program ACIR opcodes (+/-) % Circuit size (+/-) %
rollup_base_private 138,590 (+2) +0.00% 1,772,615 (+6) +0.00%
rollup_base_public 366,997 (+2) +0.00% 3,236,019 (+6) +0.00%

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, 11:26 AM EST: Graphite rebased this pull request as part of a merge.
  • Jan 13, 12:04 PM EST: A user merged this pull request with Graphite.

@benesjan benesjan changed the base branch from 01-10-refactor_variablemerkletree_readability_improvements to graphite-base/11174 January 13, 2025 16:24
@benesjan benesjan changed the base branch from graphite-base/11174 to master January 13, 2025 16:25
@benesjan benesjan force-pushed the 01-10-fix_underconstrained_bug branch from 26c5817 to 52422a8 Compare January 13, 2025 16:25
@benesjan benesjan merged commit 0b3088b into master Jan 13, 2025
49 checks passed
@benesjan benesjan deleted the 01-10-fix_underconstrained_bug branch January 13, 2025 17:04
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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants