Skip to content

Commit

Permalink
Tests for Array Object in stdlib (#262)
Browse files Browse the repository at this point in the history
* basic array tests.

* no assertion is need since we anyway check the overflow in loop.
  • Loading branch information
suyash67 committed Apr 6, 2023
1 parent 13a2b86 commit 78c4356
Show file tree
Hide file tree
Showing 2 changed files with 304 additions and 20 deletions.
32 changes: 12 additions & 20 deletions cpp/src/barretenberg/stdlib/primitives/field/array.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#pragma once
#include "field.hpp"
#include "../safe_uint/safe_uint.hpp"
#include "../bool/bool.hpp"

namespace plonk {
namespace stdlib {

/**
* Gets the number of contiguous nonzero values of an array.
* Note: this assumes `0` always means 'not used', so be careful. If you actually want `0` to be counted, you'll need
* something else.
* Gets the number of contiguous nonzero values of an array from the start.
* Note: This assumes `0` always means 'not used', so be careful. As soon as we locate 0, we stop the counting.
* If you actually want `0` to be counted, you'll need something else.
*/
// TODO: move to own file of helper functions.
template <typename Composer, size_t SIZE> field_t<Composer> array_length(std::array<field_t<Composer>, SIZE> const& arr)
{
field_t<Composer> length = 0;
Expand All @@ -31,15 +31,15 @@ template <typename Composer, size_t SIZE> field_t<Composer> array_length(std::ar
*/
template <typename Composer, size_t SIZE> field_t<Composer> array_pop(std::array<field_t<Composer>, SIZE> const& arr)
{
field_t<Composer> popped_value;
field_t<Composer> popped_value = 0;
bool_t<Composer> already_popped = false;
for (size_t i = arr.size() - 1; i != (size_t)-1; i--) {
bool_t<Composer> is_non_zero = arr[i] != 0;
popped_value = field_t<Composer>::conditional_assign(!already_popped && is_non_zero, arr[i], popped_value);

already_popped |= is_non_zero;
}
already_popped.assert_equal(true, "Cannot pop from an empty array");
already_popped.assert_equal(true, "array_pop cannot pop from an empty array");

return popped_value;
};
Expand All @@ -58,7 +58,7 @@ void array_push(std::array<field_t<Composer>, SIZE>& arr, field_t<Composer> cons

already_pushed |= is_zero;
}
already_pushed.assert_equal(true, "Cannot push to a full array");
already_pushed.assert_equal(true, "array_push cannot push to a full array");
};

template <typename Composer, size_t SIZE>
Expand All @@ -70,7 +70,7 @@ inline size_t array_push(std::array<std::optional<field_t<Composer>>, SIZE>& arr
return i;
}
}
throw_or_abort("Cannot push to a full array");
throw_or_abort("array_push cannot push to a full array");
};

template <typename T, size_t SIZE>
Expand All @@ -82,7 +82,7 @@ inline size_t array_push(std::array<std::shared_ptr<T>, SIZE>& arr, std::shared_
return i;
}
}
throw_or_abort("Cannot push to a full array");
throw_or_abort("array_push cannot push to a full array");
};

/**
Expand Down Expand Up @@ -111,17 +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());

info("source: ", source);
info("target before: ", target);
// 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 All @@ -141,12 +135,10 @@ void push_array_to_array(std::array<field_t<Composer>, size_1> const& source,

next_target_index++;

next_target_index.assert_not_equal(overflow_capacity, "Target array capacity exceeded");
next_target_index.assert_not_equal(overflow_capacity, "push_array_to_array target array capacity exceeded");

j_ct = i + 1;
}

info("target after: ", target);
}

} // namespace stdlib
Expand Down
Loading

0 comments on commit 78c4356

Please sign in to comment.