Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions barretenberg/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ if(ENABLE_ASAN)
endif()

if(FUZZING)
if(SHOW_INFORMATION)
add_definitions(-DSHOW_INFORMATION=1)
endif()

add_definitions(-DFUZZING=1)

if(DISABLE_CUSTOM_MUTATORS)
Expand Down
11 changes: 1 addition & 10 deletions barretenberg/cpp/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@
"binaryDir": "build-fuzzing",
"cacheVariables": {
"FUZZING": "ON"
},
"environment": {
"CFLAGS": "-fsanitize-coverage=indirect-calls,trace-pc-guard",
"CXXFLAGS": "-fsanitize-coverage=indirect-calls,trace-pc-guard",
"LDFLAGS": "-fsanitize-coverage=indirect-calls,trace-pc-guard"
}
},
{
Expand All @@ -304,14 +299,10 @@
"inherits": "clang16-dbg",
"binaryDir": "build-fuzzing-asan",
"cacheVariables": {
"SHOW_INFORMATION": "ON",
"FUZZING": "ON",
"ENABLE_ASAN": "ON",
"DISABLE_ASM": "ON"
},
"environment": {
"CFLAGS": "-fsanitize-coverage=func",
"CXXFLAGS": "-fsanitize-coverage=func",
"LDFLAGS": "-fsanitize-coverage=func"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, size_t size)
for (size_t i = 0; i < polynomial_size; i++) {
auto b = offset[i / 8];

poly[i] = polynomial_coefficients[i];
poly.at(i) = polynomial_coefficients[i];
if ((b >> (i % 8)) & 1) {
poly[i].self_from_montgomery_form();
poly.at(i).self_from_montgomery_form();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ field_t<Builder>::field_t(Builder* parent_context, const bb::fr& value)
: context(parent_context)
{
additive_constant = value;
multiplicative_constant = bb::fr::zero();
multiplicative_constant = bb::fr::one();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why? It is constant, so doesn't have a witness?

Copy link
Contributor

Choose a reason for hiding this comment

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

Was madd failing?

witness_index = IS_CONSTANT;
}

Expand Down Expand Up @@ -105,7 +105,7 @@ template <typename Builder> field_t<Builder> field_t<Builder>::operator+(const f
field_t<Builder> result(ctx);
ASSERT(ctx || (witness_index == IS_CONSTANT && other.witness_index == IS_CONSTANT));

if (witness_index == other.witness_index) {
if (witness_index == other.witness_index && witness_index != IS_CONSTANT) {
result.additive_constant = additive_constant + other.additive_constant;
result.multiplicative_constant = multiplicative_constant + other.multiplicative_constant;
result.witness_index = witness_index;
Expand Down Expand Up @@ -149,7 +149,9 @@ template <typename Builder> field_t<Builder> field_t<Builder>::operator-(const f
{
field_t<Builder> rhs(other);
rhs.additive_constant.self_neg();
rhs.multiplicative_constant.self_neg();
if (rhs.witness_index != IS_CONSTANT) {
rhs.multiplicative_constant.self_neg();
}
return operator+(rhs);
}

Expand Down Expand Up @@ -713,6 +715,7 @@ template <typename Builder> bb::fr field_t<Builder>::get_value() const
ASSERT(context != nullptr);
return (multiplicative_constant * context->get_variable(witness_index)) + additive_constant;
} else {
ASSERT(this->multiplicative_constant == bb::fr::one());
// A constant field_t's value is tracked wholly by its additive_constant member.
return additive_constant;
}
Expand All @@ -733,8 +736,7 @@ template <typename Builder> bool_t<Builder> field_t<Builder>::operator==(const f
bool is_equal = (fa == fb);
bb::fr fc = is_equal ? bb::fr::one() : fd.invert();
bool_t result(ctx, is_equal);
auto result_witness = witness_t(ctx, is_equal);
result.witness_index = result_witness.witness_index;
result.witness_index = ctx->add_variable(is_equal);
result.witness_bool = is_equal;

field_t x(witness_t(ctx, fc));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ template <typename Builder> class FieldBase {
instruction_opcode == Instruction::OPCODE::WITNESS ||
instruction_opcode == Instruction::OPCODE::CONSTANT_WITNESS) {
*Data = instruction.id;
bb::fr::serialize_to_buffer(insturction.arguments.element.data, Data + 1);
bb::fr::serialize_to_buffer(instruction.arguments.element.data, Data + 1);
}

if constexpr (instruction_opcode == Instruction::OPCODE::ASSERT_ZERO ||
Expand Down Expand Up @@ -2011,4 +2011,4 @@ extern "C" size_t LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
return 0;
}

#pragma clang diagnostic pop
#pragma clang diagnostic pop
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ template <typename Builder> class field_t {
, witness_index(IS_CONSTANT)
{
additive_constant = bb::fr(value);
multiplicative_constant = bb::fr(0);
multiplicative_constant = bb::fr::one();
}

// NOLINTNEXTLINE(google-runtime-int) intended behavior
Expand All @@ -31,15 +31,15 @@ template <typename Builder> class field_t {
, witness_index(IS_CONSTANT)
{
additive_constant = bb::fr(value);
multiplicative_constant = bb::fr(0);
multiplicative_constant = bb::fr::one();
}

field_t(const unsigned int value)
: context(nullptr)
, witness_index(IS_CONSTANT)
{
additive_constant = bb::fr(value);
multiplicative_constant = bb::fr(0);
multiplicative_constant = bb::fr::one();
}

// NOLINTNEXTLINE(google-runtime-int) intended behavior
Expand All @@ -48,20 +48,20 @@ template <typename Builder> class field_t {
, witness_index(IS_CONSTANT)
{
additive_constant = bb::fr(value);
multiplicative_constant = bb::fr(0);
multiplicative_constant = bb::fr::one();
}

field_t(const bb::fr& value)
: context(nullptr)
, additive_constant(value)
, multiplicative_constant(bb::fr(1))
, multiplicative_constant(bb::fr::one())
, witness_index(IS_CONSTANT)
{}

field_t(const uint256_t& value)
: context(nullptr)
, additive_constant(value)
, multiplicative_constant(bb::fr(1))
, multiplicative_constant(bb::fr::one())
, witness_index(IS_CONSTANT)
{}

Expand Down Expand Up @@ -188,9 +188,10 @@ template <typename Builder> class field_t {
field_t operator-() const
{
field_t result(*this);
result.multiplicative_constant = -multiplicative_constant;
result.additive_constant = -additive_constant;

result.additive_constant.self_neg();
if (this->witness_index != IS_CONSTANT) {
result.multiplicative_constant.self_neg();
}
return result;
}

Expand Down Expand Up @@ -251,6 +252,7 @@ template <typename Builder> class field_t {
* factors).
*
* If the witness_index of `this` is ever needed, `normalize` should be called first.
* but it's better to call `get_normalized_witness_index` in such case
*
* Will cost 1 constraint if the field element is not already normalized, as a new witness value would need to be
* created.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,46 @@ template <typename Builder> class stdlib_field : public testing::Test {
bool_ct b_false = bool_ct(one * field_ct(0));
EXPECT_FALSE(b_false.get_value());
}

/**
* @brief Test that conditional assign doesn't produce a new witness
*
*/
static void test_conditional_assign_regression()
{
Builder builder = Builder();

field_ct x(2);
field_ct y(2);
field_ct z(1);
field_ct alpha = x.madd(y, -z);
field_ct beta(3);
field_ct zeta = field_ct::conditional_assign(bool_ct(witness_ct(&builder, false)), alpha, beta);

EXPECT_TRUE(zeta.is_constant());
}

/**
* @brief Test that multiplicative_constant of constants is no longer affected
* by any arithimetic operation
*
*/
static void test_multiplicative_constant_regression()
{
Builder builder = Builder();

field_ct a(1);
field_ct b(1);
EXPECT_TRUE(a.multiplicative_constant == bb::fr::one());
EXPECT_TRUE(b.multiplicative_constant == bb::fr::one());
auto c = a + b;
EXPECT_TRUE(c.multiplicative_constant == bb::fr::one());
c = a - b;
EXPECT_TRUE(c.multiplicative_constant == bb::fr::one());
c = -c;
EXPECT_TRUE(c.multiplicative_constant == bb::fr::one());
}

/**
* @brief Demonstrate current behavior of assert_equal.
*/
Expand Down Expand Up @@ -1105,6 +1145,14 @@ TYPED_TEST(stdlib_field, test_create_range_constraint)
{
TestFixture::create_range_constraint();
}
TYPED_TEST(stdlib_field, test_conditional_assign_regression)
{
TestFixture::test_conditional_assign_regression();
}
TYPED_TEST(stdlib_field, test_multiplicative_constant_regression)
{
TestFixture::test_multiplicative_constant_regression();
}
TYPED_TEST(stdlib_field, test_assert_equal)
{
TestFixture::test_assert_equal();
Expand Down
Loading