diff --git a/avm-transpiler/src/transpile.rs b/avm-transpiler/src/transpile.rs index 73f1ebfa12cd..085b48227951 100644 --- a/avm-transpiler/src/transpile.rs +++ b/avm-transpiler/src/transpile.rs @@ -475,17 +475,6 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode]) -> (Vec< *value = resolved_location; } - // TEMPORARY: Add a "magic number" instruction to the end of the program. - // This makes it possible to know that the bytecode corresponds to the AVM. - // We are adding a MOV instruction that moves a value to itself. - // This should therefore not affect the program's execution. - avm_instrs.push(AvmInstruction { - opcode: AvmOpcode::MOV_16, - indirect: Some(AddressingModeBuilder::default().build()), - operands: vec![AvmOperand::U16 { value: 0x18ca }, AvmOperand::U16 { value: 0x18ca }], - ..Default::default() - }); - dbg_print_avm_program(&avm_instrs); // Constructing bytecode from instructions diff --git a/yarn-project/simulator/src/public/avm/avm_simulator.test.ts b/yarn-project/simulator/src/public/avm/avm_simulator.test.ts index dbbbffdff5cc..18ffcec821ff 100644 --- a/yarn-project/simulator/src/public/avm/avm_simulator.test.ts +++ b/yarn-project/simulator/src/public/avm/avm_simulator.test.ts @@ -38,7 +38,6 @@ import type { AvmContext } from './avm_context.js'; import type { AvmExecutionEnvironment } from './avm_execution_environment.js'; import { type MemoryValue, TypeTag, type Uint8, type Uint64 } from './avm_memory_types.js'; import { AvmSimulator } from './avm_simulator.js'; -import { isAvmBytecode, markBytecodeAsAvm } from './bytecode_utils.js'; import { getAvmGadgetsTestContractBytecode, getAvmTestContractArtifact, @@ -108,13 +107,9 @@ describe('AVM simulator: injected bytecode', () => { ]); }); - it('Should not be recognized as AVM bytecode (magic missing)', () => { - expect(!isAvmBytecode(bytecode)); - }); - it('Should execute bytecode that performs basic addition', async () => { const context = initContext({ env: initExecutionEnvironment({ calldata }) }); - const results = await new AvmSimulator(context).executeBytecode(markBytecodeAsAvm(bytecode)); + const results = await new AvmSimulator(context).executeBytecode(bytecode); expect(results.reverted).toBe(false); expect(results.output).toEqual([new Fr(3)]); @@ -126,7 +121,7 @@ describe('AVM simulator: injected bytecode', () => { machineState: initMachineState({ l2GasLeft: 5 }), }); - const results = await new AvmSimulator(context).executeBytecode(markBytecodeAsAvm(bytecode)); + const results = await new AvmSimulator(context).executeBytecode(bytecode); expect(results.reverted).toBe(true); expect(results.output).toEqual([]); expect(results.revertReason?.message).toEqual('Not enough L2GAS gas left'); @@ -141,7 +136,7 @@ describe('AVM simulator: injected bytecode', () => { const badBytecode = encodeToBytecode([ new Div(/*indirect=*/ 0, /*aOffset=*/ 0, /*bOffset=*/ 0, /*dstOffset=*/ 0).as(Opcode.DIV_8, Div.wireFormat8), ]); - const results = await new AvmSimulator(context).executeBytecode(markBytecodeAsAvm(badBytecode)); + const results = await new AvmSimulator(context).executeBytecode(badBytecode); expect(results.reverted).toBe(true); expect(results.output).toEqual([]); expect(results.revertReason?.message).toMatch(/Tag mismatch/); @@ -212,11 +207,6 @@ describe('AVM simulator: transpiled Noir contracts', () => { expect(results.output).toEqual([new Fr(0)]); }); - it('Should be recognized as AVM bytecode (magic present)', () => { - const bytecode = getAvmTestContractBytecode('add_args_return'); - expect(isAvmBytecode(bytecode)); - }); - it('Should handle calldata oracle', async () => { const calldata: Fr[] = [new Fr(1), new Fr(2), new Fr(3)]; const context = initContext({ env: initExecutionEnvironment({ calldata }) }); @@ -1093,7 +1083,7 @@ describe('AVM simulator: transpiled Noir contracts', () => { new Jump(/*jumpOffset*/ 15), ]); const context = initContext({ persistableState }); - const results = await new AvmSimulator(context).executeBytecode(markBytecodeAsAvm(bytecode)); + const results = await new AvmSimulator(context).executeBytecode(bytecode); expect(results.reverted).toBe(true); expect(results.output).toEqual([]); expect(results.revertReason?.message).toMatch('Reached the limit'); diff --git a/yarn-project/simulator/src/public/avm/avm_simulator.ts b/yarn-project/simulator/src/public/avm/avm_simulator.ts index 454e3581b349..db3865e54b25 100644 --- a/yarn-project/simulator/src/public/avm/avm_simulator.ts +++ b/yarn-project/simulator/src/public/avm/avm_simulator.ts @@ -12,7 +12,6 @@ import { AvmContractCallResult } from './avm_contract_call_result.js'; import { AvmExecutionEnvironment } from './avm_execution_environment.js'; import type { Gas } from './avm_gas.js'; import { AvmMachineState } from './avm_machine_state.js'; -import { isAvmBytecode } from './bytecode_utils.js'; import { AvmExecutionError, AvmRevertReason, @@ -139,7 +138,6 @@ export class AvmSimulator { */ public async executeBytecode(bytecode: Buffer): Promise { const startTotalTime = performance.now(); - assert(isAvmBytecode(bytecode), "AVM simulator can't execute non-AVM bytecode"); assert(bytecode.length > 0, "AVM simulator can't execute empty bytecode"); this.bytecode = bytecode; diff --git a/yarn-project/simulator/src/public/avm/bytecode_utils.ts b/yarn-project/simulator/src/public/avm/bytecode_utils.ts deleted file mode 100644 index 73a2282ab67a..000000000000 --- a/yarn-project/simulator/src/public/avm/bytecode_utils.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Opcode } from './serialization/instruction_serialization.js'; - -const AVM_MAGIC_SUFFIX = Buffer.from([ - Opcode.MOV_16, // opcode - 0x00, // indirect - ...Buffer.from('18ca', 'hex'), // srcOffset - ...Buffer.from('18ca', 'hex'), // dstOffset -]); - -export function markBytecodeAsAvm(bytecode: Buffer): Buffer { - return Buffer.concat([bytecode, AVM_MAGIC_SUFFIX]); -} - -export function isAvmBytecode(bytecode: Buffer): boolean { - const magicSize = AVM_MAGIC_SUFFIX.length; - return bytecode.subarray(-magicSize).equals(AVM_MAGIC_SUFFIX); -} diff --git a/yarn-project/simulator/src/public/avm/opcodes/external_calls.test.ts b/yarn-project/simulator/src/public/avm/opcodes/external_calls.test.ts index 3f2f65524c7e..b852226ceba1 100644 --- a/yarn-project/simulator/src/public/avm/opcodes/external_calls.test.ts +++ b/yarn-project/simulator/src/public/avm/opcodes/external_calls.test.ts @@ -9,7 +9,6 @@ import type { PublicSideEffectTraceInterface } from '../../../public/side_effect import type { PublicContractsDB, PublicTreesDB } from '../../public_db_sources.js'; import type { AvmContext } from '../avm_context.js'; import { Field, TypeTag, Uint1, Uint32 } from '../avm_memory_types.js'; -import { markBytecodeAsAvm } from '../bytecode_utils.js'; import { initContext, initPersistableStateManager } from '../fixtures/index.js'; import type { AvmPersistableStateManager } from '../journal/journal.js'; import { encodeToBytecode } from '../serialization/bytecode_serialization.js'; @@ -118,16 +117,13 @@ describe('External Calls', () => { // Define dst offset for SuccessCopy const successDstOffset = 6; - const otherContextInstructionsBytecode = markBytecodeAsAvm( - encodeToBytecode([ - 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), - ]), - ); - + const otherContextInstructionsBytecode = encodeToBytecode([ + 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), + ]); const contractClass = await makeContractClassPublic(0, { bytecode: otherContextInstructionsBytecode, selector: FunctionSelector.random(), @@ -174,16 +170,14 @@ describe('External Calls', () => { // Define dst offset for SuccessCopy const successDstOffset = 6; - const otherContextInstructionsBytecode = markBytecodeAsAvm( - encodeToBytecode([ - new GetEnvVar(/*indirect=*/ 0, /*dstOffset=*/ 0, /*envVar=*/ EnvironmentVariable.L2GASLEFT).as( - Opcode.GETENVVAR_16, - GetEnvVar.wireFormat16, - ), - new Set(/*indirect=*/ 0, /*dstOffset=*/ 1, TypeTag.UINT32, 1).as(Opcode.SET_8, Set.wireFormat8), - new Return(/*indirect=*/ 0, /*retOffset=*/ 0, /*size=*/ 1), - ]), - ); + const otherContextInstructionsBytecode = encodeToBytecode([ + new GetEnvVar(/*indirect=*/ 0, /*dstOffset=*/ 0, /*envVar=*/ EnvironmentVariable.L2GASLEFT).as( + Opcode.GETENVVAR_16, + GetEnvVar.wireFormat16, + ), + new Set(/*indirect=*/ 0, /*dstOffset=*/ 1, TypeTag.UINT32, 1).as(Opcode.SET_8, Set.wireFormat8), + new Return(/*indirect=*/ 0, /*retOffset=*/ 0, /*size=*/ 1), + ]); mockGetNullifierIndex(treesDB, addr); const contractClass = await makeContractClassPublic(0, { @@ -264,7 +258,7 @@ describe('External Calls', () => { new SStore(/*indirect=*/ 0, /*srcOffset=*/ 0, /*slotOffset=*/ 0), ]; - const otherContextInstructionsBytecode = markBytecodeAsAvm(encodeToBytecode(otherContextInstructions)); + const otherContextInstructionsBytecode = encodeToBytecode(otherContextInstructions); mockGetNullifierIndex(treesDB, addr.toFr()); const contractClass = await makeContractClassPublic(0, {