2#include "../byte_array/byte_array.hpp"
8#include "gmock/gmock.h"
10#include <gtest/gtest.h>
14#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
15#pragma GCC diagnostic ignored "-Wunused-const-variable"
17#define STDLIB_TYPE_ALIASES \
18 using Builder = TypeParam; \
19 using witness_ct = stdlib::witness_t<Builder>; \
20 using field_ct = stdlib::field_t<Builder>; \
21 using bool_ct = stdlib::bool_t<Builder>; \
22 using suint_ct = stdlib::safe_uint_t<Builder>; \
23 using byte_array_ct = stdlib::byte_array<Builder>; \
24 using public_witness_ct = stdlib::public_witness_t<Builder>;
34template <
class Builder>
class SafeUintTest :
public ::testing::Test {};
52 a.set_origin_tag(first_and_third_merged_tag);
53 suint_ct
b(
a, 2,
"b");
56 EXPECT_EQ(
b.get_origin_tag(), first_and_third_merged_tag);
83 c.set_origin_tag(submitted_value_origin_tag);
85 d.set_origin_tag(challenge_origin_tag);
89 EXPECT_EQ(c.get_origin_tag(), first_two_merged_tag);
106 for (
auto i = 0; i < 159; i++) {
113 FAIL() <<
"Expected out of range error";
114 }
catch (std::runtime_error
const& err) {
116 EXPECT_THAT(err.what(), testing::HasSubstr(
"exceeded modulus in safe_uint class"));
118 FAIL() <<
"Expected std::runtime_error modulus in safe_uint class";
137 for (
auto i = 0; i < 252; i++) {
146 FAIL() <<
"Expected out of range error";
147 }
catch (std::runtime_error
const& err) {
149 EXPECT_THAT(err.what(), testing::HasSubstr(
"exceeded modulus in safe_uint class"));
151 FAIL() <<
"Expected std::runtime_error modulus in safe_uint class";
166 c.set_origin_tag(submitted_value_origin_tag);
168 d.set_origin_tag(challenge_origin_tag);
172 EXPECT_EQ(c.get_origin_tag(), first_two_merged_tag);
186 for (
auto i = 0; i < 159; i++) {
193 FAIL() <<
"Expected out of range error";
194 }
catch (std::runtime_error
const& err) {
196 EXPECT_THAT(err.what(), testing::HasSubstr(
"exceeded modulus in safe_uint class"));
198 FAIL() <<
"Expected std::runtime_error modulus in safe_uint class";
216 c.set_origin_tag(submitted_value_origin_tag);
218 d.set_origin_tag(challenge_origin_tag);
219 c = d.subtract(c, 3);
223 EXPECT_EQ(c.get_origin_tag(), first_two_merged_tag);
238 suint_ct c(
a, 2,
"c");
239 suint_ct d(
b, 4,
"d");
240 c = d.subtract(c, 2,
"d - c");
250#if !defined(__wasm__)
260 c = c.subtract(d, suint_ct::MAX_BIT_NUM);
269#if !defined(__wasm__)
278 suint_ct d(
b, suint_ct::MAX_BIT_NUM);
280 c = c.subtract(d, suint_ct::MAX_BIT_NUM);
281 FAIL() <<
"Expected out of range error";
282 }
catch (std::runtime_error
const& err) {
284 EXPECT_EQ(err.what(), std::string(
"maximum value exceeded in safe_uint subtract"));
286 FAIL() <<
"Expected std::runtime_error modulus in safe_uint class";
304 c.set_origin_tag(submitted_value_origin_tag);
306 d.set_origin_tag(challenge_origin_tag);
311 EXPECT_EQ(c.get_origin_tag(), first_two_merged_tag);
317#if !defined(__wasm__)
336#if !defined(__wasm__)
345 suint_ct d(
b, suint_ct::MAX_BIT_NUM);
355#if !defined(__wasm__)
376#if !defined(__wasm__)
384 suint_ct c(
a, suint_ct::MAX_BIT_NUM);
385 suint_ct d(
b, suint_ct::MAX_BIT_NUM);
389 FAIL() <<
"Expected error to be thrown";
390 }
catch (std::runtime_error
const& err) {
392 EXPECT_EQ(err.what(),
393 std::string(
"maximum value exceeded in safe_uint minus operator"));
396 FAIL() <<
"Expected no error, got other error";
407#if !defined(__wasm__)
415 suint_ct c(
a, suint_ct::MAX_BIT_NUM);
416 suint_ct d(
b, suint_ct::MAX_BIT_NUM);
419 FAIL() <<
"Expected error to be thrown";
420 }
catch (std::runtime_error
const& err) {
422 EXPECT_EQ(err.what(),
423 std::string(
"maximum value exceeded in safe_uint minus operator"));
426 FAIL() <<
"Expected no error, got other error";
443 c1.set_origin_tag(submitted_value_origin_tag);
445 d1.set_origin_tag(challenge_origin_tag);
446 c1 = d1.divide(c1, 3, 1);
449 EXPECT_EQ(c1.get_origin_tag(), first_two_merged_tag);
455 c2 = d2.divide(c2, 32, 8);
469 d = d.divide(c, 4, 1,
"d/c");
474#if !defined(__wasm__)
485 EXPECT_ANY_THROW(
b = c / d);
527 a.set_origin_tag(submitted_value_origin_tag);
528 suint_ct
b(2, 2,
"b");
529 b.set_origin_tag(challenge_origin_tag);
535 EXPECT_EQ(
a.get_origin_tag(), first_two_merged_tag);
560 EXPECT_EQ(result,
true);
571 EXPECT_EQ(result,
false);
582 EXPECT_EQ(result,
false);
593 EXPECT_EQ(result,
false);
604 EXPECT_EQ(result,
false);
622 a.set_origin_tag(next_challenge_tag);
623 auto slice_data =
a.slice(10, 3);
625 EXPECT_EQ(slice_data[0].get_value(),
fr(3));
626 EXPECT_EQ(slice_data[1].get_value(),
fr(169));
627 EXPECT_EQ(slice_data[2].get_value(),
fr(61));
630 EXPECT_EQ(slice_data[0].get_origin_tag(), next_challenge_tag);
631 EXPECT_EQ(slice_data[1].get_origin_tag(), next_challenge_tag);
632 EXPECT_EQ(slice_data[2].get_origin_tag(), next_challenge_tag);
650 auto slice_data =
a.slice(6, 6);
652 EXPECT_EQ(slice_data[0].get_value(),
fr(11));
653 EXPECT_EQ(slice_data[1].get_value(),
fr(1));
654 EXPECT_EQ(slice_data[2].get_value(),
fr(986));
669 auto slice =
a.slice(msb, lsb);
674 (
uint256_t(a_) >> uint64_t(msb + 1)) & ((
uint256_t(1) << (uint64_t(252 - msb) - 1)) - 1);
676 EXPECT_EQ(
slice[0].get_value(),
fr(expected0));
677 EXPECT_EQ(
slice[1].get_value(),
fr(expected1));
678 EXPECT_EQ(
slice[2].get_value(),
fr(expected2));
702 suint_ct quotient(quotient_field, (
size_t)(
a.current_max.get_msb() + 1));
703 suint_ct remainder(remainder_field, (
size_t)(
a.current_max.get_msb() + 1));
705 suint_ct int_val = quotient *
b + remainder;
709 suint_ct delta =
b - remainder - 1;
732 a.assert_equal(int_val);
735 EXPECT_EQ(result,
false);
757 a.divide(
b, 32, 32,
"", supply_bad_witnesses);
760 EXPECT_EQ(result,
false);
770 suint_ct safe(elt, 63);
772 std::string expected = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
773 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
774 0x00, 0x00, 0x7f, 0x6f, 0x5f, 0x4f, 0x00, 0x01, 0x02, 0x03 };
780 for (
const auto& single_byte : arr.
bytes()) {
781 EXPECT_EQ(single_byte.get_origin_tag(), next_challenge_tag);
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
Represents a dynamic array of bytes in-circuit.
byte_array & write(byte_array const &other)
Appends the contents of another byte_array (other) to the end of this one.
bytes_t const & bytes() const
std::string get_string() const
Given a byte_array, compute a vector containing the values of its entries and convert it to a string.
bb::OriginTag get_origin_tag() const
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
void create_range_constraint(size_t num_bits, std::string const &msg="field_t::range_constraint") const
Let x = *this.normalize(), constrain x.v < 2^{num_bits}.
void unset_free_witness_tag() const
Unset the free witness flag for the field element's tag.
static constexpr uint256_t modulus
void set_origin_tag(const OriginTag &new_tag) const
ECCVMCircuitBuilder Builder
bn254::witness_ct witness_ct
testing::Types< bb::UltraCircuitBuilder, bb::MegaCircuitBuilder > CircuitTypes
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Entry point for Barretenberg command-line interface.
TYPED_TEST_SUITE(ShpleminiTest, TestSettings)
field< Bn254FrParams > fr
C slice(C const &container, size_t start)
TYPED_TEST(ShpleminiTest, CorrectnessOfMultivariateClaimBatching)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
#define STANDARD_TESTING_TAGS
#define STDLIB_TYPE_ALIASES
static constexpr uint256_t modulus
static field random_element(numeric::RNG *engine=nullptr) noexcept