Skip to content

Commit

Permalink
getting a bunch of composers out
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Sep 19, 2023
1 parent 974d859 commit 5b26236
Show file tree
Hide file tree
Showing 111 changed files with 4,260 additions and 4,329 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace stdlib {

constexpr uint64_t WNAF_MASK = crypto::generators::WNAF_MASK;

template <typename ComposerContext> class pedersen_commitment {
template <typename CircuitBuilder> class pedersen_commitment {
private:
typedef stdlib::field_t<ComposerContext> field_t;
typedef stdlib::point<ComposerContext> point;
typedef stdlib::byte_array<ComposerContext> byte_array;
typedef stdlib::bool_t<ComposerContext> bool_t;
typedef stdlib::field_t<CircuitBuilder> field_t;
typedef stdlib::point<CircuitBuilder> point;
typedef stdlib::byte_array<CircuitBuilder> byte_array;
typedef stdlib::bool_t<CircuitBuilder> bool_t;

public:
static point commit(const std::vector<field_t>& inputs, const size_t hash_index = 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ namespace {
auto& engine = numeric::random::get_debug_engine();
}

template <typename Composer> class stdlib_pedersen : public testing::Test {
typedef stdlib::bn254<Composer> curve;
template <typename Builder> class stdlib_pedersen : public testing::Test {
typedef stdlib::bn254<Builder> curve;

typedef typename curve::byte_array_ct byte_array_ct;
typedef typename curve::ScalarField fr_ct;
typedef typename curve::witness_ct witness_ct;
typedef typename curve::public_witness_ct public_witness_ct;
typedef typename stdlib::pedersen_commitment<Composer> pedersen_commitment;
typedef typename stdlib::pedersen_commitment<Builder> pedersen_commitment;

public:
static grumpkin::g1::element pedersen_recover(const fr& left_in, const fr& right_in)
Expand Down Expand Up @@ -135,7 +135,7 @@ template <typename Composer> class stdlib_pedersen : public testing::Test {
static void test_pedersen()
{

Composer composer;
Builder builder;

fr left_in = fr::random_element();
fr right_in = fr::random_element();
Expand All @@ -148,17 +148,17 @@ template <typename Composer> class stdlib_pedersen : public testing::Test {
right_in += fr::one();
}

fr_ct left = public_witness_ct(&composer, left_in);
fr_ct right = witness_ct(&composer, right_in);
fr_ct left = public_witness_ct(&builder, left_in);
fr_ct right = witness_ct(&builder, right_in);

composer.fix_witness(left.witness_index, left.get_value());
composer.fix_witness(right.witness_index, right.get_value());
builder.fix_witness(left.witness_index, left.get_value());
builder.fix_witness(right.witness_index, right.get_value());

fr_ct out = pedersen_commitment::compress(left, right);

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool result = composer.check_circuit();
bool result = builder.check_circuit();
EXPECT_EQ(result, true);

auto hash_output = pedersen_recover(left_in, right_in);
Expand All @@ -176,29 +176,29 @@ template <typename Composer> class stdlib_pedersen : public testing::Test {

static void test_pedersen_edge_cases()
{
Composer composer;
Builder builder;

fr zero_fr = fr::zero();
fr one_fr = fr::one();
fr r_minus_one_fr = fr::modulus - 1;
fr r_minus_two_fr = fr::modulus - 2;
fr r_fr = fr::modulus;

fr_ct zero = witness_ct(&composer, zero_fr);
fr_ct one = witness_ct(&composer, one_fr);
fr_ct r_minus_one = witness_ct(&composer, r_minus_one_fr);
fr_ct r_minus_two = witness_ct(&composer, r_minus_two_fr);
fr_ct r = witness_ct(&composer, r_fr);
fr_ct zero = witness_ct(&builder, zero_fr);
fr_ct one = witness_ct(&builder, one_fr);
fr_ct r_minus_one = witness_ct(&builder, r_minus_one_fr);
fr_ct r_minus_two = witness_ct(&builder, r_minus_two_fr);
fr_ct r = witness_ct(&builder, r_fr);

fr_ct out_1_with_zero = pedersen_commitment::compress(zero, one);
fr_ct out_1_with_r = pedersen_commitment::compress(r, one);
fr_ct out_2 = pedersen_commitment::compress(r_minus_one, r_minus_two);
fr_ct out_with_zero = pedersen_commitment::compress(out_1_with_zero, out_2);
fr_ct out_with_r = pedersen_commitment::compress(out_1_with_r, out_2);

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool result = composer.check_circuit();
bool result = builder.check_circuit();
EXPECT_EQ(result, true);

auto hash_output_1_with_zero = pedersen_recover(zero_fr, one_fr);
Expand Down Expand Up @@ -242,7 +242,7 @@ template <typename Composer> class stdlib_pedersen : public testing::Test {

static void test_pedersen_large()
{
Composer composer;
Builder builder;

fr left_in = fr::random_element();
fr right_in = fr::random_element();
Expand All @@ -253,26 +253,26 @@ template <typename Composer> class stdlib_pedersen : public testing::Test {
if ((right_in.from_montgomery_form().data[0] & 1) == 0) {
right_in += fr::one();
}
fr_ct left = witness_ct(&composer, left_in);
fr_ct right = witness_ct(&composer, right_in);
fr_ct left = witness_ct(&builder, left_in);
fr_ct right = witness_ct(&builder, right_in);

for (size_t i = 0; i < 256; ++i) {
left = pedersen_commitment::compress(left, right);
}

composer.set_public_input(left.witness_index);
builder.set_public_input(left.witness_index);

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool result = composer.check_circuit();
bool result = builder.check_circuit();
EXPECT_EQ(result, true);
}

static void test_compress_byte_array()
{
const size_t num_input_bytes = 351;

Composer composer;
Builder builder;

std::vector<uint8_t> input;
input.reserve(num_input_bytes);
Expand All @@ -282,20 +282,20 @@ template <typename Composer> class stdlib_pedersen : public testing::Test {

fr expected = crypto::pedersen_commitment::compress_native(input);

byte_array_ct circuit_input(&composer, input);
byte_array_ct circuit_input(&builder, input);
auto result = pedersen_commitment::compress(circuit_input);

EXPECT_EQ(result.get_value(), expected);

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool proof_result = composer.check_circuit();
bool proof_result = builder.check_circuit();
EXPECT_EQ(proof_result, true);
}

static void test_multi_compress()
{
Composer composer;
Builder builder;

for (size_t i = 0; i < 7; ++i) {
std::vector<barretenberg::fr> inputs;
Expand Down Expand Up @@ -329,7 +329,7 @@ template <typename Composer> class stdlib_pedersen : public testing::Test {
}
std::vector<fr_ct> witnesses;
for (auto input : inputs) {
witnesses.push_back(witness_ct(&composer, input));
witnesses.push_back(witness_ct(&builder, input));
}

barretenberg::fr expected = crypto::pedersen_commitment::compress_native(inputs);
Expand All @@ -338,23 +338,23 @@ template <typename Composer> class stdlib_pedersen : public testing::Test {
EXPECT_EQ(result.get_value(), expected);
}

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool proof_result = composer.check_circuit();
bool proof_result = builder.check_circuit();
EXPECT_EQ(proof_result, true);
}

static void test_compress_eight()
{
Composer composer;
Builder builder;

std::vector<grumpkin::fq> inputs;
inputs.reserve(8);
std::vector<stdlib::field_t<Composer>> witness_inputs;
std::vector<stdlib::field_t<Builder>> witness_inputs;

for (size_t i = 0; i < 8; ++i) {
inputs.emplace_back(barretenberg::fr::random_element());
witness_inputs.emplace_back(witness_ct(&composer, inputs[i]));
witness_inputs.emplace_back(witness_ct(&builder, inputs[i]));
}

constexpr size_t hash_idx = 10;
Expand All @@ -366,17 +366,17 @@ template <typename Composer> class stdlib_pedersen : public testing::Test {

static void test_compress_constants()
{
Composer composer;
Builder builder;

std::vector<barretenberg::fr> inputs;
std::vector<stdlib::field_t<Composer>> witness_inputs;
std::vector<stdlib::field_t<Builder>> witness_inputs;

for (size_t i = 0; i < 8; ++i) {
inputs.push_back(barretenberg::fr::random_element());
if (i % 2 == 1) {
witness_inputs.push_back(witness_ct(&composer, inputs[i]));
witness_inputs.push_back(witness_ct(&builder, inputs[i]));
} else {
witness_inputs.push_back(fr_ct(&composer, inputs[i]));
witness_inputs.push_back(fr_ct(&builder, inputs[i]));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace proof_system::plonk {
namespace stdlib {

template <typename ComposerContext> class pedersen_plookup_commitment {
template <typename CircuitBuilder> class pedersen_plookup_commitment {
private:
typedef stdlib::field_t<ComposerContext> field_t;
typedef stdlib::point<ComposerContext> point;
typedef stdlib::packed_byte_array<ComposerContext> packed_byte_array;
typedef stdlib::bool_t<ComposerContext> bool_t;
typedef stdlib::field_t<CircuitBuilder> field_t;
typedef stdlib::point<CircuitBuilder> point;
typedef stdlib::packed_byte_array<CircuitBuilder> packed_byte_array;
typedef stdlib::bool_t<CircuitBuilder> bool_t;

public:
static point commit(const std::vector<field_t>& inputs, const size_t hash_index = 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@ typedef stdlib::field_t<proof_system::UltraCircuitBuilder> field_ct;
typedef stdlib::witness_t<proof_system::UltraCircuitBuilder> witness_ct;
TEST(stdlib_pedersen, test_pedersen_plookup)
{
proof_system::UltraCircuitBuilder composer = proof_system::UltraCircuitBuilder();
proof_system::UltraCircuitBuilder builder = proof_system::UltraCircuitBuilder();

fr left_in = fr::random_element();
fr right_in = fr::random_element();

field_ct left = witness_ct(&composer, left_in);
field_ct right = witness_ct(&composer, right_in);
field_ct left = witness_ct(&builder, left_in);
field_ct right = witness_ct(&builder, right_in);

field_ct result = stdlib::pedersen_plookup_commitment<proof_system::UltraCircuitBuilder>::compress(left, right);

fr expected = crypto::pedersen_hash::lookup::hash_pair(left_in, right_in);

EXPECT_EQ(result.get_value(), expected);

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool proof_result = composer.check_circuit();
bool proof_result = builder.check_circuit();
EXPECT_EQ(proof_result, true);
}

TEST(stdlib_pedersen, test_compress_many_plookup)
{
proof_system::UltraCircuitBuilder composer = proof_system::UltraCircuitBuilder();
proof_system::UltraCircuitBuilder builder = proof_system::UltraCircuitBuilder();

std::vector<fr> input_values{
fr::random_element(), fr::random_element(), fr::random_element(),
fr::random_element(), fr::random_element(), fr::random_element(),
};
std::vector<field_ct> inputs;
for (const auto& input : input_values) {
inputs.emplace_back(witness_ct(&composer, input));
inputs.emplace_back(witness_ct(&builder, input));
}

const size_t hash_idx = 20;
Expand All @@ -61,25 +61,25 @@ TEST(stdlib_pedersen, test_compress_many_plookup)

EXPECT_EQ(result.get_value(), expected);

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool proof_result = composer.check_circuit();
bool proof_result = builder.check_circuit();
EXPECT_EQ(proof_result, true);
}

TEST(stdlib_pedersen, test_merkle_damgard_compress_plookup)
{
proof_system::UltraCircuitBuilder composer = proof_system::UltraCircuitBuilder();
proof_system::UltraCircuitBuilder builder = proof_system::UltraCircuitBuilder();

std::vector<fr> input_values{
fr::random_element(), fr::random_element(), fr::random_element(),
fr::random_element(), fr::random_element(), fr::random_element(),
};
std::vector<field_ct> inputs;
for (const auto& input : input_values) {
inputs.emplace_back(witness_ct(&composer, input));
inputs.emplace_back(witness_ct(&builder, input));
}
field_ct iv = witness_ct(&composer, fr(10));
field_ct iv = witness_ct(&builder, fr(10));

field_ct result =
stdlib::pedersen_plookup_commitment<proof_system::UltraCircuitBuilder>::merkle_damgard_compress(inputs, iv).x;
Expand All @@ -88,15 +88,15 @@ TEST(stdlib_pedersen, test_merkle_damgard_compress_plookup)

EXPECT_EQ(result.get_value(), expected.normalize().x);

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool proof_result = composer.check_circuit();
bool proof_result = builder.check_circuit();
EXPECT_EQ(proof_result, true);
}

TEST(stdlib_pedersen, test_merkle_damgard_compress_multiple_iv_plookup)
{
proof_system::UltraCircuitBuilder composer = proof_system::UltraCircuitBuilder();
proof_system::UltraCircuitBuilder builder = proof_system::UltraCircuitBuilder();

const size_t m = 10;
std::vector<fr> input_values;
Expand All @@ -109,8 +109,8 @@ TEST(stdlib_pedersen, test_merkle_damgard_compress_multiple_iv_plookup)
std::vector<field_ct> inputs;
std::vector<field_ct> ivs;
for (size_t i = 0; i < m; i++) {
inputs.emplace_back(witness_ct(&composer, input_values[i]));
ivs.emplace_back(witness_ct(&composer, fr(iv_values[i])));
inputs.emplace_back(witness_ct(&builder, input_values[i]));
ivs.emplace_back(witness_ct(&builder, fr(iv_values[i])));
}

field_ct result =
Expand All @@ -120,15 +120,15 @@ TEST(stdlib_pedersen, test_merkle_damgard_compress_multiple_iv_plookup)

EXPECT_EQ(result.get_value(), expected.normalize().x);

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool proof_result = composer.check_circuit();
bool proof_result = builder.check_circuit();
EXPECT_EQ(proof_result, true);
}

TEST(stdlib_pedersen, test_merkle_damgard_tree_compress_plookup)
{
proof_system::UltraCircuitBuilder composer = proof_system::UltraCircuitBuilder();
proof_system::UltraCircuitBuilder builder = proof_system::UltraCircuitBuilder();

const size_t m = 16;
std::vector<fr> input_values;
Expand All @@ -141,8 +141,8 @@ TEST(stdlib_pedersen, test_merkle_damgard_tree_compress_plookup)
std::vector<field_ct> inputs;
std::vector<field_ct> ivs;
for (size_t i = 0; i < m; i++) {
inputs.emplace_back(witness_ct(&composer, input_values[i]));
ivs.emplace_back(witness_ct(&composer, fr(iv_values[i])));
inputs.emplace_back(witness_ct(&builder, input_values[i]));
ivs.emplace_back(witness_ct(&builder, fr(iv_values[i])));
}

field_ct result =
Expand All @@ -154,9 +154,9 @@ TEST(stdlib_pedersen, test_merkle_damgard_tree_compress_plookup)

EXPECT_EQ(result.get_value(), expected.normalize().x);

info("composer gates = ", composer.get_num_gates());
info("num gates = ", builder.get_num_gates());

bool proof_result = composer.check_circuit();
bool proof_result = builder.check_circuit();
EXPECT_EQ(proof_result, true);
}

Expand Down
Loading

0 comments on commit 5b26236

Please sign in to comment.