-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: accrued substate instructions #4197
Changes from 7 commits
0b12421
02de79f
31d47b2
7a9f463
0f8ce4e
1e21250
2725d2d
07012bc
dbd7307
8cdc33c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { mock } from 'jest-mock-extended'; | ||
|
||
import { AvmMachineState } from '../avm_machine_state.js'; | ||
import { Field } from '../avm_memory_types.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(() => { | ||
const hostStorage = mock<HostStorage>(); | ||
journal = new AvmJournal(hostStorage); | ||
machineState = new AvmMachineState(initExecutionEnvironment()); | ||
}); | ||
|
||
it('Should append a new note hash correctly', async () => { | ||
const value = new Field(69n); | ||
machineState.memory.set(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 Field(69n); | ||
machineState.memory.set(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 values = [new Field(69n), new Field(420n), new Field(Field.MODULUS - 1n)]; | ||
machineState.memory.setSlice(0, values); | ||
|
||
const length = values.length; | ||
|
||
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 values = [new Field(69n), new Field(420n), new Field(Field.MODULUS - 1n)]; | ||
machineState.memory.setSlice(0, values); | ||
|
||
const length = values.length; | ||
|
||
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); | ||
} | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Fr } from '@aztec/foundation/fields'; | ||
|
||
import { AvmMachineState } from '../avm_machine_state.js'; | ||
import { TypeTag } from '../avm_memory_types.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(); | ||
} | ||
|
||
Instruction.checkTags(machineState, TypeTag.FIELD, this.noteHashOffset); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am checking the tag to make sure the cast below is safe. As at the moment we only want to emit sets of field elements There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be fine to just say "any type can get put into storage/notehash/nullifier/log trace" and they just get upcast to field. But I also think it's fine to just say that they all must be fields. If we don't update the YP to say that these instructions enforce input is a field, then we should create a ticket to align the YP with this one way or the other. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i remove the check this makes sense |
||
const noteHash = machineState.memory.getAs<Fr>(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(); | ||
} | ||
|
||
Instruction.checkTags(machineState, TypeTag.FIELD, this.nullifierOffset); | ||
const nullifier = machineState.memory.getAs<Fr>(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(); | ||
} | ||
|
||
// Check log tags are all fields | ||
const offsets = Array.from({ length: this.logSize }, (_, i) => this.logOffset + i); | ||
Instruction.checkTags(machineState, TypeTag.FIELD, ...offsets); | ||
|
||
const log = machineState.memory.getSliceAs<Fr>(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(); | ||
} | ||
|
||
// Check log tags are all fields | ||
const offsets = Array.from({ length: this.msgSize }, (_, i) => this.msgOffset + i); | ||
Instruction.checkTags(machineState, TypeTag.FIELD, ...offsets); | ||
|
||
const msg = machineState.memory.getSliceAs<Fr>(this.msgOffset, this.msgSize); | ||
|
||
journal.writeLog(msg); | ||
|
||
this.incrementPc(machineState); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix comment.
See this comment inline on Graphite.