Skip to content

Commit

Permalink
chore(avm): Migrate memory data structure in AVM circuit to unordered…
Browse files Browse the repository at this point in the history
… map (#5506)
  • Loading branch information
jeanmon committed Mar 28, 2024
1 parent a066544 commit ccd09aa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
33 changes: 19 additions & 14 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_mem_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ AvmMemTraceBuilder::AvmMemTraceBuilder()
void AvmMemTraceBuilder::reset()
{
mem_trace.clear();
memory.fill(FF(0));
memory.clear();
}

/**
Expand Down Expand Up @@ -133,7 +133,8 @@ void AvmMemTraceBuilder::load_mismatch_tag_in_mem_trace(uint32_t const m_clk,
bool AvmMemTraceBuilder::load_from_mem_trace(
uint32_t clk, uint32_t sub_clk, uint32_t addr, FF const& val, AvmMemoryTag r_in_tag, AvmMemoryTag w_in_tag)
{
auto m_tag = memory_tag.at(addr);
AvmMemoryTag m_tag = memory.contains(addr) ? memory.at(addr).tag : AvmMemoryTag::U0;

if (m_tag == AvmMemoryTag::U0 || m_tag == r_in_tag) {
insert_in_mem_trace(clk, sub_clk, addr, val, r_in_tag, r_in_tag, w_in_tag, false);
return true;
Expand Down Expand Up @@ -187,23 +188,22 @@ void AvmMemTraceBuilder::store_in_mem_trace(
* @return Result of the read operation containing the value and the tag of the memory cell
* at the supplied address.
*/
std::pair<FF, AvmMemoryTag> AvmMemTraceBuilder::read_and_load_mov_opcode(uint32_t const clk, uint32_t const addr)
AvmMemTraceBuilder::MemEntry AvmMemTraceBuilder::read_and_load_mov_opcode(uint32_t const clk, uint32_t const addr)
{
FF const& val = memory.at(addr);
AvmMemoryTag m_tag = memory_tag.at(addr);
MemEntry memEntry = memory.contains(addr) ? memory.at(addr) : MemEntry{};

mem_trace.emplace_back(MemoryTraceEntry{
.m_clk = clk,
.m_sub_clk = SUB_CLK_LOAD_A,
.m_addr = addr,
.m_val = val,
.m_tag = m_tag,
.r_in_tag = m_tag,
.w_in_tag = m_tag,
.m_val = memEntry.val,
.m_tag = memEntry.tag,
.r_in_tag = memEntry.tag,
.w_in_tag = memEntry.tag,
.m_sel_mov = true,
});

return std::make_pair(val, m_tag);
return memEntry;
}

/**
Expand Down Expand Up @@ -239,7 +239,7 @@ AvmMemTraceBuilder::MemRead AvmMemTraceBuilder::read_and_load_from_memory(uint32
break;
}

FF val = memory.at(addr);
FF val = memory.contains(addr) ? memory.at(addr).val : 0;
bool tagMatch = load_from_mem_trace(clk, sub_clk, addr, val, r_in_tag, w_in_tag);

return MemRead{
Expand All @@ -265,7 +265,7 @@ AvmMemTraceBuilder::MemRead AvmMemTraceBuilder::indirect_read_and_load_from_memo
break;
}

FF val = memory.at(addr);
FF val = memory.contains(addr) ? memory.at(addr).val : 0;
bool tagMatch = load_from_mem_trace(clk, sub_clk, addr, val, AvmMemoryTag::U32, AvmMemoryTag::U0);

return MemRead{
Expand Down Expand Up @@ -293,8 +293,13 @@ void AvmMemTraceBuilder::write_into_memory(uint32_t const clk,
AvmMemoryTag r_in_tag,
AvmMemoryTag w_in_tag)
{
memory.at(addr) = val;
memory_tag.at(addr) = w_in_tag;
MemEntry memEntry{ val, w_in_tag };
auto it = memory.find(addr);
if (it != memory.end()) {
it->second = memEntry;
} else {
memory.emplace(addr, memEntry);
}
store_in_mem_trace(clk, interm_reg, addr, val, r_in_tag, w_in_tag);
}

Expand Down
14 changes: 9 additions & 5 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_mem_trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class AvmMemTraceBuilder {
}
};

// Structure representing an entry for the memory used in the simulation (not the trace).
struct MemEntry {
FF val{};
AvmMemoryTag tag = AvmMemoryTag::U0;
};

// Structure to return value and tag matching boolean after a memory read.
struct MemRead {
bool tag_match = false;
Expand All @@ -77,7 +83,7 @@ class AvmMemTraceBuilder {

std::vector<MemoryTraceEntry> finalize();

std::pair<FF, AvmMemoryTag> read_and_load_mov_opcode(uint32_t clk, uint32_t addr);
MemEntry read_and_load_mov_opcode(uint32_t clk, uint32_t addr);
MemRead read_and_load_from_memory(
uint32_t clk, IntermRegister interm_reg, uint32_t addr, AvmMemoryTag r_in_tag, AvmMemoryTag w_in_tag);
MemRead indirect_read_and_load_from_memory(uint32_t clk, IndirectRegister ind_reg, uint32_t addr);
Expand All @@ -89,10 +95,8 @@ class AvmMemTraceBuilder {
AvmMemoryTag w_in_tag);

private:
std::vector<MemoryTraceEntry> mem_trace; // Entries will be sorted by m_clk, m_sub_clk after finalize().
std::array<FF, MEM_SIZE> memory{}; // Memory table (used for simulation)
std::array<AvmMemoryTag, MEM_SIZE> memory_tag{}; // The tag of the corresponding memory
// entry (aligned with the memory array).
std::vector<MemoryTraceEntry> mem_trace; // Entries will be sorted by m_clk, m_sub_clk after finalize().
std::unordered_map<uint32_t, MemEntry> memory; // Memory table (used for simulation)

void insert_in_mem_trace(uint32_t m_clk,
uint32_t m_sub_clk,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ TEST_F(AvmArithmeticTestsFF, subtraction)
EXPECT_EQ(alu_row.avm_alu_cf, FF(0));
EXPECT_EQ(alu_row.avm_alu_u8_r0, FF(0));

avm_trace::log_avm_trace(trace, 0, 10);
validate_trace_proof(std::move(trace));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,29 @@ TEST_F(AvmMemOpcodeTests, sameAddressMov)
validate_trace(false, 11, 356, 356, AvmMemoryTag::U16);
}

TEST_F(AvmMemOpcodeTests, uninitializedValueMov)
{
auto trace_builder = AvmTraceBuilder();
trace_builder.set(4, 1, AvmMemoryTag::U32);
trace_builder.op_mov(0, 0, 1);
trace_builder.return_op(0, 0, 0);
trace = trace_builder.finalize();

validate_trace(false, 0, 0, 1, AvmMemoryTag::U0);
}

TEST_F(AvmMemOpcodeTests, indUninitializedValueMov)
{
auto trace_builder = AvmTraceBuilder();
trace_builder.set(1, 3, AvmMemoryTag::U32);
trace_builder.set(4, 1, AvmMemoryTag::U32);
trace_builder.op_mov(3, 2, 3);
trace_builder.return_op(0, 0, 0);
trace = trace_builder.finalize();

validate_trace(true, 0, 2, 3, AvmMemoryTag::U0, 0, 1);
}

TEST_F(AvmMemOpcodeTests, indirectMov)
{
buildTrace(true, 23, 0, 1, AvmMemoryTag::U8, 2, 3);
Expand Down

0 comments on commit ccd09aa

Please sign in to comment.