@aztec/simulator 0.55.0 → 0.56.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/avm/avm_execution_environment.d.ts +0 -8
- package/dest/avm/avm_execution_environment.d.ts.map +1 -1
- package/dest/avm/avm_execution_environment.js +1 -19
- package/dest/avm/avm_gas.d.ts +2 -2
- package/dest/avm/avm_gas.d.ts.map +1 -1
- package/dest/avm/avm_gas.js +31 -115
- package/dest/avm/avm_machine_state.d.ts.map +1 -1
- package/dest/avm/avm_machine_state.js +6 -6
- package/dest/avm/avm_memory_types.d.ts +35 -7
- package/dest/avm/avm_memory_types.d.ts.map +1 -1
- package/dest/avm/avm_memory_types.js +25 -11
- package/dest/avm/avm_simulator.d.ts.map +1 -1
- package/dest/avm/avm_simulator.js +5 -6
- package/dest/avm/bytecode_utils.d.ts +1 -2
- package/dest/avm/bytecode_utils.d.ts.map +1 -1
- package/dest/avm/bytecode_utils.js +3 -17
- package/dest/avm/fixtures/index.d.ts +4 -16
- package/dest/avm/fixtures/index.d.ts.map +1 -1
- package/dest/avm/fixtures/index.js +9 -25
- package/dest/avm/journal/index.d.ts +0 -1
- package/dest/avm/journal/index.d.ts.map +1 -1
- package/dest/avm/journal/index.js +1 -2
- package/dest/avm/journal/journal.d.ts +4 -4
- package/dest/avm/journal/journal.d.ts.map +1 -1
- package/dest/avm/journal/journal.js +14 -13
- package/dest/avm/opcodes/bitwise.d.ts +10 -4
- package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
- package/dest/avm/opcodes/bitwise.js +17 -11
- package/dest/avm/opcodes/commitment.js +2 -2
- package/dest/avm/opcodes/comparators.js +2 -2
- package/dest/avm/opcodes/conversion.d.ts +3 -2
- package/dest/avm/opcodes/conversion.d.ts.map +1 -1
- package/dest/avm/opcodes/conversion.js +18 -11
- package/dest/avm/opcodes/environment_getters.d.ts +27 -63
- package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
- package/dest/avm/opcodes/environment_getters.js +75 -80
- package/dest/avm/opcodes/hashing.d.ts +7 -5
- package/dest/avm/opcodes/hashing.d.ts.map +1 -1
- package/dest/avm/opcodes/hashing.js +40 -24
- package/dest/avm/opcodes/index.d.ts +3 -0
- package/dest/avm/opcodes/index.d.ts.map +1 -1
- package/dest/avm/opcodes/index.js +4 -1
- package/dest/avm/opcodes/instruction_impl.d.ts +0 -26
- package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
- package/dest/avm/opcodes/instruction_impl.js +1 -51
- package/dest/avm/opcodes/memory.d.ts +7 -2
- package/dest/avm/opcodes/memory.d.ts.map +1 -1
- package/dest/avm/opcodes/memory.js +23 -6
- package/dest/avm/opcodes/multi_scalar_mul.d.ts.map +1 -1
- package/dest/avm/opcodes/multi_scalar_mul.js +7 -6
- package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/bytecode_serialization.js +5 -24
- package/dest/avm/serialization/instruction_serialization.d.ts +48 -60
- package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/instruction_serialization.js +50 -63
- package/dest/avm/test_utils.d.ts +8 -8
- package/dest/avm/test_utils.d.ts.map +1 -1
- package/dest/avm/test_utils.js +15 -15
- package/dest/client/simulator.d.ts +1 -1
- package/dest/client/test_utils.d.ts.map +1 -1
- package/dest/client/test_utils.js +12 -6
- package/dest/client/unconstrained_execution.d.ts +2 -2
- package/dest/client/unconstrained_execution.d.ts.map +1 -1
- package/dest/client/unconstrained_execution.js +3 -3
- package/dest/public/enqueued_call_simulator.d.ts +43 -0
- package/dest/public/enqueued_call_simulator.d.ts.map +1 -0
- package/dest/public/enqueued_call_simulator.js +155 -0
- package/dest/public/enqueued_calls_processor.d.ts +44 -0
- package/dest/public/enqueued_calls_processor.d.ts.map +1 -0
- package/dest/public/enqueued_calls_processor.js +218 -0
- package/dest/public/execution.d.ts +4 -9
- package/dest/public/execution.d.ts.map +1 -1
- package/dest/public/execution.js +5 -10
- package/dest/public/executor.d.ts +3 -5
- package/dest/public/executor.d.ts.map +1 -1
- package/dest/public/executor.js +5 -9
- package/dest/public/hints_builder.d.ts +4 -3
- package/dest/public/hints_builder.d.ts.map +1 -1
- package/dest/public/hints_builder.js +4 -4
- package/dest/public/index.d.ts +3 -2
- package/dest/public/index.d.ts.map +1 -1
- package/dest/public/index.js +3 -3
- package/dest/public/public_db_sources.d.ts +17 -24
- package/dest/public/public_db_sources.d.ts.map +1 -1
- package/dest/public/public_db_sources.js +58 -65
- package/dest/public/public_kernel.d.ts +3 -9
- package/dest/public/public_kernel.d.ts.map +1 -1
- package/dest/public/public_kernel.js +13 -40
- package/dest/public/public_kernel_circuit_simulator.d.ts +5 -11
- package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -1
- package/dest/public/public_kernel_tail_simulator.d.ts +19 -0
- package/dest/public/public_kernel_tail_simulator.d.ts.map +1 -0
- package/dest/public/public_kernel_tail_simulator.js +45 -0
- package/dest/public/public_processor.d.ts +8 -5
- package/dest/public/public_processor.d.ts.map +1 -1
- package/dest/public/public_processor.js +31 -48
- package/dest/public/public_processor_metrics.d.ts +3 -3
- package/dest/public/public_processor_metrics.d.ts.map +1 -1
- package/dest/public/public_processor_metrics.js +1 -1
- package/dest/public/side_effect_trace.d.ts.map +1 -1
- package/dest/public/side_effect_trace.js +4 -8
- package/package.json +9 -9
- package/src/avm/avm_execution_environment.ts +1 -21
- package/src/avm/avm_gas.ts +30 -114
- package/src/avm/avm_machine_state.ts +5 -5
- package/src/avm/avm_memory_types.ts +31 -9
- package/src/avm/avm_simulator.ts +4 -5
- package/src/avm/bytecode_utils.ts +2 -17
- package/src/avm/fixtures/index.ts +12 -44
- package/src/avm/journal/index.ts +0 -1
- package/src/avm/journal/journal.ts +13 -18
- package/src/avm/opcodes/bitwise.ts +14 -10
- package/src/avm/opcodes/commitment.ts +1 -1
- package/src/avm/opcodes/comparators.ts +2 -2
- package/src/avm/opcodes/conversion.ts +19 -10
- package/src/avm/opcodes/environment_getters.ts +72 -99
- package/src/avm/opcodes/hashing.ts +47 -22
- package/src/avm/opcodes/index.ts +3 -0
- package/src/avm/opcodes/instruction_impl.ts +0 -62
- package/src/avm/opcodes/memory.ts +20 -6
- package/src/avm/opcodes/multi_scalar_mul.ts +6 -6
- package/src/avm/serialization/bytecode_serialization.ts +11 -33
- package/src/avm/serialization/instruction_serialization.ts +5 -18
- package/src/avm/test_utils.ts +20 -16
- package/src/client/test_utils.ts +36 -9
- package/src/client/unconstrained_execution.ts +3 -8
- package/src/public/enqueued_call_simulator.ts +336 -0
- package/src/public/enqueued_calls_processor.ts +388 -0
- package/src/public/execution.ts +5 -12
- package/src/public/executor.ts +4 -12
- package/src/public/hints_builder.ts +14 -12
- package/src/public/index.ts +3 -2
- package/src/public/public_db_sources.ts +62 -67
- package/src/public/public_kernel.ts +15 -49
- package/src/public/public_kernel_circuit_simulator.ts +6 -10
- package/src/public/public_kernel_tail_simulator.ts +106 -0
- package/src/public/public_processor.ts +61 -86
- package/src/public/public_processor_metrics.ts +3 -3
- package/src/public/side_effect_trace.ts +5 -12
- package/dest/avm/journal/host_storage.d.ts +0 -13
- package/dest/avm/journal/host_storage.d.ts.map +0 -1
- package/dest/avm/journal/host_storage.js +0 -13
- package/dest/avm/opcodes/context_getters.d.ts +0 -15
- package/dest/avm/opcodes/context_getters.d.ts.map +0 -1
- package/dest/avm/opcodes/context_getters.js +0 -19
- package/dest/public/abstract_phase_manager.d.ts +0 -93
- package/dest/public/abstract_phase_manager.d.ts.map +0 -1
- package/dest/public/abstract_phase_manager.js +0 -285
- package/dest/public/app_logic_phase_manager.d.ts +0 -25
- package/dest/public/app_logic_phase_manager.d.ts.map +0 -1
- package/dest/public/app_logic_phase_manager.js +0 -52
- package/dest/public/phase_manager_factory.d.ts +0 -18
- package/dest/public/phase_manager_factory.d.ts.map +0 -1
- package/dest/public/phase_manager_factory.js +0 -56
- package/dest/public/setup_phase_manager.d.ts +0 -25
- package/dest/public/setup_phase_manager.d.ts.map +0 -1
- package/dest/public/setup_phase_manager.js +0 -39
- package/dest/public/tail_phase_manager.d.ts +0 -23
- package/dest/public/tail_phase_manager.d.ts.map +0 -1
- package/dest/public/tail_phase_manager.js +0 -50
- package/dest/public/teardown_phase_manager.d.ts +0 -27
- package/dest/public/teardown_phase_manager.d.ts.map +0 -1
- package/dest/public/teardown_phase_manager.js +0 -56
- package/src/avm/journal/host_storage.ts +0 -14
- package/src/avm/opcodes/context_getters.ts +0 -23
- package/src/public/abstract_phase_manager.ts +0 -522
- package/src/public/app_logic_phase_manager.ts +0 -81
- package/src/public/phase_manager_factory.ts +0 -126
- package/src/public/setup_phase_manager.ts +0 -61
- package/src/public/tail_phase_manager.ts +0 -127
- package/src/public/teardown_phase_manager.ts +0 -86
|
@@ -1,112 +1,85 @@
|
|
|
1
1
|
import type { AvmContext } from '../avm_context.js';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class StorageAddress extends EnvironmentGetterInstruction {
|
|
25
|
-
static type: string = 'STORAGEADDRESS';
|
|
26
|
-
static readonly opcode: Opcode = Opcode.STORAGEADDRESS;
|
|
27
|
-
|
|
28
|
-
protected getEnvironmentValue(env: AvmExecutionEnvironment) {
|
|
29
|
-
return new Field(env.storageAddress.toField());
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export class Sender extends EnvironmentGetterInstruction {
|
|
34
|
-
static type: string = 'SENDER';
|
|
35
|
-
static readonly opcode: Opcode = Opcode.SENDER;
|
|
36
|
-
|
|
37
|
-
protected getEnvironmentValue(env: AvmExecutionEnvironment) {
|
|
38
|
-
return new Field(env.sender.toField());
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class FunctionSelector extends EnvironmentGetterInstruction {
|
|
43
|
-
static type: string = 'FUNCTIONSELECTOR';
|
|
44
|
-
static readonly opcode: Opcode = Opcode.FUNCTIONSELECTOR;
|
|
45
|
-
|
|
46
|
-
protected getEnvironmentValue(env: AvmExecutionEnvironment) {
|
|
47
|
-
return new Uint32(env.functionSelector.value);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export class TransactionFee extends EnvironmentGetterInstruction {
|
|
52
|
-
static type: string = 'TRANSACTIONFEE';
|
|
53
|
-
static readonly opcode: Opcode = Opcode.TRANSACTIONFEE;
|
|
54
|
-
|
|
55
|
-
protected getEnvironmentValue(env: AvmExecutionEnvironment) {
|
|
56
|
-
return new Field(env.transactionFee);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export class ChainId extends EnvironmentGetterInstruction {
|
|
61
|
-
static type: string = 'CHAINID';
|
|
62
|
-
static readonly opcode: Opcode = Opcode.CHAINID;
|
|
63
|
-
|
|
64
|
-
protected getEnvironmentValue(env: AvmExecutionEnvironment) {
|
|
65
|
-
return new Field(env.globals.chainId);
|
|
66
|
-
}
|
|
2
|
+
import { Field, Uint32, Uint64 } from '../avm_memory_types.js';
|
|
3
|
+
import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
|
|
4
|
+
import { Addressing } from './addressing_mode.js';
|
|
5
|
+
import { Instruction } from './instruction.js';
|
|
6
|
+
|
|
7
|
+
export enum EnvironmentVariable {
|
|
8
|
+
ADDRESS,
|
|
9
|
+
STORAGEADDRESS,
|
|
10
|
+
SENDER,
|
|
11
|
+
FUNCTIONSELECTOR,
|
|
12
|
+
TRANSACTIONFEE,
|
|
13
|
+
CHAINID,
|
|
14
|
+
VERSION,
|
|
15
|
+
BLOCKNUMBER,
|
|
16
|
+
TIMESTAMP,
|
|
17
|
+
FEEPERL2GAS,
|
|
18
|
+
FEEPERDAGAS,
|
|
19
|
+
ISSTATICCALL,
|
|
20
|
+
L2GASLEFT,
|
|
21
|
+
DAGASLEFT,
|
|
67
22
|
}
|
|
68
23
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
24
|
+
function getValue(e: EnvironmentVariable, ctx: AvmContext) {
|
|
25
|
+
switch (e) {
|
|
26
|
+
case EnvironmentVariable.ADDRESS:
|
|
27
|
+
return new Field(ctx.environment.address.toField());
|
|
28
|
+
case EnvironmentVariable.STORAGEADDRESS:
|
|
29
|
+
return new Field(ctx.environment.storageAddress.toField());
|
|
30
|
+
case EnvironmentVariable.SENDER:
|
|
31
|
+
return new Field(ctx.environment.sender.toField());
|
|
32
|
+
case EnvironmentVariable.FUNCTIONSELECTOR:
|
|
33
|
+
return new Uint32(ctx.environment.functionSelector.value);
|
|
34
|
+
case EnvironmentVariable.TRANSACTIONFEE:
|
|
35
|
+
return new Field(ctx.environment.transactionFee);
|
|
36
|
+
case EnvironmentVariable.CHAINID:
|
|
37
|
+
return new Field(ctx.environment.globals.chainId);
|
|
38
|
+
case EnvironmentVariable.VERSION:
|
|
39
|
+
return new Field(ctx.environment.globals.version);
|
|
40
|
+
case EnvironmentVariable.BLOCKNUMBER:
|
|
41
|
+
return new Field(ctx.environment.globals.blockNumber);
|
|
42
|
+
case EnvironmentVariable.TIMESTAMP:
|
|
43
|
+
return new Uint64(ctx.environment.globals.timestamp.toBigInt());
|
|
44
|
+
case EnvironmentVariable.FEEPERL2GAS:
|
|
45
|
+
return new Field(ctx.environment.globals.gasFees.feePerL2Gas);
|
|
46
|
+
case EnvironmentVariable.FEEPERDAGAS:
|
|
47
|
+
return new Field(ctx.environment.globals.gasFees.feePerDaGas);
|
|
48
|
+
case EnvironmentVariable.ISSTATICCALL:
|
|
49
|
+
return new Field(ctx.environment.isStaticCall ? 1 : 0);
|
|
50
|
+
case EnvironmentVariable.L2GASLEFT:
|
|
51
|
+
return new Field(ctx.machineState.l2GasLeft);
|
|
52
|
+
case EnvironmentVariable.DAGASLEFT:
|
|
53
|
+
return new Field(ctx.machineState.daGasLeft);
|
|
54
|
+
default:
|
|
55
|
+
throw new Error(`Unknown environment variable ${e}`);
|
|
75
56
|
}
|
|
76
57
|
}
|
|
77
58
|
|
|
78
|
-
export class
|
|
79
|
-
static type: string = '
|
|
80
|
-
static readonly opcode: Opcode = Opcode.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
59
|
+
export class GetEnvVar extends Instruction {
|
|
60
|
+
public static readonly type: string = 'GETENVVAR';
|
|
61
|
+
public static readonly opcode: Opcode = Opcode.GETENVVAR_16;
|
|
62
|
+
static readonly wireFormat16: OperandType[] = [
|
|
63
|
+
OperandType.UINT8, // opcode
|
|
64
|
+
OperandType.UINT8, // indirect
|
|
65
|
+
OperandType.UINT8, // variable enum (immediate)
|
|
66
|
+
OperandType.UINT16, // dstOffset
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
constructor(private indirect: number, private varEnum: EnvironmentVariable, private dstOffset: number) {
|
|
70
|
+
super();
|
|
84
71
|
}
|
|
85
|
-
}
|
|
86
72
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
73
|
+
public async execute(context: AvmContext): Promise<void> {
|
|
74
|
+
const memoryOperations = { writes: 1, indirect: this.indirect };
|
|
75
|
+
const memory = context.machineState.memory.track(this.type);
|
|
76
|
+
context.machineState.consumeGas(this.gasCost(memoryOperations));
|
|
90
77
|
|
|
91
|
-
|
|
92
|
-
return new Uint64(env.globals.timestamp.toBigInt());
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export class FeePerL2Gas extends EnvironmentGetterInstruction {
|
|
97
|
-
static type: string = 'FEEPERL2GAS';
|
|
98
|
-
static readonly opcode: Opcode = Opcode.FEEPERL2GAS;
|
|
99
|
-
|
|
100
|
-
protected getEnvironmentValue(env: AvmExecutionEnvironment) {
|
|
101
|
-
return new Field(env.globals.gasFees.feePerL2Gas);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
78
|
+
const [dstOffset] = Addressing.fromWire(this.indirect).resolve([this.dstOffset], memory);
|
|
104
79
|
|
|
105
|
-
|
|
106
|
-
static type: string = 'FEEPERDAGAS';
|
|
107
|
-
static readonly opcode: Opcode = Opcode.FEEPERDAGAS;
|
|
80
|
+
memory.set(dstOffset, getValue(this.varEnum, context));
|
|
108
81
|
|
|
109
|
-
|
|
110
|
-
|
|
82
|
+
memory.assert(memoryOperations);
|
|
83
|
+
context.machineState.incrementPc();
|
|
111
84
|
}
|
|
112
85
|
}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
keccak256,
|
|
3
|
+
keccakf1600,
|
|
4
|
+
pedersenHash,
|
|
5
|
+
poseidon2Permutation,
|
|
6
|
+
sha256Compression,
|
|
7
|
+
} from '@aztec/foundation/crypto';
|
|
2
8
|
|
|
3
9
|
import { strict as assert } from 'assert';
|
|
4
10
|
|
|
5
11
|
import { type AvmContext } from '../avm_context.js';
|
|
6
|
-
import { Field, TypeTag, Uint8, Uint64 } from '../avm_memory_types.js';
|
|
12
|
+
import { Field, TypeTag, Uint8, Uint32, Uint64 } from '../avm_memory_types.js';
|
|
13
|
+
import { InstructionExecutionError } from '../errors.js';
|
|
7
14
|
import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
|
|
8
15
|
import { Addressing } from './addressing_mode.js';
|
|
9
16
|
import { Instruction } from './instruction.js';
|
|
@@ -80,7 +87,7 @@ export class Keccak extends Instruction {
|
|
|
80
87
|
memory.checkTag(TypeTag.UINT32, messageSizeOffset);
|
|
81
88
|
const messageSize = memory.get(messageSizeOffset).toNumber();
|
|
82
89
|
const memoryOperations = { reads: messageSize + 1, writes: 32, indirect: this.indirect };
|
|
83
|
-
context.machineState.consumeGas(this.gasCost(memoryOperations));
|
|
90
|
+
context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: messageSize }));
|
|
84
91
|
|
|
85
92
|
memory.checkTagsRange(TypeTag.UINT8, messageOffset, messageSize);
|
|
86
93
|
|
|
@@ -145,9 +152,9 @@ export class KeccakF1600 extends Instruction {
|
|
|
145
152
|
}
|
|
146
153
|
}
|
|
147
154
|
|
|
148
|
-
export class
|
|
149
|
-
static type: string = '
|
|
150
|
-
static readonly opcode: Opcode = Opcode.
|
|
155
|
+
export class Sha256Compression extends Instruction {
|
|
156
|
+
static type: string = 'SHA256COMPRESSION';
|
|
157
|
+
static readonly opcode: Opcode = Opcode.SHA256COMPRESSION;
|
|
151
158
|
|
|
152
159
|
// Informs (de)serialization. See Instruction.deserialize.
|
|
153
160
|
static readonly wireFormat: OperandType[] = [
|
|
@@ -156,36 +163,54 @@ export class Sha256 extends Instruction {
|
|
|
156
163
|
OperandType.UINT32,
|
|
157
164
|
OperandType.UINT32,
|
|
158
165
|
OperandType.UINT32,
|
|
166
|
+
OperandType.UINT32,
|
|
167
|
+
OperandType.UINT32,
|
|
159
168
|
];
|
|
160
169
|
|
|
161
170
|
constructor(
|
|
162
171
|
private indirect: number,
|
|
163
|
-
private
|
|
164
|
-
private
|
|
165
|
-
private
|
|
172
|
+
private outputOffset: number,
|
|
173
|
+
private stateOffset: number,
|
|
174
|
+
private stateSizeOffset: number,
|
|
175
|
+
private inputsOffset: number,
|
|
176
|
+
private inputsSizeOffset: number,
|
|
166
177
|
) {
|
|
167
178
|
super();
|
|
168
179
|
}
|
|
169
180
|
|
|
170
|
-
// pub fn sha256_slice(input: [u8]) -> [u8; 32]
|
|
171
181
|
public async execute(context: AvmContext): Promise<void> {
|
|
182
|
+
const STATE_SIZE = 8;
|
|
183
|
+
const INPUTS_SIZE = 16;
|
|
184
|
+
|
|
172
185
|
const memory = context.machineState.memory.track(this.type);
|
|
173
|
-
const [
|
|
174
|
-
|
|
186
|
+
const [outputOffset, stateOffset, stateSizeOffset, inputsOffset, inputsSizeOffset] = Addressing.fromWire(
|
|
187
|
+
this.indirect,
|
|
188
|
+
).resolve(
|
|
189
|
+
[this.outputOffset, this.stateOffset, this.stateSizeOffset, this.inputsOffset, this.inputsSizeOffset],
|
|
175
190
|
memory,
|
|
176
191
|
);
|
|
177
|
-
memory.
|
|
178
|
-
const
|
|
179
|
-
|
|
192
|
+
const stateSize = memory.get(stateSizeOffset).toNumber();
|
|
193
|
+
const inputsSize = memory.get(inputsSizeOffset).toNumber();
|
|
194
|
+
if (stateSize !== STATE_SIZE) {
|
|
195
|
+
throw new InstructionExecutionError('`state` argument to SHA256 compression must be of length 8');
|
|
196
|
+
}
|
|
197
|
+
if (inputsSize !== INPUTS_SIZE) {
|
|
198
|
+
throw new InstructionExecutionError('`inputs` argument to SHA256 compression must be of length 16');
|
|
199
|
+
}
|
|
200
|
+
// +2 to account for both size offsets (stateSizeOffset and inputsSizeOffset)
|
|
201
|
+
// Note: size of output is same as size of state
|
|
202
|
+
const memoryOperations = { reads: stateSize + inputsSize + 2, writes: stateSize, indirect: this.indirect };
|
|
180
203
|
context.machineState.consumeGas(this.gasCost(memoryOperations));
|
|
181
|
-
memory.checkTagsRange(TypeTag.
|
|
204
|
+
memory.checkTagsRange(TypeTag.UINT32, inputsOffset, inputsSize);
|
|
205
|
+
memory.checkTagsRange(TypeTag.UINT32, stateOffset, stateSize);
|
|
182
206
|
|
|
183
|
-
const
|
|
184
|
-
const
|
|
207
|
+
const state = Uint32Array.from(memory.getSlice(stateOffset, stateSize).map(word => word.toNumber()));
|
|
208
|
+
const inputs = Uint32Array.from(memory.getSlice(inputsOffset, inputsSize).map(word => word.toNumber()));
|
|
209
|
+
const output = sha256Compression(state, inputs);
|
|
185
210
|
|
|
186
|
-
//
|
|
187
|
-
const res = [...
|
|
188
|
-
memory.setSlice(
|
|
211
|
+
// Conversion required from Uint32Array to Uint32[] (can't map directly, need `...`)
|
|
212
|
+
const res = [...output].map(word => new Uint32(word));
|
|
213
|
+
memory.setSlice(outputOffset, res);
|
|
189
214
|
|
|
190
215
|
memory.assert(memoryOperations);
|
|
191
216
|
context.machineState.incrementPc();
|
|
@@ -231,7 +256,7 @@ export class Pedersen extends Instruction {
|
|
|
231
256
|
const hashData = memory.getSlice(messageOffset, messageSize);
|
|
232
257
|
|
|
233
258
|
const memoryOperations = { reads: messageSize + 2, writes: 1, indirect: this.indirect };
|
|
234
|
-
context.machineState.consumeGas(this.gasCost(memoryOperations));
|
|
259
|
+
context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: messageSize }));
|
|
235
260
|
|
|
236
261
|
memory.checkTagsRange(TypeTag.FIELD, messageOffset, messageSize);
|
|
237
262
|
|
package/src/avm/opcodes/index.ts
CHANGED
|
@@ -1,25 +1,6 @@
|
|
|
1
|
-
import { type AvmContext } from '../avm_context.js';
|
|
2
|
-
import { type MemoryValue } from '../avm_memory_types.js';
|
|
3
1
|
import { OperandType } from '../serialization/instruction_serialization.js';
|
|
4
|
-
import { Addressing } from './addressing_mode.js';
|
|
5
2
|
import { Instruction } from './instruction.js';
|
|
6
3
|
|
|
7
|
-
/** Wire format that informs deserialization for instructions with two operands. */
|
|
8
|
-
export const TwoOperandWireFormat8 = [
|
|
9
|
-
OperandType.UINT8,
|
|
10
|
-
OperandType.UINT8,
|
|
11
|
-
OperandType.UINT8,
|
|
12
|
-
OperandType.UINT8,
|
|
13
|
-
OperandType.UINT8,
|
|
14
|
-
];
|
|
15
|
-
export const TwoOperandWireFormat16 = [
|
|
16
|
-
OperandType.UINT8,
|
|
17
|
-
OperandType.UINT8,
|
|
18
|
-
OperandType.UINT8,
|
|
19
|
-
OperandType.UINT16,
|
|
20
|
-
OperandType.UINT16,
|
|
21
|
-
];
|
|
22
|
-
|
|
23
4
|
/** Wire format that informs deserialization for instructions with three operands. */
|
|
24
5
|
export const ThreeOperandWireFormat8 = [
|
|
25
6
|
OperandType.UINT8,
|
|
@@ -38,25 +19,6 @@ export const ThreeOperandWireFormat16 = [
|
|
|
38
19
|
OperandType.UINT16,
|
|
39
20
|
];
|
|
40
21
|
|
|
41
|
-
/**
|
|
42
|
-
* Covers (de)serialization for an instruction with:
|
|
43
|
-
* indirect, inTag, and two operands.
|
|
44
|
-
*/
|
|
45
|
-
export abstract class TwoOperandInstruction extends Instruction {
|
|
46
|
-
// Informs (de)serialization. See Instruction.deserialize.
|
|
47
|
-
static readonly wireFormat8: OperandType[] = TwoOperandWireFormat8;
|
|
48
|
-
static readonly wireFormat16: OperandType[] = TwoOperandWireFormat16;
|
|
49
|
-
|
|
50
|
-
constructor(
|
|
51
|
-
protected indirect: number,
|
|
52
|
-
protected inTag: number,
|
|
53
|
-
protected aOffset: number,
|
|
54
|
-
protected dstOffset: number,
|
|
55
|
-
) {
|
|
56
|
-
super();
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
22
|
/**
|
|
61
23
|
* Covers (de)serialization for an instruction with:
|
|
62
24
|
* indirect, inTag, and three operands.
|
|
@@ -75,27 +37,3 @@ export abstract class ThreeOperandInstruction extends Instruction {
|
|
|
75
37
|
super();
|
|
76
38
|
}
|
|
77
39
|
}
|
|
78
|
-
|
|
79
|
-
export abstract class GetterInstruction extends Instruction {
|
|
80
|
-
// Informs (de)serialization. See Instruction.deserialize.
|
|
81
|
-
static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT8, OperandType.UINT32];
|
|
82
|
-
|
|
83
|
-
constructor(protected indirect: number, protected dstOffset: number) {
|
|
84
|
-
super();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
public async execute(context: AvmContext): Promise<void> {
|
|
88
|
-
const memoryOperations = { writes: 1, indirect: this.indirect };
|
|
89
|
-
const memory = context.machineState.memory.track(this.type);
|
|
90
|
-
context.machineState.consumeGas(this.gasCost(memoryOperations));
|
|
91
|
-
|
|
92
|
-
const [dstOffset] = Addressing.fromWire(this.indirect).resolve([this.dstOffset], memory);
|
|
93
|
-
|
|
94
|
-
memory.set(dstOffset, this.getValue(context));
|
|
95
|
-
|
|
96
|
-
memory.assert(memoryOperations);
|
|
97
|
-
context.machineState.incrementPc();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
protected abstract getValue(env: AvmContext): MemoryValue;
|
|
101
|
-
}
|
|
@@ -3,7 +3,6 @@ import { Field, TaggedMemory } from '../avm_memory_types.js';
|
|
|
3
3
|
import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
|
|
4
4
|
import { Addressing } from './addressing_mode.js';
|
|
5
5
|
import { Instruction } from './instruction.js';
|
|
6
|
-
import { TwoOperandInstruction } from './instruction_impl.js';
|
|
7
6
|
|
|
8
7
|
export class Set extends Instruction {
|
|
9
8
|
static readonly type: string = 'SET';
|
|
@@ -121,12 +120,27 @@ export class CMov extends Instruction {
|
|
|
121
120
|
}
|
|
122
121
|
}
|
|
123
122
|
|
|
124
|
-
export class Cast extends
|
|
123
|
+
export class Cast extends Instruction {
|
|
125
124
|
static readonly type: string = 'CAST';
|
|
126
125
|
static readonly opcode = Opcode.CAST_8;
|
|
127
126
|
|
|
128
|
-
|
|
129
|
-
|
|
127
|
+
static readonly wireFormat8 = [
|
|
128
|
+
OperandType.UINT8,
|
|
129
|
+
OperandType.UINT8,
|
|
130
|
+
OperandType.UINT8,
|
|
131
|
+
OperandType.UINT8,
|
|
132
|
+
OperandType.UINT8,
|
|
133
|
+
];
|
|
134
|
+
static readonly wireFormat16 = [
|
|
135
|
+
OperandType.UINT8,
|
|
136
|
+
OperandType.UINT8,
|
|
137
|
+
OperandType.UINT8,
|
|
138
|
+
OperandType.UINT16,
|
|
139
|
+
OperandType.UINT16,
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
constructor(private indirect: number, private dstTag: number, private srcOffset: number, private dstOffset: number) {
|
|
143
|
+
super();
|
|
130
144
|
}
|
|
131
145
|
|
|
132
146
|
public async execute(context: AvmContext): Promise<void> {
|
|
@@ -134,10 +148,10 @@ export class Cast extends TwoOperandInstruction {
|
|
|
134
148
|
const memory = context.machineState.memory.track(this.type);
|
|
135
149
|
context.machineState.consumeGas(this.gasCost(memoryOperations));
|
|
136
150
|
|
|
137
|
-
const [srcOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.
|
|
151
|
+
const [srcOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.srcOffset, this.dstOffset], memory);
|
|
138
152
|
|
|
139
153
|
const a = memory.get(srcOffset);
|
|
140
|
-
const casted = TaggedMemory.buildFromTagTruncating(a.toBigInt(), this.
|
|
154
|
+
const casted = TaggedMemory.buildFromTagTruncating(a.toBigInt(), this.dstTag);
|
|
141
155
|
|
|
142
156
|
memory.set(dstOffset, casted);
|
|
143
157
|
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { Fq, Point } from '@aztec/circuits.js';
|
|
2
2
|
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
|
|
3
3
|
|
|
4
|
-
import { strict as assert } from 'assert';
|
|
5
|
-
|
|
6
4
|
import { type AvmContext } from '../avm_context.js';
|
|
7
5
|
import { Field, TypeTag } from '../avm_memory_types.js';
|
|
8
6
|
import { InstructionExecutionError } from '../errors.js';
|
|
@@ -46,7 +44,9 @@ export class MultiScalarMul extends Instruction {
|
|
|
46
44
|
memory.checkTag(TypeTag.UINT32, this.pointsLengthOffset);
|
|
47
45
|
// Get the size of the unrolled (x, y , inf) points vector
|
|
48
46
|
const pointsReadLength = memory.get(this.pointsLengthOffset).toNumber();
|
|
49
|
-
|
|
47
|
+
if (pointsReadLength % 3 !== 0) {
|
|
48
|
+
throw new InstructionExecutionError(`Points vector offset should be a multiple of 3, was ${pointsReadLength}`);
|
|
49
|
+
}
|
|
50
50
|
// Divide by 3 since each point is represented as a triplet to get the number of points
|
|
51
51
|
const numPoints = pointsReadLength / 3;
|
|
52
52
|
// The tag for each triplet will be (Field, Field, Uint8)
|
|
@@ -54,8 +54,8 @@ export class MultiScalarMul extends Instruction {
|
|
|
54
54
|
const offset = pointsOffset + i * 3;
|
|
55
55
|
// Check (Field, Field)
|
|
56
56
|
memory.checkTagsRange(TypeTag.FIELD, offset, 2);
|
|
57
|
-
// Check
|
|
58
|
-
memory.checkTag(TypeTag.
|
|
57
|
+
// Check Uint1 (inf flag)
|
|
58
|
+
memory.checkTag(TypeTag.UINT1, offset + 2);
|
|
59
59
|
}
|
|
60
60
|
// Get the unrolled (x, y, inf) representing the points
|
|
61
61
|
const pointsVector = memory.getSlice(pointsOffset, pointsReadLength);
|
|
@@ -68,7 +68,7 @@ export class MultiScalarMul extends Instruction {
|
|
|
68
68
|
writes: 3 /* output triplet */,
|
|
69
69
|
indirect: this.indirect,
|
|
70
70
|
};
|
|
71
|
-
context.machineState.consumeGas(this.gasCost(memoryOperations));
|
|
71
|
+
context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: pointsReadLength }));
|
|
72
72
|
// Get the unrolled scalar (lo & hi) representing the scalars
|
|
73
73
|
const scalarsVector = memory.getSlice(scalarsOffset, scalarReadLength);
|
|
74
74
|
memory.checkTagsRange(TypeTag.FIELD, scalarsOffset, scalarReadLength);
|
|
@@ -1,33 +1,27 @@
|
|
|
1
|
-
import { PedersenCommitment } from '../opcodes/commitment.js';
|
|
2
|
-
import { DAGasLeft, L2GasLeft } from '../opcodes/context_getters.js';
|
|
3
|
-
import { EcAdd } from '../opcodes/ec_add.js';
|
|
4
|
-
import { Keccak, KeccakF1600, Pedersen, Poseidon2, Sha256 } from '../opcodes/hashing.js';
|
|
5
1
|
import {
|
|
6
2
|
Add,
|
|
7
|
-
Address,
|
|
8
3
|
And,
|
|
9
|
-
BlockNumber,
|
|
10
4
|
CMov,
|
|
11
5
|
Call,
|
|
12
6
|
CalldataCopy,
|
|
13
7
|
Cast,
|
|
14
|
-
ChainId,
|
|
15
8
|
DebugLog,
|
|
16
9
|
Div,
|
|
10
|
+
EcAdd,
|
|
17
11
|
EmitNoteHash,
|
|
18
12
|
EmitNullifier,
|
|
19
13
|
EmitUnencryptedLog,
|
|
20
14
|
Eq,
|
|
21
|
-
FeePerDAGas,
|
|
22
|
-
FeePerL2Gas,
|
|
23
15
|
FieldDiv,
|
|
24
|
-
FunctionSelector,
|
|
25
16
|
GetContractInstance,
|
|
17
|
+
GetEnvVar,
|
|
26
18
|
Instruction,
|
|
27
19
|
InternalCall,
|
|
28
20
|
InternalReturn,
|
|
29
21
|
Jump,
|
|
30
22
|
JumpI,
|
|
23
|
+
Keccak,
|
|
24
|
+
KeccakF1600,
|
|
31
25
|
L1ToL2MessageExists,
|
|
32
26
|
Lt,
|
|
33
27
|
Lte,
|
|
@@ -37,22 +31,21 @@ import {
|
|
|
37
31
|
NoteHashExists,
|
|
38
32
|
NullifierExists,
|
|
39
33
|
Or,
|
|
34
|
+
Pedersen,
|
|
35
|
+
PedersenCommitment,
|
|
36
|
+
Poseidon2,
|
|
40
37
|
Return,
|
|
41
38
|
Revert,
|
|
42
39
|
SLoad,
|
|
43
40
|
SStore,
|
|
44
41
|
SendL2ToL1Message,
|
|
45
|
-
Sender,
|
|
46
42
|
Set,
|
|
43
|
+
Sha256Compression,
|
|
47
44
|
Shl,
|
|
48
45
|
Shr,
|
|
49
46
|
StaticCall,
|
|
50
|
-
StorageAddress,
|
|
51
47
|
Sub,
|
|
52
|
-
Timestamp,
|
|
53
48
|
ToRadixLE,
|
|
54
|
-
TransactionFee,
|
|
55
|
-
Version,
|
|
56
49
|
Xor,
|
|
57
50
|
} from '../opcodes/index.js';
|
|
58
51
|
import { MultiScalarMul } from '../opcodes/multi_scalar_mul.js';
|
|
@@ -104,25 +97,10 @@ const INSTRUCTION_SET = () =>
|
|
|
104
97
|
[Opcode.SHR_16, Shr.as(Shr.wireFormat16).deserialize],
|
|
105
98
|
[Opcode.CAST_8, Cast.as(Cast.wireFormat8).deserialize],
|
|
106
99
|
[Opcode.CAST_16, Cast.as(Cast.wireFormat16).deserialize],
|
|
107
|
-
|
|
108
|
-
[
|
|
109
|
-
[Sender.opcode, Instruction.deserialize.bind(Sender)],
|
|
110
|
-
[FunctionSelector.opcode, Instruction.deserialize.bind(FunctionSelector)],
|
|
111
|
-
[TransactionFee.opcode, Instruction.deserialize.bind(TransactionFee)],
|
|
112
|
-
// Execution Environment - Globals
|
|
113
|
-
[ChainId.opcode, Instruction.deserialize.bind(ChainId)],
|
|
114
|
-
[Version.opcode, Instruction.deserialize.bind(Version)],
|
|
115
|
-
[BlockNumber.opcode, Instruction.deserialize.bind(BlockNumber)],
|
|
116
|
-
[Timestamp.opcode, Instruction.deserialize.bind(Timestamp)],
|
|
117
|
-
[FeePerL2Gas.opcode, Instruction.deserialize.bind(FeePerL2Gas)],
|
|
118
|
-
[FeePerDAGas.opcode, Instruction.deserialize.bind(FeePerDAGas)],
|
|
119
|
-
// Execution Environment - Calldata
|
|
100
|
+
// Execution Environment
|
|
101
|
+
[Opcode.GETENVVAR_16, GetEnvVar.as(GetEnvVar.wireFormat16).deserialize],
|
|
120
102
|
[CalldataCopy.opcode, Instruction.deserialize.bind(CalldataCopy)],
|
|
121
103
|
|
|
122
|
-
// Machine State
|
|
123
|
-
// Machine State - Gas
|
|
124
|
-
[L2GasLeft.opcode, Instruction.deserialize.bind(L2GasLeft)],
|
|
125
|
-
[DAGasLeft.opcode, Instruction.deserialize.bind(DAGasLeft)],
|
|
126
104
|
// Machine State - Internal Control Flow
|
|
127
105
|
[Jump.opcode, Instruction.deserialize.bind(Jump)],
|
|
128
106
|
[JumpI.opcode, Instruction.deserialize.bind(JumpI)],
|
|
@@ -167,7 +145,7 @@ const INSTRUCTION_SET = () =>
|
|
|
167
145
|
[EcAdd.opcode, Instruction.deserialize.bind(EcAdd)],
|
|
168
146
|
[Keccak.opcode, Instruction.deserialize.bind(Keccak)],
|
|
169
147
|
[Poseidon2.opcode, Instruction.deserialize.bind(Poseidon2)],
|
|
170
|
-
[
|
|
148
|
+
[Sha256Compression.opcode, Instruction.deserialize.bind(Sha256Compression)],
|
|
171
149
|
[Pedersen.opcode, Instruction.deserialize.bind(Pedersen)],
|
|
172
150
|
[MultiScalarMul.opcode, Instruction.deserialize.bind(MultiScalarMul)],
|
|
173
151
|
[PedersenCommitment.opcode, Instruction.deserialize.bind(PedersenCommitment)],
|
|
@@ -39,21 +39,8 @@ export enum Opcode {
|
|
|
39
39
|
CAST_8,
|
|
40
40
|
CAST_16,
|
|
41
41
|
// Execution environment
|
|
42
|
-
|
|
43
|
-
STORAGEADDRESS,
|
|
44
|
-
SENDER,
|
|
45
|
-
FUNCTIONSELECTOR,
|
|
46
|
-
TRANSACTIONFEE,
|
|
47
|
-
CHAINID,
|
|
48
|
-
VERSION,
|
|
49
|
-
BLOCKNUMBER,
|
|
50
|
-
TIMESTAMP,
|
|
51
|
-
FEEPERL2GAS,
|
|
52
|
-
FEEPERDAGAS,
|
|
42
|
+
GETENVVAR_16,
|
|
53
43
|
CALLDATACOPY,
|
|
54
|
-
// Gas
|
|
55
|
-
L2GASLEFT,
|
|
56
|
-
DAGASLEFT,
|
|
57
44
|
// Control flow
|
|
58
45
|
JUMP_16,
|
|
59
46
|
JUMPI_16,
|
|
@@ -92,16 +79,14 @@ export enum Opcode {
|
|
|
92
79
|
// Gadgets
|
|
93
80
|
KECCAK,
|
|
94
81
|
POSEIDON2,
|
|
95
|
-
|
|
82
|
+
SHA256COMPRESSION,
|
|
83
|
+
KECCAKF1600,
|
|
96
84
|
PEDERSEN, // temp - may be removed, but alot of contracts rely on it
|
|
97
85
|
ECADD,
|
|
98
86
|
MSM,
|
|
99
87
|
PEDERSENCOMMITMENT,
|
|
100
88
|
// Conversion
|
|
101
89
|
TORADIXLE,
|
|
102
|
-
// Future Gadgets -- pending changes in noir
|
|
103
|
-
SHA256COMPRESSION,
|
|
104
|
-
KECCAKF1600, // Here for when we eventually support this
|
|
105
90
|
}
|
|
106
91
|
|
|
107
92
|
// Possible types for an instruction's operand in its wire format. (Keep in sync with CPP code.
|
|
@@ -109,6 +94,7 @@ export enum Opcode {
|
|
|
109
94
|
// Note that cpp code introduced an additional enum value TAG to express the instruction tag. In TS,
|
|
110
95
|
// this one is parsed as UINT8.
|
|
111
96
|
export enum OperandType {
|
|
97
|
+
UINT1,
|
|
112
98
|
UINT8,
|
|
113
99
|
UINT16,
|
|
114
100
|
UINT32,
|
|
@@ -122,6 +108,7 @@ type OperandWriter = (value: any) => void;
|
|
|
122
108
|
|
|
123
109
|
// Specifies how to read and write each operand type.
|
|
124
110
|
const OPERAND_SPEC = new Map<OperandType, [number, () => OperandNativeType, OperandWriter]>([
|
|
111
|
+
[OperandType.UINT1, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
|
|
125
112
|
[OperandType.UINT8, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
|
|
126
113
|
[OperandType.UINT16, [2, Buffer.prototype.readUint16BE, Buffer.prototype.writeUint16BE]],
|
|
127
114
|
[OperandType.UINT32, [4, Buffer.prototype.readUint32BE, Buffer.prototype.writeUint32BE]],
|