Skip to content

Commit

Permalink
4598 - refactor: move op_not and op_eq routines
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmon committed Feb 15, 2024
1 parent 28b1f78 commit cb08f8c
Showing 1 changed file with 92 additions and 91 deletions.
183 changes: 92 additions & 91 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,98 @@ void AvmTraceBuilder::op_div(uint32_t a_offset, uint32_t b_offset, uint32_t dst_
});
}

/**
* @brief Bitwise not with direct memory access.
*
* @param a_offset An index in memory pointing to the only operand of Not.
* @param dst_offset An index in memory pointing to the output of Not.
* @param in_tag The instruction memory tag of the operands.
*/
void AvmTraceBuilder::op_not(uint32_t a_offset, uint32_t dst_offset, AvmMemoryTag in_tag)
{
auto clk = static_cast<uint32_t>(main_trace.size());

// Reading from memory and loading into ia.
auto read_a = mem_trace_builder.read_and_load_from_memory(clk, IntermRegister::IA, a_offset, in_tag);

// ~a = c
FF a = read_a.tag_match ? read_a.val : FF(0);
// TODO(4613): If tag_match == false, then the value of c
// will not be zero which would not satisfy the constraint that
// ic == 0 whenever tag_err == 1. This constraint might be removed
// as part of #4613.
FF c = alu_trace_builder.op_not(a, in_tag, clk);

// Write into memory value c from intermediate register ic.
mem_trace_builder.write_into_memory(clk, IntermRegister::IC, dst_offset, c, in_tag);

main_trace.push_back(Row{
.avm_main_clk = clk,
.avm_main_pc = FF(pc++),
.avm_main_internal_return_ptr = FF(internal_return_ptr),
.avm_main_sel_op_not = FF(1),
.avm_main_in_tag = FF(static_cast<uint32_t>(in_tag)),
.avm_main_tag_err = FF(static_cast<uint32_t>(!read_a.tag_match)),
.avm_main_ia = a,
.avm_main_ic = c,
.avm_main_mem_op_a = FF(1),
.avm_main_mem_op_c = FF(1),
.avm_main_rwc = FF(1),
.avm_main_mem_idx_a = FF(a_offset),
.avm_main_mem_idx_c = FF(dst_offset),
});
};

/**
* @brief Equality with direct memory access.
*
* @param a_offset An index in memory pointing to the first operand of the equality.
* @param b_offset An index in memory pointing to the second operand of the equality.
* @param dst_offset An index in memory pointing to the output of the equality.
* @param in_tag The instruction memory tag of the operands.
*/
void AvmTraceBuilder::op_eq(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag)
{
auto clk = static_cast<uint32_t>(main_trace.size());

// Reading from memory and loading into ia resp. ib.
auto read_a = mem_trace_builder.read_and_load_from_memory(clk, IntermRegister::IA, a_offset, in_tag);
auto read_b = mem_trace_builder.read_and_load_from_memory(clk, IntermRegister::IB, b_offset, in_tag);
bool tag_match = read_a.tag_match && read_b.tag_match;

// c = a == b ? 1 : 0
FF a = tag_match ? read_a.val : FF(0);
FF b = tag_match ? read_b.val : FF(0);

// TODO(4613): If tag_match == false, then the value of c
// will not be zero which would not satisfy the constraint that
// ic == 0 whenever tag_err == 1. This constraint might be removed
// as part of #4613.
FF c = alu_trace_builder.op_eq(a, b, in_tag, clk);

// Write into memory value c from intermediate register ic.
mem_trace_builder.write_into_memory(clk, IntermRegister::IC, dst_offset, c, in_tag);

main_trace.push_back(Row{
.avm_main_clk = clk,
.avm_main_pc = FF(pc++),
.avm_main_internal_return_ptr = FF(internal_return_ptr),
.avm_main_sel_op_eq = FF(1),
.avm_main_in_tag = FF(static_cast<uint32_t>(in_tag)),
.avm_main_tag_err = FF(static_cast<uint32_t>(!tag_match)),
.avm_main_ia = a,
.avm_main_ib = b,
.avm_main_ic = c,
.avm_main_mem_op_a = FF(1),
.avm_main_mem_op_b = FF(1),
.avm_main_mem_op_c = FF(1),
.avm_main_rwc = FF(1),
.avm_main_mem_idx_a = FF(a_offset),
.avm_main_mem_idx_b = FF(b_offset),
.avm_main_mem_idx_c = FF(dst_offset),
});
}

// TODO: Finish SET opcode implementation. This is a partial implementation
// facilitating testing of arithmetic operations over non finite field types.
// We add an entry in the memory trace and a simplified one in the main trace
Expand Down Expand Up @@ -708,95 +800,4 @@ std::vector<Row> AvmTraceBuilder::finalize()
return trace;
}

/**
* @brief Bitwise not with direct memory access.
*
* @param a_offset An index in memory pointing to the only operand of Not.
* @param dst_offset An index in memory pointing to the output of Not.
* @param in_tag The instruction memory tag of the operands.
*/
void AvmTraceBuilder::op_not(uint32_t a_offset, uint32_t dst_offset, AvmMemoryTag in_tag)
{
auto clk = static_cast<uint32_t>(main_trace.size());

// Reading from memory and loading into ia.
auto read_a = mem_trace_builder.read_and_load_from_memory(clk, IntermRegister::IA, a_offset, in_tag);

// ~a = c
FF a = read_a.tag_match ? read_a.val : FF(0);
// TODO(4613): If tag_match == false, then the value of c
// will not be zero which would not satisfy the constraint that
// ic == 0 whenever tag_err == 1. This constraint might be removed
// as part of #4613.
FF c = alu_trace_builder.op_not(a, in_tag, clk);

// Write into memory value c from intermediate register ic.
mem_trace_builder.write_into_memory(clk, IntermRegister::IC, dst_offset, c, in_tag);

main_trace.push_back(Row{
.avm_main_clk = clk,
.avm_main_pc = FF(pc++),
.avm_main_internal_return_ptr = FF(internal_return_ptr),
.avm_main_sel_op_not = FF(1),
.avm_main_in_tag = FF(static_cast<uint32_t>(in_tag)),
.avm_main_tag_err = FF(static_cast<uint32_t>(!read_a.tag_match)),
.avm_main_ia = a,
.avm_main_ic = c,
.avm_main_mem_op_a = FF(1),
.avm_main_mem_op_c = FF(1),
.avm_main_rwc = FF(1),
.avm_main_mem_idx_a = FF(a_offset),
.avm_main_mem_idx_c = FF(dst_offset),
});
};

/**
* @brief Equality with direct memory access.
*
* @param a_offset An index in memory pointing to the first operand of the equality.
* @param b_offset An index in memory pointing to the second operand of the equality.
* @param dst_offset An index in memory pointing to the output of the equality.
* @param in_tag The instruction memory tag of the operands.
*/
void AvmTraceBuilder::op_eq(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag)
{
auto clk = static_cast<uint32_t>(main_trace.size());

// Reading from memory and loading into ia resp. ib.
auto read_a = mem_trace_builder.read_and_load_from_memory(clk, IntermRegister::IA, a_offset, in_tag);
auto read_b = mem_trace_builder.read_and_load_from_memory(clk, IntermRegister::IB, b_offset, in_tag);
bool tag_match = read_a.tag_match && read_b.tag_match;

// c = a == b ? 1 : 0
FF a = tag_match ? read_a.val : FF(0);
FF b = tag_match ? read_b.val : FF(0);

// TODO(4613): If tag_match == false, then the value of c
// will not be zero which would not satisfy the constraint that
// ic == 0 whenever tag_err == 1. This constraint might be removed
// as part of #4613.
FF c = alu_trace_builder.op_eq(a, b, in_tag, clk);

// Write into memory value c from intermediate register ic.
mem_trace_builder.write_into_memory(clk, IntermRegister::IC, dst_offset, c, in_tag);

main_trace.push_back(Row{
.avm_main_clk = clk,
.avm_main_pc = FF(pc++),
.avm_main_internal_return_ptr = FF(internal_return_ptr),
.avm_main_sel_op_eq = FF(1),
.avm_main_in_tag = FF(static_cast<uint32_t>(in_tag)),
.avm_main_tag_err = FF(static_cast<uint32_t>(!tag_match)),
.avm_main_ia = a,
.avm_main_ib = b,
.avm_main_ic = c,
.avm_main_mem_op_a = FF(1),
.avm_main_mem_op_b = FF(1),
.avm_main_mem_op_c = FF(1),
.avm_main_rwc = FF(1),
.avm_main_mem_idx_a = FF(a_offset),
.avm_main_mem_idx_b = FF(b_offset),
.avm_main_mem_idx_c = FF(dst_offset),
});
}
} // namespace avm_trace

0 comments on commit cb08f8c

Please sign in to comment.