Skip to content

Commit

Permalink
Merge f7f7449 into b6551a9
Browse files Browse the repository at this point in the history
  • Loading branch information
Rumata888 authored Sep 30, 2024
2 parents b6551a9 + f7f7449 commit c4a6ab0
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 6 deletions.
11 changes: 11 additions & 0 deletions barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "bool.hpp"
#include "../circuit_builders/circuit_builders.hpp"
#include "barretenberg/transcript/origin_tag.hpp"

using namespace bb;

Expand Down Expand Up @@ -50,6 +51,7 @@ bool_t<Builder>::bool_t(const bool_t<Builder>& other)
witness_index = other.witness_index;
witness_bool = other.witness_bool;
witness_inverted = other.witness_inverted;
tag = other.tag;
}

template <typename Builder>
Expand All @@ -59,6 +61,7 @@ bool_t<Builder>::bool_t(bool_t<Builder>&& other)
witness_index = other.witness_index;
witness_bool = other.witness_bool;
witness_inverted = other.witness_inverted;
tag = other.tag;
}

template <typename Builder> bool_t<Builder>& bool_t<Builder>::operator=(const bool other)
Expand All @@ -76,6 +79,7 @@ template <typename Builder> bool_t<Builder>& bool_t<Builder>::operator=(const bo
witness_index = other.witness_index;
witness_bool = other.witness_bool;
witness_inverted = other.witness_inverted;
tag = other.tag;
return *this;
}

Expand All @@ -85,6 +89,7 @@ template <typename Builder> bool_t<Builder>& bool_t<Builder>::operator=(bool_t&&
witness_index = other.witness_index;
witness_bool = other.witness_bool;
witness_inverted = other.witness_inverted;
tag = other.tag;
return *this;
}

Expand Down Expand Up @@ -177,6 +182,7 @@ template <typename Builder> bool_t<Builder> bool_t<Builder>::operator&(const boo
result.witness_index = IS_CONSTANT;
result.witness_inverted = false;
}
result.tag = OriginTag(tag, other.tag);
return result;
}

Expand Down Expand Up @@ -257,6 +263,7 @@ template <typename Builder> bool_t<Builder> bool_t<Builder>::operator|(const boo
result.witness_inverted = false;
result.witness_index = IS_CONSTANT;
}
result.tag = OriginTag(tag, other.tag);
return result;
}

Expand Down Expand Up @@ -316,6 +323,7 @@ template <typename Builder> bool_t<Builder> bool_t<Builder>::operator^(const boo
result.witness_inverted = false;
result.witness_index = IS_CONSTANT;
}
result.tag = OriginTag(tag, other.tag);
return result;
}

Expand All @@ -333,6 +341,7 @@ template <typename Builder> bool_t<Builder> bool_t<Builder>::operator==(const bo
bool_t<Builder> result(context == nullptr ? other.context : context);
result.witness_bool = (witness_bool ^ witness_inverted) == (other.witness_bool ^ other.witness_inverted);
result.witness_index = IS_CONSTANT;
result.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
return result;
} else if ((witness_index != IS_CONSTANT) && (other.witness_index == IS_CONSTANT)) {
if (other.witness_bool ^ other.witness_inverted) {
Expand Down Expand Up @@ -376,6 +385,8 @@ template <typename Builder> bool_t<Builder> bool_t<Builder>::operator==(const bo
right_coefficient,
bb::fr::neg_one(),
constant_coefficient });

result.tag = OriginTag(tag, other.tag);
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "../circuit_builders/circuit_builders_fwd.hpp"
#include "../witness/witness.hpp"
#include "barretenberg/transcript/origin_tag.hpp"

namespace bb::stdlib {

Expand Down Expand Up @@ -64,10 +65,13 @@ template <typename Builder> class bool_t {

Builder* get_context() const { return context; }

void set_origin_tag(const OriginTag& new_tag) const { tag = new_tag; }
OriginTag get_origin_tag() const { return tag; }
mutable Builder* context = nullptr;
mutable bool witness_bool = false;
mutable bool witness_inverted = false;
mutable uint32_t witness_index = IS_CONSTANT;
mutable OriginTag tag{};
};

template <typename T> inline std::ostream& operator<<(std::ostream& os, bool_t<T> const& v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "barretenberg/circuit_checker/circuit_checker.hpp"
#include "barretenberg/stdlib/primitives/byte_array/byte_array.cpp"
#include "barretenberg/stdlib/primitives/circuit_builders/circuit_builders.hpp"
#include "barretenberg/transcript/origin_tag.hpp"
#include <gtest/gtest.h>
#include <tuple>

#define STDLIB_TYPE_ALIASES \
using Builder = TypeParam; \
Expand All @@ -19,6 +21,16 @@ template <class Builder> class BoolTest : public ::testing::Test {};

using CircuitTypes = ::testing::Types<bb::CircuitSimulatorBN254, bb::StandardCircuitBuilder, bb::UltraCircuitBuilder>;

// Tags reused in tests
const size_t parent_id = 0;
const auto submitted_value_origin_tag = OriginTag(parent_id, /*round_id=*/0, /*is_submitted=*/true);
const auto challenge_origin_tag = OriginTag(parent_id, /*round_id=*/0, /*is_submitted=*/false);
const auto next_challenge_tag = OriginTag(parent_id, /*round_id=*/1, /*is_submitted=*/false);

const auto first_two_merged_tag = OriginTag(submitted_value_origin_tag, challenge_origin_tag);
const auto first_and_third_merged_tag = OriginTag(submitted_value_origin_tag, next_challenge_tag);
const auto all_merged_tag = OriginTag(first_two_merged_tag, next_challenge_tag);

TYPED_TEST_SUITE(BoolTest, CircuitTypes);
TYPED_TEST(BoolTest, TestBasicOperations)
{
Expand All @@ -30,21 +42,50 @@ TYPED_TEST(BoolTest, TestBasicOperations)

bool_ct a = witness_ct(&builder, bb::fr::one());
bool_ct b = witness_ct(&builder, bb::fr::zero());

a.set_origin_tag(submitted_value_origin_tag);
b.set_origin_tag(challenge_origin_tag);

a = a ^ b; // a = 1
EXPECT_EQ(a.get_value(), 1);

// Tags are merged on XOR
EXPECT_EQ(a.get_origin_tag(), first_two_merged_tag);

b = !b; // b = 1 (witness 0)
EXPECT_EQ(b.get_value(), 1);

// Tag is preserved on NOT
EXPECT_EQ(b.get_origin_tag(), challenge_origin_tag);

a.set_origin_tag(submitted_value_origin_tag);

bool_ct d = (a == b); //
EXPECT_EQ(d.get_value(), 1);

// Tags are merged on ==
EXPECT_EQ(d.get_origin_tag(), first_two_merged_tag);

d = false; // d = 0
d.set_origin_tag(challenge_origin_tag);
EXPECT_EQ(d.get_value(), 0);

bool_ct e = a | d; // e = 1 = a
EXPECT_EQ(e.get_value(), 1);

// Tags are merged on OR
EXPECT_EQ(e.get_origin_tag(), first_two_merged_tag);

bool_ct f = e ^ b; // f = 0
EXPECT_EQ(f.get_value(), 0);

f.set_origin_tag(challenge_origin_tag);
d = (!f) & a; // d = 1
EXPECT_EQ(d.get_value(), 1);

// Tags are merged on AND
EXPECT_EQ(d.get_origin_tag(), first_two_merged_tag);

bool result = CircuitChecker::check(builder);
EXPECT_EQ(result, true);

Expand Down Expand Up @@ -137,7 +178,14 @@ TYPED_TEST(BoolTest, LogicalAnd)

bool_ct a = witness_ct(&builder, 1);
bool_ct b = witness_ct(&builder, 1);
(!a) && (!b);

a.set_origin_tag(submitted_value_origin_tag);
b.set_origin_tag(challenge_origin_tag);

auto c = (!a) && (!b);

// Tags are merged on logical AND
EXPECT_EQ(c.get_origin_tag(), first_two_merged_tag);

bool result = CircuitChecker::check(builder);
EXPECT_EQ(result, true);
Expand Down Expand Up @@ -289,8 +337,16 @@ TYPED_TEST(BoolTest, Implies)
bool b_val = (bool)(i > 1 ? true : false);
bool_ct a = lhs_constant ? bool_ct(a_val) : (witness_ct(&builder, a_val));
bool_ct b = rhs_constant ? bool_ct(b_val) : (witness_ct(&builder, b_val));
if (!(lhs_constant || rhs_constant)) {
a.set_origin_tag(submitted_value_origin_tag);
b.set_origin_tag(challenge_origin_tag);
}
bool_ct c = a.implies(b);
EXPECT_EQ(c.get_value(), !a.get_value() || b.get_value());
if (!(lhs_constant || rhs_constant)) {
// Tags are merged on implies
EXPECT_EQ(c.get_origin_tag(), first_two_merged_tag);
}
}
}

Expand All @@ -312,8 +368,17 @@ TYPED_TEST(BoolTest, ImpliesBothWays)
bool b_val = (bool)(i > 1 ? true : false);
bool_ct a = lhs_constant ? bool_ct(a_val) : (witness_ct(&builder, a_val));
bool_ct b = rhs_constant ? bool_ct(b_val) : (witness_ct(&builder, b_val));
if (!(lhs_constant || rhs_constant)) {
a.set_origin_tag(submitted_value_origin_tag);
b.set_origin_tag(challenge_origin_tag);
}
bool_ct c = a.implies_both_ways(b);
EXPECT_EQ(c.get_value(), !(a.get_value() ^ b.get_value()));

if (!(lhs_constant || rhs_constant)) {
// Tags are merged on implies both ways
EXPECT_EQ(c.get_origin_tag(), first_two_merged_tag);
}
}
}

Expand Down Expand Up @@ -465,8 +530,18 @@ TYPED_TEST(BoolTest, ConditionalAssign)
bool_ct r_ct = rhs_constant ? bool_ct(right) : (witness_ct(&builder, right));
bool_ct cond = (witness_ct(&builder, condition));

if (!(lhs_constant | rhs_constant)) {
cond.set_origin_tag(submitted_value_origin_tag);
l_ct.set_origin_tag(challenge_origin_tag);
r_ct.set_origin_tag(next_challenge_tag);
}
auto result = bool_ct::conditional_assign(cond, l_ct, r_ct);

if (!(lhs_constant | rhs_constant)) {
// Tags are merged on conditional assign
EXPECT_EQ(result.get_origin_tag(), all_merged_tag);
}

EXPECT_EQ(result.get_value(), condition ? left : right);
}
info("num gates = ", builder.get_num_gates());
Expand Down Expand Up @@ -514,8 +589,14 @@ TYPED_TEST(BoolTest, Normalize)
auto generate_constraints = [&builder](bool value, bool is_constant, bool is_inverted) {
bool_ct a = is_constant ? bool_ct(&builder, value) : witness_ct(&builder, value);
bool_ct b = is_inverted ? !a : a;
if (!is_constant) {
b.set_origin_tag(submitted_value_origin_tag);
}
bool_ct c = b.normalize();
EXPECT_EQ(c.get_value(), value ^ is_inverted);
if (!is_constant) {
EXPECT_EQ(c.get_origin_tag(), submitted_value_origin_tag);
}
};

generate_constraints(false, false, false);
Expand Down
Loading

0 comments on commit c4a6ab0

Please sign in to comment.