-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
203 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
yarn-project/acir-simulator/src/avm/opcodes/accrued_substate.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Fr } from '@aztec/foundation/fields'; | ||
|
||
import { mock } from 'jest-mock-extended'; | ||
|
||
import { AvmMachineState } from '../avm_machine_state.js'; | ||
import { initExecutionEnvironment } from '../fixtures/index.js'; | ||
import { HostStorage } from '../journal/host_storage.js'; | ||
import { AvmJournal } from '../journal/journal.js'; | ||
import { EmitNoteHash, EmitNullifier, EmitUnencryptedLog, SendL2ToL1Message } from './accrued_substate.js'; | ||
import { StaticCallStorageAlterError } from './storage.js'; | ||
|
||
describe('Accrued Substate', () => { | ||
let journal: AvmJournal; | ||
let machineState: AvmMachineState; | ||
|
||
beforeEach(async () => { | ||
const hostStorage = mock<HostStorage>(); | ||
journal = new AvmJournal(hostStorage); | ||
machineState = new AvmMachineState(initExecutionEnvironment()); | ||
}); | ||
|
||
it('Should append a new note hash correctly', async () => { | ||
const value = new Fr(69n); | ||
machineState.writeMemory(0, value); | ||
|
||
await new EmitNoteHash(0).execute(machineState, journal); | ||
|
||
const journalState = journal.flush(); | ||
expect(journalState.newNoteHashes).toEqual([value]); | ||
}); | ||
|
||
it('Should append a new nullifier correctly', async () => { | ||
const value = new Fr(69n); | ||
machineState.writeMemory(0, value); | ||
|
||
await new EmitNullifier(0).execute(machineState, journal); | ||
|
||
const journalState = journal.flush(); | ||
expect(journalState.newNullifiers).toEqual([value]); | ||
}); | ||
|
||
it('Should append unencrypted logs correctly', async () => { | ||
const startOffset = 0; | ||
const length = 2; | ||
|
||
const values = [new Fr(69n), new Fr(420n)]; | ||
machineState.writeMemoryChunk(0, values); | ||
|
||
await new EmitUnencryptedLog(startOffset, length).execute(machineState, journal); | ||
|
||
const journalState = journal.flush(); | ||
expect(journalState.newLogs).toEqual([values]); | ||
}); | ||
|
||
it('Should append l1 to l2 messages correctly', async () => { | ||
const startOffset = 0; | ||
const length = 2; | ||
|
||
const values = [new Fr(69n), new Fr(420n)]; | ||
machineState.writeMemoryChunk(0, values); | ||
|
||
await new SendL2ToL1Message(startOffset, length).execute(machineState, journal); | ||
|
||
const journalState = journal.flush(); | ||
expect(journalState.newLogs).toEqual([values]); | ||
}); | ||
|
||
it('All substate instructions should fail within a static call', async () => { | ||
const executionEnvironment = initExecutionEnvironment({ isStaticCall: true }); | ||
machineState = new AvmMachineState(executionEnvironment); | ||
|
||
const instructions = [ | ||
new EmitNoteHash(0), | ||
new EmitNullifier(0), | ||
new EmitUnencryptedLog(0, 1), | ||
new SendL2ToL1Message(0, 1), | ||
]; | ||
|
||
for (const instruction of instructions) { | ||
const inst = () => instruction.execute(machineState, journal); | ||
await expect(inst()).rejects.toThrowError(StaticCallStorageAlterError); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { AvmMachineState } from '../avm_machine_state.js'; | ||
import { AvmJournal } from '../journal/journal.js'; | ||
import { Instruction } from './instruction.js'; | ||
import { StaticCallStorageAlterError } from './storage.js'; | ||
|
||
export class EmitNoteHash extends Instruction { | ||
static type: string = 'EMITNOTEHASH'; | ||
static numberOfOperands = 1; | ||
|
||
constructor(private noteHashOffset: number) { | ||
super(); | ||
} | ||
|
||
async execute(machineState: AvmMachineState, journal: AvmJournal): Promise<void> { | ||
if (machineState.executionEnvironment.isStaticCall) { | ||
throw new StaticCallStorageAlterError(); | ||
} | ||
|
||
const noteHash = machineState.readMemory(this.noteHashOffset); | ||
|
||
journal.writeNoteHash(noteHash); | ||
|
||
this.incrementPc(machineState); | ||
} | ||
} | ||
|
||
export class EmitNullifier extends Instruction { | ||
static type: string = 'EMITNULLIFIER'; | ||
static numberOfOperands = 1; | ||
|
||
constructor(private nullifierOffset: number) { | ||
super(); | ||
} | ||
|
||
async execute(machineState: AvmMachineState, journal: AvmJournal): Promise<void> { | ||
if (machineState.executionEnvironment.isStaticCall) { | ||
throw new StaticCallStorageAlterError(); | ||
} | ||
|
||
const nullifier = machineState.readMemory(this.nullifierOffset); | ||
|
||
journal.writeNullifier(nullifier); | ||
|
||
this.incrementPc(machineState); | ||
} | ||
} | ||
|
||
export class EmitUnencryptedLog extends Instruction { | ||
static type: string = 'EMITUNENCRYPTEDLOG'; | ||
static numberOfOperands = 2; | ||
|
||
constructor(private logOffset: number, private logSize: number) { | ||
super(); | ||
} | ||
|
||
async execute(machineState: AvmMachineState, journal: AvmJournal): Promise<void> { | ||
if (machineState.executionEnvironment.isStaticCall) { | ||
throw new StaticCallStorageAlterError(); | ||
} | ||
|
||
const log = machineState.readMemoryChunk(this.logOffset, this.logSize); | ||
|
||
journal.writeLog(log); | ||
|
||
this.incrementPc(machineState); | ||
} | ||
} | ||
|
||
export class SendL2ToL1Message extends Instruction { | ||
static type: string = 'EMITUNENCRYPTEDLOG'; | ||
static numberOfOperands = 2; | ||
|
||
constructor(private msgOffset: number, private msgSize: number) { | ||
super(); | ||
} | ||
|
||
async execute(machineState: AvmMachineState, journal: AvmJournal): Promise<void> { | ||
if (machineState.executionEnvironment.isStaticCall) { | ||
throw new StaticCallStorageAlterError(); | ||
} | ||
|
||
const msg = machineState.readMemoryChunk(this.msgOffset, this.msgSize); | ||
|
||
journal.writeLog(msg); | ||
|
||
this.incrementPc(machineState); | ||
} | ||
} |