-
Notifications
You must be signed in to change notification settings - Fork 607
chore: uint audit part 1 (operator<<, operator>>, ror)
#15823
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7939225
consistent arg names, fix basic warnings.
suyash67 b168a9c
make byte_array's constructor more efficient.
suyash67 1dde319
make bit_array's constructor more efficient.
suyash67 70a4dab
add new function split_at in field_t. use that in bitwise ops in uint…
suyash67 652615a
resolve comments.
suyash67 c5a2e0e
fix type err.
suyash67 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| #include "barretenberg/circuit_checker/circuit_checker.hpp" | ||
| #include "barretenberg/common/streams.hpp" | ||
| #include "barretenberg/numeric/random/engine.hpp" | ||
| #include "barretenberg/numeric/uint256/uint256.hpp" | ||
| #include "barretenberg/stdlib/primitives/circuit_builders/circuit_builders.hpp" | ||
| #include <gtest/gtest.h> | ||
| #include <utility> | ||
|
|
@@ -859,6 +860,56 @@ template <typename Builder> class stdlib_field : public testing::Test { | |
| EXPECT_TRUE(builder.err() == "slice: hi value too large."); | ||
| } | ||
|
|
||
| static void test_split_at() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible to produce a failure test with the current circuit design? |
||
| { | ||
| Builder builder = Builder(); | ||
|
|
||
| // Test different bit sizes | ||
| std::vector<size_t> test_bit_sizes = { 8, 16, 32, 100, 252 }; | ||
|
|
||
| // Lambda to check split_at functionality | ||
| auto check_split_at = [&](const field_ct& a, size_t start, size_t num_bits) { | ||
| const uint256_t a_native = a.get_value(); | ||
| auto split_data = a.split_at(start, num_bits); | ||
| EXPECT_EQ(split_data.first.get_value(), a_native & ((uint256_t(1) << start) - 1)); | ||
| EXPECT_EQ(split_data.second.get_value(), (a_native >> start) & ((uint256_t(1) << num_bits) - 1)); | ||
|
|
||
| if (a.is_constant()) { | ||
| EXPECT_TRUE(split_data.first.is_constant()); | ||
| EXPECT_TRUE(split_data.second.is_constant()); | ||
| } | ||
|
|
||
| if (start == 0) { | ||
| EXPECT_TRUE(split_data.first.is_constant()); | ||
| EXPECT_TRUE(split_data.first.get_value() == 0); | ||
| EXPECT_EQ(split_data.second.get_value(), a.get_value()); | ||
| } | ||
| }; | ||
|
|
||
| for (size_t num_bits : test_bit_sizes) { | ||
| uint256_t a_native = engine.get_random_uint256() & ((uint256_t(1) << num_bits) - 1); | ||
|
|
||
| // check split_at for a constant | ||
| field_ct a_constant(a_native); | ||
| check_split_at(a_constant, 0, num_bits); | ||
| check_split_at(a_constant, num_bits / 4, num_bits); | ||
| check_split_at(a_constant, num_bits / 3, num_bits); | ||
| check_split_at(a_constant, num_bits / 2, num_bits); | ||
| check_split_at(a_constant, num_bits - 1, num_bits); | ||
|
|
||
| // check split_at for a witness | ||
| field_ct a_witness(witness_ct(&builder, a_native)); | ||
| check_split_at(a_witness, 0, num_bits); | ||
| check_split_at(a_witness, num_bits / 4, num_bits); | ||
| check_split_at(a_witness, num_bits / 3, num_bits); | ||
| check_split_at(a_witness, num_bits / 2, num_bits); | ||
| check_split_at(a_witness, num_bits - 1, num_bits); | ||
| } | ||
|
|
||
| bool result = CircuitChecker::check(builder); | ||
| EXPECT_EQ(result, true); | ||
| } | ||
|
|
||
| static void test_three_bit_table() | ||
| { | ||
| Builder builder = Builder(); | ||
|
|
@@ -1362,6 +1413,12 @@ template <typename Builder> class stdlib_field : public testing::Test { | |
| EXPECT_EQ(element.get_origin_tag(), submitted_value_origin_tag); | ||
| } | ||
|
|
||
| // Split preserves tags | ||
| const size_t num_bits = uint256_t(a.get_value()).get_msb() + 1; | ||
| auto split_data = a.split_at(num_bits / 2, num_bits); | ||
| EXPECT_EQ(split_data.first.get_origin_tag(), submitted_value_origin_tag); | ||
| EXPECT_EQ(split_data.second.get_origin_tag(), submitted_value_origin_tag); | ||
|
|
||
| // Decomposition preserves tags | ||
|
|
||
| auto decomposed_bits = a.decompose_into_bits(); | ||
|
|
@@ -1555,6 +1612,10 @@ TYPED_TEST(stdlib_field, test_slice_random) | |
| { | ||
| TestFixture::test_slice_random(); | ||
| } | ||
| TYPED_TEST(stdlib_field, test_split_at) | ||
| { | ||
| TestFixture::test_split_at(); | ||
| } | ||
| TYPED_TEST(stdlib_field, test_three_bit_table) | ||
| { | ||
| TestFixture::test_three_bit_table(); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! I can re-use it in
byte_arraybyte decomposition method