From cb08f8c67be16cd2c1ba46cd1a8f1e2e63ae13b4 Mon Sep 17 00:00:00 2001 From: jeanmon Date: Thu, 15 Feb 2024 10:24:26 +0000 Subject: [PATCH] 4598 - refactor: move op_not and op_eq routines --- .../barretenberg/vm/avm_trace/avm_trace.cpp | 183 +++++++++--------- 1 file changed, 92 insertions(+), 91 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_trace.cpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_trace.cpp index fc3458b8996..35639ae7276 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_trace.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_trace.cpp @@ -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(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(in_tag)), + .avm_main_tag_err = FF(static_cast(!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(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(in_tag)), + .avm_main_tag_err = FF(static_cast(!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 @@ -708,95 +800,4 @@ std::vector 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(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(in_tag)), - .avm_main_tag_err = FF(static_cast(!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(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(in_tag)), - .avm_main_tag_err = FF(static_cast(!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