From 3a7d31b200e6e604eea06a40dcf5bf02b088ab79 Mon Sep 17 00:00:00 2001 From: Ilyas Ridhuan Date: Tue, 13 Feb 2024 11:53:50 +0000 Subject: [PATCH] feat: bitwise_not avm circuit (#4548) Adds the bitwise not ciricuit in the avm. Closes #4454 --------- Co-authored-by: Ilyas Ridhuan --- barretenberg/cpp/pil/avm/alu_chip.pil | 65 +++-- barretenberg/cpp/pil/avm/avm_mini.pil | 5 +- .../flavor/generated/AvmMini_flavor.hpp | 115 +++++--- .../generated/AvmMini_circuit_builder.hpp | 46 ++-- .../relations/generated/AvmMini/alu_chip.hpp | 86 ++++-- .../relations/generated/AvmMini/avm_mini.hpp | 159 ++++++----- .../generated/AvmMini/declare_views.hpp | 16 +- .../relations/generated/AvmMini/mem_trace.hpp | 30 +-- .../vm/avm_trace/AvmMini_alu_trace.cpp | 57 +++- .../vm/avm_trace/AvmMini_alu_trace.hpp | 10 +- .../vm/avm_trace/AvmMini_common.hpp | 2 +- .../vm/avm_trace/AvmMini_deserialization.cpp | 4 +- .../vm/avm_trace/AvmMini_execution.cpp | 40 +-- .../vm/avm_trace/AvmMini_helper.cpp | 2 +- .../vm/avm_trace/AvmMini_opcode.cpp | 4 +- .../vm/avm_trace/AvmMini_trace.cpp | 53 +++- .../vm/avm_trace/AvmMini_trace.hpp | 11 +- .../vm/generated/AvmMini_prover.cpp | 1 + .../vm/generated/AvmMini_prover.hpp | 2 +- .../vm/generated/AvmMini_verifier.cpp | 6 + .../vm/generated/AvmMini_verifier.hpp | 2 +- .../vm/tests/AvmMini_arithmetic.test.cpp | 116 ++++---- .../vm/tests/AvmMini_bitwise.test.cpp | 255 ++++++++++++++++++ .../vm/tests/AvmMini_memory.test.cpp | 18 +- .../barretenberg/vm/tests/helpers.test.cpp | 2 +- 25 files changed, 792 insertions(+), 315 deletions(-) create mode 100644 barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_bitwise.test.cpp diff --git a/barretenberg/cpp/pil/avm/alu_chip.pil b/barretenberg/cpp/pil/avm/alu_chip.pil index 09cf037b016..05ff63cb574 100644 --- a/barretenberg/cpp/pil/avm/alu_chip.pil +++ b/barretenberg/cpp/pil/avm/alu_chip.pil @@ -15,6 +15,7 @@ namespace aluChip(256); pol commit alu_op_sub; pol commit alu_op_mul; pol commit alu_op_div; + pol commit alu_op_not; // Flattened boolean instruction tags pol commit alu_ff_tag; @@ -107,12 +108,12 @@ namespace aluChip(256); // serves to an algebraic expression of commited polynomials in a more concise way. // Bit slices partial sums - pol sum_8 = alu_u8_r0; - pol sum_16 = sum_8 + alu_u8_r1 * 2**8; - pol sum_32 = sum_16 + alu_u16_r0 * 2**16; - pol sum_64 = sum_32 + alu_u16_r1 * 2**32 + alu_u16_r2 * 2**48; - pol sum_96 = sum_64 + alu_u16_r3 * 2**64 + alu_u16_r4 * 2**80; - pol sum_128 = sum_96 + alu_u16_r5 * 2**96 + alu_u16_r6 * 2**112; + pol SUM_8 = alu_u8_r0; + pol SUM_16 = SUM_8 + alu_u8_r1 * 2**8; + pol SUM_32 = SUM_16 + alu_u16_r0 * 2**16; + pol SUM_64 = SUM_32 + alu_u16_r1 * 2**32 + alu_u16_r2 * 2**48; + pol SUM_96 = SUM_64 + alu_u16_r3 * 2**64 + alu_u16_r4 * 2**80; + pol SUM_128 = SUM_96 + alu_u16_r5 * 2**96 + alu_u16_r6 * 2**112; // ========= ADDITION/SUBTRACTION Operation Constraints =============================== // @@ -134,13 +135,13 @@ namespace aluChip(256); // The second relation will consist in showing that sum_N - c = 0 for N = 8, 16, 32, 64, 128. #[ALU_ADD_SUB_1] - (alu_op_add + alu_op_sub) * (sum_128 - alu_ia + alu_ff_tag * alu_ic) + (alu_op_add - alu_op_sub) * (alu_cf * 2**128 - alu_ib) = 0; + (alu_op_add + alu_op_sub) * (SUM_128 - alu_ia + alu_ff_tag * alu_ic) + (alu_op_add - alu_op_sub) * (alu_cf * 2**128 - alu_ib) = 0; // Helper polynomial - pol sum_tag = alu_u8_tag * sum_8 + alu_u16_tag * sum_16 + alu_u32_tag * sum_32 + alu_u64_tag * sum_64 + alu_u128_tag * sum_128; + pol SUM_TAG = alu_u8_tag * SUM_8 + alu_u16_tag * SUM_16 + alu_u32_tag * SUM_32 + alu_u64_tag * SUM_64 + alu_u128_tag * SUM_128; #[ALU_ADD_SUB_2] - (alu_op_add + alu_op_sub) * (sum_tag + alu_ff_tag * alu_ia - alu_ic) + alu_ff_tag * (alu_op_add - alu_op_sub) * alu_ib = 0; + (alu_op_add + alu_op_sub) * (SUM_TAG + alu_ff_tag * alu_ia - alu_ic) + alu_ff_tag * (alu_op_add - alu_op_sub) * alu_ib = 0; // ========= MULTIPLICATION Operation Constraints =============================== @@ -155,13 +156,13 @@ namespace aluChip(256); // We group relations for u8, u16, u32, u64 together. // Helper polynomial - pol sum_tag_no_128 = alu_u8_tag * sum_8 + alu_u16_tag * sum_16 + alu_u32_tag * sum_32 + alu_u64_tag * sum_64; + pol SUM_TAG_NO_128 = alu_u8_tag * SUM_8 + alu_u16_tag * SUM_16 + alu_u32_tag * SUM_32 + alu_u64_tag * SUM_64; #[ALU_MUL_COMMON_1] - (1 - alu_ff_tag - alu_u128_tag) * alu_op_mul * (sum_128 - alu_ia * alu_ib) = 0; + (1 - alu_ff_tag - alu_u128_tag) * alu_op_mul * (SUM_128 - alu_ia * alu_ib) = 0; #[ALU_MUL_COMMON_2] - alu_op_mul * (sum_tag_no_128 - (1 - alu_ff_tag - alu_u128_tag) * alu_ic) = 0; + alu_op_mul * (SUM_TAG_NO_128 - (1 - alu_ff_tag - alu_u128_tag) * alu_ic) = 0; // ========= u128 MULTIPLICATION Operation Constraints =============================== // @@ -178,24 +179,48 @@ namespace aluChip(256); // R' is stored in alu_u64_r0 // 64-bit lower limb - pol sum_low_64 = alu_u16_r0 + alu_u16_r1 * 2**16 + alu_u16_r2 * 2**32 + alu_u16_r3 * 2**48; + pol SUM_LOW_64 = alu_u16_r0 + alu_u16_r1 * 2**16 + alu_u16_r2 * 2**32 + alu_u16_r3 * 2**48; // 64-bit higher limb - pol sum_high_64 = alu_u16_r4 + alu_u16_r5 * 2**16 + alu_u16_r6 * 2**32 + alu_u16_r7 * 2**48; + pol SUM_HIGH_64 = alu_u16_r4 + alu_u16_r5 * 2**16 + alu_u16_r6 * 2**32 + alu_u16_r7 * 2**48; // 64-bit lower limb for next row - pol sum_low_shifted_64 = alu_u16_r0' + alu_u16_r1' * 2**16 + alu_u16_r2' * 2**32 + alu_u16_r3' * 2**48; + pol SUM_LOW_SHIFTED_64 = alu_u16_r0' + alu_u16_r1' * 2**16 + alu_u16_r2' * 2**32 + alu_u16_r3' * 2**48; // 64-bit higher limb for next row - pol sum_high_shifted_64 = alu_u16_r4' + alu_u16_r5' * 2**16 + alu_u16_r6' * 2**32 + alu_u16_r7' * 2**48; + pol SUM_HIGH_SHIFTED_64 = alu_u16_r4' + alu_u16_r5' * 2**16 + alu_u16_r6' * 2**32 + alu_u16_r7' * 2**48; // Arithmetic relations - alu_u128_tag * alu_op_mul * (sum_low_64 + sum_high_64 * 2**64 - alu_ia) = 0; - alu_u128_tag * alu_op_mul * (sum_low_shifted_64 + sum_high_shifted_64 * 2**64 - alu_ib) = 0; + alu_u128_tag * alu_op_mul * (SUM_LOW_64 + SUM_HIGH_64 * 2**64 - alu_ia) = 0; + alu_u128_tag * alu_op_mul * (SUM_LOW_SHIFTED_64 + SUM_HIGH_SHIFTED_64 * 2**64 - alu_ib) = 0; #[ALU_MULTIPLICATION_OUT_U128] alu_u128_tag * alu_op_mul * ( - alu_ia * sum_low_shifted_64 - + sum_low_64 * sum_high_shifted_64 * 2**64 + alu_ia * SUM_LOW_SHIFTED_64 + + SUM_LOW_64 * SUM_HIGH_SHIFTED_64 * 2**64 - (alu_cf * 2**64 + alu_u64_r0) * 2**128 - alu_ic ) = 0; + + // ========= BITWISE NOT Operation Constraints =============================== + // Constrain mem_tag to not be FF (BITWISE NOT doesn't make sense for FF) + // TODO decide if it is done here or in another trace + + // Do not allow alu_ff_tag to be set if we are doing bitwise + pol BITWISE_SEL = alu_op_not; // Add more bitwise operations + #[ALU_FF_NOT_XOR] + BITWISE_SEL * alu_ff_tag = 0; + + // The value 2^k - 1 + pol UINT_MAX = alu_u8_tag * 2**8 + + alu_u16_tag * 2**16 + + alu_u32_tag * 2**32 + + alu_u64_tag * 2**64 + + alu_u128_tag * 2**128 - 1; + + // BITWISE NOT relation is: a + ~a = 2^k - 1 + // Or (a + ~a - 2^k + 1) = 0; + // value of "a" stored in alu_ia and "~a" stored in alu_ic + #[ALU_OP_NOT] + alu_op_not * (alu_ia + alu_ic - UINT_MAX) = 0; + + diff --git a/barretenberg/cpp/pil/avm/avm_mini.pil b/barretenberg/cpp/pil/avm/avm_mini.pil index 26268fe25cd..c7421de63b8 100644 --- a/barretenberg/cpp/pil/avm/avm_mini.pil +++ b/barretenberg/cpp/pil/avm/avm_mini.pil @@ -34,6 +34,8 @@ namespace avmMini(256); pol commit sel_op_mul; // DIV pol commit sel_op_div; + // NOT + pol commit sel_op_not; // Instruction memory tag (0: uninitialized, 1: u8, 2: u16, 3: u32, 4: u64, 5: u128, 6:field) pol commit in_tag; @@ -79,6 +81,7 @@ namespace avmMini(256); sel_op_sub * (1 - sel_op_sub) = 0; sel_op_mul * (1 - sel_op_mul) = 0; sel_op_div * (1 - sel_op_div) = 0; + sel_op_not * (1 - sel_op_not) = 0; sel_internal_call * (1 - sel_internal_call) = 0; sel_internal_return * (1 - sel_internal_return) = 0; @@ -172,7 +175,7 @@ namespace avmMini(256); //===== CONTROL_FLOW_CONSISTENCY ============================================ pol INTERNAL_CALL_STACK_SELECTORS = (first + sel_internal_call + sel_internal_return + sel_halt); - pol OPCODE_SELECTORS = (sel_op_add + sel_op_sub + sel_op_div + sel_op_mul); + pol OPCODE_SELECTORS = (sel_op_add + sel_op_sub + sel_op_div + sel_op_mul + sel_op_not); // Program counter must increment if not jumping or returning #[PC_INCREMENT] diff --git a/barretenberg/cpp/src/barretenberg/flavor/generated/AvmMini_flavor.hpp b/barretenberg/cpp/src/barretenberg/flavor/generated/AvmMini_flavor.hpp index 1ee03bc93b8..5cd19997c69 100644 --- a/barretenberg/cpp/src/barretenberg/flavor/generated/AvmMini_flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/flavor/generated/AvmMini_flavor.hpp @@ -37,13 +37,13 @@ class AvmMiniFlavor { using RelationSeparator = FF; static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 2; - static constexpr size_t NUM_WITNESS_ENTITIES = 64; + static constexpr size_t NUM_WITNESS_ENTITIES = 66; static constexpr size_t NUM_WIRES = NUM_WITNESS_ENTITIES + NUM_PRECOMPUTED_ENTITIES; // We have two copies of the witness entities, so we subtract the number of fixed ones (they have no shift), one for // the unshifted and one for the shifted - static constexpr size_t NUM_ALL_ENTITIES = 80; + static constexpr size_t NUM_ALL_ENTITIES = 82; - using Relations = std::tuple, AvmMini_vm::avm_mini, AvmMini_vm::mem_trace>; + using Relations = std::tuple, AvmMini_vm::alu_chip, AvmMini_vm::avm_mini>; static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length(); @@ -96,6 +96,7 @@ class AvmMiniFlavor { aluChip_alu_op_sub, aluChip_alu_op_mul, aluChip_alu_op_div, + aluChip_alu_op_not, aluChip_alu_ff_tag, aluChip_alu_u8_tag, aluChip_alu_u16_tag, @@ -124,6 +125,7 @@ class AvmMiniFlavor { avmMini_sel_op_sub, avmMini_sel_op_mul, avmMini_sel_op_div, + avmMini_sel_op_not, avmMini_in_tag, avmMini_op_err, avmMini_tag_err, @@ -163,6 +165,7 @@ class AvmMiniFlavor { aluChip_alu_op_sub, aluChip_alu_op_mul, aluChip_alu_op_div, + aluChip_alu_op_not, aluChip_alu_ff_tag, aluChip_alu_u8_tag, aluChip_alu_u16_tag, @@ -191,6 +194,7 @@ class AvmMiniFlavor { avmMini_sel_op_sub, avmMini_sel_op_mul, avmMini_sel_op_div, + avmMini_sel_op_not, avmMini_in_tag, avmMini_op_err, avmMini_tag_err, @@ -236,6 +240,7 @@ class AvmMiniFlavor { aluChip_alu_op_sub, aluChip_alu_op_mul, aluChip_alu_op_div, + aluChip_alu_op_not, aluChip_alu_ff_tag, aluChip_alu_u8_tag, aluChip_alu_u16_tag, @@ -264,6 +269,7 @@ class AvmMiniFlavor { avmMini_sel_op_sub, avmMini_sel_op_mul, avmMini_sel_op_div, + avmMini_sel_op_not, avmMini_in_tag, avmMini_op_err, avmMini_tag_err, @@ -281,20 +287,20 @@ class AvmMiniFlavor { avmMini_mem_idx_b, avmMini_mem_idx_c, avmMini_last, + memTrace_m_rw_shift, + memTrace_m_val_shift, + memTrace_m_addr_shift, + memTrace_m_tag_shift, aluChip_alu_u16_r1_shift, - aluChip_alu_u16_r0_shift, aluChip_alu_u16_r4_shift, aluChip_alu_u16_r7_shift, + aluChip_alu_u16_r0_shift, aluChip_alu_u16_r5_shift, - aluChip_alu_u16_r2_shift, aluChip_alu_u16_r6_shift, + aluChip_alu_u16_r2_shift, aluChip_alu_u16_r3_shift, - avmMini_pc_shift, avmMini_internal_return_ptr_shift, - memTrace_m_tag_shift, - memTrace_m_addr_shift, - memTrace_m_val_shift, - memTrace_m_rw_shift) + avmMini_pc_shift) RefVector get_wires() { @@ -319,6 +325,7 @@ class AvmMiniFlavor { aluChip_alu_op_sub, aluChip_alu_op_mul, aluChip_alu_op_div, + aluChip_alu_op_not, aluChip_alu_ff_tag, aluChip_alu_u8_tag, aluChip_alu_u16_tag, @@ -347,6 +354,7 @@ class AvmMiniFlavor { avmMini_sel_op_sub, avmMini_sel_op_mul, avmMini_sel_op_div, + avmMini_sel_op_not, avmMini_in_tag, avmMini_op_err, avmMini_tag_err, @@ -364,20 +372,20 @@ class AvmMiniFlavor { avmMini_mem_idx_b, avmMini_mem_idx_c, avmMini_last, + memTrace_m_rw_shift, + memTrace_m_val_shift, + memTrace_m_addr_shift, + memTrace_m_tag_shift, aluChip_alu_u16_r1_shift, - aluChip_alu_u16_r0_shift, aluChip_alu_u16_r4_shift, aluChip_alu_u16_r7_shift, + aluChip_alu_u16_r0_shift, aluChip_alu_u16_r5_shift, - aluChip_alu_u16_r2_shift, aluChip_alu_u16_r6_shift, + aluChip_alu_u16_r2_shift, aluChip_alu_u16_r3_shift, - avmMini_pc_shift, avmMini_internal_return_ptr_shift, - memTrace_m_tag_shift, - memTrace_m_addr_shift, - memTrace_m_val_shift, - memTrace_m_rw_shift }; + avmMini_pc_shift }; }; RefVector get_unshifted() { @@ -402,6 +410,7 @@ class AvmMiniFlavor { aluChip_alu_op_sub, aluChip_alu_op_mul, aluChip_alu_op_div, + aluChip_alu_op_not, aluChip_alu_ff_tag, aluChip_alu_u8_tag, aluChip_alu_u16_tag, @@ -430,6 +439,7 @@ class AvmMiniFlavor { avmMini_sel_op_sub, avmMini_sel_op_mul, avmMini_sel_op_div, + avmMini_sel_op_not, avmMini_in_tag, avmMini_op_err, avmMini_tag_err, @@ -450,23 +460,37 @@ class AvmMiniFlavor { }; RefVector get_to_be_shifted() { - return { aluChip_alu_u16_r1, aluChip_alu_u16_r0, - aluChip_alu_u16_r4, aluChip_alu_u16_r7, - aluChip_alu_u16_r5, aluChip_alu_u16_r2, - aluChip_alu_u16_r6, aluChip_alu_u16_r3, - avmMini_pc, avmMini_internal_return_ptr, - memTrace_m_tag, memTrace_m_addr, - memTrace_m_val, memTrace_m_rw }; + return { memTrace_m_rw, + memTrace_m_val, + memTrace_m_addr, + memTrace_m_tag, + aluChip_alu_u16_r1, + aluChip_alu_u16_r4, + aluChip_alu_u16_r7, + aluChip_alu_u16_r0, + aluChip_alu_u16_r5, + aluChip_alu_u16_r6, + aluChip_alu_u16_r2, + aluChip_alu_u16_r3, + avmMini_internal_return_ptr, + avmMini_pc }; }; RefVector get_shifted() { - return { aluChip_alu_u16_r1_shift, aluChip_alu_u16_r0_shift, - aluChip_alu_u16_r4_shift, aluChip_alu_u16_r7_shift, - aluChip_alu_u16_r5_shift, aluChip_alu_u16_r2_shift, - aluChip_alu_u16_r6_shift, aluChip_alu_u16_r3_shift, - avmMini_pc_shift, avmMini_internal_return_ptr_shift, - memTrace_m_tag_shift, memTrace_m_addr_shift, - memTrace_m_val_shift, memTrace_m_rw_shift }; + return { memTrace_m_rw_shift, + memTrace_m_val_shift, + memTrace_m_addr_shift, + memTrace_m_tag_shift, + aluChip_alu_u16_r1_shift, + aluChip_alu_u16_r4_shift, + aluChip_alu_u16_r7_shift, + aluChip_alu_u16_r0_shift, + aluChip_alu_u16_r5_shift, + aluChip_alu_u16_r6_shift, + aluChip_alu_u16_r2_shift, + aluChip_alu_u16_r3_shift, + avmMini_internal_return_ptr_shift, + avmMini_pc_shift }; }; }; @@ -479,13 +503,20 @@ class AvmMiniFlavor { RefVector get_to_be_shifted() { - return { aluChip_alu_u16_r1, aluChip_alu_u16_r0, - aluChip_alu_u16_r4, aluChip_alu_u16_r7, - aluChip_alu_u16_r5, aluChip_alu_u16_r2, - aluChip_alu_u16_r6, aluChip_alu_u16_r3, - avmMini_pc, avmMini_internal_return_ptr, - memTrace_m_tag, memTrace_m_addr, - memTrace_m_val, memTrace_m_rw }; + return { memTrace_m_rw, + memTrace_m_val, + memTrace_m_addr, + memTrace_m_tag, + aluChip_alu_u16_r1, + aluChip_alu_u16_r4, + aluChip_alu_u16_r7, + aluChip_alu_u16_r0, + aluChip_alu_u16_r5, + aluChip_alu_u16_r6, + aluChip_alu_u16_r2, + aluChip_alu_u16_r3, + avmMini_internal_return_ptr, + avmMini_pc }; }; // The plookup wires that store plookup read data. @@ -583,6 +614,7 @@ class AvmMiniFlavor { Base::aluChip_alu_op_sub = "ALUCHIP_ALU_OP_SUB"; Base::aluChip_alu_op_mul = "ALUCHIP_ALU_OP_MUL"; Base::aluChip_alu_op_div = "ALUCHIP_ALU_OP_DIV"; + Base::aluChip_alu_op_not = "ALUCHIP_ALU_OP_NOT"; Base::aluChip_alu_ff_tag = "ALUCHIP_ALU_FF_TAG"; Base::aluChip_alu_u8_tag = "ALUCHIP_ALU_U8_TAG"; Base::aluChip_alu_u16_tag = "ALUCHIP_ALU_U16_TAG"; @@ -611,6 +643,7 @@ class AvmMiniFlavor { Base::avmMini_sel_op_sub = "AVMMINI_SEL_OP_SUB"; Base::avmMini_sel_op_mul = "AVMMINI_SEL_OP_MUL"; Base::avmMini_sel_op_div = "AVMMINI_SEL_OP_DIV"; + Base::avmMini_sel_op_not = "AVMMINI_SEL_OP_NOT"; Base::avmMini_in_tag = "AVMMINI_IN_TAG"; Base::avmMini_op_err = "AVMMINI_OP_ERR"; Base::avmMini_tag_err = "AVMMINI_TAG_ERR"; @@ -666,6 +699,7 @@ class AvmMiniFlavor { Commitment aluChip_alu_op_sub; Commitment aluChip_alu_op_mul; Commitment aluChip_alu_op_div; + Commitment aluChip_alu_op_not; Commitment aluChip_alu_ff_tag; Commitment aluChip_alu_u8_tag; Commitment aluChip_alu_u16_tag; @@ -694,6 +728,7 @@ class AvmMiniFlavor { Commitment avmMini_sel_op_sub; Commitment avmMini_sel_op_mul; Commitment avmMini_sel_op_div; + Commitment avmMini_sel_op_not; Commitment avmMini_in_tag; Commitment avmMini_op_err; Commitment avmMini_tag_err; @@ -749,6 +784,7 @@ class AvmMiniFlavor { aluChip_alu_op_sub = deserialize_from_buffer(Transcript::proof_data, num_frs_read); aluChip_alu_op_mul = deserialize_from_buffer(Transcript::proof_data, num_frs_read); aluChip_alu_op_div = deserialize_from_buffer(Transcript::proof_data, num_frs_read); + aluChip_alu_op_not = deserialize_from_buffer(Transcript::proof_data, num_frs_read); aluChip_alu_ff_tag = deserialize_from_buffer(Transcript::proof_data, num_frs_read); aluChip_alu_u8_tag = deserialize_from_buffer(Transcript::proof_data, num_frs_read); aluChip_alu_u16_tag = deserialize_from_buffer(Transcript::proof_data, num_frs_read); @@ -777,6 +813,7 @@ class AvmMiniFlavor { avmMini_sel_op_sub = deserialize_from_buffer(Transcript::proof_data, num_frs_read); avmMini_sel_op_mul = deserialize_from_buffer(Transcript::proof_data, num_frs_read); avmMini_sel_op_div = deserialize_from_buffer(Transcript::proof_data, num_frs_read); + avmMini_sel_op_not = deserialize_from_buffer(Transcript::proof_data, num_frs_read); avmMini_in_tag = deserialize_from_buffer(Transcript::proof_data, num_frs_read); avmMini_op_err = deserialize_from_buffer(Transcript::proof_data, num_frs_read); avmMini_tag_err = deserialize_from_buffer(Transcript::proof_data, num_frs_read); @@ -836,6 +873,7 @@ class AvmMiniFlavor { serialize_to_buffer(aluChip_alu_op_sub, Transcript::proof_data); serialize_to_buffer(aluChip_alu_op_mul, Transcript::proof_data); serialize_to_buffer(aluChip_alu_op_div, Transcript::proof_data); + serialize_to_buffer(aluChip_alu_op_not, Transcript::proof_data); serialize_to_buffer(aluChip_alu_ff_tag, Transcript::proof_data); serialize_to_buffer(aluChip_alu_u8_tag, Transcript::proof_data); serialize_to_buffer(aluChip_alu_u16_tag, Transcript::proof_data); @@ -864,6 +902,7 @@ class AvmMiniFlavor { serialize_to_buffer(avmMini_sel_op_sub, Transcript::proof_data); serialize_to_buffer(avmMini_sel_op_mul, Transcript::proof_data); serialize_to_buffer(avmMini_sel_op_div, Transcript::proof_data); + serialize_to_buffer(avmMini_sel_op_not, Transcript::proof_data); serialize_to_buffer(avmMini_in_tag, Transcript::proof_data); serialize_to_buffer(avmMini_op_err, Transcript::proof_data); serialize_to_buffer(avmMini_tag_err, Transcript::proof_data); diff --git a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp index 5392923eb1f..d5f3d1e5786 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp @@ -16,6 +16,8 @@ #include "barretenberg/relations/generated/AvmMini/avm_mini.hpp" #include "barretenberg/relations/generated/AvmMini/mem_trace.hpp" +using namespace bb; + namespace bb { template struct AvmMiniFullRow { @@ -40,6 +42,7 @@ template struct AvmMiniFullRow { FF aluChip_alu_op_sub{}; FF aluChip_alu_op_mul{}; FF aluChip_alu_op_div{}; + FF aluChip_alu_op_not{}; FF aluChip_alu_ff_tag{}; FF aluChip_alu_u8_tag{}; FF aluChip_alu_u16_tag{}; @@ -68,6 +71,7 @@ template struct AvmMiniFullRow { FF avmMini_sel_op_sub{}; FF avmMini_sel_op_mul{}; FF avmMini_sel_op_div{}; + FF avmMini_sel_op_not{}; FF avmMini_in_tag{}; FF avmMini_op_err{}; FF avmMini_tag_err{}; @@ -85,20 +89,20 @@ template struct AvmMiniFullRow { FF avmMini_mem_idx_b{}; FF avmMini_mem_idx_c{}; FF avmMini_last{}; + FF memTrace_m_rw_shift{}; + FF memTrace_m_val_shift{}; + FF memTrace_m_addr_shift{}; + FF memTrace_m_tag_shift{}; FF aluChip_alu_u16_r1_shift{}; - FF aluChip_alu_u16_r0_shift{}; FF aluChip_alu_u16_r4_shift{}; FF aluChip_alu_u16_r7_shift{}; + FF aluChip_alu_u16_r0_shift{}; FF aluChip_alu_u16_r5_shift{}; - FF aluChip_alu_u16_r2_shift{}; FF aluChip_alu_u16_r6_shift{}; + FF aluChip_alu_u16_r2_shift{}; FF aluChip_alu_u16_r3_shift{}; - FF avmMini_pc_shift{}; FF avmMini_internal_return_ptr_shift{}; - FF memTrace_m_tag_shift{}; - FF memTrace_m_addr_shift{}; - FF memTrace_m_val_shift{}; - FF memTrace_m_rw_shift{}; + FF avmMini_pc_shift{}; }; class AvmMiniCircuitBuilder { @@ -111,8 +115,8 @@ class AvmMiniCircuitBuilder { using Polynomial = Flavor::Polynomial; using ProverPolynomials = Flavor::ProverPolynomials; - static constexpr size_t num_fixed_columns = 80; - static constexpr size_t num_polys = 66; + static constexpr size_t num_fixed_columns = 82; + static constexpr size_t num_polys = 68; std::vector rows; void set_trace(std::vector&& trace) { rows = std::move(trace); } @@ -149,6 +153,7 @@ class AvmMiniCircuitBuilder { polys.aluChip_alu_op_sub[i] = rows[i].aluChip_alu_op_sub; polys.aluChip_alu_op_mul[i] = rows[i].aluChip_alu_op_mul; polys.aluChip_alu_op_div[i] = rows[i].aluChip_alu_op_div; + polys.aluChip_alu_op_not[i] = rows[i].aluChip_alu_op_not; polys.aluChip_alu_ff_tag[i] = rows[i].aluChip_alu_ff_tag; polys.aluChip_alu_u8_tag[i] = rows[i].aluChip_alu_u8_tag; polys.aluChip_alu_u16_tag[i] = rows[i].aluChip_alu_u16_tag; @@ -177,6 +182,7 @@ class AvmMiniCircuitBuilder { polys.avmMini_sel_op_sub[i] = rows[i].avmMini_sel_op_sub; polys.avmMini_sel_op_mul[i] = rows[i].avmMini_sel_op_mul; polys.avmMini_sel_op_div[i] = rows[i].avmMini_sel_op_div; + polys.avmMini_sel_op_not[i] = rows[i].avmMini_sel_op_not; polys.avmMini_in_tag[i] = rows[i].avmMini_in_tag; polys.avmMini_op_err[i] = rows[i].avmMini_op_err; polys.avmMini_tag_err[i] = rows[i].avmMini_tag_err; @@ -196,20 +202,20 @@ class AvmMiniCircuitBuilder { polys.avmMini_last[i] = rows[i].avmMini_last; } + polys.memTrace_m_rw_shift = Polynomial(polys.memTrace_m_rw.shifted()); + polys.memTrace_m_val_shift = Polynomial(polys.memTrace_m_val.shifted()); + polys.memTrace_m_addr_shift = Polynomial(polys.memTrace_m_addr.shifted()); + polys.memTrace_m_tag_shift = Polynomial(polys.memTrace_m_tag.shifted()); polys.aluChip_alu_u16_r1_shift = Polynomial(polys.aluChip_alu_u16_r1.shifted()); - polys.aluChip_alu_u16_r0_shift = Polynomial(polys.aluChip_alu_u16_r0.shifted()); polys.aluChip_alu_u16_r4_shift = Polynomial(polys.aluChip_alu_u16_r4.shifted()); polys.aluChip_alu_u16_r7_shift = Polynomial(polys.aluChip_alu_u16_r7.shifted()); + polys.aluChip_alu_u16_r0_shift = Polynomial(polys.aluChip_alu_u16_r0.shifted()); polys.aluChip_alu_u16_r5_shift = Polynomial(polys.aluChip_alu_u16_r5.shifted()); - polys.aluChip_alu_u16_r2_shift = Polynomial(polys.aluChip_alu_u16_r2.shifted()); polys.aluChip_alu_u16_r6_shift = Polynomial(polys.aluChip_alu_u16_r6.shifted()); + polys.aluChip_alu_u16_r2_shift = Polynomial(polys.aluChip_alu_u16_r2.shifted()); polys.aluChip_alu_u16_r3_shift = Polynomial(polys.aluChip_alu_u16_r3.shifted()); - polys.avmMini_pc_shift = Polynomial(polys.avmMini_pc.shifted()); polys.avmMini_internal_return_ptr_shift = Polynomial(polys.avmMini_internal_return_ptr.shifted()); - polys.memTrace_m_tag_shift = Polynomial(polys.memTrace_m_tag.shifted()); - polys.memTrace_m_addr_shift = Polynomial(polys.memTrace_m_addr.shifted()); - polys.memTrace_m_val_shift = Polynomial(polys.memTrace_m_val.shifted()); - polys.memTrace_m_rw_shift = Polynomial(polys.memTrace_m_rw.shifted()); + polys.avmMini_pc_shift = Polynomial(polys.avmMini_pc.shifted()); return polys; } @@ -247,6 +253,10 @@ class AvmMiniCircuitBuilder { return true; }; + if (!evaluate_relation.template operator()>( + "mem_trace", AvmMini_vm::get_relation_label_mem_trace)) { + return false; + } if (!evaluate_relation.template operator()>("alu_chip", AvmMini_vm::get_relation_label_alu_chip)) { return false; @@ -255,10 +265,6 @@ class AvmMiniCircuitBuilder { AvmMini_vm::get_relation_label_avm_mini)) { return false; } - if (!evaluate_relation.template operator()>( - "mem_trace", AvmMini_vm::get_relation_label_mem_trace)) { - return false; - } return true; } diff --git a/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/alu_chip.hpp b/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/alu_chip.hpp index c1cd8a6e970..4c14d6617b4 100644 --- a/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/alu_chip.hpp +++ b/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/alu_chip.hpp @@ -7,60 +7,67 @@ namespace bb::AvmMini_vm { template struct Alu_chipRow { - FF aluChip_alu_u16_r0{}; - FF aluChip_alu_u16_r3{}; - FF aluChip_alu_u16_r5{}; FF aluChip_alu_u16_r1_shift{}; - FF aluChip_alu_u16_r0_shift{}; + FF aluChip_alu_u16_r1{}; + FF aluChip_alu_u16_r6{}; FF aluChip_alu_op_add{}; - FF aluChip_alu_ia{}; + FF aluChip_alu_u8_r1{}; FF aluChip_alu_u16_r4_shift{}; + FF aluChip_alu_op_sub{}; + FF aluChip_alu_u16_r7_shift{}; FF aluChip_alu_ib{}; + FF aluChip_alu_u16_tag{}; FF aluChip_alu_u16_r7{}; FF aluChip_alu_u8_r0{}; - FF aluChip_alu_u16_r7_shift{}; - FF aluChip_alu_op_sub{}; - FF aluChip_alu_u16_r6{}; - FF aluChip_alu_u16_r5_shift{}; + FF aluChip_alu_u16_r2{}; FF aluChip_alu_op_mul{}; + FF aluChip_alu_u8_tag{}; + FF aluChip_alu_u16_r0_shift{}; FF aluChip_alu_u64_tag{}; - FF aluChip_alu_u16_r2_shift{}; - FF aluChip_alu_u64_r0{}; - FF aluChip_alu_ff_tag{}; + FF aluChip_alu_ia{}; + FF aluChip_alu_u16_r0{}; + FF aluChip_alu_ic{}; FF aluChip_alu_u32_tag{}; - FF aluChip_alu_u16_tag{}; + FF aluChip_alu_u16_r5_shift{}; + FF aluChip_alu_ff_tag{}; FF aluChip_alu_u16_r4{}; + FF aluChip_alu_u16_r5{}; FF aluChip_alu_u16_r6_shift{}; - FF aluChip_alu_u16_r2{}; - FF aluChip_alu_ic{}; - FF aluChip_alu_u8_tag{}; + FF aluChip_alu_u64_r0{}; + FF aluChip_alu_op_not{}; + FF aluChip_alu_u128_tag{}; FF aluChip_alu_cf{}; + FF aluChip_alu_u16_r3{}; + FF aluChip_alu_u16_r2_shift{}; FF aluChip_alu_u16_r3_shift{}; - FF aluChip_alu_u8_r1{}; - FF aluChip_alu_u16_r1{}; - FF aluChip_alu_u128_tag{}; }; inline std::string get_relation_label_alu_chip(int index) { switch (index) { - case 9: - return "ALU_MUL_COMMON_1"; - - case 8: - return "ALU_MULTIPLICATION_FF"; + case 14: + return "ALU_FF_NOT_XOR"; case 6: return "ALU_ADD_SUB_1"; + case 13: + return "ALU_MULTIPLICATION_OUT_U128"; + + case 15: + return "ALU_OP_NOT"; + case 7: return "ALU_ADD_SUB_2"; + case 8: + return "ALU_MULTIPLICATION_FF"; + + case 9: + return "ALU_MUL_COMMON_1"; + case 10: return "ALU_MUL_COMMON_2"; - - case 13: - return "ALU_MULTIPLICATION_OUT_U128"; } return std::to_string(index); } @@ -69,8 +76,8 @@ template class alu_chipImpl { public: using FF = FF_; - static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ - 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 8, + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ + 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 8, 3, 4, }; template @@ -272,6 +279,27 @@ template class alu_chipImpl { tmp *= scaling_factor; std::get<13>(evals) += tmp; } + // Contribution 14 + { + AvmMini_DECLARE_VIEWS(14); + + auto tmp = (aluChip_alu_op_not * aluChip_alu_ff_tag); + tmp *= scaling_factor; + std::get<14>(evals) += tmp; + } + // Contribution 15 + { + AvmMini_DECLARE_VIEWS(15); + + auto tmp = (aluChip_alu_op_not * ((aluChip_alu_ia + aluChip_alu_ic) - + ((((((aluChip_alu_u8_tag * FF(256)) + (aluChip_alu_u16_tag * FF(65536))) + + (aluChip_alu_u32_tag * FF(4294967296UL))) + + (aluChip_alu_u64_tag * FF(uint256_t{ 0, 1, 0, 0 }))) + + (aluChip_alu_u128_tag * FF(uint256_t{ 0, 0, 1, 0 }))) - + FF(1)))); + tmp *= scaling_factor; + std::get<15>(evals) += tmp; + } } }; diff --git a/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/avm_mini.hpp b/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/avm_mini.hpp index 3347e38ff96..7f883d2a0bc 100644 --- a/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/avm_mini.hpp +++ b/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/avm_mini.hpp @@ -7,61 +7,62 @@ namespace bb::AvmMini_vm { template struct Avm_miniRow { + FF avmMini_sel_op_div{}; + FF avmMini_ic{}; + FF avmMini_sel_internal_call{}; + FF avmMini_mem_idx_b{}; + FF avmMini_rwc{}; + FF avmMini_mem_idx_a{}; FF avmMini_sel_op_sub{}; - FF avmMini_inv{}; - FF avmMini_first{}; + FF avmMini_rwa{}; + FF avmMini_sel_halt{}; FF avmMini_tag_err{}; - FF avmMini_pc_shift{}; - FF avmMini_mem_op_a{}; - FF avmMini_rwb{}; - FF avmMini_mem_op_c{}; - FF avmMini_internal_return_ptr_shift{}; - FF avmMini_rwc{}; - FF avmMini_sel_op_div{}; - FF avmMini_internal_return_ptr{}; - FF avmMini_pc{}; - FF avmMini_ia{}; - FF avmMini_sel_op_mul{}; - FF avmMini_mem_op_b{}; - FF avmMini_ib{}; FF avmMini_sel_jump{}; - FF avmMini_sel_halt{}; + FF avmMini_ib{}; + FF avmMini_pc{}; FF avmMini_sel_internal_return{}; + FF avmMini_inv{}; + FF avmMini_sel_op_mul{}; + FF avmMini_internal_return_ptr_shift{}; + FF avmMini_rwb{}; FF avmMini_op_err{}; - FF avmMini_mem_idx_a{}; - FF avmMini_mem_idx_b{}; - FF avmMini_sel_internal_call{}; + FF avmMini_internal_return_ptr{}; FF avmMini_sel_op_add{}; - FF avmMini_rwa{}; - FF avmMini_ic{}; + FF avmMini_mem_op_b{}; + FF avmMini_pc_shift{}; + FF avmMini_sel_op_not{}; + FF avmMini_ia{}; + FF avmMini_first{}; + FF avmMini_mem_op_c{}; + FF avmMini_mem_op_a{}; }; inline std::string get_relation_label_avm_mini(int index) { switch (index) { case 20: - return "SUBOP_DIVISION_ZERO_ERR1"; - - case 22: - return "SUBOP_ERROR_RELEVANT_OP"; + return "SUBOP_DIVISION_FF"; - case 30: - return "RETURN_POINTER_DECREMENT"; + case 37: + return "INTERNAL_RETURN_POINTER_CONSISTENCY"; case 36: - return "INTERNAL_RETURN_POINTER_CONSISTENCY"; + return "PC_INCREMENT"; case 21: - return "SUBOP_DIVISION_ZERO_ERR2"; - - case 35: - return "PC_INCREMENT"; + return "SUBOP_DIVISION_ZERO_ERR1"; - case 24: + case 25: return "RETURN_POINTER_INCREMENT"; - case 19: - return "SUBOP_DIVISION_FF"; + case 23: + return "SUBOP_ERROR_RELEVANT_OP"; + + case 22: + return "SUBOP_DIVISION_ZERO_ERR2"; + + case 31: + return "RETURN_POINTER_DECREMENT"; } return std::to_string(index); } @@ -70,8 +71,9 @@ template class avm_miniImpl { public: using FF = FF_; - static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 5, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, }; template @@ -117,7 +119,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(4); - auto tmp = (avmMini_sel_internal_call * (-avmMini_sel_internal_call + FF(1))); + auto tmp = (avmMini_sel_op_not * (-avmMini_sel_op_not + FF(1))); tmp *= scaling_factor; std::get<4>(evals) += tmp; } @@ -125,7 +127,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(5); - auto tmp = (avmMini_sel_internal_return * (-avmMini_sel_internal_return + FF(1))); + auto tmp = (avmMini_sel_internal_call * (-avmMini_sel_internal_call + FF(1))); tmp *= scaling_factor; std::get<5>(evals) += tmp; } @@ -133,7 +135,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(6); - auto tmp = (avmMini_sel_jump * (-avmMini_sel_jump + FF(1))); + auto tmp = (avmMini_sel_internal_return * (-avmMini_sel_internal_return + FF(1))); tmp *= scaling_factor; std::get<6>(evals) += tmp; } @@ -141,7 +143,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(7); - auto tmp = (avmMini_sel_halt * (-avmMini_sel_halt + FF(1))); + auto tmp = (avmMini_sel_jump * (-avmMini_sel_jump + FF(1))); tmp *= scaling_factor; std::get<7>(evals) += tmp; } @@ -149,7 +151,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(8); - auto tmp = (avmMini_op_err * (-avmMini_op_err + FF(1))); + auto tmp = (avmMini_sel_halt * (-avmMini_sel_halt + FF(1))); tmp *= scaling_factor; std::get<8>(evals) += tmp; } @@ -157,7 +159,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(9); - auto tmp = (avmMini_tag_err * (-avmMini_tag_err + FF(1))); + auto tmp = (avmMini_op_err * (-avmMini_op_err + FF(1))); tmp *= scaling_factor; std::get<9>(evals) += tmp; } @@ -165,7 +167,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(10); - auto tmp = (avmMini_mem_op_a * (-avmMini_mem_op_a + FF(1))); + auto tmp = (avmMini_tag_err * (-avmMini_tag_err + FF(1))); tmp *= scaling_factor; std::get<10>(evals) += tmp; } @@ -173,7 +175,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(11); - auto tmp = (avmMini_mem_op_b * (-avmMini_mem_op_b + FF(1))); + auto tmp = (avmMini_mem_op_a * (-avmMini_mem_op_a + FF(1))); tmp *= scaling_factor; std::get<11>(evals) += tmp; } @@ -181,7 +183,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(12); - auto tmp = (avmMini_mem_op_c * (-avmMini_mem_op_c + FF(1))); + auto tmp = (avmMini_mem_op_b * (-avmMini_mem_op_b + FF(1))); tmp *= scaling_factor; std::get<12>(evals) += tmp; } @@ -189,7 +191,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(13); - auto tmp = (avmMini_rwa * (-avmMini_rwa + FF(1))); + auto tmp = (avmMini_mem_op_c * (-avmMini_mem_op_c + FF(1))); tmp *= scaling_factor; std::get<13>(evals) += tmp; } @@ -197,7 +199,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(14); - auto tmp = (avmMini_rwb * (-avmMini_rwb + FF(1))); + auto tmp = (avmMini_rwa * (-avmMini_rwa + FF(1))); tmp *= scaling_factor; std::get<14>(evals) += tmp; } @@ -205,7 +207,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(15); - auto tmp = (avmMini_rwc * (-avmMini_rwc + FF(1))); + auto tmp = (avmMini_rwb * (-avmMini_rwb + FF(1))); tmp *= scaling_factor; std::get<15>(evals) += tmp; } @@ -213,7 +215,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(16); - auto tmp = (avmMini_tag_err * avmMini_ia); + auto tmp = (avmMini_rwc * (-avmMini_rwc + FF(1))); tmp *= scaling_factor; std::get<16>(evals) += tmp; } @@ -221,7 +223,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(17); - auto tmp = (avmMini_tag_err * avmMini_ib); + auto tmp = (avmMini_tag_err * avmMini_ia); tmp *= scaling_factor; std::get<17>(evals) += tmp; } @@ -229,7 +231,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(18); - auto tmp = (avmMini_tag_err * avmMini_ic); + auto tmp = (avmMini_tag_err * avmMini_ib); tmp *= scaling_factor; std::get<18>(evals) += tmp; } @@ -237,7 +239,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(19); - auto tmp = ((avmMini_sel_op_div * (-avmMini_op_err + FF(1))) * ((avmMini_ic * avmMini_ib) - avmMini_ia)); + auto tmp = (avmMini_tag_err * avmMini_ic); tmp *= scaling_factor; std::get<19>(evals) += tmp; } @@ -245,7 +247,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(20); - auto tmp = (avmMini_sel_op_div * (((avmMini_ib * avmMini_inv) - FF(1)) + avmMini_op_err)); + auto tmp = ((avmMini_sel_op_div * (-avmMini_op_err + FF(1))) * ((avmMini_ic * avmMini_ib) - avmMini_ia)); tmp *= scaling_factor; std::get<20>(evals) += tmp; } @@ -253,7 +255,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(21); - auto tmp = ((avmMini_sel_op_div * avmMini_op_err) * (-avmMini_inv + FF(1))); + auto tmp = (avmMini_sel_op_div * (((avmMini_ib * avmMini_inv) - FF(1)) + avmMini_op_err)); tmp *= scaling_factor; std::get<21>(evals) += tmp; } @@ -261,7 +263,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(22); - auto tmp = (avmMini_op_err * (avmMini_sel_op_div - FF(1))); + auto tmp = ((avmMini_sel_op_div * avmMini_op_err) * (-avmMini_inv + FF(1))); tmp *= scaling_factor; std::get<22>(evals) += tmp; } @@ -269,7 +271,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(23); - auto tmp = (avmMini_sel_jump * (avmMini_pc_shift - avmMini_ia)); + auto tmp = (avmMini_op_err * (avmMini_sel_op_div - FF(1))); tmp *= scaling_factor; std::get<23>(evals) += tmp; } @@ -277,8 +279,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(24); - auto tmp = (avmMini_sel_internal_call * - (avmMini_internal_return_ptr_shift - (avmMini_internal_return_ptr + FF(1)))); + auto tmp = (avmMini_sel_jump * (avmMini_pc_shift - avmMini_ia)); tmp *= scaling_factor; std::get<24>(evals) += tmp; } @@ -286,7 +287,8 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(25); - auto tmp = (avmMini_sel_internal_call * (avmMini_internal_return_ptr - avmMini_mem_idx_b)); + auto tmp = (avmMini_sel_internal_call * + (avmMini_internal_return_ptr_shift - (avmMini_internal_return_ptr + FF(1)))); tmp *= scaling_factor; std::get<25>(evals) += tmp; } @@ -294,7 +296,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(26); - auto tmp = (avmMini_sel_internal_call * (avmMini_pc_shift - avmMini_ia)); + auto tmp = (avmMini_sel_internal_call * (avmMini_internal_return_ptr - avmMini_mem_idx_b)); tmp *= scaling_factor; std::get<26>(evals) += tmp; } @@ -302,7 +304,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(27); - auto tmp = (avmMini_sel_internal_call * ((avmMini_pc + FF(1)) - avmMini_ib)); + auto tmp = (avmMini_sel_internal_call * (avmMini_pc_shift - avmMini_ia)); tmp *= scaling_factor; std::get<27>(evals) += tmp; } @@ -310,7 +312,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(28); - auto tmp = (avmMini_sel_internal_call * (avmMini_rwb - FF(1))); + auto tmp = (avmMini_sel_internal_call * ((avmMini_pc + FF(1)) - avmMini_ib)); tmp *= scaling_factor; std::get<28>(evals) += tmp; } @@ -318,7 +320,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(29); - auto tmp = (avmMini_sel_internal_call * (avmMini_mem_op_b - FF(1))); + auto tmp = (avmMini_sel_internal_call * (avmMini_rwb - FF(1))); tmp *= scaling_factor; std::get<29>(evals) += tmp; } @@ -326,8 +328,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(30); - auto tmp = (avmMini_sel_internal_return * - (avmMini_internal_return_ptr_shift - (avmMini_internal_return_ptr - FF(1)))); + auto tmp = (avmMini_sel_internal_call * (avmMini_mem_op_b - FF(1))); tmp *= scaling_factor; std::get<30>(evals) += tmp; } @@ -335,7 +336,8 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(31); - auto tmp = (avmMini_sel_internal_return * ((avmMini_internal_return_ptr - FF(1)) - avmMini_mem_idx_a)); + auto tmp = (avmMini_sel_internal_return * + (avmMini_internal_return_ptr_shift - (avmMini_internal_return_ptr - FF(1)))); tmp *= scaling_factor; std::get<31>(evals) += tmp; } @@ -343,7 +345,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(32); - auto tmp = (avmMini_sel_internal_return * (avmMini_pc_shift - avmMini_ia)); + auto tmp = (avmMini_sel_internal_return * ((avmMini_internal_return_ptr - FF(1)) - avmMini_mem_idx_a)); tmp *= scaling_factor; std::get<32>(evals) += tmp; } @@ -351,7 +353,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(33); - auto tmp = (avmMini_sel_internal_return * avmMini_rwa); + auto tmp = (avmMini_sel_internal_return * (avmMini_pc_shift - avmMini_ia)); tmp *= scaling_factor; std::get<33>(evals) += tmp; } @@ -359,7 +361,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(34); - auto tmp = (avmMini_sel_internal_return * (avmMini_mem_op_a - FF(1))); + auto tmp = (avmMini_sel_internal_return * avmMini_rwa); tmp *= scaling_factor; std::get<34>(evals) += tmp; } @@ -367,9 +369,7 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(35); - auto tmp = ((((-avmMini_first + FF(1)) * (-avmMini_sel_halt + FF(1))) * - (((avmMini_sel_op_add + avmMini_sel_op_sub) + avmMini_sel_op_div) + avmMini_sel_op_mul)) * - (avmMini_pc_shift - (avmMini_pc + FF(1)))); + auto tmp = (avmMini_sel_internal_return * (avmMini_mem_op_a - FF(1))); tmp *= scaling_factor; std::get<35>(evals) += tmp; } @@ -377,12 +377,23 @@ template class avm_miniImpl { { AvmMini_DECLARE_VIEWS(36); + auto tmp = ((((-avmMini_first + FF(1)) * (-avmMini_sel_halt + FF(1))) * + ((((avmMini_sel_op_add + avmMini_sel_op_sub) + avmMini_sel_op_div) + avmMini_sel_op_mul) + + avmMini_sel_op_not)) * + (avmMini_pc_shift - (avmMini_pc + FF(1)))); + tmp *= scaling_factor; + std::get<36>(evals) += tmp; + } + // Contribution 37 + { + AvmMini_DECLARE_VIEWS(37); + auto tmp = ((-(((avmMini_first + avmMini_sel_internal_call) + avmMini_sel_internal_return) + avmMini_sel_halt) + FF(1)) * (avmMini_internal_return_ptr_shift - avmMini_internal_return_ptr)); tmp *= scaling_factor; - std::get<36>(evals) += tmp; + std::get<37>(evals) += tmp; } } }; diff --git a/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/declare_views.hpp b/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/declare_views.hpp index 1b036e25919..00bea658bd9 100644 --- a/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/declare_views.hpp +++ b/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/declare_views.hpp @@ -23,6 +23,7 @@ [[maybe_unused]] auto aluChip_alu_op_sub = View(new_term.aluChip_alu_op_sub); \ [[maybe_unused]] auto aluChip_alu_op_mul = View(new_term.aluChip_alu_op_mul); \ [[maybe_unused]] auto aluChip_alu_op_div = View(new_term.aluChip_alu_op_div); \ + [[maybe_unused]] auto aluChip_alu_op_not = View(new_term.aluChip_alu_op_not); \ [[maybe_unused]] auto aluChip_alu_ff_tag = View(new_term.aluChip_alu_ff_tag); \ [[maybe_unused]] auto aluChip_alu_u8_tag = View(new_term.aluChip_alu_u8_tag); \ [[maybe_unused]] auto aluChip_alu_u16_tag = View(new_term.aluChip_alu_u16_tag); \ @@ -51,6 +52,7 @@ [[maybe_unused]] auto avmMini_sel_op_sub = View(new_term.avmMini_sel_op_sub); \ [[maybe_unused]] auto avmMini_sel_op_mul = View(new_term.avmMini_sel_op_mul); \ [[maybe_unused]] auto avmMini_sel_op_div = View(new_term.avmMini_sel_op_div); \ + [[maybe_unused]] auto avmMini_sel_op_not = View(new_term.avmMini_sel_op_not); \ [[maybe_unused]] auto avmMini_in_tag = View(new_term.avmMini_in_tag); \ [[maybe_unused]] auto avmMini_op_err = View(new_term.avmMini_op_err); \ [[maybe_unused]] auto avmMini_tag_err = View(new_term.avmMini_tag_err); \ @@ -68,17 +70,17 @@ [[maybe_unused]] auto avmMini_mem_idx_b = View(new_term.avmMini_mem_idx_b); \ [[maybe_unused]] auto avmMini_mem_idx_c = View(new_term.avmMini_mem_idx_c); \ [[maybe_unused]] auto avmMini_last = View(new_term.avmMini_last); \ + [[maybe_unused]] auto memTrace_m_rw_shift = View(new_term.memTrace_m_rw_shift); \ + [[maybe_unused]] auto memTrace_m_val_shift = View(new_term.memTrace_m_val_shift); \ + [[maybe_unused]] auto memTrace_m_addr_shift = View(new_term.memTrace_m_addr_shift); \ + [[maybe_unused]] auto memTrace_m_tag_shift = View(new_term.memTrace_m_tag_shift); \ [[maybe_unused]] auto aluChip_alu_u16_r1_shift = View(new_term.aluChip_alu_u16_r1_shift); \ - [[maybe_unused]] auto aluChip_alu_u16_r0_shift = View(new_term.aluChip_alu_u16_r0_shift); \ [[maybe_unused]] auto aluChip_alu_u16_r4_shift = View(new_term.aluChip_alu_u16_r4_shift); \ [[maybe_unused]] auto aluChip_alu_u16_r7_shift = View(new_term.aluChip_alu_u16_r7_shift); \ + [[maybe_unused]] auto aluChip_alu_u16_r0_shift = View(new_term.aluChip_alu_u16_r0_shift); \ [[maybe_unused]] auto aluChip_alu_u16_r5_shift = View(new_term.aluChip_alu_u16_r5_shift); \ - [[maybe_unused]] auto aluChip_alu_u16_r2_shift = View(new_term.aluChip_alu_u16_r2_shift); \ [[maybe_unused]] auto aluChip_alu_u16_r6_shift = View(new_term.aluChip_alu_u16_r6_shift); \ + [[maybe_unused]] auto aluChip_alu_u16_r2_shift = View(new_term.aluChip_alu_u16_r2_shift); \ [[maybe_unused]] auto aluChip_alu_u16_r3_shift = View(new_term.aluChip_alu_u16_r3_shift); \ - [[maybe_unused]] auto avmMini_pc_shift = View(new_term.avmMini_pc_shift); \ [[maybe_unused]] auto avmMini_internal_return_ptr_shift = View(new_term.avmMini_internal_return_ptr_shift); \ - [[maybe_unused]] auto memTrace_m_tag_shift = View(new_term.memTrace_m_tag_shift); \ - [[maybe_unused]] auto memTrace_m_addr_shift = View(new_term.memTrace_m_addr_shift); \ - [[maybe_unused]] auto memTrace_m_val_shift = View(new_term.memTrace_m_val_shift); \ - [[maybe_unused]] auto memTrace_m_rw_shift = View(new_term.memTrace_m_rw_shift); + [[maybe_unused]] auto avmMini_pc_shift = View(new_term.avmMini_pc_shift); diff --git a/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/mem_trace.hpp b/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/mem_trace.hpp index 49cc8062e82..e5ece879c11 100644 --- a/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/mem_trace.hpp +++ b/barretenberg/cpp/src/barretenberg/relations/generated/AvmMini/mem_trace.hpp @@ -7,41 +7,41 @@ namespace bb::AvmMini_vm { template struct Mem_traceRow { - FF memTrace_m_val{}; - FF memTrace_m_last{}; + FF memTrace_m_rw_shift{}; FF memTrace_m_in_tag{}; - FF memTrace_m_tag{}; - FF memTrace_m_rw{}; - FF memTrace_m_tag_shift{}; - FF memTrace_m_addr_shift{}; - FF memTrace_m_tag_err{}; - FF memTrace_m_one_min_inv{}; + FF memTrace_m_last{}; FF memTrace_m_val_shift{}; FF memTrace_m_lastAccess{}; + FF memTrace_m_addr_shift{}; + FF memTrace_m_tag_err{}; + FF memTrace_m_val{}; + FF memTrace_m_tag{}; FF memTrace_m_addr{}; - FF memTrace_m_rw_shift{}; + FF memTrace_m_one_min_inv{}; + FF memTrace_m_tag_shift{}; + FF memTrace_m_rw{}; }; inline std::string get_relation_label_mem_trace(int index) { switch (index) { + case 4: + return "MEM_LAST_ACCESS_DELIMITER"; + case 8: return "MEM_IN_TAG_CONSISTENCY_1"; case 9: return "MEM_IN_TAG_CONSISTENCY_2"; - case 5: - return "MEM_READ_WRITE_VAL_CONSISTENCY"; - - case 4: - return "MEM_LAST_ACCESS_DELIMITER"; - case 6: return "MEM_READ_WRITE_TAG_CONSISTENCY"; case 7: return "MEM_ZERO_INIT"; + + case 5: + return "MEM_READ_WRITE_VAL_CONSISTENCY"; } return std::to_string(index); } diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_alu_trace.cpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_alu_trace.cpp index 1425fb6bf78..ca8fae2cda1 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_alu_trace.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_alu_trace.cpp @@ -45,7 +45,7 @@ std::vector AvmMiniAluTraceBuilder::final * * @return FF The result of the addition casted in a finite field element */ -FF AvmMiniAluTraceBuilder::add(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t const clk) +FF AvmMiniAluTraceBuilder::op_add(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t const clk) { FF c = 0; bool carry = false; @@ -134,7 +134,7 @@ FF AvmMiniAluTraceBuilder::add(FF const& a, FF const& b, AvmMemoryTag in_tag, ui * * @return FF The result of the subtraction casted in a finite field element */ -FF AvmMiniAluTraceBuilder::sub(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t const clk) +FF AvmMiniAluTraceBuilder::op_sub(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t const clk) { FF c = 0; bool carry = false; @@ -218,7 +218,7 @@ FF AvmMiniAluTraceBuilder::sub(FF const& a, FF const& b, AvmMemoryTag in_tag, ui * * @return FF The result of the multiplication casted in a finite field element */ -FF AvmMiniAluTraceBuilder::mul(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t const clk) +FF AvmMiniAluTraceBuilder::op_mul(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t const clk) { FF c = 0; bool carry = false; @@ -343,5 +343,56 @@ FF AvmMiniAluTraceBuilder::mul(FF const& a, FF const& b, AvmMemoryTag in_tag, ui return c; } +/** + * @brief Build Alu trace and compute the result of a Bitwise Not of type defined by in_tag. + * + * @param a Unary operand of Not + * @param in_tag Instruction tag defining the number of bits on which the addition applies. + * It is assumed that the caller never uses the type u0. + * @param clk Clock referring to the operation in the main trace. + * + * @return FF The result of the not casted in a finite field element + */ +FF AvmMiniAluTraceBuilder::op_not(FF const& a, AvmMemoryTag in_tag, uint32_t const clk) +{ + FF c = 0; + uint128_t a_u128{ a }; + uint128_t c_u128 = ~a_u128; + + switch (in_tag) { + case AvmMemoryTag::U8: + c = FF{ static_cast(c_u128) }; + break; + case AvmMemoryTag::U16: + c = FF{ static_cast(c_u128) }; + break; + case AvmMemoryTag::U32: + c = FF{ static_cast(c_u128) }; + break; + case AvmMemoryTag::U64: + c = FF{ static_cast(c_u128) }; + break; + case AvmMemoryTag::U128: + c = FF{ uint256_t::from_uint128(c_u128) }; + break; + case AvmMemoryTag::FF: // Unsupported as instruction tag {} + case AvmMemoryTag::U0: // Unsupported as instruction tag {} + return FF{ 0 }; + } + + alu_trace.push_back(AvmMiniAluTraceBuilder::AluTraceEntry{ + .alu_clk = clk, + .alu_op_not = true, + .alu_u8_tag = in_tag == AvmMemoryTag::U8, + .alu_u16_tag = in_tag == AvmMemoryTag::U16, + .alu_u32_tag = in_tag == AvmMemoryTag::U32, + .alu_u64_tag = in_tag == AvmMemoryTag::U64, + .alu_u128_tag = in_tag == AvmMemoryTag::U128, + .alu_ia = a, + .alu_ic = c, + }); + + return c; +} } // namespace avm_trace diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_alu_trace.hpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_alu_trace.hpp index cda0263cb8d..d567bf8ab1d 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_alu_trace.hpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_alu_trace.hpp @@ -13,6 +13,7 @@ class AvmMiniAluTraceBuilder { bool alu_op_add = false; bool alu_op_sub = false; bool alu_op_mul = false; + bool alu_op_not = false; bool alu_ff_tag = false; bool alu_u8_tag = false; @@ -39,11 +40,12 @@ class AvmMiniAluTraceBuilder { void reset(); std::vector finalize(); - FF add(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t clk); - FF sub(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t clk); - FF mul(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t clk); + FF op_add(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t clk); + FF op_sub(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t clk); + FF op_mul(FF const& a, FF const& b, AvmMemoryTag in_tag, uint32_t clk); + FF op_not(FF const& a, AvmMemoryTag in_tag, uint32_t clk); private: std::vector alu_trace; }; -} // namespace avm_trace \ No newline at end of file +} // namespace avm_trace diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_common.hpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_common.hpp index 1a8508377e6..c0ddae1a869 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_common.hpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_common.hpp @@ -18,4 +18,4 @@ enum class IntermRegister : uint32_t { IA = 0, IB = 1, IC = 2 }; enum class AvmMemoryTag : uint32_t { U0 = 0, U8 = 1, U16 = 2, U32 = 3, U64 = 4, U128 = 5, FF = 6 }; static const uint32_t MAX_MEM_TAG = 6; -} // namespace avm_trace \ No newline at end of file +} // namespace avm_trace diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_deserialization.cpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_deserialization.cpp index dace5225ecf..5c5d60118bb 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_deserialization.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_deserialization.cpp @@ -29,6 +29,8 @@ const std::unordered_map> OPCODE_WIRE_FORMAT = { OpCode::SUB, three_operand_format }, { OpCode::MUL, three_operand_format }, { OpCode::DIV, three_operand_format }, + // Compute - Bitwise + { OpCode::NOT, { OperandType::TAG, OperandType::UINT32, OperandType::UINT32 } }, // Execution Environment - Calldata { OpCode::CALLDATACOPY, { OperandType::UINT32, OperandType::UINT32, OperandType::UINT32 } }, // Machine State - Internal Control Flow @@ -173,4 +175,4 @@ std::vector Deserialization::parse(std::vector const& byte } return instructions; }; -} // namespace avm_trace \ No newline at end of file +} // namespace avm_trace diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_execution.cpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_execution.cpp index 0aae8948100..1bf1056301a 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_execution.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_execution.cpp @@ -62,28 +62,34 @@ std::vector Execution::gen_trace(std::vector const& instructio // Compute // Compute - Arithmetic case OpCode::ADD: - trace_builder.add(std::get(inst.operands.at(1)), - std::get(inst.operands.at(2)), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(0))); + trace_builder.op_add(std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), + std::get(inst.operands.at(3)), + std::get(inst.operands.at(0))); break; case OpCode::SUB: - trace_builder.sub(std::get(inst.operands.at(1)), - std::get(inst.operands.at(2)), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(0))); + trace_builder.op_sub(std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), + std::get(inst.operands.at(3)), + std::get(inst.operands.at(0))); break; case OpCode::MUL: - trace_builder.mul(std::get(inst.operands.at(1)), - std::get(inst.operands.at(2)), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(0))); + trace_builder.op_mul(std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), + std::get(inst.operands.at(3)), + std::get(inst.operands.at(0))); break; case OpCode::DIV: - trace_builder.div(std::get(inst.operands.at(1)), - std::get(inst.operands.at(2)), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(0))); + trace_builder.op_div(std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), + std::get(inst.operands.at(3)), + std::get(inst.operands.at(0))); + break; + // Compute - Bitwise + case OpCode::NOT: + trace_builder.op_not(std::get(inst.operands.at(1)), + std::get(inst.operands.at(3)), + std::get(inst.operands.at(0))); break; // Execution Environment - Calldata case OpCode::CALLDATACOPY: @@ -143,4 +149,4 @@ std::vector Execution::gen_trace(std::vector const& instructio return trace_builder.finalize(); } -} // namespace avm_trace \ No newline at end of file +} // namespace avm_trace diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_helper.cpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_helper.cpp index 9e1fd096c89..2e084f6f3b8 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_helper.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_helper.cpp @@ -71,4 +71,4 @@ void log_avmMini_trace(std::vector const& trace, size_t beg, size_t end) } } -} // namespace avm_trace \ No newline at end of file +} // namespace avm_trace diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_opcode.cpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_opcode.cpp index 01f90c32c3e..d7225e38dca 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_opcode.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_opcode.cpp @@ -21,7 +21,7 @@ const std::unordered_map Bytecode::OPERANDS_NUM = { //{OpCode::AND, }, //{OpCode::OR, }, //{OpCode::XOR, }, - //{OpCode::NOT, }, + // { OpCode::NOT, 2 }, //{OpCode::SHL, }, //{OpCode::SHR, }, //// Compute - Type Conversions @@ -165,4 +165,4 @@ std::string to_hex(OpCode opcode) return stream.str(); } -} // namespace avm_trace \ No newline at end of file +} // namespace avm_trace diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_trace.cpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_trace.cpp index 85f9906533f..944183a8826 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_trace.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_trace.cpp @@ -40,7 +40,7 @@ void AvmMiniTraceBuilder::reset() * @param dst_offset An index in memory pointing to the output of the addition. * @param in_tag The instruction memory tag of the operands. */ -void AvmMiniTraceBuilder::add(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag) +void AvmMiniTraceBuilder::op_add(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag) { auto clk = static_cast(main_trace.size()); @@ -52,7 +52,7 @@ void AvmMiniTraceBuilder::add(uint32_t a_offset, uint32_t b_offset, uint32_t dst // a + b = c FF a = tag_match ? read_a.val : FF(0); FF b = tag_match ? read_b.val : FF(0); - FF c = alu_trace_builder.add(a, b, in_tag, clk); + FF c = alu_trace_builder.op_add(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); @@ -85,7 +85,7 @@ void AvmMiniTraceBuilder::add(uint32_t a_offset, uint32_t b_offset, uint32_t dst * @param dst_offset An index in memory pointing to the output of the subtraction. * @param in_tag The instruction memory tag of the operands. */ -void AvmMiniTraceBuilder::sub(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag) +void AvmMiniTraceBuilder::op_sub(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag) { auto clk = static_cast(main_trace.size()); @@ -97,7 +97,7 @@ void AvmMiniTraceBuilder::sub(uint32_t a_offset, uint32_t b_offset, uint32_t dst // a - b = c FF a = tag_match ? read_a.val : FF(0); FF b = tag_match ? read_b.val : FF(0); - FF c = alu_trace_builder.sub(a, b, in_tag, clk); + FF c = alu_trace_builder.op_sub(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); @@ -130,7 +130,7 @@ void AvmMiniTraceBuilder::sub(uint32_t a_offset, uint32_t b_offset, uint32_t dst * @param dst_offset An index in memory pointing to the output of the multiplication. * @param in_tag The instruction memory tag of the operands. */ -void AvmMiniTraceBuilder::mul(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag) +void AvmMiniTraceBuilder::op_mul(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag) { auto clk = static_cast(main_trace.size()); @@ -142,7 +142,7 @@ void AvmMiniTraceBuilder::mul(uint32_t a_offset, uint32_t b_offset, uint32_t dst // a * b = c FF a = tag_match ? read_a.val : FF(0); FF b = tag_match ? read_b.val : FF(0); - FF c = alu_trace_builder.mul(a, b, in_tag, clk); + FF c = alu_trace_builder.op_mul(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); @@ -175,7 +175,7 @@ void AvmMiniTraceBuilder::mul(uint32_t a_offset, uint32_t b_offset, uint32_t dst * @param dst_offset An index in memory pointing to the output of the division. * @param in_tag The instruction memory tag of the operands. */ -void AvmMiniTraceBuilder::div(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag) +void AvmMiniTraceBuilder::op_div(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag) { auto clk = static_cast(main_trace.size()); @@ -643,6 +643,7 @@ std::vector AvmMiniTraceBuilder::finalize() dest.aluChip_alu_op_add = FF(static_cast(src.alu_op_add)); dest.aluChip_alu_op_sub = FF(static_cast(src.alu_op_sub)); dest.aluChip_alu_op_mul = FF(static_cast(src.alu_op_mul)); + dest.aluChip_alu_op_not = FF(static_cast(src.alu_op_not)); dest.aluChip_alu_ff_tag = FF(static_cast(src.alu_ff_tag)); dest.aluChip_alu_u8_tag = FF(static_cast(src.alu_u8_tag)); @@ -682,4 +683,40 @@ std::vector AvmMiniTraceBuilder::finalize() return trace; } -} // namespace avm_trace \ No newline at end of file +/** + * @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 AvmMiniTraceBuilder::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 c = alu_trace_builder.op_not(read_a.val, 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{ + .avmMini_clk = clk, + .avmMini_pc = FF(pc++), + .avmMini_internal_return_ptr = FF(internal_return_ptr), + .avmMini_sel_op_not = FF(1), + .avmMini_in_tag = FF(static_cast(in_tag)), + .avmMini_ia = read_a.val, + .avmMini_ic = c, + .avmMini_mem_op_a = FF(1), + .avmMini_mem_op_c = FF(1), + .avmMini_rwc = FF(1), + .avmMini_mem_idx_a = FF(a_offset), + .avmMini_mem_idx_c = FF(dst_offset), + }); +}; + +} // namespace avm_trace diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_trace.hpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_trace.hpp index 381f8f1586b..b700caf7ac2 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_trace.hpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_trace.hpp @@ -29,16 +29,19 @@ class AvmMiniTraceBuilder { uint32_t getPc() const { return pc; } // Addition with direct memory access. - void add(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag); + void op_add(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag); // Subtraction with direct memory access. - void sub(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag); + void op_sub(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag); // Multiplication with direct memory access. - void mul(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag); + void op_mul(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag); // Division with direct memory access. - void div(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag); + void op_div(uint32_t a_offset, uint32_t b_offset, uint32_t dst_offset, AvmMemoryTag in_tag); + + // Bitwise not with direct memory access. + void op_not(uint32_t a_offset, uint32_t dst_offset, AvmMemoryTag in_tag); // Set a constant from bytecode with direct memory access. void set(uint128_t val, uint32_t dst_offset, AvmMemoryTag in_tag); diff --git a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.cpp b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.cpp index 9e79beae75f..420a06956fb 100644 --- a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.cpp @@ -73,6 +73,7 @@ void AvmMiniProver::execute_relation_check_rounds() using Sumcheck = SumcheckProver; auto sumcheck = Sumcheck(key->circuit_size, transcript); + FF alpha = transcript->template get_challenge("Sumcheck:alpha"); std::vector gate_challenges(numeric::get_msb(key->circuit_size)); diff --git a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.hpp b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.hpp index 726ea473bf2..c1aec1a2f9a 100644 --- a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.hpp +++ b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_prover.hpp @@ -3,7 +3,7 @@ #pragma once #include "barretenberg/commitment_schemes/zeromorph/zeromorph.hpp" #include "barretenberg/flavor/generated/AvmMini_flavor.hpp" -#include "barretenberg/honk/proof_system/types/proof.hpp" +#include "barretenberg/plonk/proof_system/types/proof.hpp" #include "barretenberg/relations/relation_parameters.hpp" #include "barretenberg/sumcheck/sumcheck_output.hpp" #include "barretenberg/transcript/transcript.hpp" diff --git a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_verifier.cpp b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_verifier.cpp index e73881458ce..34ad124fa92 100644 --- a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_verifier.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_verifier.cpp @@ -82,6 +82,8 @@ bool AvmMiniVerifier::verify_proof(const HonkProof& proof) transcript->template receive_from_prover(commitment_labels.aluChip_alu_op_mul); commitments.aluChip_alu_op_div = transcript->template receive_from_prover(commitment_labels.aluChip_alu_op_div); + commitments.aluChip_alu_op_not = + transcript->template receive_from_prover(commitment_labels.aluChip_alu_op_not); commitments.aluChip_alu_ff_tag = transcript->template receive_from_prover(commitment_labels.aluChip_alu_ff_tag); commitments.aluChip_alu_u8_tag = @@ -136,6 +138,8 @@ bool AvmMiniVerifier::verify_proof(const HonkProof& proof) transcript->template receive_from_prover(commitment_labels.avmMini_sel_op_mul); commitments.avmMini_sel_op_div = transcript->template receive_from_prover(commitment_labels.avmMini_sel_op_div); + commitments.avmMini_sel_op_not = + transcript->template receive_from_prover(commitment_labels.avmMini_sel_op_not); commitments.avmMini_in_tag = transcript->template receive_from_prover(commitment_labels.avmMini_in_tag); commitments.avmMini_op_err = transcript->template receive_from_prover(commitment_labels.avmMini_op_err); commitments.avmMini_tag_err = @@ -164,7 +168,9 @@ bool AvmMiniVerifier::verify_proof(const HonkProof& proof) // Execute Sumcheck Verifier const size_t log_circuit_size = numeric::get_msb(circuit_size); auto sumcheck = SumcheckVerifier(log_circuit_size, transcript); + FF alpha = transcript->template get_challenge("Sumcheck:alpha"); + auto gate_challenges = std::vector(log_circuit_size); for (size_t idx = 0; idx < log_circuit_size; idx++) { gate_challenges[idx] = transcript->template get_challenge("Sumcheck:gate_challenge_" + std::to_string(idx)); diff --git a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_verifier.hpp b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_verifier.hpp index 152950496c0..8250b37c64e 100644 --- a/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_verifier.hpp +++ b/barretenberg/cpp/src/barretenberg/vm/generated/AvmMini_verifier.hpp @@ -2,7 +2,7 @@ #pragma once #include "barretenberg/flavor/generated/AvmMini_flavor.hpp" -#include "barretenberg/honk/proof_system/types/proof.hpp" +#include "barretenberg/plonk/proof_system/types/proof.hpp" #include "barretenberg/sumcheck/sumcheck.hpp" namespace bb { diff --git a/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_arithmetic.test.cpp b/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_arithmetic.test.cpp index c486d6af888..a0d821c933f 100644 --- a/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_arithmetic.test.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_arithmetic.test.cpp @@ -137,7 +137,7 @@ std::vector gen_mutated_trace_add(FF const& a, FF const& b, FF const& c_mut auto trace_builder = avm_trace::AvmMiniTraceBuilder(); trace_builder.set(uint128_t{ a }, 0, tag); trace_builder.set(uint128_t{ b }, 1, tag); - trace_builder.add(0, 1, 2, tag); + trace_builder.op_add(0, 1, 2, tag); trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -155,7 +155,7 @@ std::vector gen_mutated_trace_sub(FF const& a, FF const& b, FF const& c_mut auto trace_builder = avm_trace::AvmMiniTraceBuilder(); trace_builder.set(uint128_t{ a }, 0, tag); trace_builder.set(uint128_t{ b }, 1, tag); - trace_builder.sub(0, 1, 2, tag); + trace_builder.op_sub(0, 1, 2, tag); trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -173,7 +173,7 @@ std::vector gen_mutated_trace_mul(FF const& a, FF const& b, FF const& c_mut auto trace_builder = avm_trace::AvmMiniTraceBuilder(); trace_builder.set(uint128_t{ a }, 0, tag); trace_builder.set(uint128_t{ b }, 1, tag); - trace_builder.mul(0, 1, 2, tag); + trace_builder.op_mul(0, 1, 2, tag); trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -250,7 +250,7 @@ TEST_F(AvmMiniArithmeticTestsFF, addition) trace_builder.calldata_copy(0, 3, 0, std::vector{ 37, 4, 11 }); // Memory layout: [37,4,11,0,0,0,....] - trace_builder.add(0, 1, 4, AvmMemoryTag::FF); // [37,4,11,0,41,0,....] + trace_builder.op_add(0, 1, 4, AvmMemoryTag::FF); // [37,4,11,0,41,0,....] trace_builder.return_op(0, 5); auto trace = trace_builder.finalize(); @@ -269,7 +269,7 @@ TEST_F(AvmMiniArithmeticTestsFF, subtraction) trace_builder.calldata_copy(0, 3, 0, std::vector{ 8, 4, 17 }); // Memory layout: [8,4,17,0,0,0,....] - trace_builder.sub(2, 0, 1, AvmMemoryTag::FF); // [8,9,17,0,0,0....] + trace_builder.op_sub(2, 0, 1, AvmMemoryTag::FF); // [8,9,17,0,0,0....] trace_builder.return_op(0, 3); auto trace = trace_builder.finalize(); @@ -288,7 +288,7 @@ TEST_F(AvmMiniArithmeticTestsFF, multiplication) trace_builder.calldata_copy(0, 3, 0, std::vector{ 5, 0, 20 }); // Memory layout: [5,0,20,0,0,0,....] - trace_builder.mul(2, 0, 1, AvmMemoryTag::FF); // [5,100,20,0,0,0....] + trace_builder.op_mul(2, 0, 1, AvmMemoryTag::FF); // [5,100,20,0,0,0....] trace_builder.return_op(0, 3); auto trace = trace_builder.finalize(); @@ -308,7 +308,7 @@ TEST_F(AvmMiniArithmeticTestsFF, multiplicationByZero) trace_builder.calldata_copy(0, 1, 0, std::vector{ 127 }); // Memory layout: [127,0,0,0,0,0,....] - trace_builder.mul(0, 1, 2, AvmMemoryTag::FF); // [127,0,0,0,0,0....] + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::FF); // [127,0,0,0,0,0....] trace_builder.return_op(0, 3); auto trace = trace_builder.finalize(); @@ -328,7 +328,7 @@ TEST_F(AvmMiniArithmeticTestsFF, division) trace_builder.calldata_copy(0, 2, 0, std::vector{ 15, 315 }); // Memory layout: [15,315,0,0,0,0,....] - trace_builder.div(1, 0, 2, AvmMemoryTag::FF); // [15,315,21,0,0,0....] + trace_builder.op_div(1, 0, 2, AvmMemoryTag::FF); // [15,315,21,0,0,0....] trace_builder.return_op(0, 3); auto trace = trace_builder.finalize(); @@ -351,7 +351,7 @@ TEST_F(AvmMiniArithmeticTestsFF, divisionNumeratorZero) trace_builder.calldata_copy(0, 1, 0, std::vector{ 15 }); // Memory layout: [15,0,0,0,0,0,....] - trace_builder.div(1, 0, 0, AvmMemoryTag::FF); // [0,0,0,0,0,0....] + trace_builder.op_div(1, 0, 0, AvmMemoryTag::FF); // [0,0,0,0,0,0....] trace_builder.return_op(0, 3); auto trace = trace_builder.finalize(); @@ -375,7 +375,7 @@ TEST_F(AvmMiniArithmeticTestsFF, divisionByZeroError) trace_builder.calldata_copy(0, 1, 0, std::vector{ 15 }); // Memory layout: [15,0,0,0,0,0,....] - trace_builder.div(0, 1, 2, AvmMemoryTag::FF); // [15,0,0,0,0,0....] + trace_builder.op_div(0, 1, 2, AvmMemoryTag::FF); // [15,0,0,0,0,0....] trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -398,7 +398,7 @@ TEST_F(AvmMiniArithmeticTestsFF, divisionByZeroError) TEST_F(AvmMiniArithmeticTestsFF, divisionZeroByZeroError) { // Memory layout: [0,0,0,0,0,0,....] - trace_builder.div(0, 1, 2, AvmMemoryTag::FF); // [0,0,0,0,0,0....] + trace_builder.op_div(0, 1, 2, AvmMemoryTag::FF); // [0,0,0,0,0,0....] trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -425,15 +425,15 @@ TEST_F(AvmMiniArithmeticTestsFF, mixedOperationsWithError) trace_builder.calldata_copy(0, 3, 2, std::vector{ 45, 23, 12 }); // Memory layout: [0,0,45,23,12,0,0,0,....] - trace_builder.add(2, 3, 4, AvmMemoryTag::FF); // [0,0,45,23,68,0,0,0,....] - trace_builder.add(4, 5, 5, AvmMemoryTag::FF); // [0,0,45,23,68,68,0,0,....] - trace_builder.add(5, 5, 5, AvmMemoryTag::FF); // [0,0,45,23,68,136,0,0,....] - trace_builder.add(5, 6, 7, AvmMemoryTag::FF); // [0,0,45,23,68,136,0,136,0....] - trace_builder.sub(7, 6, 8, AvmMemoryTag::FF); // [0,0,45,23,68,136,0,136,136,0....] - trace_builder.mul(8, 8, 8, AvmMemoryTag::FF); // [0,0,45,23,68,136,0,136,136^2,0....] - trace_builder.div(3, 5, 1, AvmMemoryTag::FF); // [0,23*136^(-1),45,23,68,136,0,136,136^2,0....] - trace_builder.div(1, 1, 9, AvmMemoryTag::FF); // [0,23*136^(-1),45,23,68,136,0,136,136^2,1,0....] - trace_builder.div( + trace_builder.op_add(2, 3, 4, AvmMemoryTag::FF); // [0,0,45,23,68,0,0,0,....] + trace_builder.op_add(4, 5, 5, AvmMemoryTag::FF); // [0,0,45,23,68,68,0,0,....] + trace_builder.op_add(5, 5, 5, AvmMemoryTag::FF); // [0,0,45,23,68,136,0,0,....] + trace_builder.op_add(5, 6, 7, AvmMemoryTag::FF); // [0,0,45,23,68,136,0,136,0....] + trace_builder.op_sub(7, 6, 8, AvmMemoryTag::FF); // [0,0,45,23,68,136,0,136,136,0....] + trace_builder.op_mul(8, 8, 8, AvmMemoryTag::FF); // [0,0,45,23,68,136,0,136,136^2,0....] + trace_builder.op_div(3, 5, 1, AvmMemoryTag::FF); // [0,23*136^(-1),45,23,68,136,0,136,136^2,0....] + trace_builder.op_div(1, 1, 9, AvmMemoryTag::FF); // [0,23*136^(-1),45,23,68,136,0,136,136^2,1,0....] + trace_builder.op_div( 9, 0, 4, AvmMemoryTag::FF); // [0,23*136^(-1),45,23,1/0,136,0,136,136^2,1,0....] Error: division by 0 trace_builder.halt(); @@ -453,7 +453,7 @@ TEST_F(AvmMiniArithmeticTestsU8, addition) trace_builder.set(29, 1, AvmMemoryTag::U8); // Memory layout: [62,29,0,0,0,....] - trace_builder.add(0, 1, 2, AvmMemoryTag::U8); // [62,29,91,0,0,....] + trace_builder.op_add(0, 1, 2, AvmMemoryTag::U8); // [62,29,91,0,0,....] trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -473,7 +473,7 @@ TEST_F(AvmMiniArithmeticTestsU8, additionCarry) trace_builder.set(100, 1, AvmMemoryTag::U8); // Memory layout: [159,100,0,0,0,....] - trace_builder.add(0, 1, 2, AvmMemoryTag::U8); // [159,100,3,0,0,....] + trace_builder.op_add(0, 1, 2, AvmMemoryTag::U8); // [159,100,3,0,0,....] trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -495,7 +495,7 @@ TEST_F(AvmMiniArithmeticTestsU8, subtraction) trace_builder.set(29, 1, AvmMemoryTag::U8); // Memory layout: [162,29,0,0,0,....] - trace_builder.sub(0, 1, 2, AvmMemoryTag::U8); // [162,29,133,0,0,....] + trace_builder.op_sub(0, 1, 2, AvmMemoryTag::U8); // [162,29,133,0,0,....] trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -517,7 +517,7 @@ TEST_F(AvmMiniArithmeticTestsU8, subtractionCarry) trace_builder.set(29, 1, AvmMemoryTag::U8); // Memory layout: [5,29,0,0,0,....] - trace_builder.sub(0, 1, 2, AvmMemoryTag::U8); // [5,29,232,0,0,....] + trace_builder.op_sub(0, 1, 2, AvmMemoryTag::U8); // [5,29,232,0,0,....] trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -545,7 +545,7 @@ TEST_F(AvmMiniArithmeticTestsU8, multiplication) trace_builder.set(13, 0, AvmMemoryTag::U8); trace_builder.set(15, 1, AvmMemoryTag::U8); - trace_builder.mul(0, 1, 2, AvmMemoryTag::U8); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U8); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -568,7 +568,7 @@ TEST_F(AvmMiniArithmeticTestsU8, multiplicationOverflow) trace_builder.set(200, 0, AvmMemoryTag::U8); trace_builder.set(170, 1, AvmMemoryTag::U8); - trace_builder.mul(0, 1, 2, AvmMemoryTag::U8); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U8); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -596,7 +596,7 @@ TEST_F(AvmMiniArithmeticTestsU16, addition) trace_builder.set(1775, 119, AvmMemoryTag::U16); trace_builder.set(33005, 546, AvmMemoryTag::U16); - trace_builder.add(546, 119, 5, AvmMemoryTag::U16); + trace_builder.op_add(546, 119, 5, AvmMemoryTag::U16); trace_builder.return_op(5, 1); auto trace = trace_builder.finalize(); @@ -618,7 +618,7 @@ TEST_F(AvmMiniArithmeticTestsU16, additionCarry) trace_builder.set(UINT16_MAX - 982, 0, AvmMemoryTag::U16); trace_builder.set(1000, 1, AvmMemoryTag::U16); - trace_builder.add(1, 0, 0, AvmMemoryTag::U16); + trace_builder.op_add(1, 0, 0, AvmMemoryTag::U16); trace_builder.return_op(0, 1); auto trace = trace_builder.finalize(); @@ -640,7 +640,7 @@ TEST_F(AvmMiniArithmeticTestsU16, subtraction) trace_builder.set(1775, 119, AvmMemoryTag::U16); trace_builder.set(33005, 546, AvmMemoryTag::U16); - trace_builder.sub(546, 119, 5, AvmMemoryTag::U16); + trace_builder.op_sub(546, 119, 5, AvmMemoryTag::U16); trace_builder.return_op(5, 1); auto trace = trace_builder.finalize(); @@ -664,7 +664,7 @@ TEST_F(AvmMiniArithmeticTestsU16, subtractionCarry) trace_builder.set(UINT16_MAX - 982, 0, AvmMemoryTag::U16); trace_builder.set(1000, 1, AvmMemoryTag::U16); - trace_builder.sub(1, 0, 0, AvmMemoryTag::U16); + trace_builder.op_sub(1, 0, 0, AvmMemoryTag::U16); trace_builder.return_op(0, 1); auto trace = trace_builder.finalize(); @@ -693,7 +693,7 @@ TEST_F(AvmMiniArithmeticTestsU16, multiplication) trace_builder.set(200, 0, AvmMemoryTag::U16); trace_builder.set(245, 1, AvmMemoryTag::U16); - trace_builder.mul(0, 1, 2, AvmMemoryTag::U16); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U16); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -718,7 +718,7 @@ TEST_F(AvmMiniArithmeticTestsU16, multiplicationOverflow) trace_builder.set(512, 0, AvmMemoryTag::U16); trace_builder.set(1024, 1, AvmMemoryTag::U16); - trace_builder.mul(0, 1, 2, AvmMemoryTag::U16); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U16); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -748,7 +748,7 @@ TEST_F(AvmMiniArithmeticTestsU32, addition) trace_builder.set(1000000000, 8, AvmMemoryTag::U32); trace_builder.set(1234567891, 9, AvmMemoryTag::U32); - trace_builder.add(8, 9, 0, AvmMemoryTag::U32); + trace_builder.op_add(8, 9, 0, AvmMemoryTag::U32); trace_builder.return_op(0, 1); auto trace = trace_builder.finalize(); @@ -771,7 +771,7 @@ TEST_F(AvmMiniArithmeticTestsU32, additionCarry) trace_builder.set(UINT32_MAX - 1293, 8, AvmMemoryTag::U32); trace_builder.set(2293, 9, AvmMemoryTag::U32); - trace_builder.add(8, 9, 0, AvmMemoryTag::U32); + trace_builder.op_add(8, 9, 0, AvmMemoryTag::U32); trace_builder.return_op(0, 1); auto trace = trace_builder.finalize(); @@ -793,7 +793,7 @@ TEST_F(AvmMiniArithmeticTestsU32, subtraction) trace_builder.set(1345678991, 8, AvmMemoryTag::U32); trace_builder.set(1234567891, 9, AvmMemoryTag::U32); - trace_builder.sub(8, 9, 0, AvmMemoryTag::U32); + trace_builder.op_sub(8, 9, 0, AvmMemoryTag::U32); trace_builder.return_op(0, 1); auto trace = trace_builder.finalize(); @@ -820,7 +820,7 @@ TEST_F(AvmMiniArithmeticTestsU32, subtractionCarry) trace_builder.set(UINT32_MAX - 99, 8, AvmMemoryTag::U32); trace_builder.set(3210987654, 9, AvmMemoryTag::U32); - trace_builder.sub(9, 8, 0, AvmMemoryTag::U32); + trace_builder.op_sub(9, 8, 0, AvmMemoryTag::U32); trace_builder.return_op(0, 1); auto trace = trace_builder.finalize(); @@ -851,7 +851,7 @@ TEST_F(AvmMiniArithmeticTestsU32, multiplication) trace_builder.set(11111, 0, AvmMemoryTag::U32); trace_builder.set(11111, 1, AvmMemoryTag::U32); - trace_builder.mul(0, 1, 2, AvmMemoryTag::U32); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U32); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -880,7 +880,7 @@ TEST_F(AvmMiniArithmeticTestsU32, multiplicationOverflow) trace_builder.set(11 << 25, 0, AvmMemoryTag::U32); trace_builder.set(13 << 22, 1, AvmMemoryTag::U32); - trace_builder.mul(0, 1, 2, AvmMemoryTag::U32); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U32); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -917,7 +917,7 @@ TEST_F(AvmMiniArithmeticTestsU64, addition) trace_builder.set(a, 8, AvmMemoryTag::U64); trace_builder.set(b, 9, AvmMemoryTag::U64); - trace_builder.add(8, 9, 9, AvmMemoryTag::U64); + trace_builder.op_add(8, 9, 9, AvmMemoryTag::U64); trace_builder.return_op(9, 1); auto trace = trace_builder.finalize(); @@ -947,7 +947,7 @@ TEST_F(AvmMiniArithmeticTestsU64, additionCarry) trace_builder.set(a, 0, AvmMemoryTag::U64); trace_builder.set(b, 1, AvmMemoryTag::U64); - trace_builder.add(0, 1, 0, AvmMemoryTag::U64); + trace_builder.op_add(0, 1, 0, AvmMemoryTag::U64); trace_builder.return_op(0, 1); auto trace = trace_builder.finalize(); @@ -975,7 +975,7 @@ TEST_F(AvmMiniArithmeticTestsU64, subtraction) trace_builder.set(a, 8, AvmMemoryTag::U64); trace_builder.set(b, 9, AvmMemoryTag::U64); - trace_builder.sub(8, 9, 9, AvmMemoryTag::U64); + trace_builder.op_sub(8, 9, 9, AvmMemoryTag::U64); trace_builder.return_op(9, 1); auto trace = trace_builder.finalize(); @@ -1007,7 +1007,7 @@ TEST_F(AvmMiniArithmeticTestsU64, subtractionCarry) trace_builder.set(a, 0, AvmMemoryTag::U64); trace_builder.set(b, 1, AvmMemoryTag::U64); - trace_builder.sub(0, 1, 0, AvmMemoryTag::U64); + trace_builder.op_sub(0, 1, 0, AvmMemoryTag::U64); trace_builder.return_op(0, 1); auto trace = trace_builder.finalize(); @@ -1035,7 +1035,7 @@ TEST_F(AvmMiniArithmeticTestsU64, multiplication) trace_builder.set(999888777, 0, AvmMemoryTag::U64); trace_builder.set(555444333, 1, AvmMemoryTag::U64); - trace_builder.mul(0, 1, 2, AvmMemoryTag::U64); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U64); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -1068,7 +1068,7 @@ TEST_F(AvmMiniArithmeticTestsU64, multiplicationOverflow) trace_builder.set(a, 0, AvmMemoryTag::U64); trace_builder.set(b, 1, AvmMemoryTag::U64); - trace_builder.mul(0, 1, 2, AvmMemoryTag::U64); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U64); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -1107,7 +1107,7 @@ TEST_F(AvmMiniArithmeticTestsU128, addition) trace_builder.set(a, 8, AvmMemoryTag::U128); trace_builder.set(b, 9, AvmMemoryTag::U128); - trace_builder.add(8, 9, 9, AvmMemoryTag::U128); + trace_builder.op_add(8, 9, 9, AvmMemoryTag::U128); trace_builder.return_op(9, 1); auto trace = trace_builder.finalize(); @@ -1147,7 +1147,7 @@ TEST_F(AvmMiniArithmeticTestsU128, additionCarry) trace_builder.set(a, 8, AvmMemoryTag::U128); trace_builder.set(b, 9, AvmMemoryTag::U128); - trace_builder.add(8, 9, 9, AvmMemoryTag::U128); + trace_builder.op_add(8, 9, 9, AvmMemoryTag::U128); trace_builder.return_op(9, 1); auto trace = trace_builder.finalize(); @@ -1186,7 +1186,7 @@ TEST_F(AvmMiniArithmeticTestsU128, subtraction) trace_builder.set(a, 8, AvmMemoryTag::U128); trace_builder.set(b, 9, AvmMemoryTag::U128); - trace_builder.sub(8, 9, 9, AvmMemoryTag::U128); + trace_builder.op_sub(8, 9, 9, AvmMemoryTag::U128); trace_builder.return_op(9, 1); auto trace = trace_builder.finalize(); @@ -1228,7 +1228,7 @@ TEST_F(AvmMiniArithmeticTestsU128, subtractionCarry) trace_builder.set(a, 8, AvmMemoryTag::U128); trace_builder.set(b, 9, AvmMemoryTag::U128); - trace_builder.sub(8, 9, 9, AvmMemoryTag::U128); + trace_builder.op_sub(8, 9, 9, AvmMemoryTag::U128); trace_builder.return_op(9, 1); auto trace = trace_builder.finalize(); @@ -1266,7 +1266,7 @@ TEST_F(AvmMiniArithmeticTestsU128, multiplication) // Integer multiplication output in HEX: 70289AEB0A7DDA0BAE60CA3A5 FF c{ uint256_t{ 0xA7DDA0BAE60CA3A5, 0x70289AEB0, 0, 0 } }; - trace_builder.mul(0, 1, 2, AvmMemoryTag::U128); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U128); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -1303,7 +1303,7 @@ TEST_F(AvmMiniArithmeticTestsU128, multiplicationOverflow) trace_builder.set(a, 0, AvmMemoryTag::U128); trace_builder.set(b, 1, AvmMemoryTag::U128); - trace_builder.mul(0, 1, 2, AvmMemoryTag::U128); + trace_builder.op_mul(0, 1, 2, AvmMemoryTag::U128); trace_builder.return_op(2, 1); auto trace = trace_builder.finalize(); @@ -1403,7 +1403,7 @@ TEST_F(AvmMiniArithmeticNegativeTestsFF, divisionFF) trace_builder.calldata_copy(0, 2, 0, std::vector{ 15, 315 }); // Memory layout: [15,315,0,0,0,0,....] - trace_builder.div(1, 0, 2, AvmMemoryTag::FF); // [15,315,21,0,0,0....] + trace_builder.op_div(1, 0, 2, AvmMemoryTag::FF); // [15,315,21,0,0,0....] trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -1420,7 +1420,7 @@ TEST_F(AvmMiniArithmeticNegativeTestsFF, divisionNoZeroButError) trace_builder.calldata_copy(0, 2, 0, std::vector{ 15, 315 }); // Memory layout: [15,315,0,0,0,0,....] - trace_builder.div(1, 0, 2, AvmMemoryTag::FF); // [15,315,21,0,0,0....] + trace_builder.op_div(1, 0, 2, AvmMemoryTag::FF); // [15,315,21,0,0,0....] trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -1446,7 +1446,7 @@ TEST_F(AvmMiniArithmeticNegativeTestsFF, divisionByZeroNoError) trace_builder.calldata_copy(0, 1, 0, std::vector{ 15 }); // Memory layout: [15,0,0,0,0,0,....] - trace_builder.div(0, 1, 2, AvmMemoryTag::FF); // [15,0,0,0,0,0....] + trace_builder.op_div(0, 1, 2, AvmMemoryTag::FF); // [15,0,0,0,0,0....] trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -1463,7 +1463,7 @@ TEST_F(AvmMiniArithmeticNegativeTestsFF, divisionByZeroNoError) TEST_F(AvmMiniArithmeticNegativeTestsFF, divisionZeroByZeroNoError) { // Memory layout: [0,0,0,0,0,0,....] - trace_builder.div(0, 1, 2, AvmMemoryTag::FF); // [0,0,0,0,0,0....] + trace_builder.op_div(0, 1, 2, AvmMemoryTag::FF); // [0,0,0,0,0,0....] trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -1483,7 +1483,7 @@ TEST_F(AvmMiniArithmeticNegativeTestsFF, operationWithErrorFlag) trace_builder.calldata_copy(0, 3, 0, std::vector{ 37, 4, 11 }); // Memory layout: [37,4,11,0,0,0,....] - trace_builder.add(0, 1, 4, AvmMemoryTag::FF); // [37,4,11,0,41,0,....] + trace_builder.op_add(0, 1, 4, AvmMemoryTag::FF); // [37,4,11,0,41,0,....] trace_builder.return_op(0, 5); trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -1501,7 +1501,7 @@ TEST_F(AvmMiniArithmeticNegativeTestsFF, operationWithErrorFlag) trace_builder.calldata_copy(0, 3, 0, std::vector{ 8, 4, 17 }); // Memory layout: [8,4,17,0,0,0,....] - trace_builder.sub(2, 0, 1, AvmMemoryTag::FF); // [8,9,17,0,0,0....] + trace_builder.op_sub(2, 0, 1, AvmMemoryTag::FF); // [8,9,17,0,0,0....] trace_builder.return_op(0, 3); trace = trace_builder.finalize(); @@ -1518,7 +1518,7 @@ TEST_F(AvmMiniArithmeticNegativeTestsFF, operationWithErrorFlag) trace_builder.calldata_copy(0, 3, 0, std::vector{ 5, 0, 20 }); // Memory layout: [5,0,20,0,0,0,....] - trace_builder.mul(2, 0, 1, AvmMemoryTag::FF); // [5,100,20,0,0,0....] + trace_builder.op_mul(2, 0, 1, AvmMemoryTag::FF); // [5,100,20,0,0,0....] trace_builder.return_op(0, 3); trace = trace_builder.finalize(); @@ -1680,4 +1680,4 @@ TEST_F(AvmMiniArithmeticNegativeTestsU128, multiplication) EXPECT_THROW_WITH_MESSAGE(validate_trace_proof(std::move(trace)), "ALU_MULTIPLICATION_OUT_U128"); } -} // namespace tests_avm \ No newline at end of file +} // namespace tests_avm diff --git a/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_bitwise.test.cpp b/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_bitwise.test.cpp new file mode 100644 index 00000000000..9292667e0ed --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_bitwise.test.cpp @@ -0,0 +1,255 @@ +#include "AvmMini_common.test.hpp" + +#include "barretenberg/numeric/uint128/uint128.hpp" +#include +#include +#include + +using namespace bb; +using namespace bb::numeric; + +namespace { +using namespace tests_avm; + +Row common_validate_op_not(std::vector const& trace, + FF const& a, + FF const& c, + FF const& addr_a, + FF const& addr_c, + avm_trace::AvmMemoryTag const tag) +{ + + // Find the first row enabling the not selector + auto row = std::ranges::find_if(trace.begin(), trace.end(), [](Row r) { return r.avmMini_sel_op_not == FF(1); }); + + // Use the row in the main trace to find the same operation in the alu trace. + FF clk = row->avmMini_clk; + auto alu_row = std::ranges::find_if(trace.begin(), trace.end(), [clk](Row r) { return r.aluChip_alu_clk == clk; }); + + // Check that both rows were found + EXPECT_TRUE(row != trace.end()); + EXPECT_TRUE(alu_row != trace.end()); + + // Check that the correct result is stored at the expected memory location. + EXPECT_EQ(row->avmMini_ic, c); + EXPECT_EQ(row->avmMini_mem_idx_c, addr_c); + EXPECT_EQ(row->avmMini_mem_op_c, FF(1)); + EXPECT_EQ(row->avmMini_rwc, FF(1)); + + // Check that ia register is correctly set with memory load operations. + EXPECT_EQ(row->avmMini_ia, a); + EXPECT_EQ(row->avmMini_mem_idx_a, addr_a); + EXPECT_EQ(row->avmMini_mem_op_a, FF(1)); + EXPECT_EQ(row->avmMini_rwa, FF(0)); + + // Check the instruction tag + EXPECT_EQ(row->avmMini_in_tag, FF(static_cast(tag))); + + // Check that intermediate registers are correctly copied in Alu trace + EXPECT_EQ(alu_row->aluChip_alu_ia, a); + EXPECT_EQ(alu_row->aluChip_alu_ib, FF(0)); + EXPECT_EQ(alu_row->aluChip_alu_ic, c); + + // Check that not selector is set. + EXPECT_EQ(row->avmMini_sel_op_not, FF(1)); + EXPECT_EQ(alu_row->aluChip_alu_op_not, FF(1)); + + return *alu_row; +} + +std::vector gen_mutated_trace_not(FF const& a, FF const& c_mutated, avm_trace::AvmMemoryTag tag) +{ + auto trace_builder = avm_trace::AvmMiniTraceBuilder(); + trace_builder.set(uint128_t{ a }, 0, tag); + trace_builder.op_not(0, 1, tag); + trace_builder.halt(); + auto trace = trace_builder.finalize(); + + auto select_row = [](Row r) { return r.avmMini_sel_op_not == FF(1); }; + mutate_ic_in_trace(trace, select_row, c_mutated, true); + + return trace; +} +} // namespace + +namespace tests_avm { +using namespace avm_trace; + +class AvmMiniBitwiseTests : public ::testing::Test { + public: + AvmMiniTraceBuilder trace_builder; + + protected: + // TODO(640): The Standard Honk on Grumpkin test suite fails unless the SRS is initialised for every test. + void SetUp() override + { + srs::init_crs_factory("../srs_db/ignition"); + trace_builder = AvmMiniTraceBuilder(); // Clean instance for every run. + }; +}; + +class AvmMiniBitwiseTestsU8 : public AvmMiniBitwiseTests {}; +class AvmMiniBitwiseTestsU16 : public AvmMiniBitwiseTests {}; +class AvmMiniBitwiseTestsU32 : public AvmMiniBitwiseTests {}; +class AvmMiniBitwiseTestsU64 : public AvmMiniBitwiseTests {}; +class AvmMiniBitwiseTestsU128 : public AvmMiniBitwiseTests {}; + +class AvmMiniBitwiseNegativeTestsFF : public AvmMiniBitwiseTests {}; +class AvmMiniBitwiseNegativeTestsU8 : public AvmMiniBitwiseTests {}; +class AvmMiniBitwiseNegativeTestsU16 : public AvmMiniBitwiseTests {}; +class AvmMiniBitwiseNegativeTestsU32 : public AvmMiniBitwiseTests {}; +class AvmMiniBitwiseNegativeTestsU64 : public AvmMiniBitwiseTests {}; +class AvmMiniBitwiseNegativeTestsU128 : public AvmMiniBitwiseTests {}; + +/****************************************************************************** + * + * POSITIVE TESTS + * + ****************************************************************************** + * See AvmMini_arithmetic.cpp for explanation of positive tests + ******************************************************************************/ + +/****************************************************************************** + * Positive Tests - U8 + ******************************************************************************/ + +TEST_F(AvmMiniBitwiseTestsU8, BitwiseNot) +{ + trace_builder.set(1, 0, AvmMemoryTag::U8); // Memory Layout: [1,0,0,...] + trace_builder.op_not(0, 1, AvmMemoryTag::U8); // [1,254,0,0,....] + trace_builder.return_op(1, 1); + auto trace = trace_builder.finalize(); + + auto alu_row = common_validate_op_not(trace, FF(1), FF(254), FF(0), FF(1), AvmMemoryTag::U8); + + EXPECT_EQ(alu_row.aluChip_alu_u8_tag, FF(1)); + validate_trace_proof(std::move(trace)); +} + +TEST_F(AvmMiniBitwiseTestsU16, BitwiseNot) +{ + trace_builder.set(512, 0, AvmMemoryTag::U16); // Memory Layout: [512,0,0,...] + trace_builder.op_not(0, 1, AvmMemoryTag::U16); // [512,65023,0,0,0,....] + trace_builder.return_op(1, 1); + auto trace = trace_builder.finalize(); + + auto alu_row = common_validate_op_not(trace, FF(512), FF(65'023), FF(0), FF(1), AvmMemoryTag::U16); + + EXPECT_EQ(alu_row.aluChip_alu_u16_tag, FF(1)); + validate_trace_proof(std::move(trace)); +} + +TEST_F(AvmMiniBitwiseTestsU32, BitwiseNot) +{ + trace_builder.set(131'072, 0, AvmMemoryTag::U32); // Memory Layout: [131072,0,0,...] + trace_builder.op_not(0, 1, AvmMemoryTag::U32); // [131072,4294836223,,0,0,....] + trace_builder.return_op(1, 1); + auto trace = trace_builder.finalize(); + + auto alu_row = common_validate_op_not(trace, FF(131'072), FF(4'294'836'223LLU), FF(0), FF(1), AvmMemoryTag::U32); + + EXPECT_EQ(alu_row.aluChip_alu_u32_tag, FF(1)); + validate_trace_proof(std::move(trace)); +} + +TEST_F(AvmMiniBitwiseTestsU64, BitwiseNot) +{ + trace_builder.set(0x100000000LLU, 0, AvmMemoryTag::U64); // Memory Layout: [8589934592,0,0,...] + trace_builder.op_not(0, 1, AvmMemoryTag::U64); // [8589934592,18446744069414584319,0,0,....] + trace_builder.return_op(1, 1); + auto trace = trace_builder.finalize(); + + auto alu_row = + common_validate_op_not(trace, FF(0x100000000LLU), FF(0xfffffffeffffffffLLU), FF(0), FF(1), AvmMemoryTag::U64); + + EXPECT_EQ(alu_row.aluChip_alu_u64_tag, FF(1)); + validate_trace_proof(std::move(trace)); +} + +TEST_F(AvmMiniBitwiseTestsU128, BitwiseNot) +{ + + uint128_t const a = uint128_t{ 0x4000000000000 } << 64; + trace_builder.set(a, 0, AvmMemoryTag::U128); + trace_builder.op_not(0, 1, AvmMemoryTag::U128); + trace_builder.return_op(1, 1); + auto trace = trace_builder.finalize(); + + uint128_t const res = (uint128_t{ 0xfffbffffffffffff } << 64) + uint128_t{ 0xffffffffffffffff }; + auto alu_row = common_validate_op_not( + trace, FF(uint256_t::from_uint128(a)), FF(uint256_t::from_uint128(res)), FF(0), FF(1), AvmMemoryTag::U128); + + EXPECT_EQ(alu_row.aluChip_alu_u128_tag, FF(1)); + validate_trace_proof(std::move(trace)); +} + +/****************************************************************************** + * + * NEGATIVE TESTS - Finite Field Type + * + ****************************************************************************** + * See AvmMini_arithmetic.cpp for explanation of negative tests + ******************************************************************************/ + +/****************************************************************************** + * Negative Tests - FF + ******************************************************************************/ + +TEST_F(AvmMiniBitwiseNegativeTestsFF, UndefinedOverFF) +{ + auto trace_builder = avm_trace::AvmMiniTraceBuilder(); + // Triggers a write row 1 of mem_trace and alu_trace + trace_builder.set(10, 0, AvmMemoryTag::U8); + // Triggers a write in row 2 of alu_trace + trace_builder.op_not(0, 1, AvmMemoryTag::U8); + // Finally, we will have a write in row 3 of the mem_trace to copy the result + // from the op_not operation. + // Manually update the memory tags in the relevant trace; + auto trace = trace_builder.finalize(); + // TODO(ilyas): When the SET opcodes applies relational constraints, this will fail + // we will need to look at a new way of doing this test. + for (size_t i = 1; i < 4; i++) { + trace.at(i).memTrace_m_tag = FF(6); + trace.at(i).memTrace_m_in_tag = FF(6); + trace.at(i).aluChip_alu_ff_tag = FF::one(); + trace.at(i).aluChip_alu_u8_tag = FF::zero(); + trace.at(i).avmMini_in_tag = FF(6); + } + + EXPECT_THROW_WITH_MESSAGE(validate_trace_proof(std::move(trace)), "ALU_FF_NOT_XOR"); +} + +TEST_F(AvmMiniBitwiseNegativeTestsU8, BitwiseNot) +{ + std::vector trace = gen_mutated_trace_not(FF{ 1 }, FF{ 2 }, AvmMemoryTag::U8); + EXPECT_THROW_WITH_MESSAGE(validate_trace_proof(std::move(trace)), "ALU_OP_NOT"); +} + +TEST_F(AvmMiniBitwiseNegativeTestsU16, BitwiseNot) +{ + std::vector trace = gen_mutated_trace_not(FF{ 32'768 }, FF{ 8'192 }, AvmMemoryTag::U16); + EXPECT_THROW_WITH_MESSAGE(validate_trace_proof(std::move(trace)), "ALU_OP_NOT"); +} + +TEST_F(AvmMiniBitwiseNegativeTestsU32, BitwiseNot) +{ + std::vector trace = gen_mutated_trace_not(FF{ 0xdeadbeef }, FF{ 0x20020af }, AvmMemoryTag::U64); + EXPECT_THROW_WITH_MESSAGE(validate_trace_proof(std::move(trace)), "ALU_OP_NOT"); +} + +TEST_F(AvmMiniBitwiseNegativeTestsU64, BitwiseNot) +{ + std::vector trace = + gen_mutated_trace_not(FF{ 0x10000000000000LLU }, FF{ 0x10000fed0100000LLU }, AvmMemoryTag::U64); + EXPECT_THROW_WITH_MESSAGE(validate_trace_proof(std::move(trace)), "ALU_OP_NOT"); +} + +TEST_F(AvmMiniBitwiseNegativeTestsU128, BitwiseNot) +{ + uint128_t const a = uint128_t{ 0x4000000000000 } << 64; + uint128_t const b = uint128_t{ 0x300000ae921000 } << 64; + std::vector trace = + gen_mutated_trace_not(FF{ uint256_t::from_uint128(a) }, FF{ uint256_t::from_uint128(b) }, AvmMemoryTag::U128); + EXPECT_THROW_WITH_MESSAGE(validate_trace_proof(std::move(trace)), "ALU_OP_NOT"); +} +} // namespace tests_avm diff --git a/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_memory.test.cpp b/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_memory.test.cpp index 668462d65a8..ec82799eb71 100644 --- a/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_memory.test.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/tests/AvmMini_memory.test.cpp @@ -37,7 +37,7 @@ TEST_F(AvmMiniMemoryTests, mismatchedTag) { trace_builder.calldata_copy(0, 2, 0, std::vector{ 98, 12 }); - trace_builder.add(0, 1, 4, AvmMemoryTag::U8); + trace_builder.op_add(0, 1, 4, AvmMemoryTag::U8); trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -85,7 +85,7 @@ TEST_F(AvmMiniMemoryTests, mLastAccessViolation) trace_builder.calldata_copy(0, 2, 0, std::vector{ 4, 9 }); // Memory layout: [4,9,0,0,0,0,....] - trace_builder.sub(1, 0, 2, AvmMemoryTag::U8); // [4,9,5,0,0,0.....] + trace_builder.op_sub(1, 0, 2, AvmMemoryTag::U8); // [4,9,5,0,0,0.....] trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -115,8 +115,8 @@ TEST_F(AvmMiniMemoryTests, readWriteConsistencyValViolation) trace_builder.calldata_copy(0, 2, 0, std::vector{ 4, 9 }); // Memory layout: [4,9,0,0,0,0,....] - trace_builder.mul(1, 0, 2, AvmMemoryTag::U8); // [4,9,36,0,0,0.....] - trace_builder.return_op(2, 1); // Return single memory word at position 2 (36) + trace_builder.op_mul(1, 0, 2, AvmMemoryTag::U8); // [4,9,36,0,0,0.....] + trace_builder.return_op(2, 1); // Return single memory word at position 2 (36) auto trace = trace_builder.finalize(); // Find the row with multiplication operation @@ -145,8 +145,8 @@ TEST_F(AvmMiniMemoryTests, readWriteConsistencyTagViolation) trace_builder.calldata_copy(0, 2, 0, std::vector{ 4, 9 }); // Memory layout: [4,9,0,0,0,0,....] - trace_builder.mul(1, 0, 2, AvmMemoryTag::U8); // [4,9,36,0,0,0.....] - trace_builder.return_op(2, 1); // Return single memory word at position 2 (36) + trace_builder.op_mul(1, 0, 2, AvmMemoryTag::U8); // [4,9,36,0,0,0.....] + trace_builder.return_op(2, 1); // Return single memory word at position 2 (36) auto trace = trace_builder.finalize(); // Find the row with multiplication operation @@ -185,7 +185,7 @@ TEST_F(AvmMiniMemoryTests, mismatchedTagErrorViolation) { trace_builder.calldata_copy(0, 2, 0, std::vector{ 98, 12 }); - trace_builder.sub(0, 1, 4, AvmMemoryTag::U8); + trace_builder.op_sub(0, 1, 4, AvmMemoryTag::U8); trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -219,7 +219,7 @@ TEST_F(AvmMiniMemoryTests, consistentTagNoErrorViolation) { trace_builder.calldata_copy(0, 2, 0, std::vector{ 84, 7 }); - trace_builder.div(0, 1, 4, AvmMemoryTag::FF); + trace_builder.op_div(0, 1, 4, AvmMemoryTag::FF); trace_builder.halt(); auto trace = trace_builder.finalize(); @@ -239,4 +239,4 @@ TEST_F(AvmMiniMemoryTests, consistentTagNoErrorViolation) EXPECT_THROW_WITH_MESSAGE(validate_trace_proof(std::move(trace)), "MEM_IN_TAG_CONSISTENCY_1"); } -} // namespace tests_avm \ No newline at end of file +} // namespace tests_avm diff --git a/barretenberg/cpp/src/barretenberg/vm/tests/helpers.test.cpp b/barretenberg/cpp/src/barretenberg/vm/tests/helpers.test.cpp index dc2a1e29b77..28e82af7643 100644 --- a/barretenberg/cpp/src/barretenberg/vm/tests/helpers.test.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/tests/helpers.test.cpp @@ -70,4 +70,4 @@ void mutate_ic_in_trace(std::vector& trace, std::function&& sele EXPECT_TRUE(mem_row != trace.end()); mem_row->memTrace_m_val = newValue; }; -} // namespace tests_avm \ No newline at end of file +} // namespace tests_avm