Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for Array Object in stdlib #262

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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