Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
dynamic_array.test.cpp
Go to the documentation of this file.
1#include "dynamic_array.hpp"
2
3#include <gtest/gtest.h>
4
6
7#include "../bool/bool.hpp"
8#include "../circuit_builders/circuit_builders.hpp"
11
12using namespace bb;
13
14namespace {
16}
17
18// Defining ultra-specific types for local testing.
24
26
31TEST(DynamicArray, TagCorrectness)
32{
33
35 const size_t max_size = 4;
36
37 DynamicArray_ct array(&builder, max_size);
38
39 // Create random entries
44
45 // Assign a different tag to each entry
46 entry_1.set_origin_tag(submitted_value_origin_tag);
47 entry_2.set_origin_tag(challenge_origin_tag);
48 entry_3.set_origin_tag(next_challenge_tag);
49 // Entry 4 has an "instant death" tag, that triggers an exception when merged with another tag
50 entry_4.set_origin_tag(instant_death_tag);
51
52 // Fill out the dynamic array with the first 3 entries
53 array.push(entry_1);
54 array.push(entry_2);
55 array.push(entry_3);
56
57 // Check that the tags are preserved
58 EXPECT_EQ(array.read(1).get_origin_tag(), challenge_origin_tag);
59 EXPECT_EQ(array.read(2).get_origin_tag(), next_challenge_tag);
60 EXPECT_EQ(array.read(0).get_origin_tag(), submitted_value_origin_tag);
61 // Update an element of the array
62 array.write(0, entry_2);
63 // Check that the tag changed
64 EXPECT_EQ(array.read(0).get_origin_tag(), challenge_origin_tag);
65
66#ifndef NDEBUG
67 // Check that "instant death" happens when an "instant death"-tagged element is taken from the array and added to
68 // another one
69 array.pop();
70 array.pop();
71 array.push(entry_4);
72 array.push(entry_2);
73 array.push(entry_3);
74
75 EXPECT_THROW(array.read(witness_ct(&builder, 1)) + array.read(witness_ct(&builder, 2)), std::runtime_error);
76#endif
77}
78
79TEST(DynamicArray, DynamicArrayReadWriteConsistency)
80{
81
83 const size_t max_size = 10;
84
85 DynamicArray_ct array(&builder, max_size);
86
87 for (size_t i = 0; i < max_size; ++i) {
89 EXPECT_EQ(array.read(i).get_value(), i);
90 }
91
92 EXPECT_EQ(array.native_size(), max_size);
93 for (size_t i = 0; i < max_size; ++i) {
94 array.pop();
95 }
96 EXPECT_EQ(array.native_size(), 0);
97
98 array.resize(max_size - 1, 7);
99
100 EXPECT_EQ(array.native_size(), max_size - 1);
101 for (size_t i = 0; i < max_size - 1; ++i) {
102 EXPECT_EQ(array.read(i).get_value(), 7);
103 }
104
105 array.conditional_push(false, 100);
106 EXPECT_EQ(array.native_size(), max_size - 1);
107
108 array.conditional_push(true, 100);
109 EXPECT_EQ(array.native_size(), max_size);
110 EXPECT_EQ(array.read(max_size - 1).get_value(), 100);
111
112 array.conditional_pop(false);
113 EXPECT_EQ(array.native_size(), max_size);
114
115 array.conditional_pop(true);
116 EXPECT_EQ(array.native_size(), max_size - 1);
117
118 bool verified = CircuitChecker::check(builder);
119 EXPECT_EQ(verified, true);
120}
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
A dynamic array of field elements.
void conditional_pop(const bool_pt &predicate)
Conditionallhy pop a field element off of the dynamic array.
void write(const field_pt &index, const field_pt &value)
Write a field element into the dynamic array at an index value.
void resize(const field_pt &new_length, const field_pt default_value=0)
Resize array. Current method v. inefficient!
void push(const field_pt &index)
Push a field element onto the dynamic array.
field_pt read(const field_pt &index) const
Read a field element from the dynamic array at an index value.
void pop()
Pop a field element off of the dynamic array.
void conditional_push(const bool_pt &predicate, const field_pt &index)
Conditionally push a field element onto the dynamic array.
Implements boolean logic in-circuit.
Definition bool.hpp:59
OriginTag get_origin_tag() const
Definition field.hpp:333
bb::fr get_value() const
Given a := *this, compute its value given by a.v * a.mul + a.add.
Definition field.cpp:827
static field_t from_witness(Builder *ctx, const bb::fr &input)
Definition field.hpp:424
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:332
AluTraceBuilder builder
Definition alu.test.cpp:123
stdlib::witness_t< Builder > witness_ct
numeric::RNG & engine
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
Entry point for Barretenberg command-line interface.
TEST(MegaCircuitBuilder, CopyConstructor)
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
#define STANDARD_TESTING_TAGS
static field random_element(numeric::RNG *engine=nullptr) noexcept