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

chore: Document requirement for range opcode on r_witness in GeneratedAcir::euclidean_division #2437

Merged
merged 4 commits into from
Sep 4, 2023

Conversation

TomAFrench
Copy link
Member

@TomAFrench TomAFrench commented Aug 25, 2023

Description

Problem*

We currently emit two range opcodes for r_witness here where the second is always more strict than the first.

Summary*

This PR removes the first range opcode emitted for r_witness in euclidean_division as r < rhs < 2^{rhs_max_bits} can be relaxed to r < rhs without affecting the constraints on r.

The restrictions on r_witness in euclidean_division are a little misleading as in a lot of cases they result in duplicated range opcodes, e.g.

BLACKBOX::RANGE [(_7, num_bits: 32)] [ ] // q_witness
BLACKBOX::RANGE [(_8, num_bits: 33)] [ ] // r_witness
BLACKBOX::RANGE [(_8, num_bits: 32)] [ ] // r_witness

This duplication comes from an optimization from inside bound_constraint_with_offset which isn't guaranteed to happen (but does for a large number of cases). This PR documents this so that people don't go around trying to remove it in future based on ACIR which looks like it's unnecessary.

Documentation

  • This PR requires documentation updates when merged.

    • I will submit a noir-lang/docs PR.
    • I will request for and support Dev Rel's help in documenting this PR.

Additional Context

PR Checklist*

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

@TomAFrench
Copy link
Member Author

Bump on this. I think this should be fairly uncontroversial

@kevaundray kevaundray requested review from guipublic and removed request for jfecher and kevaundray August 30, 2023 21:56
Copy link
Contributor

@guipublic guipublic left a comment

Choose a reason for hiding this comment

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

Bound constraints work on inputs having a specified bit size, which is given in argument to the function. This is why the range constraint on r_witness is important and cannot be removed.

@TomAFrench
Copy link
Member Author

I'm seeing two range opcodes being emitted for the same witness here. Looking again at this we're creating new witnesses in bound_constraint_with_witness so maybe I'm not making the change I want to.

* master: (47 commits)
  fix: Initialize structs during def collection, not name resolution (#2528)
  feat: Apply optimizations to unconstrained code (#2348)
  chore(ci): Distinguish between expected failures and compiler panics in `compile_failure` tests. (#2518)
  chore: improve types in `acvm-backend-barretenberg` (#2516)
  feat(aztec_noir): abstract kernel return types (#2521)
  chore: remove usage of `Backend` trait (#2514)
  chore: delete `ProveAndVerifyCommand` (#2520)
  chore: Remove dead code from `acvm_backend_barretenberg` (#2512)
  chore: only install `tokio-util` dependency on windows (#2425)
  chore(aztec_noir):  imply the open keyword (#2508)
  chore: pull `acvm-backend-barretenberg` into main Noir repo (#2495)
  chore: clippy fix (#2507)
  chore: check if the noir aztec library is installed (#2505)
  chore: update ACIR artifacts (#2503)
  chore!: Update to `acvm-backend-barretenberg` v0.12.0 (#2377)
  fix: Bring back accidentally deleted double_verify_proof test. (#2501)
  chore(aztec_noir): import aztec library if not found yet (#2492)
  chore(abi)!: Replace struct name with fully qualified struct path (#2374)
  chore!: Remove keys from preprocessed artifacts (#2283)
  chore(noir): Release 0.10.5 (#2482)
  ...
@TomAFrench
Copy link
Member Author

TomAFrench commented Sep 1, 2023

Seems like we're getting this duplicated range opcode due to euclidean division calling bound_constraint_with_offset with with a constant rhs which is a round power of two. This results in aor = lhs + 0 = lhs so

let witness = self.get_or_create_witness(&aor);
self.range_constraint(witness, bit_size)?;

then gets simplified to creating a new range constraint on lhs (r_witness) instead of creating a new witness.

Looks like we could make this optimization but I think relying on another optimization to make it safe feels a bit iffy. Especially as ACVM will optimize out the redundant opcode anyway. Euclidean division isn't guaranteed to use a denominator of a power of 2, it's just the most common usage of euclidean division in ACIR does.

@TomAFrench
Copy link
Member Author

I'm going to add this range constraint back in and add a little documentation instead.

@TomAFrench TomAFrench changed the title feat: remove unnecessary range constraint in euclidean_division chore: Document requirement for range opcode on r_witness in GeneratedAcir::euclidean_division Sep 1, 2023
@TomAFrench TomAFrench requested a review from guipublic September 1, 2023 22:00
@TomAFrench TomAFrench added this pull request to the merge queue Sep 4, 2023
Merged via the queue into master with commit 1d9bcd0 Sep 4, 2023
@TomAFrench TomAFrench deleted the remove-unnecessary-range-constraint branch September 4, 2023 10:04
TomAFrench added a commit that referenced this pull request Sep 5, 2023
* master:
  chore: Replace hashers of hashmaps in dfg with fxhashes (#2490)
  chore: remove duplicate span from FunctionReturnType (#2546)
  feat: Add support for brillig call stacks in runtime errors (#2549)
  feat: add `noirc_abi_wasm` crate for ABI encoding in JS (#1945)
  chore: move CRS files into their own directory (#2558)
  chore: Cleanup `rebuild.sh` script (#2470)
  chore(ci): add mocked backend binary to improve `compile_success_empty` tests (#2554)
  chore: add noir-source-resolver (#2485)
  chore: fix double verify proof (#2556)
  feat: add `nargo backend ls` and `nargo backend use` command to switch between backends (#2552)
  chore(ci): bump checkout action to v4 (#2551)
  feat: Support for optional assertion messages (#2491)
  fix: allow usage of decimal string encoding for fields larger than a `i128` (#2547)
  feat(nargo): add hidden option to produce JSON output from `nargo info` (#2542)
  chore(stdlib)!: Rename `fixed_base_scalar_mul` to be more descriptive (#2488)
  chore: Document requirement for range opcode on `r_witness` in  `GeneratedAcir::euclidean_division` (#2437)
  chore!: ACVM 0.24 (#2504)
  fix(aztec_noir): generalise loop to not always inject a hasher instance (#2529)
  chore: create helper functions for writing programs and contracts to file (#2526)
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