Skip to content

chore: bool_t from witness index functionality #16688

Merged
iakovenkos merged 11 commits into
merge-train/barretenbergfrom
si/bool-t-from-witness-idx
Sep 4, 2025
Merged

chore: bool_t from witness index functionality #16688
iakovenkos merged 11 commits into
merge-train/barretenbergfrom
si/bool-t-from-witness-idx

Conversation

@iakovenkos

@iakovenkos iakovenkos commented Sep 1, 2025

Copy link
Copy Markdown
Contributor

Several bool_t improvements:

As a byproduct:

  • some simplifications in compute_naf method
  • using get_normalized_witness_index on bool_t outside of the friend field_t and biggroup classes
  • slightly improved used_witnesses handling in a couple of places.

The vks and the number of gates have changed due to a tiny opt related to re-using a couple of bool_t results in + and - in biggroup

builder.fix_witness(input.result_infinite, infinite.get_value());
} else {
builder.assert_equal(infinite.witness_index, input.result_infinite);
builder.assert_equal(infinite.get_normalized_witness_index(), input.result_infinite);

@iakovenkos iakovenkos Sep 1, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's safer to always access the normalized index.

bit.witness_bool = false;
ctx->create_new_range_constraint(
bit.witness_index, 1, "biggroup_nafs: compute_naf extracted too many bits in next_entry case");
bool_ct bit(witness_t<C>(ctx, !next_entry));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

use the appropriate constructor to create a constrained bool witness

* @warning The witness value **is not** constrained to be boolean. We simply perform an out-of-circuit sanity check.
*/
template <typename Builder>
bool_t<Builder> bool_t<Builder>::from_witness_index_unsafe(Builder* ctx, const uint32_t witness_index)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@guipublic here's the promissed functionality. It feels safer to me if all from_witness_index calls would be marked as unsafe, so that it's clear that such calls have to be justified + analyzed more carefully during the audit

for @suyash67 - Luke has spotted that in #16663, the witness index is set up directly to avoid creating an extra boolean gate which is redundant in this case (see the comments there). so I added this method + turned all class variables private

bool get_value() const { return witness_bool ^ witness_inverted; }

bool is_constant() const { return witness_index == IS_CONSTANT; }
bool is_inverted() const

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

seems cleaner than adding a bunch of friend classes as friends

@iakovenkos iakovenkos requested a review from suyash67 September 1, 2025 11:35
@iakovenkos iakovenkos changed the base branch from next to merge-train/barretenberg September 1, 2025 11:35
@iakovenkos iakovenkos changed the base branch from merge-train/barretenberg to next September 1, 2025 11:36
@iakovenkos iakovenkos marked this pull request as ready for review September 1, 2025 11:36
Comment thread barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp Outdated
@iakovenkos iakovenkos self-assigned this Sep 1, 2025
Comment thread barretenberg/cpp/src/barretenberg/stdlib/encryption/schnorr/schnorr.test.cpp Outdated
@iakovenkos iakovenkos added the bberg-int-audit All things related to barretenberg internal audit label Sep 4, 2025

if (use_range_constraint) {
// Create a range constraint gate
context->create_new_range_constraint(witness_index, 3, "bool_t: witness value is not 0 or 1");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should the range be [0, 1] and hence create_new_range_constraint(witness_index, 1, ...)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh noooo

@iakovenkos iakovenkos changed the base branch from next to merge-train/barretenberg September 4, 2025 16:00
@iakovenkos iakovenkos merged commit 912b542 into merge-train/barretenberg Sep 4, 2025
6 checks passed
@iakovenkos iakovenkos deleted the si/bool-t-from-witness-idx branch September 4, 2025 16:57
github-merge-queue Bot pushed a commit that referenced this pull request Sep 4, 2025
BEGIN_COMMIT_OVERRIDE
chore: compute beta products for max log dyadic size instead of 21
(#16772)
chore: bool_t from witness index functionality  (#16688)
chore: audit the set relation of the ECCVM (#16706)
chore: update readme in the eccvm to include description of multiset and
lookups (#16765)
END_COMMIT_OVERRIDE
Maddiaa0 pushed a commit that referenced this pull request Sep 4, 2025
Several `bool_t` improvements:
* added a `from_witness_index_unsafe` constructor that implicitly
appeared in #16663
* turned `witness_index` and other class members private
* added a more efficient constructor that takes a `witness_t` and uses
range constraints instead of `bool_gate`. see [related
issue](AztecProtocol/barretenberg#1533) + the
tests

As a byproduct:
* some simplifications in `compute_naf` method
* using `get_normalized_witness_index` on `bool_t` outside of the friend
`field_t` and `biggroup` classes
* slightly improved `used_witnesses` handling in a couple of places. 

The vks and the number of gates have changed due to a tiny opt related
to re-using a couple of `bool_t` results in `+` and `-` in `biggroup`
mralj pushed a commit that referenced this pull request Oct 13, 2025
Several `bool_t` improvements:
* added a `from_witness_index_unsafe` constructor that implicitly
appeared in #16663
* turned `witness_index` and other class members private
* added a more efficient constructor that takes a `witness_t` and uses
range constraints instead of `bool_gate`. see [related
issue](AztecProtocol/barretenberg#1533) + the
tests

As a byproduct:
* some simplifications in `compute_naf` method
* using `get_normalized_witness_index` on `bool_t` outside of the friend
`field_t` and `biggroup` classes
* slightly improved `used_witnesses` handling in a couple of places.

The vks and the number of gates have changed due to a tiny opt related
to re-using a couple of `bool_t` results in `+` and `-` in `biggroup`
ludamad pushed a commit that referenced this pull request Dec 16, 2025
Several `bool_t` improvements:
* added a `from_witness_index_unsafe` constructor that implicitly
appeared in #16663
* turned `witness_index` and other class members private
* added a more efficient constructor that takes a `witness_t` and uses
range constraints instead of `bool_gate`. see [related
issue](AztecProtocol/barretenberg#1533) + the
tests

As a byproduct:
* some simplifications in `compute_naf` method
* using `get_normalized_witness_index` on `bool_t` outside of the friend
`field_t` and `biggroup` classes
* slightly improved `used_witnesses` handling in a couple of places. 

The vks and the number of gates have changed due to a tiny opt related
to re-using a couple of `bool_t` results in `+` and `-` in `biggroup`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bberg-int-audit All things related to barretenberg internal audit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants