diff --git a/barretenberg/cpp/CMakeLists.txt b/barretenberg/cpp/CMakeLists.txt index 0a65e2f9d487..95a08e769bbb 100644 --- a/barretenberg/cpp/CMakeLists.txt +++ b/barretenberg/cpp/CMakeLists.txt @@ -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) diff --git a/barretenberg/cpp/CMakePresets.json b/barretenberg/cpp/CMakePresets.json index 1edfe3b54013..c7fa1b5b3673 100644 --- a/barretenberg/cpp/CMakePresets.json +++ b/barretenberg/cpp/CMakePresets.json @@ -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" } }, { @@ -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" } }, { diff --git a/barretenberg/cpp/src/barretenberg/commitment_schemes/ipa/ipa.fuzzer.cpp b/barretenberg/cpp/src/barretenberg/commitment_schemes/ipa/ipa.fuzzer.cpp index 5c2dbfc12182..2f075251e30a 100644 --- a/barretenberg/cpp/src/barretenberg/commitment_schemes/ipa/ipa.fuzzer.cpp +++ b/barretenberg/cpp/src/barretenberg/commitment_schemes/ipa/ipa.fuzzer.cpp @@ -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(); } } diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp index b6e47a9f6ce4..079a9500c806 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp @@ -36,7 +36,7 @@ field_t::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(); witness_index = IS_CONSTANT; } @@ -105,7 +105,7 @@ template field_t field_t::operator+(const f field_t 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; @@ -149,7 +149,9 @@ template field_t field_t::operator-(const f { field_t 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); } @@ -713,6 +715,7 @@ template bb::fr field_t::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; } @@ -733,8 +736,7 @@ template bool_t field_t::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)); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.fuzzer.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.fuzzer.hpp index 5c3d721f59f7..0eea9cac4741 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.fuzzer.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.fuzzer.hpp @@ -648,7 +648,7 @@ template 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 || @@ -2011,4 +2011,4 @@ extern "C" size_t LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) return 0; } -#pragma clang diagnostic pop \ No newline at end of file +#pragma clang diagnostic pop diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.hpp index f677415a2152..0386751c44b6 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.hpp @@ -22,7 +22,7 @@ template 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 @@ -31,7 +31,7 @@ template 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) @@ -39,7 +39,7 @@ template 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 @@ -48,20 +48,20 @@ template 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) {} @@ -188,9 +188,10 @@ template 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; } @@ -251,6 +252,7 @@ template 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. diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp index 546e79012ec4..b75c4a0323f3 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp @@ -118,6 +118,46 @@ template 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. */ @@ -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(); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.cpp index b683b82e7aab..a744c7044a0d 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.cpp @@ -10,6 +10,13 @@ #include "barretenberg/transcript/origin_tag.hpp" namespace bb::stdlib { +/** + * @brief Construct a new cycle group::cycle group object + * defaults to a constant point at infinity. + * + * @note Please don't use this constructor in case you want to assign the + * coordinates later. + */ template cycle_group::cycle_group(Builder* _context) : x(0) @@ -28,12 +35,12 @@ cycle_group::cycle_group(Builder* _context) * @param is_infinity */ template -cycle_group::cycle_group(field_t _x, field_t _y, bool_t is_infinity, bool is_standard) +cycle_group::cycle_group(field_t _x, field_t _y, bool_t is_infinity) : x(_x.normalize()) , y(_y.normalize()) , _is_infinity(is_infinity) , _is_constant(_x.is_constant() && _y.is_constant() && is_infinity.is_constant()) - , _is_standard(is_standard) + , _is_standard(is_infinity.is_constant()) { if (_x.get_context() != nullptr) { context = _x.get_context(); @@ -43,6 +50,13 @@ cycle_group::cycle_group(field_t _x, field_t _y, bool_t is_infinity, bo context = is_infinity.get_context(); } + if (is_infinity.is_constant() && is_infinity.get_value()) { + this->x = 0; + this->y = 0; + this->_is_infinity = true; + this->_is_constant = true; + } + // TODO(https://github.com/AztecProtocol/barretenberg/issues/1067): This ASSERT is missing in the constructor but // causes schnorr acir test to fail due to a bad input (a public key that has x and y coordinate set to 0). // Investigate this to be able to enable the test. @@ -63,12 +77,12 @@ cycle_group::cycle_group(field_t _x, field_t _y, bool_t is_infinity, bo * @param is_infinity */ template -cycle_group::cycle_group(const FF& _x, const FF& _y, bool is_infinity, bool is_standard) - : x(_x) - , y(_y) +cycle_group::cycle_group(const FF& _x, const FF& _y, bool is_infinity) + : x(is_infinity ? 0 : _x) + , y(is_infinity ? 0 : _y) , _is_infinity(is_infinity) , _is_constant(true) - , _is_standard(is_standard) + , _is_standard(true) , context(nullptr) { ASSERT(get_value().on_curve()); @@ -105,7 +119,7 @@ template cycle_group cycle_group::one(Build { field_t x(_context, Group::one.x); field_t y(_context, Group::one.y); - return cycle_group(x, y, /*is_infinity=*/false, /*is_standard=*/true); + return cycle_group(x, y, /*is_infinity=*/false); } /** @@ -163,18 +177,18 @@ cycle_group cycle_group::from_constant_witness(Builder* _conte // Since we are not using these coordinates anyway // We can set them both to be zero if (_in.is_point_at_infinity()) { - result.x = field_t(witness_t(_context, FF::zero())); - result.y = field_t(witness_t(_context, FF::zero())); + result.x = FF::zero(); + result.y = FF::zero(); + result._is_constant = true; } else { result.x = field_t(witness_t(_context, _in.x)); result.y = field_t(witness_t(_context, _in.y)); + result.x.assert_equal(result.x.get_value()); + result.y.assert_equal(result.y.get_value()); + result._is_constant = false; } - result.x.assert_equal(result.x.get_value()); - result.y.assert_equal(result.y.get_value()); - // point at infinity is circuit constant result._is_infinity = _in.is_point_at_infinity(); - result._is_constant = false; result._is_standard = true; return result; } @@ -230,42 +244,61 @@ template cycle_group cycle_group::get_stand */ template void cycle_group::set_point_at_infinity(const bool_t& is_infinity) { - // No operations are performed in this case - if (is_infinity.is_constant() && !is_infinity.get_value()) { - return; - } + ASSERT((this->x.is_constant() && this->y.is_constant() && this->_is_infinity.is_constant()) == this->_is_constant); + this->_is_standard = true; - this->x = field_t::conditional_assign(is_infinity, 0, this->x); - this->y = field_t::conditional_assign(is_infinity, 0, this->y); + if (is_infinity.is_constant() && this->_is_infinity.is_constant()) { + // Check that it's not possible to enter the case when + // The point is already infinity, but `is_infinity` = false + ASSERT((this->_is_infinity.get_value() == is_infinity.get_value()) || is_infinity.get_value()); - if (is_infinity.is_constant() && is_infinity.get_value()) { - this->_is_constant = true; - this->_is_infinity = true; + if (is_infinity.get_value()) { + this->x = 0; + this->y = 0; + this->_is_infinity = true; + this->_is_constant = true; + } return; } - if (!this->x.is_constant() && this->y.is_constant()) { - auto ctx = this->x.get_context(); - this->y = field_t::from_witness_index(ctx, ctx->put_constant_variable(this->y.get_value())); - } - if (this->x.is_constant() && !this->y.is_constant()) { - auto ctx = this->y.get_context(); - this->x = field_t::from_witness_index(ctx, ctx->put_constant_variable(this->x.get_value())); + if (is_infinity.is_constant() && !this->_is_infinity.is_constant()) { + if (is_infinity.get_value()) { + this->x = 0; + this->y = 0; + this->_is_infinity = true; + this->_is_constant = true; + } else { + this->_is_infinity.assert_equal(false); + this->_is_infinity = false; + } + return; } - // Due to conditional_assign behavior - // Sometimes we won't create the gate here - // If this->x = 0 and this->y = 0 and both of them are constants - // This ensures that at least one of the switches was performed - this->_is_constant = this->x.is_constant() && this->y.is_constant(); + if (this->_is_infinity.is_constant() && this->_is_infinity.get_value()) { + // I can't imagine this case happening, but still + is_infinity.assert_equal(true); - if (!this->_is_constant) { - this->_is_infinity = is_infinity; + this->x = 0; + this->y = 0; + this->_is_constant = true; + return; } + this->x = field_t::conditional_assign(is_infinity, 0, this->x); + this->y = field_t::conditional_assign(is_infinity, 0, this->y); + + // We won't bump into the case where we end up with non constant coordinates + ASSERT(!this->x.is_constant() && !this->y.is_constant()); + this->_is_constant = false; + + // We have to check this to avoid the situation, where we change the infinity + bool_t set_allowed = (this->_is_infinity == is_infinity) || is_infinity; + set_allowed.assert_equal(true); + this->_is_infinity = is_infinity; + // In case we set point at infinity on a constant without an existing context - if (this->context == nullptr && !this->_is_constant) { + if (this->context == nullptr) { this->context = is_infinity.get_context(); } } @@ -277,10 +310,16 @@ template void cycle_group::set_point_at_infinity(con */ template void cycle_group::standardize() { + ASSERT((this->x.is_constant() && this->y.is_constant() && this->_is_infinity.is_constant()) == this->_is_constant); + if (this->_is_infinity.is_constant() && this->_is_infinity.get_value()) { + ASSERT(this->_is_constant && this->_is_standard); + } + if (this->_is_standard) { return; } this->_is_standard = true; + this->x = field_t::conditional_assign(this->_is_infinity, 0, this->x); this->y = field_t::conditional_assign(this->_is_infinity, 0, this->y); } @@ -398,7 +437,7 @@ cycle_group cycle_group::unconditional_add( auto lambda = y_diff.divide_no_zero_check(x_diff); auto x3 = lambda.madd(lambda, -other.x - x); auto y3 = lambda.madd(x - x3, -y); - cycle_group result(x3, y3, /*is_infinity=*/false, /*is_standard=*/true); + cycle_group result(x3, y3, /*is_infinity=*/false); return result; } @@ -440,10 +479,9 @@ cycle_group cycle_group::unconditional_add(const cycle_group& auto x3 = hint.value().x; auto y3 = hint.value().y; if (lhs_constant && rhs_constant) { - return cycle_group(x3, y3, /*is_infinity=*/false, /*is_standard=*/true); + return cycle_group(x3, y3, /*is_infinity=*/false); } - result = - cycle_group(witness_t(context, x3), witness_t(context, y3), /*is_infinity=*/false, /*is_standard=*/true); + result = cycle_group(witness_t(context, x3), witness_t(context, y3), /*is_infinity=*/false); } else { const auto p1 = get_value(); const auto p2 = other.get_value(); @@ -456,7 +494,7 @@ cycle_group cycle_group::unconditional_add(const cycle_group& } field_t r_x(witness_t(context, p3.x)); field_t r_y(witness_t(context, p3.y)); - result = cycle_group(r_x, r_y, /*is_infinity=*/false, /*is_standard=*/true); + result = cycle_group(r_x, r_y, /*is_infinity=*/false); } bb::ecc_add_gate_ add_gate{ .x1 = x.get_witness_index(), @@ -514,10 +552,9 @@ cycle_group cycle_group::unconditional_subtract(const cycle_gr auto x3 = hint.value().x; auto y3 = hint.value().y; if (lhs_constant && rhs_constant) { - return cycle_group(x3, y3, /*is_infinity=*/false, /*is_standard=*/true); + return cycle_group(x3, y3, /*is_infinity=*/false); } - result = cycle_group( - witness_t(context, x3), witness_t(context, y3), /*is_infinity=*/false, /*is_standard=*/true); + result = cycle_group(witness_t(context, x3), witness_t(context, y3), /*is_infinity=*/false); } else { auto p1 = get_value(); auto p2 = other.get_value(); @@ -530,7 +567,7 @@ cycle_group cycle_group::unconditional_subtract(const cycle_gr } field_t r_x(witness_t(context, p3.x)); field_t r_y(witness_t(context, p3.y)); - result = cycle_group(r_x, r_y, /*is_infinity=*/false, /*is_standard=*/true); + result = cycle_group(r_x, r_y, /*is_infinity=*/false); } bb::ecc_add_gate_ add_gate{ .x1 = x.get_witness_index(), @@ -620,7 +657,6 @@ template cycle_group cycle_group::operator+ return *this; } - Builder* context = get_context(other); const bool_t x_coordinates_match = (x == other.x); const bool_t y_coordinates_match = (y == other.y); const bool_t double_predicate = (x_coordinates_match && y_coordinates_match); @@ -638,7 +674,8 @@ template cycle_group cycle_group::operator+ if ((y1.is_constant() && y2.is_constant()) || x_diff.is_constant()) { lambda = (y2 - y1).divide_no_zero_check(x_diff); } else { - lambda = field_t::from_witness(context, (y2.get_value() - y1.get_value()) / x_diff.get_value()); + lambda = + field_t::from_witness(this->get_context(other), (y2.get_value() - y1.get_value()) / x_diff.get_value()); field_t::evaluate_polynomial_identity(x_diff, lambda, -y2, y1); } @@ -650,19 +687,18 @@ template cycle_group cycle_group::operator+ // dbl if x_match, y_match // infinity if x_match, !y_match - cycle_group result(context); - result.x = field_t::conditional_assign(double_predicate, dbl_result.x, add_result.x); - result.y = field_t::conditional_assign(double_predicate, dbl_result.y, add_result.y); + auto result_x = field_t::conditional_assign(double_predicate, dbl_result.x, add_result.x); + auto result_y = field_t::conditional_assign(double_predicate, dbl_result.y, add_result.y); const bool_t lhs_infinity = is_point_at_infinity(); const bool_t rhs_infinity = other.is_point_at_infinity(); // if lhs infinity, return rhs - result.x = field_t::conditional_assign(lhs_infinity, other.x, result.x); - result.y = field_t::conditional_assign(lhs_infinity, other.y, result.y); + result_x = field_t::conditional_assign(lhs_infinity, other.x, result_x); + result_y = field_t::conditional_assign(lhs_infinity, other.y, result_y); // if rhs infinity, return lhs - result.x = field_t::conditional_assign(rhs_infinity, x, result.x); - result.y = field_t::conditional_assign(rhs_infinity, y, result.y); + result_x = field_t::conditional_assign(rhs_infinity, x, result_x); + result_y = field_t::conditional_assign(rhs_infinity, y, result_y); // is result point at infinity? // yes = infinity_predicate && !lhs_infinity && !rhs_infinity @@ -670,11 +706,7 @@ template cycle_group cycle_group::operator+ bool_t result_is_infinity = infinity_predicate && (!lhs_infinity && !rhs_infinity); result_is_infinity = result_is_infinity || (lhs_infinity && rhs_infinity); - // need to set this before set_point_at_infinity call - result._is_constant = this->_is_constant & other._is_constant; - result.set_point_at_infinity(result_is_infinity); - - return result; + return cycle_group(result_x, result_y, result_is_infinity); } /** @@ -696,7 +728,6 @@ template cycle_group cycle_group::operator- return -other; } - Builder* context = get_context(other); const bool_t x_coordinates_match = (x == other.x); const bool_t y_coordinates_match = (y == other.y); const bool_t double_predicate = (x_coordinates_match && !y_coordinates_match).normalize(); @@ -707,7 +738,16 @@ template cycle_group cycle_group::operator- auto x2 = other.x; auto y2 = other.y; auto x_diff = x2.add_two(-x1, x_coordinates_match); - auto lambda = (-y2 - y1) / x_diff; + // Computes lambda = (-y2-y1)/x_diff, using the fact that x_diff is never 0 + field_t lambda; + if ((y1.is_constant() && y2.is_constant()) || x_diff.is_constant()) { + lambda = (-y2 - y1).divide_no_zero_check(x_diff); + } else { + lambda = + field_t::from_witness(this->get_context(other), (-y2.get_value() - y1.get_value()) / x_diff.get_value()); + field_t::evaluate_polynomial_identity(x_diff, lambda, y2, y1); + } + auto x3 = lambda.madd(lambda, -(x2 + x1)); auto y3 = lambda.madd(x1 - x3, -y1); cycle_group add_result(x3, y3, x_coordinates_match); @@ -716,19 +756,18 @@ template cycle_group cycle_group::operator- // dbl if x_match, !y_match // infinity if x_match, y_match - cycle_group result(context); - result.x = field_t::conditional_assign(double_predicate, dbl_result.x, add_result.x); - result.y = field_t::conditional_assign(double_predicate, dbl_result.y, add_result.y); + auto result_x = field_t::conditional_assign(double_predicate, dbl_result.x, add_result.x); + auto result_y = field_t::conditional_assign(double_predicate, dbl_result.y, add_result.y); const bool_t lhs_infinity = is_point_at_infinity(); const bool_t rhs_infinity = other.is_point_at_infinity(); // if lhs infinity, return -rhs - result.x = field_t::conditional_assign(lhs_infinity, other.x, result.x); - result.y = field_t::conditional_assign(lhs_infinity, (-other.y).normalize(), result.y); + result_x = field_t::conditional_assign(lhs_infinity, other.x, result_x); + result_y = field_t::conditional_assign(lhs_infinity, (-other.y).normalize(), result_y); // if rhs infinity, return lhs - result.x = field_t::conditional_assign(rhs_infinity, x, result.x); - result.y = field_t::conditional_assign(rhs_infinity, y, result.y); + result_x = field_t::conditional_assign(rhs_infinity, x, result_x); + result_y = field_t::conditional_assign(rhs_infinity, y, result_y); // is result point at infinity? // yes = infinity_predicate && !lhs_infinity && !rhs_infinity @@ -737,11 +776,7 @@ template cycle_group cycle_group::operator- bool_t result_is_infinity = infinity_predicate && (!lhs_infinity && !rhs_infinity); result_is_infinity = result_is_infinity || (lhs_infinity && rhs_infinity); - // need to set this before set_point_at_infinity call - result._is_constant = this->_is_constant & other._is_constant; - result.set_point_at_infinity(result_is_infinity); - - return result; + return cycle_group(result_x, result_y, result_is_infinity); } /** @@ -1318,7 +1353,7 @@ template cycle_group cycle_group::straus_lo // Merge tag of table with tag of index x.set_origin_tag(OriginTag(tag, _index.get_origin_tag())); y.set_origin_tag(OriginTag(tag, _index.get_origin_tag())); - return cycle_group(x, y, /*is_infinity=*/false, /*is_standard=*/true); + return cycle_group(x, y, /*is_infinity=*/false); } field_t x = _index * (point_table[1].x - point_table[0].x) + point_table[0].x; field_t y = _index * (point_table[1].y - point_table[0].y) + point_table[0].y; @@ -1326,7 +1361,7 @@ template cycle_group cycle_group::straus_lo // Merge tag of table with tag of index x.set_origin_tag(OriginTag(tag, _index.get_origin_tag())); y.set_origin_tag(OriginTag(tag, _index.get_origin_tag())); - return cycle_group(x, y, /*is_infinity=*/false, /*is_standard=*/true); + return cycle_group(x, y, /*is_infinity=*/false); } /** @@ -1577,7 +1612,7 @@ typename cycle_group::batch_mul_internal_output cycle_group::_ for (size_t j = 0; j < lookup_data[ColumnIdx::C2].size(); ++j) { const auto x = lookup_data[ColumnIdx::C2][j]; const auto y = lookup_data[ColumnIdx::C3][j]; - lookup_points.emplace_back(cycle_group(x, y, /*is_infinity=*/false, /*is_standard=*/true)); + lookup_points.emplace_back(cycle_group(x, y, /*is_infinity=*/false)); } std::optional offset_1 = @@ -1941,7 +1976,7 @@ cycle_group cycle_group::conditional_assign(const bool_t& pred bool _is_standard_res = lhs._is_standard && rhs._is_standard; if (predicate.is_constant()) { - _is_standard_res = (predicate.get_value() && lhs._is_standard) && (!predicate.get_value() && rhs._is_standard); + _is_standard_res = predicate.get_value() ? lhs._is_standard : rhs._is_standard; } // Rare case when we bump into two constants, s.t. lhs = -rhs @@ -1949,8 +1984,12 @@ cycle_group cycle_group::conditional_assign(const bool_t& pred auto ctx = predicate.get_context(); x_res = field_t::from_witness_index(ctx, ctx->put_constant_variable(x_res.get_value())); } - return { x_res, y_res, _is_infinity_res, _is_standard_res }; + + cycle_group result(x_res, y_res, _is_infinity_res); + result._is_standard = _is_standard_res; + return result; }; + template cycle_group cycle_group::operator/(const cycle_group& /*unused*/) const { // TODO(@kevaundray solve the discrete logarithm problem) diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.fuzzer.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.fuzzer.hpp index b41fa335387f..fe1697fe06bf 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.fuzzer.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.fuzzer.hpp @@ -17,49 +17,9 @@ #include "barretenberg/common/fuzzer.hpp" // #define SHOW_INFORMATION -// #define SHOW_PRETTY_INFORMATION #define DISABLE_MULTIPLICATION #ifdef SHOW_INFORMATION -#define PRINT_SINGLE_ARG_INSTRUCTION(first_index, vector, operation_name, preposition) \ - { \ - std::cout << operation_name << " " \ - << (vector[first_index].cycle_group.is_constant() ? "constant(" : "witness(") \ - << vector[first_index].cycle_group.get_value() << ") at " << first_index << " " << preposition \ - << std::flush; \ - } - -#define PRINT_TWO_ARG_INSTRUCTION(first_index, second_index, vector, operation_name, preposition) \ - { \ - std::cout << operation_name << " " \ - << (vector[first_index].cycle_group.is_constant() ? "constant(" : "witness(") \ - << vector[first_index].cycle_group.get_value() << ") at " << first_index << " " << preposition \ - << " " << (vector[second_index].cycle_group.is_constant() ? "constant(" : "witness(") \ - << vector[second_index].cycle_group.get_value() << ") at " << second_index << std::flush; \ - } - -#define PRINT_MUL_ARG_INSTRUCTION(first_index, scalar, vector, operation_name, preposition) \ - { \ - std::cout << operation_name << " " \ - << (vector[first_index].cycle_group.is_constant() ? "constant(" : "witness(") \ - << vector[first_index].cycle_group.get_value() << ") at " << first_index << " " << preposition \ - << " " << scalar << std::flush; \ - } - -#define PRINT_RESULT(prefix, action, index, value) \ - { \ - std::cout << " result(" << value.cycle_group.get_value() << ")" << action << index << std::endl \ - << std::flush; \ - } - -#else -#define PRINT_SINGLE_ARG_INSTRUCTION(first_index, vector, operation_name, preposition) -#define PRINT_TWO_ARG_INSTRUCTION(first_index, second_index, vector, operation_name, preposition) -#define PRINT_MUL_ARG_INSTRUCTION(first_index, scalar, vector, operation_name, preposition) -#define PRINT_RESULT(prefix, action, index, value) -#endif - -#ifdef SHOW_PRETTY_INFORMATION #define PREP_SINGLE_ARG(stack, first_index, output_index) \ std::string rhs = stack[first_index].cycle_group.is_constant() ? "c" : "w"; \ std::string out = rhs; \ @@ -796,19 +756,16 @@ template class CycleGroupBase { * will use the context of another input parameter */ const bool predicate_is_const = static_cast(VarianceRNG.next() & 1); -#ifdef SHOW_INFORMATION - std::cout << "Constant predicate? " << predicate_is_const << std::endl; -#endif if (predicate_is_const) { const bool predicate_has_ctx = static_cast(VarianceRNG.next() % 2); -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "bool_t(" << (predicate_has_ctx ? "&builder," : "nullptr,") - << (predicate ? "true);" : "false);"); + << (predicate ? "true)" : "false)"); #endif return bool_t(predicate_has_ctx ? builder : nullptr, predicate); } -#ifdef SHOW_PRETTY_INFORMATION - std::cout << "bool_t(witness_t(&builder, " << (predicate ? "true));" : "false));"); +#ifdef SHOW_INFORMATION + std::cout << "bool_t(witness_t(&builder, " << (predicate ? "true));" : "false))"); #endif return bool_t(witness_t(builder, predicate)); } @@ -816,9 +773,6 @@ template class CycleGroupBase { cycle_group_t cg() const { const bool reconstruct = static_cast(VarianceRNG.next() % 2); -#ifdef SHOW_INFORMATION - std::cout << " reconstruction? " << reconstruct << std::endl; -#endif if (!reconstruct) { return this->cycle_group; } @@ -844,17 +798,14 @@ template class CycleGroupBase { if (other.cg().get_value() == this->cg().get_value()) { uint8_t dbl_path = VarianceRNG.next() % 4; -#ifdef SHOW_INFORMATION - std::cout << " using " << size_t(dbl_path) << " dbl path" << std::endl; -#endif switch (dbl_path) { case 0: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "left.dbl" << std::endl; #endif return ExecutionHandler(base_scalar_res, base_res, this->cg().dbl()); case 1: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "right.dbl" << std::endl; #endif return ExecutionHandler(base_scalar_res, base_res, other.cg().dbl()); @@ -866,23 +817,26 @@ template class CycleGroupBase { } else if (other.cg().get_value() == -this->cg().get_value()) { uint8_t inf_path = VarianceRNG.next() % 4; cycle_group_t res; -#ifdef SHOW_INFORMATION - std::cout << " using " << size_t(inf_path) << " inf path" << std::endl; -#endif switch (inf_path) { case 0: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "left.set_point_at_infinity("; #endif res = this->cg(); res.set_point_at_infinity(this->construct_predicate(builder, true)); +#ifdef SHOW_INFORMATION + std::cout << ");" << std::endl; +#endif return ExecutionHandler(base_scalar_res, base_res, res); case 1: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "right.set_point_at_infinity("; #endif res = other.cg(); res.set_point_at_infinity(this->construct_predicate(builder, true)); +#ifdef SHOW_INFORMATION + std::cout << ");" << std::endl; +#endif return ExecutionHandler(base_scalar_res, base_res, res); case 2: return ExecutionHandler(base_scalar_res, base_res, this->cg() + other.cg()); @@ -893,28 +847,25 @@ template class CycleGroupBase { bool smth_inf = this->cycle_group.is_point_at_infinity().get_value() || other.cycle_group.is_point_at_infinity().get_value(); uint8_t add_option = smth_inf ? 4 + (VarianceRNG.next() % 2) : VarianceRNG.next() % 6; -#ifdef SHOW_INFORMATION - std::cout << " using " << size_t(add_option) << " add path" << std::endl; -#endif switch (add_option) { case 0: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "left.unconditional_add(right);" << std::endl; #endif return ExecutionHandler(base_scalar_res, base_res, this->cg().unconditional_add(other.cg())); case 1: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "right.unconditional_add(left);" << std::endl; #endif return ExecutionHandler(base_scalar_res, base_res, other.cg().unconditional_add(this->cg())); case 2: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "left.checked_unconditional_add(right);" << std::endl; #endif return ExecutionHandler(base_scalar_res, base_res, this->cg().checked_unconditional_add(other.cg())); case 3: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "right.checked_unconditional_add(left);" << std::endl; #endif return ExecutionHandler(base_scalar_res, base_res, other.cg().checked_unconditional_add(this->cg())); @@ -933,18 +884,15 @@ template class CycleGroupBase { if (other.cg().get_value() == -this->cg().get_value()) { uint8_t dbl_path = VarianceRNG.next() % 3; -#ifdef SHOW_INFORMATION - std::cout << " using " << size_t(dbl_path) << " dbl path" << std::endl; -#endif switch (dbl_path) { case 0: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "left.dbl();" << std::endl; #endif return ExecutionHandler(base_scalar_res, base_res, this->cg().dbl()); case 1: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "-right.dbl();" << std::endl; #endif return ExecutionHandler(base_scalar_res, base_res, -other.cg().dbl()); @@ -954,24 +902,27 @@ template class CycleGroupBase { } else if (other.cg().get_value() == this->cg().get_value()) { uint8_t inf_path = VarianceRNG.next() % 3; cycle_group_t res; -#ifdef SHOW_INFORMATION - std::cout << " using " << size_t(inf_path) << " inf path" << std::endl; -#endif switch (inf_path) { case 0: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "left.set_point_at_infinity("; #endif res = this->cg(); res.set_point_at_infinity(this->construct_predicate(builder, true)); +#ifdef SHOW_INFORMATION + std::cout << ");" << std::endl; +#endif return ExecutionHandler(base_scalar_res, base_res, res); case 1: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "right.set_point_at_infinity("; #endif res = other.cg(); res.set_point_at_infinity(this->construct_predicate(builder, true)); +#ifdef SHOW_INFORMATION + std::cout << ");" << std::endl; +#endif return ExecutionHandler(base_scalar_res, base_res, res); case 2: return ExecutionHandler(base_scalar_res, base_res, this->cg() - other.cg()); @@ -980,18 +931,15 @@ template class CycleGroupBase { bool smth_inf = this->cycle_group.is_point_at_infinity().get_value() || other.cycle_group.is_point_at_infinity().get_value(); uint8_t add_option = smth_inf ? 2 : VarianceRNG.next() % 3; -#ifdef SHOW_INFORMATION - std::cout << " using " << size_t(add_option) << " sub path" << std::endl; -#endif switch (add_option) { case 0: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "left.unconditional_subtract(right);" << std::endl; #endif return ExecutionHandler(base_scalar_res, base_res, this->cg().unconditional_subtract(other.cg())); case 1: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "left.checked_unconditional_subtract(right);" << std::endl; #endif return ExecutionHandler( @@ -1006,9 +954,6 @@ template class CycleGroupBase { { bool is_witness = VarianceRNG.next() & 1; #ifdef SHOW_INFORMATION - std::cout << "Mul is witness? " << is_witness << std::endl; -#endif -#ifdef SHOW_PRETTY_INFORMATION std::cout << " * cycle_scalar_t" << (is_witness ? "::from_witness(&builder, " : "(") << "ScalarField(\"" << multiplier << "\");"; #endif @@ -1033,9 +978,6 @@ template class CycleGroupBase { bool is_witness = VarianceRNG.next() & 1; #ifdef SHOW_INFORMATION - std::cout << " Mul is witness? " << is_witness << std::endl; -#endif -#ifdef SHOW_PRETTY_INFORMATION std::cout << "cycle_scalar_t" << (is_witness ? "::from_witness(&builder, " : "(") << "ScalarField(\"" << to_mul[i] << "\"), "; #endif @@ -1087,18 +1029,15 @@ template class CycleGroupBase { ExecutionHandler set(Builder* builder) { uint32_t switch_case = VarianceRNG.next() % 4; -#ifdef SHOW_INFORMATION - std::cout << " using " << switch_case << " constructor" << std::endl; -#endif switch (switch_case) { case 0: -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "cycle_group_t(" << std::endl; #endif /* construct via cycle_group_t */ return ExecutionHandler(this->base_scalar, this->base, cycle_group_t(this->cycle_group)); case 1: { -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "cycle_group_t::from" << (this->cycle_group.is_constant() ? "" : "_constant") << "_witness(&builder, e.get_value());"; #endif @@ -1111,7 +1050,7 @@ template class CycleGroupBase { return ExecutionHandler(this->base_scalar, this->base, cycle_group_t::from_witness(builder, e)); } case 2: { -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "tmp = el;" << std::endl; std::cout << "res = cycle_group_t(tmp);" << std::endl; #endif @@ -1121,7 +1060,7 @@ template class CycleGroupBase { return ExecutionHandler(this->base_scalar, this->base, cycle_group_t(cg_new)); } case 3: { -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "tmp = el;" << std::endl; std::cout << "res = cycle_group_t(std::move(tmp));" << std::endl; #endif @@ -1137,13 +1076,14 @@ template class CycleGroupBase { ExecutionHandler set_inf(Builder* builder) { auto res = this->set(builder); - const bool set_inf = static_cast(VarianceRNG.next() & 1); -#ifdef SHOW_PRETTY_INFORMATION + const bool set_inf = + res.cycle_group.is_point_at_infinity().get_value() ? true : static_cast(VarianceRNG.next() & 1); +#ifdef SHOW_INFORMATION std::cout << "el.set_point_at_infinty("; #endif res.set_point_at_infinity(this->construct_predicate(builder, set_inf)); -#ifdef SHOW_PRETTY_INFORMATION - std::cout << std::endl; +#ifdef SHOW_INFORMATION + std::cout << ");" << std::endl; #endif return res; } @@ -1166,10 +1106,6 @@ template class CycleGroupBase { instruction.arguments.element.value, cycle_group_t(static_cast(instruction.arguments.element.value)))); #ifdef SHOW_INFORMATION - std::cout << "Pushed constant value " << instruction.arguments.element.value << ", " - << instruction.arguments.element.scalar << " to position " << stack.size() - 1 << std::endl; -#endif -#ifdef SHOW_PRETTY_INFORMATION std::cout << "auto c" << stack.size() - 1 << " = cycle_group_t(ae(\"" << instruction.arguments.element.scalar << "\"));" << std::endl; #endif @@ -1193,10 +1129,6 @@ template class CycleGroupBase { instruction.arguments.element.value, cycle_group_t::from_witness(builder, static_cast(instruction.arguments.element.value)))); #ifdef SHOW_INFORMATION - std::cout << "Pushed witness value " << instruction.arguments.element.value << ", " - << instruction.arguments.element.scalar << " to position " << stack.size() - 1 << std::endl; -#endif -#ifdef SHOW_PRETTY_INFORMATION std::cout << "auto w" << stack.size() - 1 << " = cycle_group_t::from_witness(&builder, ae(\"" << instruction.arguments.element.scalar << "\"));" << std::endl; #endif @@ -1222,10 +1154,6 @@ template class CycleGroupBase { cycle_group_t::from_constant_witness( builder, static_cast(instruction.arguments.element.value)))); #ifdef SHOW_INFORMATION - std::cout << "Pushed constant witness value " << instruction.arguments.element.value << ", " - << instruction.arguments.element.scalar << " to position " << stack.size() - 1 << std::endl; -#endif -#ifdef SHOW_PRETTY_INFORMATION std::cout << "auto cw" << stack.size() - 1 << " = cycle_group_t::from_constant_witness(&builder, ae(\"" << instruction.arguments.element.scalar << "\"));" << std::endl; #endif @@ -1251,8 +1179,7 @@ template class CycleGroupBase { size_t first_index = instruction.arguments.twoArgs.in % stack.size(); size_t output_index = instruction.arguments.twoArgs.out; - PRINT_SINGLE_ARG_INSTRUCTION(first_index, stack, "Doubling", "doubled") -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION PREP_SINGLE_ARG(stack, first_index, output_index) std::cout << out << " = " << rhs << ".dbl();" << std::endl; #endif @@ -1260,10 +1187,8 @@ template class CycleGroupBase { result = stack[first_index].dbl(); // If the output index is larger than the number of elements in stack, append if (output_index >= stack.size()) { - PRINT_RESULT("", "pushed to ", stack.size(), result) stack.push_back(result); } else { - PRINT_RESULT("", "saved to ", output_index, result) stack[output_index] = result; } return 0; @@ -1288,8 +1213,7 @@ template class CycleGroupBase { size_t first_index = instruction.arguments.twoArgs.in % stack.size(); size_t output_index = instruction.arguments.twoArgs.out; - PRINT_SINGLE_ARG_INSTRUCTION(first_index, stack, "Negating", "negated") -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION PREP_SINGLE_ARG(stack, first_index, output_index) std::cout << out << " = -" << rhs << ";" << std::endl; #endif @@ -1297,10 +1221,8 @@ template class CycleGroupBase { result = -stack[first_index]; // If the output index is larger than the number of elements in stack, append if (output_index >= stack.size()) { - PRINT_RESULT("", "pushed to ", stack.size(), result) stack.push_back(result); } else { - PRINT_RESULT("", "saved to ", output_index, result) stack[output_index] = result; } return 0; @@ -1324,11 +1246,7 @@ template class CycleGroupBase { size_t first_index = instruction.arguments.twoArgs.in % stack.size(); size_t second_index = instruction.arguments.twoArgs.out % stack.size(); - PRINT_TWO_ARG_INSTRUCTION(first_index, second_index, stack, "ASSERT_EQUAL", "== something + ") #ifdef SHOW_INFORMATION - std::cout << std::endl; -#endif -#ifdef SHOW_PRETTY_INFORMATION PREP_TWO_ARG(stack, first_index, second_index, 0) std::cout << "assert_equal(" << lhs << ", " << rhs << ", builder);" << std::endl; #endif @@ -1356,21 +1274,18 @@ template class CycleGroupBase { size_t output_index = instruction.arguments.twoArgs.out; ExecutionHandler result; - PRINT_SINGLE_ARG_INSTRUCTION(first_index, stack, "Setting value", "") -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION PREP_SINGLE_ARG(stack, first_index, output_index) std::cout << out << " = "; #endif result = stack[first_index].set(builder); -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << rhs << ");" << std::endl; #endif // If the output index is larger than the number of elements in stack, append if (output_index >= stack.size()) { - PRINT_RESULT("", "pushed to ", stack.size(), result) stack.push_back(result); } else { - PRINT_RESULT("", "saved to ", output_index, result) stack[output_index] = result; } return 0; @@ -1396,18 +1311,15 @@ template class CycleGroupBase { size_t output_index = instruction.arguments.twoArgs.out; ExecutionHandler result; - PRINT_SINGLE_ARG_INSTRUCTION(first_index, stack, "Setting value to inf", "") -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION PREP_SINGLE_ARG(stack, first_index, output_index) std::cout << out << " = " << rhs << std::endl; #endif result = stack[first_index].set_inf(builder); // If the output index is larger than the number of elements in stack, append if (output_index >= stack.size()) { - PRINT_RESULT("", "pushed to ", stack.size(), result) stack.push_back(result); } else { - PRINT_RESULT("", "saved to ", output_index, result) stack[output_index] = result; } return 0; @@ -1433,8 +1345,7 @@ template class CycleGroupBase { size_t second_index = instruction.arguments.threeArgs.in2 % stack.size(); size_t output_index = instruction.arguments.threeArgs.out; - PRINT_TWO_ARG_INSTRUCTION(first_index, second_index, stack, "Adding", "+") -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION PREP_TWO_ARG(stack, first_index, second_index, output_index) std::cout << out << " = " << lhs << " + " << rhs << ";" << std::endl; #endif @@ -1442,10 +1353,8 @@ template class CycleGroupBase { result = stack[first_index].operator_add(builder, stack[second_index]); // If the output index is larger than the number of elements in stack, append if (output_index >= stack.size()) { - PRINT_RESULT("", "pushed to ", stack.size(), result) stack.push_back(result); } else { - PRINT_RESULT("", "saved to ", output_index, result) stack[output_index] = result; } return 0; @@ -1471,8 +1380,7 @@ template class CycleGroupBase { size_t second_index = instruction.arguments.threeArgs.in2 % stack.size(); size_t output_index = instruction.arguments.threeArgs.out; - PRINT_TWO_ARG_INSTRUCTION(first_index, second_index, stack, "Subtracting", "-") -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION PREP_TWO_ARG(stack, first_index, second_index, output_index) std::cout << out << " = " << lhs << " - " << rhs << ";" << std::endl; #endif @@ -1480,10 +1388,8 @@ template class CycleGroupBase { result = stack[first_index].operator_sub(builder, stack[second_index]); // If the output index is larger than the number of elements in stack, append if (output_index >= stack.size()) { - PRINT_RESULT("", "pushed to ", stack.size(), result) stack.push_back(result); } else { - PRINT_RESULT("", "saved to ", output_index, result) stack[output_index] = result; } return 0; @@ -1509,28 +1415,21 @@ template class CycleGroupBase { size_t second_index = instruction.arguments.fourArgs.in2 % stack.size(); size_t output_index = instruction.arguments.fourArgs.out % stack.size(); bool predicate = instruction.arguments.fourArgs.in3 % 2; -#ifdef SHOW_INFORMATION - std::cout << " using predicate: " << predicate << std::endl; -#endif ExecutionHandler result; - PRINT_TWO_ARG_INSTRUCTION( - second_index, first_index, stack, "Selecting #" + std::to_string(!predicate) + " from", ", ") -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION PREP_TWO_ARG(stack, first_index, second_index, output_index) std::cout << out << " = cycle_group_t::conditional_assign("; #endif result = stack[first_index].conditional_assign(builder, stack[second_index], predicate); -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << rhs << ", " << lhs << ");" << std::endl; #endif // If the output index is larger than the number of elements in stack, append if (output_index >= stack.size()) { - PRINT_RESULT("", "pushed to ", stack.size(), result) stack.push_back(result); } else { - PRINT_RESULT("", "saved to ", output_index, result) stack[output_index] = result; } return 0; @@ -1556,8 +1455,7 @@ template class CycleGroupBase { size_t output_index = instruction.arguments.mulArgs.out; ScalarField scalar = instruction.arguments.mulArgs.scalar; - PRINT_MUL_ARG_INSTRUCTION(first_index, scalar, stack, "Multiplying", "*") -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION PREP_SINGLE_ARG(stack, first_index, output_index) std::cout << out << " = " << rhs << std::endl; #endif @@ -1565,10 +1463,8 @@ template class CycleGroupBase { result = stack[first_index].mul(builder, scalar); // If the output index is larger than the number of elements in stack, append if (output_index >= stack.size()) { - PRINT_RESULT("", "pushed to ", stack.size(), result) stack.push_back(result); } else { - PRINT_RESULT("", "saved to ", output_index, result) stack[output_index] = result; } return 0; @@ -1592,27 +1488,12 @@ template class CycleGroupBase { } std::vector to_add; std::vector to_mul = instruction.batchMulArgs.scalars; -#ifdef SHOW_INFORMATION - std::cout << "BATCH_MUL:" << std::endl; - for (size_t i = 0; i < instruction.arguments.batchMulArgs.add_elements_count; i++) { - size_t idx = (size_t)instruction.arguments.batchMulArgs.inputs[i] % stack.size(); - ScalarField scalar = instruction.arguments.batchMulArgs.scalars[i]; - std::cout << (stack[idx].cycle_group.is_constant() ? "Constant( " : "Witness( ") - << stack[idx].cycle_group.get_value() << ") at " << idx << " * "; - std::cout << scalar; - if (i == (instruction.arguments.multOpArgs.mult_pairs_count - 1)) { - std::cout << std::endl; - } else { - std::cout << " + " << std::endl; - } - } -#endif for (size_t i = 0; i < instruction.arguments.batchMulArgs.add_elements_count; i++) { to_add.push_back(stack[(size_t)instruction.arguments.batchMulArgs.inputs[i] % stack.size()]); } size_t output_index = (size_t)instruction.arguments.multOpArgs.output_index; -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::string res = ""; bool is_const = true; for (size_t i = 0; i < instruction.arguments.batchMulArgs.add_elements_count; i++) { @@ -1628,15 +1509,13 @@ template class CycleGroupBase { std::cout << out << " = cycle_group_t::batch_mul({" << res << "}, {"; #endif auto result = ExecutionHandler::batch_mul(builder, to_add, to_mul); -#ifdef SHOW_PRETTY_INFORMATION +#ifdef SHOW_INFORMATION std::cout << "});" << std::endl; #endif // If the output index is larger than the number of elements in stack, append if (output_index >= stack.size()) { - PRINT_RESULT("", "pushed to ", stack.size(), result) stack.push_back(result); } else { - PRINT_RESULT("", "saved to ", output_index, result) stack[output_index] = result; } return 0; diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.hpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.hpp index 02dbef727815..af15a274ca86 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/cycle_group.hpp @@ -196,8 +196,8 @@ template class cycle_group { public: cycle_group(Builder* _context = nullptr); - cycle_group(field_t _x, field_t _y, bool_t _is_infinity, bool is_standard = false); - cycle_group(const FF& _x, const FF& _y, bool _is_infinity, bool is_standard = false); + cycle_group(field_t _x, field_t _y, bool_t _is_infinity); + cycle_group(const FF& _x, const FF& _y, bool _is_infinity); cycle_group(const AffineElement& _in); static cycle_group one(Builder* _context); static cycle_group from_witness(Builder* _context, const AffineElement& _in);