From 69bdf4f0341cbd95908e5e632b71a57da5df1433 Mon Sep 17 00:00:00 2001 From: Jean M <132435771+jeanmon@users.noreply.github.com> Date: Mon, 25 Nov 2024 18:45:53 +0100 Subject: [PATCH] chore(avm): operands reordering (#10182) Resolves #10136 --- avm-transpiler/Cargo.lock | 16 - avm-transpiler/Cargo.toml | 1 - avm-transpiler/README.md | 41 +- avm-transpiler/src/instructions.rs | 35 +- avm-transpiler/src/transpile.rs | 44 +- .../vm/avm/tests/execution.test.cpp | 1383 ++++++++--------- .../vm/avm/trace/deserialization.cpp | 22 +- .../barretenberg/vm/avm/trace/execution.cpp | 53 +- .../src/barretenberg/vm/avm/trace/trace.cpp | 17 +- .../src/barretenberg/vm/avm/trace/trace.hpp | 10 +- .../simulator/src/avm/avm_gas.test.ts | 4 +- .../simulator/src/avm/avm_simulator.test.ts | 12 +- .../src/avm/opcodes/contract.test.ts | 10 +- .../simulator/src/avm/opcodes/contract.ts | 4 +- .../src/avm/opcodes/control_flow.test.ts | 10 +- .../simulator/src/avm/opcodes/control_flow.ts | 4 +- .../avm/opcodes/environment_getters.test.ts | 8 +- .../src/avm/opcodes/environment_getters.ts | 4 +- .../src/avm/opcodes/external_calls.test.ts | 10 +- .../simulator/src/avm/opcodes/memory.test.ts | 76 +- .../simulator/src/avm/opcodes/memory.ts | 18 +- .../simulator/src/avm/opcodes/misc.ts | 4 +- .../bytecode_serialization.test.ts | 4 +- 23 files changed, 896 insertions(+), 894 deletions(-) diff --git a/avm-transpiler/Cargo.lock b/avm-transpiler/Cargo.lock index ce6c1592691..680ce0e1280 100644 --- a/avm-transpiler/Cargo.lock +++ b/avm-transpiler/Cargo.lock @@ -290,7 +290,6 @@ dependencies = [ "acvm", "base64 0.21.7", "env_logger", - "fxhash", "log", "noirc_errors", "serde", @@ -392,12 +391,6 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - [[package]] name = "cc" version = "1.1.6" @@ -687,15 +680,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "fxhash" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" -dependencies = [ - "byteorder", -] - [[package]] name = "generic-array" version = "0.14.7" diff --git a/avm-transpiler/Cargo.toml b/avm-transpiler/Cargo.toml index 5800a7af48e..618190ffb6a 100644 --- a/avm-transpiler/Cargo.toml +++ b/avm-transpiler/Cargo.toml @@ -11,7 +11,6 @@ license = "MIT OR Apache-2.0" # local acvm = { path = "../noir/noir-repo/acvm-repo/acvm", features = ["bn254"] } noirc_errors = { path = "../noir/noir-repo/compiler/noirc_errors" } -fxhash = "0.2.1" # external base64 = "0.21" diff --git a/avm-transpiler/README.md b/avm-transpiler/README.md index 2cd932c2451..c75853face0 100644 --- a/avm-transpiler/README.md +++ b/avm-transpiler/README.md @@ -5,7 +5,7 @@ This component transpiles Aztec public contracts code from Noir's Brillig byteco ## Build ``` -./boostrap.sh +./bootstrap.sh ``` ## Run @@ -13,3 +13,42 @@ This component transpiles Aztec public contracts code from Noir's Brillig byteco ``` cargo run ``` + +## Testing Transpiler Changes + +After bootstrap in `avm-transpiler`, go to `noir-contracts` and only compile avm_test_contract with: + +``` +nargo compile --package avm_test_contract --inliner-aggressiveness=0 --silence-warnings +``` + +Important: use the right nargo binary located in +`aztec-packages/noir/noir-repo/target/release/nargo` +If required, build nargo by going in `noir/noir-repo` and run +`cargo build --release`. + +Then, transpile it: + +``` +scripts/transpile.sh +``` + +Go to yarn-project/simulator and run: + +``` +yarn build:fast +``` + +This takes in the TS generated by the compilation and transpilation. + +Finally, run + +``` +yarn test src/avm/avm_simulator.test.ts +``` + +To test against some .cpp changes, compile the bb binary and run bb prover test: + +``` +yarn test src/avm_proving.test.ts +``` diff --git a/avm-transpiler/src/instructions.rs b/avm-transpiler/src/instructions.rs index 8289f444ac3..bbb9a68e5ca 100644 --- a/avm-transpiler/src/instructions.rs +++ b/avm-transpiler/src/instructions.rs @@ -22,8 +22,12 @@ pub struct AvmInstruction { /// Its usage will depend on the instruction. pub tag: Option, - /// Different instructions have different numbers of operands + /// Different instructions have different numbers of operands. These operands contain + /// memory addresses. pub operands: Vec, + + // Operands which are immediate, i.e., contain hardcoded constants. + pub immediates: Vec, } impl Display for AvmInstruction { @@ -32,10 +36,6 @@ impl Display for AvmInstruction { if let Some(indirect) = &self.indirect { write!(f, ", indirect: {}", indirect)?; } - // This will be either inTag or dstTag depending on the operation - if let Some(dst_tag) = self.tag { - write!(f, ", tag: {}", dst_tag as u8)?; - } if !self.operands.is_empty() { write!(f, ", operands: [")?; for operand in &self.operands { @@ -43,24 +43,39 @@ impl Display for AvmInstruction { } write!(f, "]")?; }; + // This will be either inTag or dstTag depending on the operation + if let Some(dst_tag) = self.tag { + write!(f, ", tag: {}", dst_tag as u8)?; + } + if !self.immediates.is_empty() { + write!(f, ", immediates: [")?; + for immediate in &self.immediates { + write!(f, "{immediate}, ")?; + } + write!(f, "]")?; + }; Ok(()) } } impl AvmInstruction { /// Bytes representation for generating AVM bytecode + /// Order: INDIRECT, OPERANDS, TAG, IMMEDIATES pub fn to_bytes(&self) -> Vec { let mut bytes = Vec::new(); bytes.push(self.opcode as u8); if let Some(indirect) = &self.indirect { bytes.extend_from_slice(&indirect.to_be_bytes()); } + for operand in &self.operands { + bytes.extend_from_slice(&operand.to_be_bytes()); + } // This will be either inTag or dstTag depending on the operation if let Some(tag) = self.tag { bytes.extend_from_slice(&(tag as u8).to_be_bytes()); } - for operand in &self.operands { - bytes.extend_from_slice(&operand.to_be_bytes()); + for immediate in &self.immediates { + bytes.extend_from_slice(&immediate.to_be_bytes()); } bytes } @@ -84,6 +99,7 @@ impl Default for AvmInstruction { indirect: None, tag: None, operands: vec![], + immediates: vec![], } } } @@ -102,9 +118,8 @@ pub enum AvmTypeTag { INVALID, } -/// Operands are usually 32 bits (offsets or jump destinations) -/// Constants (as used by the SET instruction) can have size -/// different from 32 bits +/// Operands are usually 8, 16 and 32 bits (offsets) +/// Immediates (as used by the SET instruction) can have different sizes #[allow(non_camel_case_types)] pub enum AvmOperand { U8 { value: u8 }, diff --git a/avm-transpiler/src/transpile.rs b/avm-transpiler/src/transpile.rs index bf98c239648..5e302c08020 100644 --- a/avm-transpiler/src/transpile.rs +++ b/avm-transpiler/src/transpile.rs @@ -1,5 +1,4 @@ use acvm::acir::brillig::{BitSize, IntegerBitSize, Opcode as BrilligOpcode}; -use fxhash::FxHashMap as HashMap; use std::collections::BTreeMap; use acvm::acir::circuit::BrilligOpcodeLocation; @@ -90,12 +89,12 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode]) -> (Vec< .direct_operand(destination) .build(), ), - tag: None, operands: vec![ make_operand(bits_needed, &lhs.to_usize()), make_operand(bits_needed, &rhs.to_usize()), make_operand(bits_needed, &destination.to_usize()), ], + ..Default::default() }); } BrilligOpcode::BinaryIntOp { destination, op, lhs, rhs, .. } => { @@ -178,12 +177,12 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode]) -> (Vec< .direct_operand(destination) .build(), ), - tag: None, operands: vec![ make_operand(bits_needed, &lhs.to_usize()), make_operand(bits_needed, &rhs.to_usize()), make_operand(bits_needed, &destination.to_usize()), ], + ..Default::default() }); } BrilligOpcode::Not { destination, source, .. } => { @@ -207,7 +206,7 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode]) -> (Vec< make_operand(bits_needed, &source.to_usize()), make_operand(bits_needed, &destination.to_usize()), ], - tag: None, + ..Default::default() }); } BrilligOpcode::CalldataCopy { destination_address, size_address, offset_address } => { @@ -236,7 +235,7 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode]) -> (Vec< assert!(location.num_bits() <= 32); avm_instrs.push(AvmInstruction { opcode: AvmOpcode::JUMP_32, - operands: vec![AvmOperand::BRILLIG_LOCATION { brillig_pc: *location as u32 }], + immediates: vec![AvmOperand::BRILLIG_LOCATION { brillig_pc: *location as u32 }], ..Default::default() }); } @@ -247,10 +246,8 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode]) -> (Vec< indirect: Some( AddressingModeBuilder::default().direct_operand(condition).build(), ), - operands: vec![ - AvmOperand::BRILLIG_LOCATION { brillig_pc: *location as u32 }, - make_operand(16, &condition.to_usize()), - ], + operands: vec![make_operand(16, &condition.to_usize())], + immediates: vec![AvmOperand::BRILLIG_LOCATION { brillig_pc: *location as u32 }], ..Default::default() }); } @@ -300,7 +297,7 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode]) -> (Vec< assert!(location.num_bits() <= 32); avm_instrs.push(AvmInstruction { opcode: AvmOpcode::INTERNALCALL, - operands: vec![AvmOperand::BRILLIG_LOCATION { brillig_pc: *location as u32 }], + immediates: vec![AvmOperand::BRILLIG_LOCATION { brillig_pc: *location as u32 }], ..Default::default() }); } @@ -353,8 +350,8 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode]) -> (Vec< .into_iter() .map(|i| match i.opcode { AvmOpcode::JUMP_32 | AvmOpcode::JUMPI_32 | AvmOpcode::INTERNALCALL => { - let new_operands = i - .operands + let new_immediates = i + .immediates .into_iter() .map(|o| match o { AvmOperand::BRILLIG_LOCATION { brillig_pc } => { @@ -365,7 +362,7 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode]) -> (Vec< _ => o, }) .collect::>(); - AvmInstruction { operands: new_operands, ..i } + AvmInstruction { immediates: new_immediates, ..i } } _ => i, }) @@ -832,10 +829,8 @@ fn handle_getter_instruction( avm_instrs.push(AvmInstruction { opcode: AvmOpcode::GETENVVAR_16, indirect: Some(AddressingModeBuilder::default().direct_operand(&dest_offset).build()), - operands: vec![ - AvmOperand::U8 { value: var_idx as u8 }, - AvmOperand::U16 { value: dest_offset.to_usize() as u16 }, - ], + operands: vec![AvmOperand::U16 { value: dest_offset.to_usize() as u16 }], + immediates: vec![AvmOperand::U8 { value: var_idx as u8 }], ..Default::default() }); } @@ -882,10 +877,8 @@ fn generate_set_instruction( Some(AddressingModeBuilder::default().direct_operand(dest).build()) }, tag: Some(tag), - operands: vec![ - make_operand(bits_needed_opcode, value), - make_operand(bits_needed_mem, &(dest.to_usize())), - ], + operands: vec![make_operand(bits_needed_mem, &(dest.to_usize()))], + immediates: vec![make_operand(bits_needed_opcode, value)], } } @@ -924,6 +917,7 @@ fn generate_cast_instruction( make_operand(bits_needed, &(source.to_usize())), make_operand(bits_needed, &(destination.to_usize())), ], + ..Default::default() } } @@ -1087,14 +1081,16 @@ fn handle_black_box_function(avm_instrs: &mut Vec, operation: &B .direct_operand(radix) .build(), ), - tag: None, operands: vec![ AvmOperand::U16 { value: input_offset as u16 }, AvmOperand::U16 { value: output_offset as u16 }, AvmOperand::U16 { value: radix_offset as u16 }, + ], + immediates: vec![ AvmOperand::U16 { value: num_limbs as u16 }, AvmOperand::U8 { value: *output_bits as u8 }, ], + ..Default::default() }); } // This will be changed to utilise relative memory offsets @@ -1202,10 +1198,10 @@ fn handle_debug_log( ), operands: vec![ AvmOperand::U16 { value: message_offset.to_usize() as u16 }, - AvmOperand::U16 { value: message_size as u16 }, AvmOperand::U16 { value: fields_offset_ptr.to_usize() as u16 }, AvmOperand::U16 { value: fields_size_ptr.to_usize() as u16 }, ], + immediates: vec![AvmOperand::U16 { value: message_size as u16 }], ..Default::default() }); } @@ -1462,11 +1458,11 @@ fn handle_get_contract_instance( .build(), ), operands: vec![ - AvmOperand::U8 { value: member_idx as u8 }, AvmOperand::U16 { value: address_offset.to_usize() as u16 }, AvmOperand::U16 { value: dest_offset.to_usize() as u16 }, AvmOperand::U16 { value: exists_offset.to_usize() as u16 }, ], + immediates: vec![AvmOperand::U8 { value: member_idx as u8 }], ..Default::default() }); } diff --git a/barretenberg/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp b/barretenberg/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp index c04ea043b62..bc07ccc88f3 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp @@ -123,30 +123,29 @@ class AvmExecutionTests : public ::testing::Test { // Parsing, trace generation and proving is verified. TEST_F(AvmExecutionTests, basicAddReturn) { - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U8) + - "00" // val - "07" // dst_offset 0 - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U8) + - "00" // val - "09" // dst_offset 0 - + to_hex(OpCode::ADD_16) + // opcode ADD - "00" // Indirect flag - "0007" // addr a 7 - "0009" // addr b 9 - "0001" // addr c 1 - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF"; // ret size offset 255 + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "07" // dst_offset 0 + "00" // Indirect flag + + to_hex(AvmMemoryTag::U8) + "00" // val + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "09" // dst_offset 0 + + to_hex(AvmMemoryTag::U8) + // + "00" // val + + to_hex(OpCode::ADD_16) + // opcode ADD + "00" // Indirect flag + "0007" // addr a 7 + "0009" // addr b 9 + "0001" // addr c 1 + + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + // + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF"; // ret size offset 255 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -167,9 +166,9 @@ TEST_F(AvmExecutionTests, basicAddReturn) AllOf(Field(&Instruction::op_code, OpCode::SET_8), Field(&Instruction::operands, ElementsAre(VariantWith(0), + VariantWith(255), VariantWith(AvmMemoryTag::U32), - VariantWith(0), - VariantWith(255))))); + VariantWith(0))))); // RETURN EXPECT_THAT( @@ -185,30 +184,30 @@ TEST_F(AvmExecutionTests, basicAddReturn) // Positive test for SET and SUB opcodes TEST_F(AvmExecutionTests, setAndSubOpcodes) { - std::string bytecode_hex = to_hex(OpCode::SET_16) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U16) + - "B813" // val 47123 - "00AA" // dst_offset 170 - + to_hex(OpCode::SET_16) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U16) + - "9103" // val 37123 - "0033" // dst_offset 51 - + to_hex(OpCode::SUB_8) + // opcode SUB - "00" // Indirect flag - "AA" // addr a - "33" // addr b - "01" // addr c 1 - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF"; // ret size offset 255 + std::string bytecode_hex = to_hex(OpCode::SET_16) + // opcode SET + "00" // Indirect flag + "00AA" // dst_offset 170 + + to_hex(AvmMemoryTag::U16) + // + "B813" // val 47123 + + to_hex(OpCode::SET_16) + // opcode SET + "00" // Indirect flag + "0033" // dst_offset 51 + + to_hex(AvmMemoryTag::U16) + // + "9103" // val 37123 + + to_hex(OpCode::SUB_8) + // opcode SUB + "00" // Indirect flag + "AA" // addr a + "33" // addr b + "01" // addr c 1 + + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + // + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF"; // ret size offset 255 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -220,18 +219,18 @@ TEST_F(AvmExecutionTests, setAndSubOpcodes) AllOf(Field(&Instruction::op_code, OpCode::SET_16), Field(&Instruction::operands, ElementsAre(VariantWith(0), + VariantWith(170), VariantWith(AvmMemoryTag::U16), - VariantWith(47123), - VariantWith(170))))); + VariantWith(47123))))); // SET EXPECT_THAT(instructions.at(1), AllOf(Field(&Instruction::op_code, OpCode::SET_16), Field(&Instruction::operands, ElementsAre(VariantWith(0), + VariantWith(51), VariantWith(AvmMemoryTag::U16), - VariantWith(37123), - VariantWith(51))))); + VariantWith(37123))))); // SUB EXPECT_THAT(instructions.at(2), @@ -259,16 +258,14 @@ TEST_F(AvmExecutionTests, setAndSubOpcodes) TEST_F(AvmExecutionTests, powerWithMulOpcodes) { const int NUM_MUL_ITERATIONS = 12; - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U64) + - "05" // val - "00" // dst_offset 0 - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U64) + - "01" // val - "01"; // dst_offset 1 + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "00" // dst_offset 0 + + to_hex(AvmMemoryTag::U64) + "05" // val + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // dst_offset 1 + + to_hex(AvmMemoryTag::U64) + "01"; // val std::string const mul_hex = to_hex(OpCode::MUL_8) + // opcode MUL "00" // Indirect flag @@ -276,11 +273,11 @@ TEST_F(AvmExecutionTests, powerWithMulOpcodes) "01" // addr b "01"; // addr c 1 - std::string const set_return_size_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" + // Indirect flag - to_hex(AvmMemoryTag::U32) + - "00" // val - "FF"; // dst_offset + std::string const set_return_size_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "FF" // dst_offset + + to_hex(AvmMemoryTag::U32) + // + "00"; // val std::string const ret_hex = to_hex(OpCode::RETURN) + // opcode RETURN "00" // Indirect flag @@ -321,9 +318,9 @@ TEST_F(AvmExecutionTests, powerWithMulOpcodes) AllOf(Field(&Instruction::op_code, OpCode::SET_8), Field(&Instruction::operands, ElementsAre(VariantWith(0), + VariantWith(255), VariantWith(AvmMemoryTag::U32), - VariantWith(0), - VariantWith(255))))); + VariantWith(0))))); // RETURN EXPECT_THAT( @@ -365,33 +362,31 @@ TEST_F(AvmExecutionTests, powerWithMulOpcodes) // PC Index 0 9 14 22 27 33 42 TEST_F(AvmExecutionTests, simpleInternalCall) { - std::string bytecode_hex = to_hex(OpCode::SET_32) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // - "0D3D2518" // val 222111000 = 0xD3D2518 - "0004" // dst_offset 4 - + to_hex(OpCode::INTERNALCALL) + // opcode INTERNALCALL - "00000021" // jmp_dest 33 - + to_hex(OpCode::ADD_16) + // opcode ADD - "00" // Indirect flag - "0004" // addr a 4 - "0007" // addr b 7 - "0009" // addr c9 - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF" // ret size offset 255 - + to_hex(OpCode::SET_32) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "075BCD15" // val 123456789 = 0x75BCD15 - "0007" // dst_offset 7 - + to_hex(OpCode::INTERNALRETURN) // opcode INTERNALRETURN + std::string bytecode_hex = to_hex(OpCode::SET_32) + // opcode SET + "00" // Indirect flag + "0004" // dst_offset 4 + + to_hex(AvmMemoryTag::U32) + // + "0D3D2518" // val 222111000 = 0xD3D2518 + + to_hex(OpCode::INTERNALCALL) + // opcode INTERNALCALL + "00000021" // jmp_dest 33 + + to_hex(OpCode::ADD_16) + // opcode ADD + "00" // Indirect flag + "0004" // addr a 4 + "0007" // addr b 7 + "0009" // addr c9 + + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF" // ret size offset 255 + + to_hex(OpCode::SET_32) + // opcode SET + "00" // Indirect flag + "0007" // dst_offset 7 + + to_hex(AvmMemoryTag::U32) + "075BCD15" // val 123456789 = 0x75BCD15 + + to_hex(OpCode::INTERNALRETURN) // opcode INTERNALRETURN ; auto bytecode = hex_to_bytes(bytecode_hex); @@ -449,7 +444,7 @@ TEST_F(AvmExecutionTests, nestedInternalCalls) // val and dst_offset is assumed to be 2 bytes return to_hex(OpCode::SET_32) // opcode SET + "00" // Indirect flag - + to_hex(AvmMemoryTag::U8) + "000000" + val + "00" + dst_offset; + + "00" + dst_offset + to_hex(AvmMemoryTag::U8) + "000000" + val; }; const std::string tag_address_arguments = "00" // Indirect Flag @@ -457,11 +452,10 @@ TEST_F(AvmExecutionTests, nestedInternalCalls) "03" // addr b 3 "02"; // addr c 2 - std::string const set_return_size_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" + // Indirect flag - to_hex(AvmMemoryTag::U32) + - "00" // val - "FF"; // dst_offset 255 + std::string const set_return_size_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "FF" // dst_offset 255 + + to_hex(AvmMemoryTag::U32) + "00"; // val const std::string return_instruction_hex = to_hex(OpCode::RETURN) // opcode RETURN + "00" // Indirect flag @@ -520,14 +514,14 @@ TEST_F(AvmExecutionTests, jumpAndCalldatacopy) { std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET "00" // Indirect flag + "00" // dst_offset + to_hex(AvmMemoryTag::U32) + // "00" // val - "00" // dst_offset + to_hex(OpCode::SET_8) + // opcode SET "00" // Indirect flag + "01" // dst_offset + to_hex(AvmMemoryTag::U32) + // "02" // val - "01" // dst_offset + to_hex(OpCode::CALLDATACOPY) + // opcode CALLDATACOPY (no in tag) "00" // Indirect flag "0000" // cd_offset @@ -547,13 +541,13 @@ TEST_F(AvmExecutionTests, jumpAndCalldatacopy) "01" // addr c 1 (156 / 13 = 12) + to_hex(OpCode::SET_8) + // opcode SET (for return size) "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF" // ret size offset 255 + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + // + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF" // ret size offset 255 ; auto bytecode = hex_to_bytes(bytecode_hex); @@ -615,14 +609,14 @@ TEST_F(AvmExecutionTests, jumpiAndCalldatacopy) { std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET "00" // Indirect flag + "00" // dst_offset + to_hex(AvmMemoryTag::U32) + // "00" // val - "00" // dst_offset + to_hex(OpCode::SET_8) + // opcode SET "00" // Indirect flag + "01" // dst_offset + to_hex(AvmMemoryTag::U32) + // "01" // val - "01" // dst_offset + to_hex(OpCode::CALLDATACOPY) + // opcode CALLDATACOPY (no in tag) "00" // Indirect flag "0000" // cd_offset @@ -630,13 +624,13 @@ TEST_F(AvmExecutionTests, jumpiAndCalldatacopy) "000A" // dst_offset 10 + to_hex(OpCode::SET_8) + // opcode SET "00" // Indirect flag + "65" // dst_offset 101 + to_hex(AvmMemoryTag::U16) + // "14" // val 20 - "65" // dst_offset 101 + to_hex(OpCode::JUMPI_32) + // opcode JUMPI "00" // Indirect flag - "00000027" // jmp_dest (MUL located at 39) "000A" // cond_offset 10 + "00000027" // jmp_dest (MUL located at 39) + to_hex(OpCode::ADD_16) + // opcode ADD "00" // Indirect flag "0065" // addr 101 @@ -649,13 +643,13 @@ TEST_F(AvmExecutionTests, jumpiAndCalldatacopy) "66" // output of MUL addr 102 + to_hex(OpCode::SET_8) + // opcode SET (for return size) "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF" // ret size offset 255 + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + // + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF" // ret size offset 255 ; auto bytecode = hex_to_bytes(bytecode_hex); @@ -670,7 +664,7 @@ TEST_F(AvmExecutionTests, jumpiAndCalldatacopy) instructions.at(4), AllOf(Field(&Instruction::op_code, OpCode::JUMPI_32), Field(&Instruction::operands, - ElementsAre(VariantWith(0), VariantWith(39), VariantWith(10))))); + ElementsAre(VariantWith(0), VariantWith(10), VariantWith(39))))); std::vector returndata; ExecutionHints execution_hints; @@ -698,24 +692,22 @@ TEST_F(AvmExecutionTests, jumpiAndCalldatacopy) // Positive test with MOV. TEST_F(AvmExecutionTests, movOpcode) { - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U8) + - "13" // val 19 - "AB" // dst_offset 171 - + to_hex(OpCode::MOV_8) + // opcode MOV - "00" // Indirect flag - "AB" // src_offset 171 - "21" // dst_offset 33 - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF"; // ret size offset 255 + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "AB" // dst_offset 171 + + to_hex(AvmMemoryTag::U8) + "13" // val 19 + + to_hex(OpCode::MOV_8) + // opcode MOV + "00" // Indirect flag + "AB" // src_offset 171 + "21" // dst_offset 33 + + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF"; // ret size offset 255 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -727,9 +719,9 @@ TEST_F(AvmExecutionTests, movOpcode) AllOf(Field(&Instruction::op_code, OpCode::SET_8), Field(&Instruction::operands, ElementsAre(VariantWith(0), + VariantWith(171), VariantWith(AvmMemoryTag::U8), - VariantWith(19), - VariantWith(171))))); + VariantWith(19))))); // MOV EXPECT_THAT( @@ -751,34 +743,30 @@ TEST_F(AvmExecutionTests, movOpcode) // Positive test with indirect MOV. TEST_F(AvmExecutionTests, indMovOpcode) { - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0A" // val 10 - "01" // dst_offset 1 - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0B" // val 11 - "02" // dst_offset 2 - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U8) + - "FF" // val 255 - "0A" // dst_offset 10 - + to_hex(OpCode::MOV_8) + // opcode MOV - "01" // Indirect flag - "01" // src_offset 1 --> direct offset 10 - "02" // dst_offset 2 --> direct offset 11 - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF"; // ret size offset 255 + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // dst_offset 1 + + to_hex(AvmMemoryTag::U32) + "0A" // val 10 + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "02" // dst_offset 2 + + to_hex(AvmMemoryTag::U32) + "0B" // val 11 + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "0A" // dst_offset 10 + + to_hex(AvmMemoryTag::U8) + "FF" // val 255 + + to_hex(OpCode::MOV_8) + // opcode MOV + "01" // Indirect flag + "01" // src_offset 1 --> direct offset 10 + "02" // dst_offset 2 --> direct offset 11 + + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF"; // ret size offset 255 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -804,39 +792,37 @@ TEST_F(AvmExecutionTests, indMovOpcode) // Positive test for SET and CAST opcodes TEST_F(AvmExecutionTests, setAndCastOpcodes) { - std::string bytecode_hex = to_hex(OpCode::SET_16) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U16) + - "B813" // val 47123 - "0011" // dst_offset 17 - + to_hex(OpCode::CAST_8) + // opcode CAST - "00" // Indirect flag - + to_hex(AvmMemoryTag::U8) + - "11" // addr a - "12" // addr casted a - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF"; // ret size offset 255 + std::string bytecode_hex = to_hex(OpCode::SET_16) + // opcode SET + "00" // Indirect flag + "0011" // dst_offset 17 + + to_hex(AvmMemoryTag::U16) + "B813" // val 47123 + + to_hex(OpCode::CAST_8) + // opcode CAST + "00" // Indirect flag + "11" // addr a + "12" // addr casted a + + to_hex(AvmMemoryTag::U8) // + + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF"; // ret size offset 255 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); ASSERT_THAT(instructions, SizeIs(4)); - // SUB + // CAST EXPECT_THAT(instructions.at(1), AllOf(Field(&Instruction::op_code, OpCode::CAST_8), Field(&Instruction::operands, ElementsAre(VariantWith(0), - VariantWith(AvmMemoryTag::U8), VariantWith(17), - VariantWith(18))))); + VariantWith(18), + VariantWith(AvmMemoryTag::U8))))); auto trace = gen_trace_from_bytecode(bytecode); @@ -850,52 +836,48 @@ TEST_F(AvmExecutionTests, setAndCastOpcodes) // Positive test with TO_RADIX_BE. TEST_F(AvmExecutionTests, toRadixBeOpcodeBytes) { - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val - "00" // dst_offset - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "01" // val - "01" // dst_offset - + to_hex(OpCode::CALLDATACOPY) + // opcode CALLDATACOPY - "00" // Indirect flag - "0000" // cd_offset - "0001" // copy_size - "0001" // dst_offset - + to_hex(OpCode::SET_8) + // opcode SET for indirect src - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "01" // value 1 (i.e. where the src from calldata is copied) - "11" // dst_offset 17 - + to_hex(OpCode::SET_8) + // opcode SET for indirect dst - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "05" // value 5 (i.e. where the dst will be written to) - "15" // dst_offset 21 - + to_hex(OpCode::SET_8) + // opcode SET for indirect dst - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "02" // value 2 (i.e. radix 2 - perform bitwise decomposition) - "80" // radix_offset 80 - + to_hex(OpCode::TORADIXBE) + // opcode TO_RADIX_BE - "03" // Indirect flag - "0011" // src_offset 17 (indirect) - "0015" // dst_offset 21 (indirect) - "0080" // radix_offset 80 (direct) - "0100" // limbs: 256 - "00" // output_bits: false - + to_hex(OpCode::SET_16) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0100" // val: 256 - "0200" + // dst_offset=512 - to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0005" // ret offset 5 - "0200"; // ret size offset 512 + std::string bytecode_hex = + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "00" // dst_offset + + to_hex(AvmMemoryTag::U32) + "00" // val + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // dst_offset + + to_hex(AvmMemoryTag::U32) + "01" // val + + to_hex(OpCode::CALLDATACOPY) + // opcode CALLDATACOPY + "00" // Indirect flag + "0000" // cd_offset + "0001" // copy_size + "0001" // dst_offset + + to_hex(OpCode::SET_8) + // opcode SET for indirect src + "00" // Indirect flag + "11" // dst_offset 17 + + to_hex(AvmMemoryTag::U32) + "01" // value 1 (i.e. where the src from calldata is copied) + + to_hex(OpCode::SET_8) + // opcode SET for indirect dst + "00" // Indirect flag + "15" // dst_offset 21 + + to_hex(AvmMemoryTag::U32) + "05" // value 5 (i.e. where the dst will be written to) + + to_hex(OpCode::SET_8) + // opcode SET for indirect dst + "00" // Indirect flag + "80" // radix_offset 80 + + to_hex(AvmMemoryTag::U32) + "02" // value 2 (i.e. radix 2 - perform bitwise decomposition) + + to_hex(OpCode::TORADIXBE) + // opcode TO_RADIX_BE + "03" // Indirect flag + "0011" // src_offset 17 (indirect) + "0015" // dst_offset 21 (indirect) + "0080" // radix_offset 80 (direct) + "0100" // limbs: 256 + "00" // output_bits: false + + to_hex(OpCode::SET_16) + // opcode SET (for return size) + "00" // Indirect flag + "0200" // dst_offset=512 + + to_hex(AvmMemoryTag::U32) + // + "0100" // val: 256 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0005" // ret offset 5 + "0200"; // ret size offset 512 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -924,16 +906,16 @@ TEST_F(AvmExecutionTests, toRadixBeOpcodeBytes) // Positive test with TO_RADIX_BE. TEST_F(AvmExecutionTests, toRadixBeOpcodeBitsMode) { - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val - "00" // dst_offset - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "01" // val + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "00" // dst_offset + + to_hex(AvmMemoryTag::U32) // + + "00" // val + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag "01" // dst_offset + + to_hex(AvmMemoryTag::U32) + // + "01" // val + to_hex(OpCode::CALLDATACOPY) + // opcode CALLDATACOPY "00" // Indirect flag "0000" // cd_offset @@ -941,19 +923,19 @@ TEST_F(AvmExecutionTests, toRadixBeOpcodeBitsMode) "0001" // dst_offset + to_hex(OpCode::SET_8) + // opcode SET for indirect src "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "01" // value 1 (i.e. where the src from calldata is copied) - "11" // dst_offset 17 - + to_hex(OpCode::SET_8) + // opcode SET for indirect dst - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "05" // value 5 (i.e. where the dst will be written to) - "15" // dst_offset 21 - + to_hex(OpCode::SET_8) + // opcode SET for indirect dst - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + + "11" // dst_offset 17 + + to_hex(AvmMemoryTag::U32) + // + "01" // value 1 (i.e. where the src from calldata is copied) + + to_hex(OpCode::SET_8) + // opcode SET for indirect dst + "00" // Indirect flag + "15" // dst_offset 21 + + to_hex(AvmMemoryTag::U32) + // + "05" // value 5 (i.e. where the dst will be written to) + + to_hex(OpCode::SET_8) + // opcode SET for indirect dst + "00" // Indirect flag + "80" // radix_offset 80 + + to_hex(AvmMemoryTag::U32) + // "02" // value 2 (i.e. radix 2 - perform bitwise decomposition) - "80" // radix_offset 80 + to_hex(OpCode::TORADIXBE) + // opcode TO_RADIX_BE "03" // Indirect flag "0011" // src_offset 17 (indirect) @@ -963,13 +945,13 @@ TEST_F(AvmExecutionTests, toRadixBeOpcodeBitsMode) "01" // output_bits: true + to_hex(OpCode::SET_16) + // opcode SET (for return size) "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0100" // val: 256 - "0200" + // dst_offset=512 - to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0005" // ret offset 5 - "0200"; // ret size offset 512 + "0200" // dst_offset=512 + + to_hex(AvmMemoryTag::U32) + // + "0100" // val: 256 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0005" // ret offset 5 + "0200"; // ret size offset 512 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -1003,36 +985,33 @@ TEST_F(AvmExecutionTests, sha256CompressionOpcode) // Test vectors taken from noir black_box_solver // State = Uint32Array.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]), for (uint8_t i = 1; i <= 8; i++) { - bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + to_hex(i) + // val i - to_hex(i); // val i + bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + + to_hex(i) // offset i + + to_hex(AvmMemoryTag::U32) + to_hex(i); // val i } // Set operations for sha256 input // Test vectors taken from noir black_box_solver // Input = Uint32Array.from([1, 2, 3, 4, 5, 6, 7, 8]), for (uint8_t i = 1; i <= 16; i++) { - bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + to_hex(i) + // val i - to_hex(i + 8); // val i + bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + + to_hex(i + 8) // offset i + 8 + + to_hex(AvmMemoryTag::U32) + to_hex(i); // val i } - std::string bytecode_hex = bytecode_preamble // Initial SET operations to store state and input - + to_hex(OpCode::SET_16) + // opcode SET for indirect dst (output) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0100" // value 256 (i.e. where the dst will be written to) - "0024" // dst_offset 36 - + to_hex(OpCode::SET_8) + // opcode SET for indirect state - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "01" // value 1 (i.e. where the state will be read from) - "22" // dst_offset 34 - + to_hex(OpCode::SET_8) + // opcode SET for indirect input - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "09" // value 9 (i.e. where the input will be read from) - "23" // dst_offset 35 + std::string bytecode_hex = bytecode_preamble // Initial SET operations to store state and input + + to_hex(OpCode::SET_16) + // opcode SET for indirect dst (output) + "00" // Indirect flag + "0024" // dst_offset 36 + + to_hex(AvmMemoryTag::U32) + "0100" // value 256 (i.e. where the dst will be written to) + + to_hex(OpCode::SET_8) + // opcode SET for indirect state + "00" // Indirect flag + "22" // dst_offset 34 + + to_hex(AvmMemoryTag::U32) + "01" // value 1 (i.e. where the state will be read from) + + to_hex(OpCode::SET_8) + // opcode SET for indirect input + "00" // Indirect flag + "23" // dst_offset 35 + + to_hex(AvmMemoryTag::U32) + "09" // value 9 (i.e. where the input will be read from) + to_hex(OpCode::SHA256COMPRESSION) + // opcode SHA256COMPRESSION "00" // Indirect flag "0100" // output offset @@ -1040,13 +1019,12 @@ TEST_F(AvmExecutionTests, sha256CompressionOpcode) "0009" // input offset + to_hex(OpCode::SET_16) + // opcode SET (for return size) "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0008" // val: 8 - "0200" // dst_offset=512 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0100" // ret offset 256 - "0200"; // ret size offset 512 + "0200" // dst_offset=512 + + to_hex(AvmMemoryTag::U32) + "0008" // val: 8 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0100" // ret offset 256 + "0200"; // ret size offset 512 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -1076,44 +1054,39 @@ TEST_F(AvmExecutionTests, poseidon2PermutationOpCode) FF(std::string("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789")), FF(std::string("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789")) }; - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val - "00" // dst_offset - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "04" // val - "01" // dst_offset - + to_hex(OpCode::CALLDATACOPY) + // opcode CALL DATA COPY - "00" // Indirect Flag - "0000" // cd_offset - "0001" // copy_size - "0001" // dst_offset 1 - + to_hex(OpCode::SET_8) + // opcode SET for indirect src (input) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "01" // value 1 (i.e. where the src will be read from) - "24" // dst_offset 36 - + to_hex(OpCode::SET_8) + // opcode SET for indirect dst (output) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "09" // value 9 (i.e. where the ouput will be written to) - "23" // dst_offset 35 - + to_hex(OpCode::POSEIDON2PERM) + // opcode POSEIDON2 - "03" // Indirect flag (first 2 operands indirect) - "0024" // input offset (indirect 36) - "0023" // output offset (indirect 35) - + to_hex(OpCode::SET_16) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0004" // val: 4 - "0200" // dst_offset=512 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0009" // ret offset 256 - "0200"; // ret size offset 512 + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "00" // dst_offset + + to_hex(AvmMemoryTag::U32) + "00" // val + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // dst_offset + + to_hex(AvmMemoryTag::U32) + "04" // val + + to_hex(OpCode::CALLDATACOPY) + // opcode CALL DATA COPY + "00" // Indirect Flag + "0000" // cd_offset + "0001" // copy_size + "0001" // dst_offset 1 + + to_hex(OpCode::SET_8) + // opcode SET for indirect src (input) + "00" // Indirect flag + "24" // dst_offset 36 + + to_hex(AvmMemoryTag::U32) + "01" // value 1 (i.e. where the src will be read from) + + to_hex(OpCode::SET_8) + // opcode SET for indirect dst (output) + "00" // Indirect flag + "23" // dst_offset 35 + + to_hex(AvmMemoryTag::U32) + "09" // value 9 (i.e. where the ouput will be written to) + + to_hex(OpCode::POSEIDON2PERM) + // opcode POSEIDON2 + "03" // Indirect flag (first 2 operands indirect) + "0024" // input offset (indirect 36) + "0023" // output offset (indirect 35) + + to_hex(OpCode::SET_16) + // opcode SET (for return size) + "00" // Indirect flag + "0200" // dst_offset=512 + + to_hex(AvmMemoryTag::U32) + "0004" // val: 4 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0009" // ret offset 256 + "0200"; // ret size offset 512 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -1160,37 +1133,35 @@ TEST_F(AvmExecutionTests, keccakf1600OpCode) std::string bytecode_preamble; // Set operations for keccak state for (uint8_t i = 0; i < KECCAKF1600_INPUT_SIZE; i++) { - bytecode_preamble += to_hex(OpCode::SET_64) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U64) + to_hex(state[i]) + // val i - to_hex(i + 1); // dst offset + bytecode_preamble += to_hex(OpCode::SET_64) + // opcode SET + "00" // Indirect flag + + to_hex(i + 1) // dst offset + + to_hex(AvmMemoryTag::U64) + to_hex(state[i]); // val i } // We use calldatacopy twice because we need to set up 4 inputs - std::string bytecode_hex = bytecode_preamble + // Initial SET operations to store state and input - to_hex(OpCode::SET_8) + // opcode SET for indirect src (input) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "01" // value 1 (i.e. where the src will be read from) - "24" // input_offset 36 - + to_hex(OpCode::SET_16) + // opcode SET for indirect dst (output) - "00" // Indirect flag + std::string bytecode_hex = bytecode_preamble + // Initial SET operations to store state and input + to_hex(OpCode::SET_8) + // opcode SET for indirect src (input) + "00" // Indirect flag + "24" // input_offset 36 + + to_hex(AvmMemoryTag::U32) + "01" // value 1 (i.e. where the src will be read from) + + to_hex(OpCode::SET_16) + // opcode SET for indirect dst (output) + "00" // Indirect flag + "0023" // dst_offset 35 + to_hex(AvmMemoryTag::U32) + "0100" // value 256 (i.e. where the ouput will be written to) - "0023" // dst_offset 35 + to_hex(OpCode::KECCAKF1600) + // opcode KECCAKF1600 "03" // Indirect flag (first 2 operands indirect) "0023" // output offset (indirect 35) "0024" // input offset (indirect 36) + to_hex(OpCode::SET_16) + // opcode SET (for return size) "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0019" // val: 25 - "0200" // dst_offset=512 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0100" // ret offset 256 - "0200"; // ret size offset 512 + "0200" // dst_offset=512 + + to_hex(AvmMemoryTag::U32) + "0019" // val: 25 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0100" // ret offset 256 + "0200"; // ret size offset 512 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -1218,14 +1189,14 @@ TEST_F(AvmExecutionTests, embeddedCurveAddOpCode) auto expected_output = std::vector{ res.x, res.y, res.is_point_at_infinity() }; std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET "00" // Indirect flag + "00" // dst_offset + to_hex(AvmMemoryTag::U32) + // "00" // val - "00" // dst_offset + to_hex(OpCode::SET_8) + // opcode SET "00" // Indirect flag + "01" // dst_offset + to_hex(AvmMemoryTag::U32) + // "06" // val - "01" // dst_offset + to_hex(OpCode::CALLDATACOPY) + // Calldatacopy "00" // Indirect flag "0000" // cd_offset @@ -1233,19 +1204,19 @@ TEST_F(AvmExecutionTests, embeddedCurveAddOpCode) "0000" // dst_offset + to_hex(OpCode::CAST_8) + // opcode CAST inf to U8 "00" // Indirect flag - + to_hex(AvmMemoryTag::U1) + // "02" // a_is_inf "02" // a_is_inf + + to_hex(AvmMemoryTag::U1) // + to_hex(OpCode::CAST_8) + // opcode CAST inf to U8 "00" // Indirect flag - + to_hex(AvmMemoryTag::U1) + // "05" // b_is_inf "05" // b_is_inf + + to_hex(AvmMemoryTag::U1) // + to_hex(OpCode::SET_8) + // opcode SET for direct src_length "00" // Indirect flag + "06" // dst_offset + to_hex(AvmMemoryTag::U32) + // "07" // value - "06" // dst_offset + to_hex(OpCode::ECADD) + // opcode ECADD "0040" // Indirect flag (sixth operand indirect) "0000" // lhs_x_offset (direct) @@ -1257,9 +1228,9 @@ TEST_F(AvmExecutionTests, embeddedCurveAddOpCode) "0006" // output_offset (indirect) and resolves to 7 + to_hex(OpCode::SET_16) + // opcode SET (for return size) "00" // Indirect flag + "0200" // dst_offset=512 + to_hex(AvmMemoryTag::U32) + // "0003" // val: 3 - "0200" // dst_offset=512 + to_hex(OpCode::RETURN) + // opcode RETURN "00" // Indirect flag "0007" // ret offset 7 @@ -1299,66 +1270,66 @@ TEST_F(AvmExecutionTests, msmOpCode) std::vector calldata = { FF(a.x), FF(a.y), a_is_inf, FF(b.x), FF(b.y), b_is_inf, scalar_a_lo, scalar_a_hi, scalar_b_lo, scalar_b_hi }; - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // - "00" // val - "00" // dst_offset - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // - "0A" // val - "01" + // - to_hex(OpCode::CALLDATACOPY) + // Calldatacopy - "00" // Indirect flag - "0000" // cd_offset 0 - "0001" // copy_size (10 elements) - "0000" // dst_offset 0 - + to_hex(OpCode::CAST_8) + // opcode CAST inf to U8 - "00" // Indirect flag - + to_hex(AvmMemoryTag::U1) + // - "02" // a_is_inf - "02" // - + to_hex(OpCode::CAST_8) + // opcode CAST inf to U8 - "00" // Indirect flag - + to_hex(AvmMemoryTag::U1) + // - "05" // b_is_inf - "05" // - + to_hex(OpCode::SET_8) + // opcode SET for length - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // - "06" // Length of point elements (6) - "0b" // dst offset (11) - + to_hex(OpCode::SET_8) + // SET Indirects - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // - "00" // points offset - "0d" // dst offset + - + to_hex(OpCode::SET_8) + // SET Indirects - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // - "06" // scalars offset - "0e" + // dst offset - to_hex(OpCode::SET_8) + // SET Indirects - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // - "0c" // output offset - "0f" + // dst offset - to_hex(OpCode::MSM) + // opcode MSM - "07" // Indirect flag (first 3 indirect) - "000d" // points offset - "000e" // scalars offset - "000f" // output offset - "000b" // length offset - + to_hex(OpCode::SET_16) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // - "0003" // val: 3 - "0200" // dst_offset=512 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "000c" // ret offset 12 (this overwrites) - "0200"; // ret size offset 512 + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "00" // dst_offset + + to_hex(AvmMemoryTag::U32) + // + "00" // val + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // + + to_hex(AvmMemoryTag::U32) + // + "0A" // val + + to_hex(OpCode::CALLDATACOPY) + // Calldatacopy + "00" // Indirect flag + "0000" // cd_offset 0 + "0001" // copy_size (10 elements) + "0000" // dst_offset 0 + + to_hex(OpCode::CAST_8) + // opcode CAST inf to U8 + "00" // Indirect flag + "02" // a_is_inf + "02" // + + to_hex(AvmMemoryTag::U1) + // + to_hex(OpCode::CAST_8) + // opcode CAST inf to U8 + "00" // Indirect flag + "05" // b_is_inf + "05" // + + to_hex(AvmMemoryTag::U1) + // + to_hex(OpCode::SET_8) + // opcode SET for length + "00" // Indirect flag + "0b" // dst offset (11) + + to_hex(AvmMemoryTag::U32) + // + "06" // Length of point elements (6) + + to_hex(OpCode::SET_8) + // SET Indirects + "00" // Indirect flag + "0d" // dst offset + + + to_hex(AvmMemoryTag::U32) + // + "00" // points offset + + to_hex(OpCode::SET_8) + // SET Indirects + "00" // Indirect flag + "0e" // dst offset + + to_hex(AvmMemoryTag::U32) + // + "06" // scalars offset + + to_hex(OpCode::SET_8) + // SET Indirects + "00" // Indirect flag + "0f" // dst offset + + to_hex(AvmMemoryTag::U32) + // + "0c" // output offset + + to_hex(OpCode::MSM) + // opcode MSM + "07" // Indirect flag (first 3 indirect) + "000d" // points offset + "000e" // scalars offset + "000f" // output offset + "000b" // length offset + + to_hex(OpCode::SET_16) + // opcode SET (for return size) + "00" // Indirect flag + "0200" // dst_offset=512 + + to_hex(AvmMemoryTag::U32) + // + "0003" // val: 3 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "000c" // ret offset 12 (this overwrites) + "0200"; // ret size offset 512 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -1377,59 +1348,59 @@ TEST_F(AvmExecutionTests, msmOpCode) TEST_F(AvmExecutionTests, getEnvOpcode) { std::string bytecode_hex = - to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::ADDRESS)) + // envvar ADDRESS - "0001" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::SENDER)) + // envvar SENDER - "0002" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::FUNCTIONSELECTOR)) + // envvar FUNCTIONSELECTOR - "0003" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::TRANSACTIONFEE)) + // envvar TRANSACTIONFEE - "0004" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::CHAINID)) + // envvar CHAINID - "0005" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::VERSION)) + // envvar VERSION - "0006" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::BLOCKNUMBER)) + // envvar BLOCKNUMBER - "0007" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::TIMESTAMP)) + // envvar TIMESTAMP - "0008" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::FEEPERL2GAS)) + // envvar FEEPERL2GAS - "0009" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::FEEPERDAGAS)) + // envvar FEEPERDAGAS - "000A" // dst_offset - + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 - "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::ISSTATICCALL)) + // envvar ISSTATICCALL - "000B" // dst_offset - + to_hex(OpCode::SET_16) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // tag U32 - "000B" // val: 12 - "0200" // dst_offset=512 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0001" // ret offset 1 - "0200"; // ret size offset 512 + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "0001" // dst_offset + + to_hex(static_cast(EnvironmentVariable::ADDRESS)) // envvar ADDRESS + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "0002" // dst_offset + + to_hex(static_cast(EnvironmentVariable::SENDER)) // envvar SENDER + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "0003" // dst_offset + + to_hex(static_cast(EnvironmentVariable::FUNCTIONSELECTOR)) // envvar FUNCTIONSELECTOR + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "0004" // dst_offset + + to_hex(static_cast(EnvironmentVariable::TRANSACTIONFEE)) // envvar TRANSACTIONFEE + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "0005" // dst_offset + + to_hex(static_cast(EnvironmentVariable::CHAINID)) // envvar CHAINID + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "0006" // dst_offset + + to_hex(static_cast(EnvironmentVariable::VERSION)) // envvar VERSION + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "0007" // dst_offset + + to_hex(static_cast(EnvironmentVariable::BLOCKNUMBER)) // envvar BLOCKNUMBER + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "0008" // dst_offset + + to_hex(static_cast(EnvironmentVariable::TIMESTAMP)) // envvar TIMESTAMP + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "0009" // dst_offset + + to_hex(static_cast(EnvironmentVariable::FEEPERL2GAS)) // envvar FEEPERL2GAS + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "000A" // dst_offset + + to_hex(static_cast(EnvironmentVariable::FEEPERDAGAS)) // envvar FEEPERDAGAS + + to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16 + "00" // Indirect flag + "000B" // dst_offset + + to_hex(static_cast(EnvironmentVariable::ISSTATICCALL)) // envvar ISSTATICCALL + + to_hex(OpCode::SET_16) + // opcode SET (for return size) + "00" // Indirect flag + "0200" // dst_offset=512 + + to_hex(AvmMemoryTag::U32) + // tag U32 + "000B" // val: 12 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0001" // ret offset 1 + "0200"; // ret size offset 512 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -1441,16 +1412,16 @@ TEST_F(AvmExecutionTests, getEnvOpcode) AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), Field(&Instruction::operands, ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::ADDRESS)), - VariantWith(1))))); + VariantWith(1), + VariantWith(static_cast(EnvironmentVariable::ADDRESS)))))); // SENDER EXPECT_THAT(instructions.at(1), AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), Field(&Instruction::operands, ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::SENDER)), - VariantWith(2))))); + VariantWith(2), + VariantWith(static_cast(EnvironmentVariable::SENDER)))))); // FUNCTIONSELECTOR EXPECT_THAT( @@ -1458,72 +1429,77 @@ TEST_F(AvmExecutionTests, getEnvOpcode) AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), Field(&Instruction::operands, ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::FUNCTIONSELECTOR)), - VariantWith(3))))); + VariantWith(3), + VariantWith(static_cast(EnvironmentVariable::FUNCTIONSELECTOR)))))); // TRANSACTIONFEE - EXPECT_THAT(instructions.at(3), - AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), - Field(&Instruction::operands, - ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::TRANSACTIONFEE)), - VariantWith(4))))); + EXPECT_THAT( + instructions.at(3), + AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), + Field(&Instruction::operands, + ElementsAre(VariantWith(0), + VariantWith(4), + VariantWith(static_cast(EnvironmentVariable::TRANSACTIONFEE)))))); // CHAINID EXPECT_THAT(instructions.at(4), AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), Field(&Instruction::operands, ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::CHAINID)), - VariantWith(5))))); + VariantWith(5), + VariantWith(static_cast(EnvironmentVariable::CHAINID)))))); // VERSION EXPECT_THAT(instructions.at(5), AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), Field(&Instruction::operands, ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::VERSION)), - VariantWith(6))))); + VariantWith(6), + VariantWith(static_cast(EnvironmentVariable::VERSION)))))); // BLOCKNUMBER - EXPECT_THAT(instructions.at(6), - AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), - Field(&Instruction::operands, - ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::BLOCKNUMBER)), - VariantWith(7))))); + EXPECT_THAT( + instructions.at(6), + AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), + Field(&Instruction::operands, + ElementsAre(VariantWith(0), + VariantWith(7), + VariantWith(static_cast(EnvironmentVariable::BLOCKNUMBER)))))); // TIMESTAMP EXPECT_THAT(instructions.at(7), AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), Field(&Instruction::operands, ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::TIMESTAMP)), - VariantWith(8))))); + VariantWith(8), + VariantWith(static_cast(EnvironmentVariable::TIMESTAMP)))))); // FEEPERL2GAS - EXPECT_THAT(instructions.at(8), - AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), - Field(&Instruction::operands, - ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::FEEPERL2GAS)), - VariantWith(9))))); + EXPECT_THAT( + instructions.at(8), + AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), + Field(&Instruction::operands, + ElementsAre(VariantWith(0), + VariantWith(9), + VariantWith(static_cast(EnvironmentVariable::FEEPERL2GAS)))))); // FEEPERDAGAS - EXPECT_THAT(instructions.at(9), - AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), - Field(&Instruction::operands, - ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::FEEPERDAGAS)), - VariantWith(10))))); + EXPECT_THAT( + instructions.at(9), + AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), + Field(&Instruction::operands, + ElementsAre(VariantWith(0), + VariantWith(10), + VariantWith(static_cast(EnvironmentVariable::FEEPERDAGAS)))))); // ISSTATICCALL - EXPECT_THAT(instructions.at(10), - AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), - Field(&Instruction::operands, - ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::ISSTATICCALL)), - VariantWith(11))))); + EXPECT_THAT( + instructions.at(10), + AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), + Field(&Instruction::operands, + ElementsAre(VariantWith(0), + VariantWith(11), + VariantWith(static_cast(EnvironmentVariable::ISSTATICCALL)))))); // Public inputs for the circuit std::vector calldata; @@ -1660,24 +1636,22 @@ TEST_F(AvmExecutionTests, getEnvOpcode) // Positive test for L2GASLEFT opcode TEST_F(AvmExecutionTests, l2GasLeft) { - std::string bytecode_hex = to_hex(OpCode::SET_16) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0101" // val 257 - "0011" // dst_offset 17 - + to_hex(OpCode::GETENVVAR_16) + // opcode L2GASLEFT - "01" // Indirect flag + std::string bytecode_hex = to_hex(OpCode::SET_16) + // opcode SET + "00" // Indirect flag + "0011" // dst_offset 17 + + to_hex(AvmMemoryTag::U32) + "0101" // val 257 + + to_hex(OpCode::GETENVVAR_16) + // opcode L2GASLEFT + "01" // Indirect flag + "0011" // dst_offset (indirect addr: 17) + to_hex(static_cast(EnvironmentVariable::L2GASLEFT)) + - "0011" // dst_offset (indirect addr: 17) - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF"; // ret size offset 255 + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF"; // ret size offset 255 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -1689,8 +1663,8 @@ TEST_F(AvmExecutionTests, l2GasLeft) AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), Field(&Instruction::operands, ElementsAre(VariantWith(1), - VariantWith(static_cast(EnvironmentVariable::L2GASLEFT)), - VariantWith(17))))); + VariantWith(17), + VariantWith(static_cast(EnvironmentVariable::L2GASLEFT)))))); auto trace = gen_trace_from_bytecode(bytecode); @@ -1716,17 +1690,16 @@ TEST_F(AvmExecutionTests, daGasLeft) "09" // addr b 9 + to_hex(OpCode::GETENVVAR_16) + // opcode DAGASLEFT "00" // Indirect flag + "0027" // dst_offset (indirect addr: 17) + to_hex(static_cast(EnvironmentVariable::DAGASLEFT)) + - "0027" // dst_offset (indirect addr: 17) - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF"; // ret size offset 255 + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF"; // ret size offset 255 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -1738,8 +1711,8 @@ TEST_F(AvmExecutionTests, daGasLeft) AllOf(Field(&Instruction::op_code, OpCode::GETENVVAR_16), Field(&Instruction::operands, ElementsAre(VariantWith(0), - VariantWith(static_cast(EnvironmentVariable::DAGASLEFT)), - VariantWith(39))))); + VariantWith(39), + VariantWith(static_cast(EnvironmentVariable::DAGASLEFT)))))); auto trace = gen_trace_from_bytecode(bytecode); @@ -1760,7 +1733,7 @@ TEST_F(AvmExecutionTests, ExecutorThrowsWithTooMuchGasAllocated) { std::string bytecode_hex = to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16(sender) "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::SENDER)) + "0007"; // addr 7 + + "0007" + to_hex(static_cast(EnvironmentVariable::SENDER)); // addr 7 std::vector calldata = {}; std::vector returndata = {}; @@ -1781,7 +1754,7 @@ TEST_F(AvmExecutionTests, ExecutorThrowsWithIncorrectNumberOfPublicInputs) { std::string bytecode_hex = to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16(sender) "00" // Indirect flag - + to_hex(static_cast(EnvironmentVariable::SENDER)) + "0007"; // addr 7 + + "0007" + to_hex(static_cast(EnvironmentVariable::SENDER)); // addr 7 std::vector calldata = {}; std::vector returndata = {}; @@ -1800,19 +1773,19 @@ TEST_F(AvmExecutionTests, kernelOutputEmitOpcodes) // Set values into the first register to emit std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode Set "00" // Indirect flag + "02" // dst_offset 2 + to_hex(AvmMemoryTag::U32) + // tag U32 "00" // value 0 - "02" // dst_offset 2 + to_hex(OpCode::SET_8) + // opcode Set "00" // Indirect flag + "01" // dst_offset 1 + to_hex(AvmMemoryTag::U32) + // tag U32 "01" // value 1 - "01" // dst_offset 1 + to_hex(OpCode::CAST_8) + // opcode CAST (to field) "00" // Indirect flag - + to_hex(AvmMemoryTag::FF) + // tag FF "01" // dst 1 "01" // dst 1 + + to_hex(AvmMemoryTag::FF) // tag FF + to_hex(OpCode::EMITNOTEHASH) + // opcode EMITNOTEHASH "00" // Indirect flag "0001" // src offset 1 @@ -1937,29 +1910,28 @@ TEST_F(AvmExecutionTests, kernelOutputEmitOpcodes) TEST_F(AvmExecutionTests, kernelOutputStorageLoadOpcodeSimple) { // Sload from a value that has not previously been written to will require a hint to process - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "09" // value 9 - "01" // dst_offset 1 - + to_hex(OpCode::CAST_8) + // opcode CAST (Cast set to field) - "00" // Indirect flag - + to_hex(AvmMemoryTag::FF) + - "01" // dst 1 - "01" // dst 1 - + to_hex(OpCode::SLOAD) + // opcode SLOAD - "00" // Indirect flag - "0001" // slot offset 1 - "0002" // write storage value to offset 2 - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF"; // ret size offset 255 + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // dst_offset 1 + + to_hex(AvmMemoryTag::U32) + "09" // value 9 + + to_hex(OpCode::CAST_8) + // opcode CAST (Cast set to field) + "00" // Indirect flag + "01" // dst 1 + "01" // dst 1 + + to_hex(AvmMemoryTag::FF) // + + to_hex(OpCode::SLOAD) + // opcode SLOAD + "00" // Indirect flag + "0001" // slot offset 1 + "0002" // write storage value to offset 2 + + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + // + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF"; // ret size offset 255 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -1998,34 +1970,34 @@ TEST_F(AvmExecutionTests, kernelOutputStorageStoreOpcodeSimple) { // SSTORE, write 2 elements of calldata to dstOffset 1 and 2. std::vector calldata = { 42, 123, 9, 10 }; - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val - "00" // dst_offset - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "04" // val - "01" + - to_hex(OpCode::CALLDATACOPY) + // opcode CALLDATACOPY - "00" // Indirect flag - "0000" // cd_offset - "0001" // copy_size - "0001" // dst_offset, (i.e. where we store the addr) - + to_hex(OpCode::SSTORE) + // opcode SSTORE - "00" // Indirect flag - "0001" // src offset - "0003" // slot offset - + to_hex(OpCode::SET_16) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + // tag U32 - "0000" // val: 0 - "0200" // dst_offset=512 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "0200"; // ret size offset 512 + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "00" // dst_offset + + to_hex(AvmMemoryTag::U32) // + + "00" // val + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // + + to_hex(AvmMemoryTag::U32) + // + "04" // val + + to_hex(OpCode::CALLDATACOPY) + // opcode CALLDATACOPY + "00" // Indirect flag + "0000" // cd_offset + "0001" // copy_size + "0001" // dst_offset, (i.e. where we store the addr) + + to_hex(OpCode::SSTORE) + // opcode SSTORE + "00" // Indirect flag + "0001" // src offset + "0003" // slot offset + + to_hex(OpCode::SET_16) + // opcode SET (for return size) + "00" // Indirect flag + "0200" // dst_offset=512 + + to_hex(AvmMemoryTag::U32) + // tag U32 + "0000" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "0200"; // ret size offset 512 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -2060,34 +2032,33 @@ TEST_F(AvmExecutionTests, kernelOutputStorageStoreOpcodeSimple) TEST_F(AvmExecutionTests, kernelOutputStorageOpcodes) { // Sload from a value that has not previously been written to will require a hint to process - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "09" // value 9 - "01" // dst_offset 1 + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // dst_offset 1 + + to_hex(AvmMemoryTag::U32) + // + "09" // value 9 // Cast set to field - + to_hex(OpCode::CAST_8) + // opcode CAST - "00" // Indirect flag - + to_hex(AvmMemoryTag::FF) + - "01" // dst 1 - "01" // dst 1 - + to_hex(OpCode::SLOAD) + // opcode SLOAD - "00" // Indirect flag - "0001" // slot offset 1 - "0002" // write storage value to offset 2 - + to_hex(OpCode::SSTORE) + // opcode SSTORE - "00" // Indirect flag - "0002" // src offset 2 (since the sload writes to 2) - "0001" // slot offset is 1 - + to_hex(OpCode::SET_8) + // opcode SET (for return size) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val: 0 - "FF" // dst_offset=255 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "0000" // ret offset 0 - "00FF"; // ret size offset 255 + + to_hex(OpCode::CAST_8) + // opcode CAST + "00" // Indirect flag + "01" // dst 1 + "01" // dst 1 + + to_hex(AvmMemoryTag::FF) // + + to_hex(OpCode::SLOAD) + // opcode SLOAD + "00" // Indirect flag + "0001" // slot offset 1 + "0002" // write storage value to offset 2 + + to_hex(OpCode::SSTORE) + // opcode SSTORE + "00" // Indirect flag + "0002" // src offset 2 (since the sload writes to 2) + "0001" // slot offset is 1 + + to_hex(OpCode::SET_8) + // opcode SET (for return size) + "00" // Indirect flag + "FF" // dst_offset=255 + + to_hex(AvmMemoryTag::U32) + "00" // val: 0 + + to_hex(OpCode::RETURN) + // opcode RETURN + "00" // Indirect flag + "0000" // ret offset 0 + "00FF"; // ret size offset 255 auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); @@ -2142,14 +2113,14 @@ TEST_F(AvmExecutionTests, kernelOutputHashExistsOpcodes) // hash exists from a value that has not previously been written to will require a hint to process std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET "00" // Indirect flag + "01" // dst_offset 1 + to_hex(AvmMemoryTag::U32) + // "01" // value 1 - "01" // dst_offset 1 + to_hex(OpCode::CAST_8) + // opcode CAST to field "00" // Indirect flag - + to_hex(AvmMemoryTag::FF) + // "01" // dst 1 "01" // dst 1 + + to_hex(AvmMemoryTag::FF) // + to_hex(OpCode::NOTEHASHEXISTS) + // opcode NOTEHASHEXISTS "00" // Indirect flag "0001" // slot offset 1 @@ -2167,9 +2138,9 @@ TEST_F(AvmExecutionTests, kernelOutputHashExistsOpcodes) "0003" // value write offset 2 (exists value) + to_hex(OpCode::SET_16) + // opcode SET (for return size) "00" // Indirect flag + "0200" // dst_offset=512 + to_hex(AvmMemoryTag::U32) + // tag U32 "0000" // val: 0 - "0200" // dst_offset=512 + to_hex(OpCode::RETURN) + // opcode RETURN "00" // Indirect flag "0000" // ret offset 0 @@ -2250,58 +2221,58 @@ TEST_F(AvmExecutionTests, opCallOpcodes) std::string bytecode_preamble; // Set up Gas offsets - bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET for gas offset indirect - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val 0 (address where gas tuple is located) - "11"; // dst_offset 17 + bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET for gas offset indirect + "00" // Indirect flag + "11" // dst_offset 17 + + to_hex(AvmMemoryTag::U32) + // + "00"; // val 0 (address where gas tuple is located) // Set up contract address offset - bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET for args offset indirect - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "02" // val 2 (where contract address is located) - "12"; // dst_offset 18 + bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET for args offset indirect + "00" // Indirect flag + "12" // dst_offset 18 + + to_hex(AvmMemoryTag::U32) + // + "02"; // val 2 (where contract address is located) // Set up args offset - bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET for ret offset indirect - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "03" // val 3 (the start of the args array) - "13"; // dst_offset 19 + bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET for ret offset indirect + "00" // Indirect flag + "13" // dst_offset 19 + + to_hex(AvmMemoryTag::U32) + // + "03"; // val 3 (the start of the args array) // Set up args size offset - bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET for args size indirect - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "04" // val 4 - resolved address - "14"; // dst_offset 20 + bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET for args size indirect + "00" // Indirect flag + "14" // dst_offset 20 + + to_hex(AvmMemoryTag::U32) + // + "04"; // val 4 - resolved address bytecode_preamble += to_hex(OpCode::SET_8) + // opcode SET "00" // Indirect flag + "04" // dst_offset 4 + to_hex(AvmMemoryTag::U32) + // - "00" // val 0 (args size) - "04"; // dst_offset 4 + "00"; // val 0 (args size) // Set up the ret offset - bytecode_preamble += to_hex(OpCode::SET_16) + // opcode SET for ret offset indirect - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0100" // val 256 (the start of where to write the return data) - "0015"; // dst_offset 21 + bytecode_preamble += to_hex(OpCode::SET_16) + // opcode SET for ret offset indirect + "00" // Indirect flag + "0015" // dst_offset 21 + + to_hex(AvmMemoryTag::U32) // + + "0100"; // val 256 (the start of where to write the return data) // Set up the success offset bytecode_preamble += to_hex(OpCode::SET_16) + // opcode SET for success offset indirect "00" // Indirect flag + "0016" // dst_offset 22 + to_hex(AvmMemoryTag::U32) + - "0102" // val 258 (write the success flag at ret_offset + ret_size) - "0016"; // dst_offset 22 + "0102"; // val 258 (write the success flag at ret_offset + ret_size) - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "00" // val - "00" // dst_offset - + to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "07" // val - "01" + - to_hex(OpCode::CALLDATACOPY) + // opcode CALLDATACOPY + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "00" // dst_offset + + to_hex(AvmMemoryTag::U32) + // + "00" // val + + to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // + + to_hex(AvmMemoryTag::U32) + // + "07" // val + + to_hex(OpCode::CALLDATACOPY) + // opcode CALLDATACOPY "00" // Indirect flag "0000" // cd_offset "0001" // copy_size @@ -2321,9 +2292,9 @@ TEST_F(AvmExecutionTests, opCallOpcodes) "0100" // dst offset + to_hex(OpCode::SET_16) + // opcode SET (for return size) "00" // Indirect flag + "0200" // dst_offset=512 + to_hex(AvmMemoryTag::U32) + // tag U32 "0003" // val: 3 (extra read is for the success flag) - "0200" // dst_offset=512 + to_hex(OpCode::RETURN) + // opcode RETURN "00" // Indirect flag "0100" // ret offset 8 @@ -2376,33 +2347,33 @@ TEST_F(AvmExecutionTests, opGetContractInstanceOpcode) }; auto execution_hints = ExecutionHints().with_contract_instance_hints({ { address, instance } }); - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::FF) + to_hex(address_byte) + // val - "01" // dst_offset 1 - + to_hex(OpCode::GETCONTRACTINSTANCE) + // opcode GETCONTRACTINSTANCE - "00" // Indirect flag - + to_hex(static_cast(ContractInstanceMember::DEPLOYER)) + // member enum - "0001" // address offset - "0010" // dst offset - "0011" // exists offset + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // dst_offset 1 + + to_hex(AvmMemoryTag::FF) + to_hex(address_byte) // val + + to_hex(OpCode::GETCONTRACTINSTANCE) + // opcode GETCONTRACTINSTANCE + "00" // Indirect flag + "0001" // address offset + "0010" // dst offset + "0011" // exists offset + + to_hex(static_cast(ContractInstanceMember::DEPLOYER)) // member enum + to_hex(OpCode::GETCONTRACTINSTANCE) + // opcode GETCONTRACTINSTANCE "00" // Indirect flag - + to_hex(static_cast(ContractInstanceMember::CLASS_ID)) + // member enum - "0001" // address offset - "0012" // dst offset - "0013" // exists offset + "0001" // address offset + "0012" // dst offset + "0013" // exists offset + + to_hex(static_cast(ContractInstanceMember::CLASS_ID)) // member enum + to_hex(OpCode::GETCONTRACTINSTANCE) + // opcode GETCONTRACTINSTANCE "00" // Indirect flag - + to_hex(static_cast(ContractInstanceMember::INIT_HASH)) + // member enum - "0001" // address offset - "0014" // dst offset - "0015" // exists offset + "0001" // address offset + "0014" // dst offset + "0015" // exists offset + + to_hex(static_cast(ContractInstanceMember::INIT_HASH)) // member enum + to_hex(OpCode::SET_16) + // opcode SET (for return size) "00" // Indirect flag + "0200" // dst_offset=512 + to_hex(AvmMemoryTag::U32) + // tag U32 "0006" // val: 6 (dst & exists for all 3) - "0200" // dst_offset=512 + to_hex(OpCode::RETURN) + // opcode RETURN "00" // Indirect flag "0010" // ret offset 1 @@ -2432,16 +2403,16 @@ TEST_F(AvmExecutionTests, opGetContractInstanceOpcodeBadEnum) { const uint8_t address_byte = 0x42; - std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET - "00" // Indirect flag - + to_hex(AvmMemoryTag::U8) + to_hex(address_byte) + // val - "01" // dst_offset 0 - + to_hex(OpCode::GETCONTRACTINSTANCE) + // opcode GETCONTRACTINSTANCE - "00" // Indirect flag - + to_hex(static_cast(ContractInstanceMember::MAX_MEMBER)) + // member enum - "0001" // address offset - "0010" // dst offset - "0011"; // exists offset + std::string bytecode_hex = to_hex(OpCode::SET_8) + // opcode SET + "00" // Indirect flag + "01" // dst_offset 0 + + to_hex(AvmMemoryTag::U8) + to_hex(address_byte) // val + + to_hex(OpCode::GETCONTRACTINSTANCE) + // opcode GETCONTRACTINSTANCE + "00" // Indirect flag + "0001" // address offset + "0010" // dst offset + "0011" // exists offset + + to_hex(static_cast(ContractInstanceMember::MAX_MEMBER)); // member enum auto bytecode = hex_to_bytes(bytecode_hex); auto instructions = Deserialization::parse_bytecode_statically(bytecode); diff --git a/barretenberg/cpp/src/barretenberg/vm/avm/trace/deserialization.cpp b/barretenberg/cpp/src/barretenberg/vm/avm/trace/deserialization.cpp index 75c9d023f0d..d3ad4039d15 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm/trace/deserialization.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm/trace/deserialization.cpp @@ -73,15 +73,15 @@ const std::unordered_map> OPCODE_WIRE_FORMAT = { OpCode::SHR_8, three_operand_format8 }, { OpCode::SHR_16, three_operand_format16 }, // Compute - Type Conversions - { OpCode::CAST_8, { OperandType::INDIRECT8, OperandType::TAG, OperandType::UINT8, OperandType::UINT8 } }, - { OpCode::CAST_16, { OperandType::INDIRECT8, OperandType::TAG, OperandType::UINT16, OperandType::UINT16 } }, + { OpCode::CAST_8, { OperandType::INDIRECT8, OperandType::UINT8, OperandType::UINT8, OperandType::TAG } }, + { OpCode::CAST_16, { OperandType::INDIRECT8, OperandType::UINT16, OperandType::UINT16, OperandType::TAG } }, // Execution Environment - Globals { OpCode::GETENVVAR_16, { OperandType::INDIRECT8, - OperandType::UINT8, // var idx OperandType::UINT16, + OperandType::UINT8, // var idx } }, // Execution Environment - Calldata @@ -92,17 +92,17 @@ const std::unordered_map> OPCODE_WIRE_FORMAT = // Machine State - Internal Control Flow { OpCode::JUMP_32, { OperandType::UINT32 } }, - { OpCode::JUMPI_32, { OperandType::INDIRECT8, OperandType::UINT32, OperandType::UINT16 } }, + { OpCode::JUMPI_32, { OperandType::INDIRECT8, OperandType::UINT16, OperandType::UINT32 } }, { OpCode::INTERNALCALL, { OperandType::UINT32 } }, { OpCode::INTERNALRETURN, {} }, // Machine State - Memory - { OpCode::SET_8, { OperandType::INDIRECT8, OperandType::TAG, OperandType::UINT8, OperandType::UINT8 } }, - { OpCode::SET_16, { OperandType::INDIRECT8, OperandType::TAG, OperandType::UINT16, OperandType::UINT16 } }, - { OpCode::SET_32, { OperandType::INDIRECT8, OperandType::TAG, OperandType::UINT32, OperandType::UINT16 } }, - { OpCode::SET_64, { OperandType::INDIRECT8, OperandType::TAG, OperandType::UINT64, OperandType::UINT16 } }, - { OpCode::SET_128, { OperandType::INDIRECT8, OperandType::TAG, OperandType::UINT128, OperandType::UINT16 } }, - { OpCode::SET_FF, { OperandType::INDIRECT8, OperandType::TAG, OperandType::FF, OperandType::UINT16 } }, + { OpCode::SET_8, { OperandType::INDIRECT8, OperandType::UINT8, OperandType::TAG, OperandType::UINT8 } }, + { OpCode::SET_16, { OperandType::INDIRECT8, OperandType::UINT16, OperandType::TAG, OperandType::UINT16 } }, + { OpCode::SET_32, { OperandType::INDIRECT8, OperandType::UINT16, OperandType::TAG, OperandType::UINT32 } }, + { OpCode::SET_64, { OperandType::INDIRECT8, OperandType::UINT16, OperandType::TAG, OperandType::UINT64 } }, + { OpCode::SET_128, { OperandType::INDIRECT8, OperandType::UINT16, OperandType::TAG, OperandType::UINT128 } }, + { OpCode::SET_FF, { OperandType::INDIRECT8, OperandType::UINT16, OperandType::TAG, OperandType::FF } }, { OpCode::MOV_8, { OperandType::INDIRECT8, OperandType::UINT8, OperandType::UINT8 } }, { OpCode::MOV_16, { OperandType::INDIRECT8, OperandType::UINT16, OperandType::UINT16 } }, @@ -138,7 +138,7 @@ const std::unordered_map> OPCODE_WIRE_FORMAT = /*TODO: leafIndexOffset is not constrained*/ OperandType::UINT16, OperandType::UINT16 } }, { OpCode::GETCONTRACTINSTANCE, - { OperandType::INDIRECT8, OperandType::UINT8, OperandType::UINT16, OperandType::UINT16, OperandType::UINT16 } }, + { OperandType::INDIRECT8, OperandType::UINT16, OperandType::UINT16, OperandType::UINT16, OperandType::UINT8 } }, { OpCode::EMITUNENCRYPTEDLOG, { OperandType::INDIRECT8, diff --git a/barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.cpp b/barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.cpp index 71385470d45..3bf6b095ad3 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.cpp @@ -494,25 +494,24 @@ std::vector Execution::gen_trace(std::vector const& calldata, // Compute - Type Conversions case OpCode::CAST_8: error = trace_builder.op_cast(std::get(inst.operands.at(0)), + std::get(inst.operands.at(1)), std::get(inst.operands.at(2)), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(1)), + std::get(inst.operands.at(3)), OpCode::CAST_8); break; case OpCode::CAST_16: error = trace_builder.op_cast(std::get(inst.operands.at(0)), + std::get(inst.operands.at(1)), std::get(inst.operands.at(2)), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(1)), + std::get(inst.operands.at(3)), OpCode::CAST_16); break; // Execution Environment - // TODO(https://github.com/AztecProtocol/aztec-packages/issues/6284): support indirect for below case OpCode::GETENVVAR_16: error = trace_builder.op_get_env_var(std::get(inst.operands.at(0)), - std::get(inst.operands.at(1)), - std::get(inst.operands.at(2))); + std::get(inst.operands.at(1)), + std::get(inst.operands.at(2))); break; // Execution Environment - Calldata @@ -541,8 +540,8 @@ std::vector Execution::gen_trace(std::vector const& calldata, break; case OpCode::JUMPI_32: error = trace_builder.op_jumpi(std::get(inst.operands.at(0)), - std::get(inst.operands.at(1)), - std::get(inst.operands.at(2))); + std::get(inst.operands.at(1)), + std::get(inst.operands.at(2))); break; case OpCode::INTERNALCALL: error = trace_builder.op_internal_call(std::get(inst.operands.at(0))); @@ -554,49 +553,49 @@ std::vector Execution::gen_trace(std::vector const& calldata, // Machine State - Memory case OpCode::SET_8: { error = trace_builder.op_set(std::get(inst.operands.at(0)), - std::get(inst.operands.at(2)), std::get(inst.operands.at(3)), - std::get(inst.operands.at(1)), + std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), OpCode::SET_8); break; } case OpCode::SET_16: { error = trace_builder.op_set(std::get(inst.operands.at(0)), - std::get(inst.operands.at(2)), std::get(inst.operands.at(3)), - std::get(inst.operands.at(1)), + std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), OpCode::SET_16); break; } case OpCode::SET_32: { error = trace_builder.op_set(std::get(inst.operands.at(0)), - std::get(inst.operands.at(2)), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(1)), + std::get(inst.operands.at(3)), + std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), OpCode::SET_32); break; } case OpCode::SET_64: { error = trace_builder.op_set(std::get(inst.operands.at(0)), - std::get(inst.operands.at(2)), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(1)), + std::get(inst.operands.at(3)), + std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), OpCode::SET_64); break; } case OpCode::SET_128: { error = trace_builder.op_set(std::get(inst.operands.at(0)), - uint256_t::from_uint128(std::get(inst.operands.at(2))), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(1)), + uint256_t::from_uint128(std::get(inst.operands.at(3))), + std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), OpCode::SET_128); break; } case OpCode::SET_FF: { error = trace_builder.op_set(std::get(inst.operands.at(0)), - std::get(inst.operands.at(2)), - std::get(inst.operands.at(3)), - std::get(inst.operands.at(1)), + std::get(inst.operands.at(3)), + std::get(inst.operands.at(1)), + std::get(inst.operands.at(2)), OpCode::SET_FF); break; } @@ -655,10 +654,10 @@ std::vector Execution::gen_trace(std::vector const& calldata, break; case OpCode::GETCONTRACTINSTANCE: error = trace_builder.op_get_contract_instance(std::get(inst.operands.at(0)), - std::get(inst.operands.at(1)), + std::get(inst.operands.at(1)), std::get(inst.operands.at(2)), std::get(inst.operands.at(3)), - std::get(inst.operands.at(4))); + std::get(inst.operands.at(4))); break; // Accrued Substate diff --git a/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp b/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp index 21c17268bb4..3a94ffd8dfd 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp @@ -1506,7 +1506,7 @@ RowWithError AvmTraceBuilder::create_kernel_lookup_opcode(uint8_t indirect, .error = res_error }; } -AvmError AvmTraceBuilder::op_get_env_var(uint8_t indirect, uint8_t env_var, uint32_t dst_offset) +AvmError AvmTraceBuilder::op_get_env_var(uint8_t indirect, uint32_t dst_offset, uint8_t env_var) { if (env_var >= static_cast(EnvironmentVariable::MAX_ENV_VAR)) { // Error, bad enum operand @@ -2011,10 +2011,10 @@ AvmError AvmTraceBuilder::op_jump(uint32_t jmp_dest, bool skip_gas) * Otherwise, program counter is incremented. * * @param indirect A byte encoding information about indirect/direct memory access. - * @param jmp_dest The destination to jump to * @param cond_offset Offset of the condition + * @param jmp_dest The destination to jump to */ -AvmError AvmTraceBuilder::op_jumpi(uint8_t indirect, uint32_t jmp_dest, uint32_t cond_offset) +AvmError AvmTraceBuilder::op_jumpi(uint8_t indirect, uint32_t cond_offset, uint32_t jmp_dest) { // We keep the first encountered error AvmError error = AvmError::NO_ERROR; @@ -2170,12 +2170,11 @@ AvmError AvmTraceBuilder::op_internal_return() * Therefore, no range check is required as part of this opcode relation. * * @param indirect A byte encoding information about indirect/direct memory access. - * @param val The constant to be written upcasted to u128 * @param dst_offset Memory destination offset where val is written to * @param in_tag The instruction memory tag */ AvmError AvmTraceBuilder::op_set( - uint8_t indirect, FF val_ff, uint32_t dst_offset, AvmMemoryTag in_tag, OpCode op_code, bool skip_gas) + uint8_t indirect, FF val, uint32_t dst_offset, AvmMemoryTag in_tag, OpCode op_code, bool skip_gas) { // We keep the first encountered error AvmError error = AvmError::NO_ERROR; @@ -2187,7 +2186,7 @@ AvmError AvmTraceBuilder::op_set( error = res_error; auto write_c = constrained_write_to_memory( - call_ptr, clk, resolved_dst_offset, val_ff, AvmMemoryTag::FF, in_tag, IntermRegister::IC); + call_ptr, clk, resolved_dst_offset, val, AvmMemoryTag::FF, in_tag, IntermRegister::IC); if (is_ok(error) && !write_c.tag_match) { error = AvmError::TAG_ERROR; @@ -2920,7 +2919,7 @@ AvmError AvmTraceBuilder::op_l1_to_l2_msg_exists(uint8_t indirect, } AvmError AvmTraceBuilder::op_get_contract_instance( - uint8_t indirect, uint8_t member_enum, uint16_t address_offset, uint16_t dst_offset, uint16_t exists_offset) + uint8_t indirect, uint16_t address_offset, uint16_t dst_offset, uint16_t exists_offset, uint8_t member_enum) { // We keep the first encountered error AvmError error = AvmError::NO_ERROR; @@ -3475,9 +3474,9 @@ ReturnDataError AvmTraceBuilder::op_revert(uint8_t indirect, uint32_t ret_offset AvmError AvmTraceBuilder::op_debug_log(uint8_t indirect, uint32_t message_offset, - uint32_t message_size, uint32_t fields_offset, - uint32_t fields_size_offset) + uint32_t fields_size_offset, + uint32_t message_size) { // We keep the first encountered error AvmError error = AvmError::NO_ERROR; diff --git a/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.hpp b/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.hpp index b001aee1f26..0df176ac640 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.hpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.hpp @@ -90,7 +90,7 @@ class AvmTraceBuilder { OpCode op_code = OpCode::CAST_16); // Execution Environment - AvmError op_get_env_var(uint8_t indirect, uint8_t env_var, uint32_t dst_offset); + AvmError op_get_env_var(uint8_t indirect, uint32_t dst_offset, uint8_t env_var); AvmError op_address(uint8_t indirect, uint32_t dst_offset); AvmError op_sender(uint8_t indirect, uint32_t dst_offset); AvmError op_function_selector(uint8_t indirect, uint32_t dst_offset); @@ -123,7 +123,7 @@ class AvmTraceBuilder { // Machine State - Internal Control Flow // TODO(8945): skip_gas boolean is temporary and should be removed once all fake rows are removed AvmError op_jump(uint32_t jmp_dest, bool skip_gas = false); - AvmError op_jumpi(uint8_t indirect, uint32_t jmp_dest, uint32_t cond_offset); + AvmError op_jumpi(uint8_t indirect, uint32_t cond_offset, uint32_t jmp_dest); AvmError op_internal_call(uint32_t jmp_dest); AvmError op_internal_return(); @@ -155,7 +155,7 @@ class AvmTraceBuilder { uint32_t leaf_index_offset, uint32_t dest_offset); AvmError op_get_contract_instance( - uint8_t indirect, uint8_t member_enum, uint16_t address_offset, uint16_t dst_offset, uint16_t exists_offset); + uint8_t indirect, uint16_t address_offset, uint16_t dst_offset, uint16_t exists_offset, uint8_t member_enum); // Accrued Substate AvmError op_emit_unencrypted_log(uint8_t indirect, uint32_t log_offset, uint32_t log_size_offset); @@ -181,9 +181,9 @@ class AvmTraceBuilder { // Misc AvmError op_debug_log(uint8_t indirect, uint32_t message_offset, - uint32_t message_size, uint32_t fields_offset, - uint32_t fields_size_offset); + uint32_t fields_size_offset, + uint32_t message_size); // Gadgets AvmError op_poseidon2_permutation(uint8_t indirect, uint32_t input_offset, uint32_t output_offset); diff --git a/yarn-project/simulator/src/avm/avm_gas.test.ts b/yarn-project/simulator/src/avm/avm_gas.test.ts index b3c822835eb..d6aa13d746e 100644 --- a/yarn-project/simulator/src/avm/avm_gas.test.ts +++ b/yarn-project/simulator/src/avm/avm_gas.test.ts @@ -7,9 +7,9 @@ import { encodeToBytecode } from './serialization/bytecode_serialization.js'; describe.skip('AVM simulator: dynamic gas costs per instruction', () => { it.each([ // BASE_GAS(10) * 1 + MEMORY_WRITE(100) = 110 - [new SetInstruction(/*indirect=*/ 0, /*inTag=*/ TypeTag.UINT8, /*value=*/ 1, /*dstOffset=*/ 0), [110, 0]], + [new SetInstruction(/*indirect=*/ 0, /*dstOffset=*/ 0, /*inTag=*/ TypeTag.UINT8, /*value=*/ 1), [110, 0]], // BASE_GAS(10) * 1 + MEMORY_WRITE(100) = 110 - [new SetInstruction(/*indirect=*/ 0, /*inTag=*/ TypeTag.UINT32, /*value=*/ 1, /*dstOffset=*/ 0), [110]], + [new SetInstruction(/*indirect=*/ 0, /*dstOffset=*/ 0, /*inTag=*/ TypeTag.UINT32, /*value=*/ 1), [110]], // BASE_GAS(10) * 1 + MEMORY_WRITE(100) = 110 [new CalldataCopy(/*indirect=*/ 0, /*cdOffset=*/ TypeTag.UINT8, /*copySize=*/ 1, /*dstOffset=*/ 0), [110]], // BASE_GAS(10) * 5 + MEMORY_WRITE(100) * 5 = 550 diff --git a/yarn-project/simulator/src/avm/avm_simulator.test.ts b/yarn-project/simulator/src/avm/avm_simulator.test.ts index 1c170d7f1c2..38345c3df36 100644 --- a/yarn-project/simulator/src/avm/avm_simulator.test.ts +++ b/yarn-project/simulator/src/avm/avm_simulator.test.ts @@ -84,11 +84,11 @@ describe('AVM simulator: injected bytecode', () => { beforeAll(() => { calldata = [new Fr(1), new Fr(2)]; bytecode = encodeToBytecode([ - new Set(/*indirect*/ 0, TypeTag.UINT32, /*value*/ 0, /*dstOffset*/ 0).as(Opcode.SET_8, Set.wireFormat8), - new Set(/*indirect*/ 0, TypeTag.UINT32, /*value*/ 2, /*dstOffset*/ 1).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect*/ 0, /*dstOffset*/ 0, TypeTag.UINT32, /*value*/ 0).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect*/ 0, /*dstOffset*/ 1, TypeTag.UINT32, /*value*/ 2).as(Opcode.SET_8, Set.wireFormat8), new CalldataCopy(/*indirect=*/ 0, /*cdOffset=*/ 0, /*copySize=*/ 1, /*dstOffset=*/ 0), new Add(/*indirect=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2).as(Opcode.ADD_8, Add.wireFormat8), - new Set(/*indirect*/ 0, TypeTag.UINT32, /*value*/ 1, /*dstOffset*/ 0).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect*/ 0, /*dstOffset*/ 0, TypeTag.UINT32, /*value*/ 1).as(Opcode.SET_8, Set.wireFormat8), new Return(/*indirect=*/ 0, /*returnOffset=*/ 2, /*copySizeOffset=*/ 0), ]); }); @@ -1130,9 +1130,9 @@ describe('AVM simulator: transpiled Noir contracts', () => { ], ])(`Overrun of %s`, async (_sideEffectType: string, createInstr: () => Instruction) => { const bytecode = encodeToBytecode([ - new Set(/*indirect*/ 0, TypeTag.FIELD, /*value*/ 0, /*dstOffset*/ 0).as(Opcode.SET_8, Set.wireFormat8), - new Set(/*indirect*/ 0, TypeTag.FIELD, /*value*/ 100, /*dstOffset*/ 100).as(Opcode.SET_8, Set.wireFormat8), - new Set(/*indirect*/ 0, TypeTag.UINT32, /*value*/ 1, /*dstOffset*/ 1).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect*/ 0, /*dstOffset*/ 0, TypeTag.FIELD, /*value*/ 0).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect*/ 0, /*dstOffset*/ 100, TypeTag.FIELD, /*value*/ 100).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect*/ 0, /*dstOffset*/ 1, TypeTag.UINT32, /*value*/ 1).as(Opcode.SET_8, Set.wireFormat8), createInstr(), // change value at memory offset 0 so each instr operates on a different value (important for nullifier emission) new Add(/*indirect=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 100, /*dstOffset=*/ 0).as( diff --git a/yarn-project/simulator/src/avm/opcodes/contract.test.ts b/yarn-project/simulator/src/avm/opcodes/contract.test.ts index df92874145a..236d49f4d7d 100644 --- a/yarn-project/simulator/src/avm/opcodes/contract.test.ts +++ b/yarn-project/simulator/src/avm/opcodes/contract.test.ts @@ -35,17 +35,17 @@ describe('Contract opcodes', () => { const buf = Buffer.from([ GetContractInstance.opcode, // opcode 0x01, // indirect - 0x02, // memberEnum (immediate) ...Buffer.from('1234', 'hex'), // addressOffset ...Buffer.from('a234', 'hex'), // dstOffset ...Buffer.from('b234', 'hex'), // existsOffset + 0x02, // memberEnum (immediate) ]); const inst = new GetContractInstance( /*indirect=*/ 0x01, - /*memberEnum=*/ 0x02, /*addressOffset=*/ 0x1234, /*dstOffset=*/ 0xa234, /*existsOffset=*/ 0xb234, + /*memberEnum=*/ 0x02, ); expect(GetContractInstance.deserialize(buf)).toEqual(inst); @@ -63,10 +63,10 @@ describe('Contract opcodes', () => { context.machineState.memory.set(0, new Field(address.toField())); await new GetContractInstance( /*indirect=*/ 0, - memberEnum, /*addressOffset=*/ 0, /*dstOffset=*/ 1, /*existsOffset=*/ 2, + memberEnum, ).execute(context); // value should be right @@ -95,10 +95,10 @@ describe('Contract opcodes', () => { context.machineState.memory.set(0, new Field(address.toField())); await new GetContractInstance( /*indirect=*/ 0, - memberEnum, /*addressOffset=*/ 0, /*dstOffset=*/ 1, /*existsOffset=*/ 2, + memberEnum, ).execute(context); // value should be 0 @@ -121,10 +121,10 @@ describe('Contract opcodes', () => { const invalidEnum = 255; const instruction = new GetContractInstance( /*indirect=*/ 0, - /*memberEnum=*/ invalidEnum, /*addressOffset=*/ 0, /*dstOffset=*/ 1, /*existsOffset=*/ 2, + /*memberEnum=*/ invalidEnum, ); await expect(instruction.execute(context)).rejects.toThrow( `Invalid GETCONSTRACTINSTANCE member enum ${invalidEnum}`, diff --git a/yarn-project/simulator/src/avm/opcodes/contract.ts b/yarn-project/simulator/src/avm/opcodes/contract.ts index f98c8d48422..f136b1e0bb3 100644 --- a/yarn-project/simulator/src/avm/opcodes/contract.ts +++ b/yarn-project/simulator/src/avm/opcodes/contract.ts @@ -18,18 +18,18 @@ export class GetContractInstance extends Instruction { static readonly wireFormat: OperandType[] = [ OperandType.UINT8, // opcode OperandType.UINT8, // indirect bits - OperandType.UINT8, // member enum (immediate) OperandType.UINT16, // addressOffset OperandType.UINT16, // dstOffset OperandType.UINT16, // existsOfsset + OperandType.UINT8, // member enum (immediate) ]; constructor( private indirect: number, - private memberEnum: number, private addressOffset: number, private dstOffset: number, private existsOffset: number, + private memberEnum: number, ) { super(); } diff --git a/yarn-project/simulator/src/avm/opcodes/control_flow.test.ts b/yarn-project/simulator/src/avm/opcodes/control_flow.test.ts index d5cad41ed7f..0e7cc0fe357 100644 --- a/yarn-project/simulator/src/avm/opcodes/control_flow.test.ts +++ b/yarn-project/simulator/src/avm/opcodes/control_flow.test.ts @@ -39,10 +39,10 @@ describe('Control Flow Opcodes', () => { const buf = Buffer.from([ JumpI.opcode, // opcode 0x01, // indirect - ...Buffer.from('12340000', 'hex'), // loc ...Buffer.from('a234', 'hex'), // condOffset + ...Buffer.from('12340000', 'hex'), // loc ]); - const inst = new JumpI(/*indirect=*/ 1, /*loc=*/ 0x12340000, /*condOffset=*/ 0xa234); + const inst = new JumpI(/*indirect=*/ 1, /*condOffset=*/ 0xa234, /*loc=*/ 0x12340000); expect(JumpI.deserialize(buf)).toEqual(inst); expect(inst.serialize()).toEqual(buf); @@ -57,12 +57,12 @@ describe('Control Flow Opcodes', () => { context.machineState.memory.set(0, new Uint16(1n)); context.machineState.memory.set(1, new Uint16(2n)); - const instruction = new JumpI(/*indirect=*/ 0, jumpLocation, /*condOffset=*/ 0); + const instruction = new JumpI(/*indirect=*/ 0, /*condOffset=*/ 0, jumpLocation); await instruction.execute(context); expect(context.machineState.pc).toBe(jumpLocation); // Truthy can be greater than 1 - const instruction1 = new JumpI(/*indirect=*/ 0, jumpLocation1, /*condOffset=*/ 1); + const instruction1 = new JumpI(/*indirect=*/ 0, /*condOffset=*/ 1, jumpLocation1); await instruction1.execute(context); expect(context.machineState.pc).toBe(jumpLocation1); }); @@ -75,7 +75,7 @@ describe('Control Flow Opcodes', () => { context.machineState.memory.set(0, new Uint16(0n)); - const instruction = new JumpI(/*indirect=*/ 0, jumpLocation, /*condOffset=*/ 0); + const instruction = new JumpI(/*indirect=*/ 0, /*condOffset=*/ 0, jumpLocation); await instruction.execute(context); expect(context.machineState.pc).toBe(30); }); diff --git a/yarn-project/simulator/src/avm/opcodes/control_flow.ts b/yarn-project/simulator/src/avm/opcodes/control_flow.ts index f4dd23246c1..2bb17c1e8ba 100644 --- a/yarn-project/simulator/src/avm/opcodes/control_flow.ts +++ b/yarn-project/simulator/src/avm/opcodes/control_flow.ts @@ -36,11 +36,11 @@ export class JumpI extends Instruction { static readonly wireFormat: OperandType[] = [ OperandType.UINT8, OperandType.UINT8, - OperandType.UINT32, OperandType.UINT16, + OperandType.UINT32, ]; - constructor(private indirect: number, private loc: number, private condOffset: number) { + constructor(private indirect: number, private condOffset: number, private loc: number) { super(); } diff --git a/yarn-project/simulator/src/avm/opcodes/environment_getters.test.ts b/yarn-project/simulator/src/avm/opcodes/environment_getters.test.ts index 8821e52cdf1..c13cc8a70e9 100644 --- a/yarn-project/simulator/src/avm/opcodes/environment_getters.test.ts +++ b/yarn-project/simulator/src/avm/opcodes/environment_getters.test.ts @@ -48,10 +48,10 @@ describe('Environment getters', () => { const buf = Buffer.from([ Opcode.GETENVVAR_16, // opcode 0x01, // indirect - 0x05, // var idx ...Buffer.from('1234', 'hex'), // dstOffset + 0x05, // var idx ]); - const instr = new GetEnvVar(/*indirect=*/ 0x01, 5, /*dstOffset=*/ 0x1234).as( + const instr = new GetEnvVar(/*indirect=*/ 0x01, /*dstOffset=*/ 0x1234, 5).as( Opcode.GETENVVAR_16, GetEnvVar.wireFormat16, ); @@ -74,7 +74,7 @@ describe('Environment getters', () => { [EnvironmentVariable.ISSTATICCALL, new Fr(isStaticCall ? 1 : 0)], ])('Environment getter instructions', (envVar: EnvironmentVariable, value: Fr, tag: TypeTag = TypeTag.FIELD) => { it(`Should read '${EnvironmentVariable[envVar]}' correctly`, async () => { - const instruction = new GetEnvVar(/*indirect=*/ 0, envVar, /*dstOffset=*/ 0); + const instruction = new GetEnvVar(/*indirect=*/ 0, /*dstOffset=*/ 0, envVar); await instruction.execute(context); @@ -86,7 +86,7 @@ describe('Environment getters', () => { it(`GETENVVAR reverts for bad enum operand`, async () => { const invalidEnum = 255; - const instruction = new GetEnvVar(/*indirect=*/ 0, invalidEnum, /*dstOffset=*/ 0); + const instruction = new GetEnvVar(/*indirect=*/ 0, /*dstOffset=*/ 0, invalidEnum); await expect(instruction.execute(context)).rejects.toThrowError(`Invalid GETENVVAR var enum ${invalidEnum}`); }); }); diff --git a/yarn-project/simulator/src/avm/opcodes/environment_getters.ts b/yarn-project/simulator/src/avm/opcodes/environment_getters.ts index 92e9de1b8e8..da29aa3948b 100644 --- a/yarn-project/simulator/src/avm/opcodes/environment_getters.ts +++ b/yarn-project/simulator/src/avm/opcodes/environment_getters.ts @@ -60,11 +60,11 @@ export class GetEnvVar extends Instruction { static readonly wireFormat16: OperandType[] = [ OperandType.UINT8, // opcode OperandType.UINT8, // indirect - OperandType.UINT8, // variable enum (immediate) OperandType.UINT16, // dstOffset + OperandType.UINT8, // variable enum (immediate) ]; - constructor(private indirect: number, private varEnum: number, private dstOffset: number) { + constructor(private indirect: number, private dstOffset: number, private varEnum: number) { super(); } diff --git a/yarn-project/simulator/src/avm/opcodes/external_calls.test.ts b/yarn-project/simulator/src/avm/opcodes/external_calls.test.ts index 24605b642da..3dbefe89fe9 100644 --- a/yarn-project/simulator/src/avm/opcodes/external_calls.test.ts +++ b/yarn-project/simulator/src/avm/opcodes/external_calls.test.ts @@ -107,9 +107,9 @@ describe('External Calls', () => { const otherContextInstructionsBytecode = markBytecodeAsAvm( encodeToBytecode([ - new Set(/*indirect=*/ 0, TypeTag.UINT32, 0, /*dstOffset=*/ 0).as(Opcode.SET_8, Set.wireFormat8), - new Set(/*indirect=*/ 0, TypeTag.UINT32, argsSize, /*dstOffset=*/ 1).as(Opcode.SET_8, Set.wireFormat8), - new Set(/*indirect=*/ 0, TypeTag.UINT32, 2, /*dstOffset=*/ 2).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect=*/ 0, /*dstOffset=*/ 0, TypeTag.UINT32, 0).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect=*/ 0, /*dstOffset=*/ 1, TypeTag.UINT32, argsSize).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect=*/ 0, /*dstOffset=*/ 2, TypeTag.UINT32, 2).as(Opcode.SET_8, Set.wireFormat8), new CalldataCopy(/*indirect=*/ 0, /*csOffsetAddress=*/ 0, /*copySizeOffset=*/ 1, /*dstOffset=*/ 3), new Return(/*indirect=*/ 0, /*retOffset=*/ 3, /*sizeOffset=*/ 2), ]), @@ -157,11 +157,11 @@ describe('External Calls', () => { const otherContextInstructionsBytecode = markBytecodeAsAvm( encodeToBytecode([ - new GetEnvVar(/*indirect=*/ 0, /*envVar=*/ EnvironmentVariable.L2GASLEFT, /*dstOffset=*/ 0).as( + new GetEnvVar(/*indirect=*/ 0, /*dstOffset=*/ 0, /*envVar=*/ EnvironmentVariable.L2GASLEFT).as( Opcode.GETENVVAR_16, GetEnvVar.wireFormat16, ), - new Set(/*indirect=*/ 0, TypeTag.UINT32, 1, /*dstOffset=*/ 1).as(Opcode.SET_8, Set.wireFormat8), + new Set(/*indirect=*/ 0, /*dstOffset=*/ 1, TypeTag.UINT32, 1).as(Opcode.SET_8, Set.wireFormat8), new Return(/*indirect=*/ 0, /*retOffset=*/ 0, /*size=*/ 1), ]), ); diff --git a/yarn-project/simulator/src/avm/opcodes/memory.test.ts b/yarn-project/simulator/src/avm/opcodes/memory.test.ts index 5f7bf1ae6e7..3a43ecc2305 100644 --- a/yarn-project/simulator/src/avm/opcodes/memory.test.ts +++ b/yarn-project/simulator/src/avm/opcodes/memory.test.ts @@ -19,11 +19,11 @@ describe('Memory instructions', () => { const buf = Buffer.from([ Opcode.SET_8, // opcode 0x01, // indirect + ...Buffer.from('56', 'hex'), // dstOffset TypeTag.UINT8, // inTag ...Buffer.from('12', 'hex'), - ...Buffer.from('56', 'hex'), // dstOffset ]); - const inst = new Set(/*indirect=*/ 0x01, /*inTag=*/ TypeTag.UINT8, /*value=*/ 0x12, /*dstOffset=*/ 0x56).as( + const inst = new Set(/*indirect=*/ 0x01, /*dstOffset=*/ 0x56, /*inTag=*/ TypeTag.UINT8, /*value=*/ 0x12).as( Opcode.SET_8, Set.wireFormat8, ); @@ -36,11 +36,11 @@ describe('Memory instructions', () => { const buf = Buffer.from([ Opcode.SET_16, // opcode 0x01, // indirect + ...Buffer.from('3456', 'hex'), // dstOffset TypeTag.UINT16, // inTag ...Buffer.from('1234', 'hex'), - ...Buffer.from('3456', 'hex'), // dstOffset ]); - const inst = new Set(/*indirect=*/ 0x01, /*inTag=*/ TypeTag.UINT16, /*value=*/ 0x1234, /*dstOffset=*/ 0x3456).as( + const inst = new Set(/*indirect=*/ 0x01, /*dstOffset=*/ 0x3456, /*inTag=*/ TypeTag.UINT16, /*value=*/ 0x1234).as( Opcode.SET_16, Set.wireFormat16, ); @@ -53,15 +53,15 @@ describe('Memory instructions', () => { const buf = Buffer.from([ Opcode.SET_32, // opcode 0x01, // indirect + ...Buffer.from('3456', 'hex'), // dstOffset TypeTag.UINT32, // inTag ...Buffer.from('12345678', 'hex'), - ...Buffer.from('3456', 'hex'), // dstOffset ]); const inst = new Set( /*indirect=*/ 0x01, + /*dstOffset=*/ 0x3456, /*inTag=*/ TypeTag.UINT32, /*value=*/ 0x12345678, - /*dstOffset=*/ 0x3456, ).as(Opcode.SET_32, Set.wireFormat32); expect(Set.as(Set.wireFormat32).deserialize(buf)).toEqual(inst); @@ -72,15 +72,15 @@ describe('Memory instructions', () => { const buf = Buffer.from([ Opcode.SET_64, // opcode 0x01, // indirect + ...Buffer.from('34567', 'hex'), // dstOffset TypeTag.UINT64, // inTag ...Buffer.from('1234567812345678', 'hex'), - ...Buffer.from('34567', 'hex'), // dstOffset ]); const inst = new Set( /*indirect=*/ 0x01, + /*dstOffset=*/ 0x3456, /*inTag=*/ TypeTag.UINT64, /*value=*/ 0x1234567812345678n, - /*dstOffset=*/ 0x3456, ).as(Opcode.SET_64, Set.wireFormat64); expect(Set.as(Set.wireFormat64).deserialize(buf)).toEqual(inst); @@ -91,15 +91,15 @@ describe('Memory instructions', () => { const buf = Buffer.from([ Opcode.SET_128, // opcode 0x01, // indirect + ...Buffer.from('3456', 'hex'), // dstOffset TypeTag.UINT128, // inTag ...Buffer.from('12345678123456781234567812345678', 'hex'), // const (will be 128 bit) - ...Buffer.from('3456', 'hex'), // dstOffset ]); const inst = new Set( /*indirect=*/ 0x01, + /*dstOffset=*/ 0x3456, /*inTag=*/ TypeTag.UINT128, /*value=*/ 0x12345678123456781234567812345678n, - /*dstOffset=*/ 0x3456, ).as(Opcode.SET_128, Set.wireFormat128); expect(Set.as(Set.wireFormat128).deserialize(buf)).toEqual(inst); @@ -110,15 +110,15 @@ describe('Memory instructions', () => { const buf = Buffer.from([ Opcode.SET_FF, // opcode 0x01, // indirect + ...Buffer.from('3456', 'hex'), // dstOffset TypeTag.UINT128, // inTag ...Buffer.from('1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', 'hex'), // const (will be 32 bytes) - ...Buffer.from('3456', 'hex'), // dstOffset ]); const inst = new Set( /*indirect=*/ 0x01, + /*dstOffset=*/ 0x3456, /*inTag=*/ TypeTag.UINT128, /*value=*/ 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdefn, - /*dstOffset=*/ 0x3456, ).as(Opcode.SET_FF, Set.wireFormatFF); expect(Set.as(Set.wireFormatFF).deserialize(buf)).toEqual(inst); @@ -126,7 +126,7 @@ describe('Memory instructions', () => { }); it('should correctly set value and tag (uninitialized)', async () => { - await new Set(/*indirect=*/ 0, /*inTag=*/ TypeTag.UINT16, /*value=*/ 1234n, /*offset=*/ 1).execute(context); + await new Set(/*indirect=*/ 0, /*offset=*/ 1, /*inTag=*/ TypeTag.UINT16, /*value=*/ 1234n).execute(context); const actual = context.machineState.memory.get(1); const tag = context.machineState.memory.getTag(1); @@ -138,7 +138,7 @@ describe('Memory instructions', () => { it('should correctly set value and tag (overwriting)', async () => { context.machineState.memory.set(1, new Field(27)); - await new Set(/*indirect=*/ 0, /*inTag=*/ TypeTag.UINT32, /*value=*/ 1234n, /*offset=*/ 1).execute(context); + await new Set(/*indirect=*/ 0, /*offset=*/ 1, /*inTag=*/ TypeTag.UINT32, /*value=*/ 1234n).execute(context); const actual = context.machineState.memory.get(1); const tag = context.machineState.memory.getTag(1); @@ -148,7 +148,7 @@ describe('Memory instructions', () => { }); it('should correctly set value and tag (truncating)', async () => { - await new Set(/*indirect=*/ 0, /*inTag=*/ TypeTag.UINT16, /*value=*/ 0x12345678n, /*offset=*/ 1).execute(context); + await new Set(/*indirect=*/ 0, /*offset=*/ 1, /*inTag=*/ TypeTag.UINT16, /*value=*/ 0x12345678n).execute(context); const actual = context.machineState.memory.get(1); const tag = context.machineState.memory.getTag(1); @@ -163,15 +163,15 @@ describe('Memory instructions', () => { const buf = Buffer.from([ Opcode.CAST_16, // opcode 0x01, // indirect - TypeTag.FIELD, // dstTag ...Buffer.from('1234', 'hex'), // aOffset ...Buffer.from('3456', 'hex'), // dstOffset + TypeTag.FIELD, // dstTag ]); const inst = new Cast( /*indirect=*/ 0x01, - /*dstTag=*/ TypeTag.FIELD, /*aOffset=*/ 0x1234, /*dstOffset=*/ 0x3456, + /*dstTag=*/ TypeTag.FIELD, ).as(Opcode.CAST_16, Cast.wireFormat16); expect(Cast.as(Cast.wireFormat16).deserialize(buf)).toEqual(inst); @@ -186,11 +186,11 @@ describe('Memory instructions', () => { context.machineState.memory.set(4, new Uint128(1n << 100n)); const ops = [ - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT16, /*aOffset=*/ 0, /*dstOffset=*/ 10), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT32, /*aOffset=*/ 1, /*dstOffset=*/ 11), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT64, /*aOffset=*/ 2, /*dstOffset=*/ 12), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT128, /*aOffset=*/ 3, /*dstOffset=*/ 13), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT128, /*aOffset=*/ 4, /*dstOffset=*/ 14), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 0, /*dstOffset=*/ 10, /*dstTag=*/ TypeTag.UINT16), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 1, /*dstOffset=*/ 11, /*dstTag=*/ TypeTag.UINT32), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 2, /*dstOffset=*/ 12, /*dstTag=*/ TypeTag.UINT64), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 3, /*dstOffset=*/ 13, /*dstTag=*/ TypeTag.UINT128), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 4, /*dstOffset=*/ 14, /*dstTag=*/ TypeTag.UINT128), ]; for (const op of ops) { @@ -217,11 +217,11 @@ describe('Memory instructions', () => { context.machineState.memory.set(4, new Uint128((1n << 100n) - 1n)); const ops = [ - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT8, /*aOffset=*/ 0, /*dstOffset=*/ 10), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT8, /*aOffset=*/ 1, /*dstOffset=*/ 11), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT16, /*aOffset=*/ 2, /*dstOffset=*/ 12), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT32, /*aOffset=*/ 3, /*dstOffset=*/ 13), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT64, /*aOffset=*/ 4, /*dstOffset=*/ 14), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 0, /*dstOffset=*/ 10, /*dstTag=*/ TypeTag.UINT8), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 1, /*dstOffset=*/ 11, /*dstTag=*/ TypeTag.UINT8), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 2, /*dstOffset=*/ 12, /*dstTag=*/ TypeTag.UINT16), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 3, /*dstOffset=*/ 13, /*dstTag=*/ TypeTag.UINT32), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 4, /*dstOffset=*/ 14, /*dstTag=*/ TypeTag.UINT64), ]; for (const op of ops) { @@ -248,11 +248,11 @@ describe('Memory instructions', () => { context.machineState.memory.set(4, new Uint128(1n << 100n)); const ops = [ - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.FIELD, /*aOffset=*/ 0, /*dstOffset=*/ 10), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.FIELD, /*aOffset=*/ 1, /*dstOffset=*/ 11), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.FIELD, /*aOffset=*/ 2, /*dstOffset=*/ 12), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.FIELD, /*aOffset=*/ 3, /*dstOffset=*/ 13), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.FIELD, /*aOffset=*/ 4, /*dstOffset=*/ 14), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 0, /*dstOffset=*/ 10, /*dstTag=*/ TypeTag.FIELD), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 1, /*dstOffset=*/ 11, /*dstTag=*/ TypeTag.FIELD), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 2, /*dstOffset=*/ 12, /*dstTag=*/ TypeTag.FIELD), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 3, /*dstOffset=*/ 13, /*dstTag=*/ TypeTag.FIELD), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 4, /*dstOffset=*/ 14, /*dstTag=*/ TypeTag.FIELD), ]; for (const op of ops) { @@ -279,11 +279,11 @@ describe('Memory instructions', () => { context.machineState.memory.set(4, new Field((1n << 200n) - 1n)); const ops = [ - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT8, /*aOffset=*/ 0, /*dstOffset=*/ 10), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT16, /*aOffset=*/ 1, /*dstOffset=*/ 11), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT32, /*aOffset=*/ 2, /*dstOffset=*/ 12), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT64, /*aOffset=*/ 3, /*dstOffset=*/ 13), - new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.UINT128, /*aOffset=*/ 4, /*dstOffset=*/ 14), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 0, /*dstOffset=*/ 10, /*dstTag=*/ TypeTag.UINT8), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 1, /*dstOffset=*/ 11, /*dstTag=*/ TypeTag.UINT16), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 2, /*dstOffset=*/ 12, /*dstTag=*/ TypeTag.UINT32), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 3, /*dstOffset=*/ 13, /*dstTag=*/ TypeTag.UINT64), + new Cast(/*indirect=*/ 0, /*aOffset=*/ 4, /*dstOffset=*/ 14, /*dstTag=*/ TypeTag.UINT128), ]; for (const op of ops) { @@ -305,7 +305,7 @@ describe('Memory instructions', () => { it('Should cast between field elements', async () => { context.machineState.memory.set(0, new Field(12345678n)); - await new Cast(/*indirect=*/ 0, /*dstTag=*/ TypeTag.FIELD, /*aOffset=*/ 0, /*dstOffset=*/ 1).execute(context); + await new Cast(/*indirect=*/ 0, /*aOffset=*/ 0, /*dstOffset=*/ 1, /*dstTag=*/ TypeTag.FIELD).execute(context); const actual = context.machineState.memory.get(1); expect(actual).toEqual(new Field(12345678n)); diff --git a/yarn-project/simulator/src/avm/opcodes/memory.ts b/yarn-project/simulator/src/avm/opcodes/memory.ts index 89706285d1d..c79d7b7e1af 100644 --- a/yarn-project/simulator/src/avm/opcodes/memory.ts +++ b/yarn-project/simulator/src/avm/opcodes/memory.ts @@ -12,51 +12,51 @@ export class Set extends Instruction { public static readonly wireFormat8: OperandType[] = [ OperandType.UINT8, // opcode OperandType.UINT8, // indirect + OperandType.UINT8, // dstOffset OperandType.UINT8, // tag OperandType.UINT8, // const (value) - OperandType.UINT8, // dstOffset ]; public static readonly wireFormat16: OperandType[] = [ OperandType.UINT8, // opcode OperandType.UINT8, // indirect + OperandType.UINT16, // dstOffset OperandType.UINT8, // tag OperandType.UINT16, // const (value) - OperandType.UINT16, // dstOffset ]; public static readonly wireFormat32: OperandType[] = [ OperandType.UINT8, // opcode OperandType.UINT8, // indirect + OperandType.UINT16, // dstOffset OperandType.UINT8, // tag OperandType.UINT32, // const (value) - OperandType.UINT16, // dstOffset ]; public static readonly wireFormat64: OperandType[] = [ OperandType.UINT8, // opcode OperandType.UINT8, // indirect + OperandType.UINT16, // dstOffset OperandType.UINT8, // tag OperandType.UINT64, // const (value) - OperandType.UINT16, // dstOffset ]; public static readonly wireFormat128: OperandType[] = [ OperandType.UINT8, // opcode OperandType.UINT8, // indirect + OperandType.UINT16, // dstOffset OperandType.UINT8, // tag OperandType.UINT128, // const (value) - OperandType.UINT16, // dstOffset ]; public static readonly wireFormatFF: OperandType[] = [ OperandType.UINT8, // opcode OperandType.UINT8, // indirect + OperandType.UINT16, // dstOffset OperandType.UINT8, // tag OperandType.FF, // const (value) - OperandType.UINT16, // dstOffset ]; constructor( private indirect: number, + private dstOffset: number, private inTag: number, private value: bigint | number, - private dstOffset: number, ) { super(); } @@ -87,14 +87,14 @@ export class Cast extends Instruction { OperandType.UINT8, ]; static readonly wireFormat16 = [ - OperandType.UINT8, OperandType.UINT8, OperandType.UINT8, OperandType.UINT16, OperandType.UINT16, + OperandType.UINT8, ]; - constructor(private indirect: number, private dstTag: number, private srcOffset: number, private dstOffset: number) { + constructor(private indirect: number, private srcOffset: number, private dstOffset: number, private dstTag: number) { super(); } diff --git a/yarn-project/simulator/src/avm/opcodes/misc.ts b/yarn-project/simulator/src/avm/opcodes/misc.ts index 4e92531052f..199fa47f988 100644 --- a/yarn-project/simulator/src/avm/opcodes/misc.ts +++ b/yarn-project/simulator/src/avm/opcodes/misc.ts @@ -16,17 +16,17 @@ export class DebugLog extends Instruction { OperandType.UINT8, // Opcode OperandType.UINT8, // Indirect OperandType.UINT16, // message memory address - OperandType.UINT16, // message size OperandType.UINT16, // fields memory address OperandType.UINT16, // fields size address + OperandType.UINT16, // message size ]; constructor( private indirect: number, private messageOffset: number, - private messageSize: number, private fieldsOffset: number, private fieldsSizeOffset: number, + private messageSize: number, ) { super(); } diff --git a/yarn-project/simulator/src/avm/serialization/bytecode_serialization.test.ts b/yarn-project/simulator/src/avm/serialization/bytecode_serialization.test.ts index 7662b5300c6..ce451ff92b0 100644 --- a/yarn-project/simulator/src/avm/serialization/bytecode_serialization.test.ts +++ b/yarn-project/simulator/src/avm/serialization/bytecode_serialization.test.ts @@ -74,7 +74,7 @@ describe('Bytecode Serialization', () => { const instructions = [ new Add(/*indirect=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2).as(Opcode.ADD_8, Add.wireFormat8), new Sub(/*indirect=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2).as(Opcode.SUB_8, Sub.wireFormat8), - new GetEnvVar(/*indirect=*/ 0, EnvironmentVariable.ADDRESS, /*dstOffset=*/ 1).as( + new GetEnvVar(/*indirect=*/ 0, /*dstOffset=*/ 1, EnvironmentVariable.ADDRESS).as( Opcode.GETENVVAR_16, GetEnvVar.wireFormat16, ), @@ -106,7 +106,7 @@ describe('Bytecode Serialization', () => { const instructions = [ new Add(/*indirect=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2).as(Opcode.ADD_8, Add.wireFormat8), new Sub(/*indirect=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2).as(Opcode.SUB_8, Sub.wireFormat8), - new GetEnvVar(/*indirect=*/ 0, EnvironmentVariable.ADDRESS, /*dstOffset=*/ 1).as( + new GetEnvVar(/*indirect=*/ 0, /*dstOffset=*/ 1, EnvironmentVariable.ADDRESS).as( Opcode.GETENVVAR_16, GetEnvVar.wireFormat16, ),