Skip to content

Commit

Permalink
no assertion is need since we anyway check the overflow in loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
suyash67 committed Mar 23, 2023
1 parent 9033ddd commit 4b6d6b7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
8 changes: 3 additions & 5 deletions cpp/src/barretenberg/stdlib/primitives/field/array.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "field.hpp"
#include "../safe_uint/safe_uint.hpp"
#include "../bool/bool.hpp"

namespace plonk {
Expand Down Expand Up @@ -110,14 +111,11 @@ void push_array_to_array(std::array<field_t<Composer>, size_1> const& source,
// TODO: inefficient to get length this way within this function. Probably best to inline the checks that we need
// into the below loops directly.
field_t<Composer> target_length = array_length<Composer>(target);
field_t<Composer> source_length = array_length<Composer>(source);
field_t<Composer> target_capacity = field_t<Composer>(target.size());
const field_t<Composer> overflow_capacity = target_capacity + 1;

// TODO: using safe_uint for an underflow check, do:
// remaining_target_capacity = target_capacity.subtract(target_length + source_length);

// ASSERT(target_capacity.get_value() + 1 > target_length.get_value() + source_length.get_value());
// ASSERT(uint256_t(target_capacity.get_value()) + 1 >
// uint256_t(target_length.get_value()) + uint256_t(source_length.get_value()));

field_t<Composer> j_ct = 0; // circuit-type index for the inner loop
field_t<Composer> next_target_index = target_length;
Expand Down
42 changes: 42 additions & 0 deletions cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,44 @@ template <typename Composer> class stdlib_field : public testing::Test {
ASSERT(target[5].get_value() == 2);
ASSERT(target[6].get_value() == 0);
ASSERT(target[7].get_value() == 0);

auto prover = composer.create_prover();
auto verifier = composer.create_verifier();
auto proof = prover.construct_proof();
info("composer gates = ", composer.get_num_gates());
bool proof_result = verifier.verify_proof(proof);
EXPECT_EQ(proof_result, true);
}

static void test_push_array_to_array_full()
{
Composer composer = Composer();

std::array<fr, 4> source = { 1, 2 };
std::array<fr, 8> target = { 3, 4, 6, 8, 7, 9, 5, 0 };
std::array<field_ct, 4> source_ct;
std::array<field_ct, 8> target_ct;
for (size_t i = 0; i < source.size(); i++) {
source_ct[i] = witness_ct(&composer, source[i]);
}
for (size_t i = 0; i < target.size(); i++) {
target_ct[i] = witness_ct(&composer, target[i]);
}

push_array_to_array<Composer>(source_ct, target_ct);

// Check that the target array is unchanged as the push operation failed
ASSERT(target_ct[0].get_value() == 3);
ASSERT(target_ct[1].get_value() == 4);
ASSERT(target_ct[2].get_value() == 6);
ASSERT(target_ct[3].get_value() == 8);
ASSERT(target_ct[4].get_value() == 7);
ASSERT(target_ct[5].get_value() == 9);
ASSERT(target_ct[6].get_value() == 5);
ASSERT(target_ct[7].get_value() == 0);

EXPECT_EQ(composer.failed(), true);
EXPECT_EQ(composer.err(), "push_array_to_array target array capacity exceeded");
}
};

Expand Down Expand Up @@ -1348,4 +1386,8 @@ TYPED_TEST(stdlib_field, test_array_push_array_to_array)
{
TestFixture::test_push_array_to_array();
}
TYPED_TEST(stdlib_field, test_array_push_array_to_array_full)
{
TestFixture::test_push_array_to_array_full();
}
} // namespace test_stdlib_field

0 comments on commit 4b6d6b7

Please sign in to comment.