@aztec/simulator 0.56.0 → 0.58.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.
Files changed (205) hide show
  1. package/dest/acvm/oracle/oracle.d.ts +2 -5
  2. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  3. package/dest/acvm/oracle/oracle.js +7 -38
  4. package/dest/acvm/oracle/typed_oracle.d.ts +4 -8
  5. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/typed_oracle.js +1 -10
  7. package/dest/avm/avm_execution_environment.d.ts +2 -3
  8. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  9. package/dest/avm/avm_execution_environment.js +3 -4
  10. package/dest/avm/avm_gas.d.ts.map +1 -1
  11. package/dest/avm/avm_gas.js +2 -3
  12. package/dest/avm/avm_memory_types.d.ts +10 -5
  13. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  14. package/dest/avm/avm_memory_types.js +31 -16
  15. package/dest/avm/avm_simulator.d.ts.map +1 -1
  16. package/dest/avm/avm_simulator.js +5 -2
  17. package/dest/avm/errors.d.ts +1 -1
  18. package/dest/avm/errors.d.ts.map +1 -1
  19. package/dest/avm/errors.js +12 -3
  20. package/dest/avm/fixtures/index.d.ts.map +1 -1
  21. package/dest/avm/fixtures/index.js +4 -4
  22. package/dest/avm/journal/journal.d.ts +2 -1
  23. package/dest/avm/journal/journal.d.ts.map +1 -1
  24. package/dest/avm/journal/journal.js +8 -8
  25. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  26. package/dest/avm/opcodes/accrued_substate.js +54 -47
  27. package/dest/avm/opcodes/addressing_mode.d.ts +3 -2
  28. package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -1
  29. package/dest/avm/opcodes/addressing_mode.js +25 -21
  30. package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
  31. package/dest/avm/opcodes/arithmetic.js +7 -6
  32. package/dest/avm/opcodes/bitwise.d.ts +3 -3
  33. package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
  34. package/dest/avm/opcodes/bitwise.js +19 -16
  35. package/dest/avm/opcodes/commitment.d.ts.map +1 -1
  36. package/dest/avm/opcodes/commitment.js +6 -5
  37. package/dest/avm/opcodes/comparators.d.ts.map +1 -1
  38. package/dest/avm/opcodes/comparators.js +7 -6
  39. package/dest/avm/opcodes/contract.d.ts.map +1 -1
  40. package/dest/avm/opcodes/contract.js +8 -6
  41. package/dest/avm/opcodes/control_flow.js +7 -7
  42. package/dest/avm/opcodes/conversion.d.ts.map +1 -1
  43. package/dest/avm/opcodes/conversion.js +11 -10
  44. package/dest/avm/opcodes/ec_add.d.ts.map +1 -1
  45. package/dest/avm/opcodes/ec_add.js +25 -15
  46. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  47. package/dest/avm/opcodes/environment_getters.js +6 -5
  48. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  49. package/dest/avm/opcodes/external_calls.js +37 -26
  50. package/dest/avm/opcodes/hashing.d.ts +1 -3
  51. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  52. package/dest/avm/opcodes/hashing.js +39 -48
  53. package/dest/avm/opcodes/instruction.d.ts +2 -6
  54. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  55. package/dest/avm/opcodes/instruction.js +3 -9
  56. package/dest/avm/opcodes/instruction_impl.d.ts +1 -2
  57. package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
  58. package/dest/avm/opcodes/instruction_impl.js +2 -5
  59. package/dest/avm/opcodes/memory.d.ts +0 -12
  60. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  61. package/dest/avm/opcodes/memory.js +24 -54
  62. package/dest/avm/opcodes/misc.d.ts.map +1 -1
  63. package/dest/avm/opcodes/misc.js +10 -9
  64. package/dest/avm/opcodes/multi_scalar_mul.d.ts.map +1 -1
  65. package/dest/avm/opcodes/multi_scalar_mul.js +35 -20
  66. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  67. package/dest/avm/opcodes/storage.js +13 -11
  68. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  69. package/dest/avm/serialization/bytecode_serialization.js +2 -3
  70. package/dest/avm/serialization/instruction_serialization.d.ts +32 -34
  71. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  72. package/dest/avm/serialization/instruction_serialization.js +34 -37
  73. package/dest/avm/test_utils.d.ts +1 -2
  74. package/dest/avm/test_utils.d.ts.map +1 -1
  75. package/dest/avm/test_utils.js +1 -1
  76. package/dest/client/client_execution_context.d.ts +6 -30
  77. package/dest/client/client_execution_context.d.ts.map +1 -1
  78. package/dest/client/client_execution_context.js +36 -54
  79. package/dest/client/db_oracle.d.ts +1 -2
  80. package/dest/client/db_oracle.d.ts.map +1 -1
  81. package/dest/client/db_oracle.js +1 -1
  82. package/dest/client/index.d.ts +1 -1
  83. package/dest/client/index.d.ts.map +1 -1
  84. package/dest/client/index.js +2 -2
  85. package/dest/client/private_execution.d.ts +12 -3
  86. package/dest/client/private_execution.d.ts.map +1 -1
  87. package/dest/client/private_execution.js +29 -23
  88. package/dest/client/simulator.d.ts +2 -3
  89. package/dest/client/simulator.d.ts.map +1 -1
  90. package/dest/client/simulator.js +1 -1
  91. package/dest/client/view_data_oracle.d.ts +1 -2
  92. package/dest/client/view_data_oracle.d.ts.map +1 -1
  93. package/dest/client/view_data_oracle.js +1 -1
  94. package/dest/common/debug_fn_name.d.ts +4 -0
  95. package/dest/common/debug_fn_name.d.ts.map +1 -0
  96. package/dest/common/debug_fn_name.js +15 -0
  97. package/dest/common/index.d.ts +0 -1
  98. package/dest/common/index.d.ts.map +1 -1
  99. package/dest/common/index.js +1 -2
  100. package/dest/public/db_interfaces.d.ts +1 -2
  101. package/dest/public/db_interfaces.d.ts.map +1 -1
  102. package/dest/public/dual_side_effect_trace.d.ts +76 -0
  103. package/dest/public/dual_side_effect_trace.d.ts.map +1 -0
  104. package/dest/public/dual_side_effect_trace.js +109 -0
  105. package/dest/public/enqueued_call_side_effect_trace.d.ts +114 -0
  106. package/dest/public/enqueued_call_side_effect_trace.d.ts.map +1 -0
  107. package/dest/public/enqueued_call_side_effect_trace.js +314 -0
  108. package/dest/public/enqueued_call_simulator.d.ts +2 -2
  109. package/dest/public/enqueued_call_simulator.d.ts.map +1 -1
  110. package/dest/public/enqueued_call_simulator.js +20 -10
  111. package/dest/public/enqueued_calls_processor.d.ts +2 -2
  112. package/dest/public/enqueued_calls_processor.d.ts.map +1 -1
  113. package/dest/public/enqueued_calls_processor.js +3 -5
  114. package/dest/public/execution.d.ts +11 -5
  115. package/dest/public/execution.d.ts.map +1 -1
  116. package/dest/public/execution.js +13 -1
  117. package/dest/public/executor.d.ts +7 -6
  118. package/dest/public/executor.d.ts.map +1 -1
  119. package/dest/public/executor.js +24 -15
  120. package/dest/public/fee_payment.d.ts +1 -1
  121. package/dest/public/fee_payment.d.ts.map +1 -1
  122. package/dest/public/fee_payment.js +4 -7
  123. package/dest/public/hints_builder.d.ts +2 -2
  124. package/dest/public/hints_builder.d.ts.map +1 -1
  125. package/dest/public/hints_builder.js +2 -2
  126. package/dest/public/public_db_sources.d.ts +4 -5
  127. package/dest/public/public_db_sources.d.ts.map +1 -1
  128. package/dest/public/public_db_sources.js +15 -11
  129. package/dest/public/public_kernel_tail_simulator.d.ts +3 -3
  130. package/dest/public/public_kernel_tail_simulator.d.ts.map +1 -1
  131. package/dest/public/public_kernel_tail_simulator.js +1 -1
  132. package/dest/public/public_processor.d.ts +7 -10
  133. package/dest/public/public_processor.d.ts.map +1 -1
  134. package/dest/public/public_processor.js +9 -10
  135. package/dest/public/side_effect_errors.d.ts +4 -0
  136. package/dest/public/side_effect_errors.d.ts.map +1 -0
  137. package/dest/public/side_effect_errors.js +7 -0
  138. package/dest/public/side_effect_trace.d.ts +3 -4
  139. package/dest/public/side_effect_trace.d.ts.map +1 -1
  140. package/dest/public/side_effect_trace.js +54 -29
  141. package/dest/public/side_effect_trace_interface.d.ts +1 -1
  142. package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
  143. package/package.json +12 -9
  144. package/src/acvm/oracle/oracle.ts +8 -86
  145. package/src/acvm/oracle/typed_oracle.ts +8 -33
  146. package/src/avm/avm_execution_environment.ts +1 -3
  147. package/src/avm/avm_gas.ts +1 -2
  148. package/src/avm/avm_memory_types.ts +38 -16
  149. package/src/avm/avm_simulator.ts +7 -1
  150. package/src/avm/errors.ts +11 -3
  151. package/src/avm/fixtures/index.ts +2 -3
  152. package/src/avm/journal/journal.ts +14 -10
  153. package/src/avm/opcodes/accrued_substate.ts +53 -61
  154. package/src/avm/opcodes/addressing_mode.ts +27 -24
  155. package/src/avm/opcodes/arithmetic.ts +6 -8
  156. package/src/avm/opcodes/bitwise.ts +18 -18
  157. package/src/avm/opcodes/commitment.ts +6 -7
  158. package/src/avm/opcodes/comparators.ts +6 -8
  159. package/src/avm/opcodes/contract.ts +7 -8
  160. package/src/avm/opcodes/control_flow.ts +6 -6
  161. package/src/avm/opcodes/conversion.ts +10 -12
  162. package/src/avm/opcodes/ec_add.ts +29 -24
  163. package/src/avm/opcodes/environment_getters.ts +5 -4
  164. package/src/avm/opcodes/external_calls.ts +37 -30
  165. package/src/avm/opcodes/hashing.ts +38 -63
  166. package/src/avm/opcodes/instruction.ts +3 -10
  167. package/src/avm/opcodes/instruction_impl.ts +0 -3
  168. package/src/avm/opcodes/memory.ts +23 -67
  169. package/src/avm/opcodes/misc.ts +9 -11
  170. package/src/avm/opcodes/multi_scalar_mul.ts +31 -26
  171. package/src/avm/opcodes/storage.ts +12 -10
  172. package/src/avm/serialization/bytecode_serialization.ts +0 -2
  173. package/src/avm/serialization/instruction_serialization.ts +1 -4
  174. package/src/avm/test_utils.ts +1 -2
  175. package/src/client/client_execution_context.ts +46 -97
  176. package/src/client/db_oracle.ts +6 -2
  177. package/src/client/index.ts +1 -1
  178. package/src/client/private_execution.ts +45 -15
  179. package/src/client/simulator.ts +2 -3
  180. package/src/client/view_data_oracle.ts +1 -2
  181. package/src/common/debug_fn_name.ts +22 -0
  182. package/src/common/index.ts +0 -1
  183. package/src/public/db_interfaces.ts +5 -2
  184. package/src/public/dual_side_effect_trace.ts +173 -0
  185. package/src/public/enqueued_call_side_effect_trace.ts +552 -0
  186. package/src/public/enqueued_call_simulator.ts +35 -14
  187. package/src/public/enqueued_calls_processor.ts +4 -6
  188. package/src/public/execution.ts +15 -6
  189. package/src/public/executor.ts +42 -19
  190. package/src/public/fee_payment.ts +4 -6
  191. package/src/public/hints_builder.ts +9 -11
  192. package/src/public/public_db_sources.ts +31 -22
  193. package/src/public/public_kernel_tail_simulator.ts +3 -3
  194. package/src/public/public_processor.ts +17 -13
  195. package/src/public/side_effect_errors.ts +6 -0
  196. package/src/public/side_effect_trace.ts +74 -29
  197. package/src/public/side_effect_trace_interface.ts +2 -2
  198. package/dest/client/execution_result.d.ts +0 -104
  199. package/dest/client/execution_result.d.ts.map +0 -1
  200. package/dest/client/execution_result.js +0 -136
  201. package/dest/common/return_values.d.ts +0 -11
  202. package/dest/common/return_values.d.ts.map +0 -1
  203. package/dest/common/return_values.js +0 -13
  204. package/src/client/execution_result.ts +0 -228
  205. package/src/common/return_values.ts +0 -18
@@ -15,10 +15,10 @@ export class DebugLog extends Instruction {
15
15
  static readonly wireFormat: OperandType[] = [
16
16
  OperandType.UINT8, // Opcode
17
17
  OperandType.UINT8, // Indirect
18
- OperandType.UINT32, // message memory address
19
- OperandType.UINT32, // message size
20
- OperandType.UINT32, // fields memory address
21
- OperandType.UINT32, // fields size address
18
+ OperandType.UINT16, // message memory address
19
+ OperandType.UINT16, // message size
20
+ OperandType.UINT16, // fields memory address
21
+ OperandType.UINT16, // fields size address
22
22
  ];
23
23
 
24
24
  constructor(
@@ -33,17 +33,15 @@ export class DebugLog extends Instruction {
33
33
 
34
34
  public async execute(context: AvmContext): Promise<void> {
35
35
  const memory = context.machineState.memory.track(this.type);
36
- const [messageOffset, fieldsOffset, fieldsSizeOffset] = Addressing.fromWire(this.indirect).resolve(
37
- [this.messageOffset, this.fieldsOffset, this.fieldsSizeOffset],
38
- memory,
39
- );
36
+ const operands = [this.messageOffset, this.fieldsOffset, this.fieldsSizeOffset];
37
+ const addressing = Addressing.fromWire(this.indirect, operands.length);
38
+ const [messageOffset, fieldsOffset, fieldsSizeOffset] = addressing.resolve(operands, memory);
40
39
 
41
40
  const fieldsSize = memory.get(fieldsSizeOffset).toNumber();
42
41
  memory.checkTagsRange(TypeTag.UINT8, messageOffset, this.messageSize);
43
42
  memory.checkTagsRange(TypeTag.FIELD, fieldsOffset, fieldsSize);
44
43
 
45
- const memoryOperations = { reads: 1 + fieldsSize + this.messageSize, writes: 0, indirect: this.indirect };
46
- context.machineState.consumeGas(this.gasCost(memoryOperations));
44
+ context.machineState.consumeGas(this.gasCost());
47
45
 
48
46
  const rawMessage = memory.getSlice(messageOffset, this.messageSize);
49
47
  const fields = memory.getSlice(fieldsOffset, fieldsSize);
@@ -57,7 +55,7 @@ export class DebugLog extends Instruction {
57
55
 
58
56
  DebugLog.logger.verbose(formattedStr);
59
57
 
60
- memory.assert(memoryOperations);
58
+ memory.assert({ reads: 1 + fieldsSize + this.messageSize, addressing });
61
59
  context.machineState.incrementPc();
62
60
  }
63
61
  }
@@ -16,10 +16,10 @@ export class MultiScalarMul extends Instruction {
16
16
  static readonly wireFormat: OperandType[] = [
17
17
  OperandType.UINT8 /* opcode */,
18
18
  OperandType.UINT8 /* indirect */,
19
- OperandType.UINT32 /* points vector offset */,
20
- OperandType.UINT32 /* scalars vector offset */,
21
- OperandType.UINT32 /* output offset (fixed triplet) */,
22
- OperandType.UINT32 /* points length offset */,
19
+ OperandType.UINT16 /* points vector offset */,
20
+ OperandType.UINT16 /* scalars vector offset */,
21
+ OperandType.UINT16 /* output offset (fixed triplet) */,
22
+ OperandType.UINT16 /* points length offset */,
23
23
  ];
24
24
 
25
25
  constructor(
@@ -35,15 +35,14 @@ export class MultiScalarMul extends Instruction {
35
35
  public async execute(context: AvmContext): Promise<void> {
36
36
  const memory = context.machineState.memory.track(this.type);
37
37
  // Resolve indirects
38
- const [pointsOffset, scalarsOffset, outputOffset] = Addressing.fromWire(this.indirect).resolve(
39
- [this.pointsOffset, this.scalarsOffset, this.outputOffset],
40
- memory,
41
- );
38
+ const operands = [this.pointsOffset, this.scalarsOffset, this.outputOffset, this.pointsLengthOffset];
39
+ const addressing = Addressing.fromWire(this.indirect, operands.length);
40
+ const [pointsOffset, scalarsOffset, outputOffset, pointsLengthOffset] = addressing.resolve(operands, memory);
42
41
 
43
42
  // Length of the points vector should be U32
44
- memory.checkTag(TypeTag.UINT32, this.pointsLengthOffset);
43
+ memory.checkTag(TypeTag.UINT32, pointsLengthOffset);
45
44
  // Get the size of the unrolled (x, y , inf) points vector
46
- const pointsReadLength = memory.get(this.pointsLengthOffset).toNumber();
45
+ const pointsReadLength = memory.get(pointsLengthOffset).toNumber();
47
46
  if (pointsReadLength % 3 !== 0) {
48
47
  throw new InstructionExecutionError(`Points vector offset should be a multiple of 3, was ${pointsReadLength}`);
49
48
  }
@@ -62,13 +61,7 @@ export class MultiScalarMul extends Instruction {
62
61
 
63
62
  // The size of the scalars vector is twice the NUMBER of points because of the scalar limb decomposition
64
63
  const scalarReadLength = numPoints * 2;
65
- // Consume gas prior to performing work
66
- const memoryOperations = {
67
- reads: 1 + pointsReadLength + scalarReadLength /* points and scalars */,
68
- writes: 3 /* output triplet */,
69
- indirect: this.indirect,
70
- };
71
- context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: pointsReadLength }));
64
+ context.machineState.consumeGas(this.gasCost(pointsReadLength));
72
65
  // Get the unrolled scalar (lo & hi) representing the scalars
73
66
  const scalarsVector = memory.getSlice(scalarsOffset, scalarReadLength);
74
67
  memory.checkTagsRange(TypeTag.FIELD, scalarsOffset, scalarReadLength);
@@ -76,10 +69,8 @@ export class MultiScalarMul extends Instruction {
76
69
  // Now we need to reconstruct the points and scalars into something we can operate on.
77
70
  const grumpkinPoints: Point[] = [];
78
71
  for (let i = 0; i < numPoints; i++) {
79
- const p: Point = new Point(pointsVector[3 * i].toFr(), pointsVector[3 * i + 1].toFr(), false);
80
- // Include this later when we have a standard for representing infinity
81
- // const isInf = pointsVector[i + 2].toBoolean();
82
-
72
+ const isInf = pointsVector[3 * i + 2].toNumber() === 1;
73
+ const p: Point = new Point(pointsVector[3 * i].toFr(), pointsVector[3 * i + 1].toFr(), isInf);
83
74
  if (!p.isOnGrumpkin()) {
84
75
  throw new InstructionExecutionError(`Point ${p.toString()} is not on the curve.`);
85
76
  }
@@ -100,15 +91,29 @@ export class MultiScalarMul extends Instruction {
100
91
  const [firstBaseScalarPair, ...rest]: Array<[Point, Fq]> = grumpkinPoints.map((p, idx) => [p, scalarFqVector[idx]]);
101
92
  // Fold the points and scalars into a single point
102
93
  // We have to ensure get the first point, since the identity element (point at infinity) isn't quite working in ts
103
- const outputPoint = rest.reduce(
104
- (acc, curr) => grumpkin.add(acc, grumpkin.mul(curr[0], curr[1])),
105
- grumpkin.mul(firstBaseScalarPair[0], firstBaseScalarPair[1]),
106
- );
94
+ const outputPoint = rest.reduce((acc, curr) => {
95
+ if (curr[1] === Fq.ZERO) {
96
+ // If we multiply by 0, the result will the point at infinity - so we ignore it
97
+ return acc;
98
+ } else if (curr[0].inf) {
99
+ // If we multiply the point at infinity by a scalar, it's still the point at infinity
100
+ return acc;
101
+ } else if (acc.inf) {
102
+ // If we accumulator is the point at infinity, we can just return the current point
103
+ return curr[0];
104
+ } else {
105
+ return grumpkin.add(acc, grumpkin.mul(curr[0], curr[1]));
106
+ }
107
+ }, grumpkin.mul(firstBaseScalarPair[0], firstBaseScalarPair[1]));
107
108
  const output = outputPoint.toFields().map(f => new Field(f));
108
109
 
109
110
  memory.setSlice(outputOffset, output);
110
111
 
111
- memory.assert(memoryOperations);
112
+ memory.assert({
113
+ reads: 1 + pointsReadLength + scalarReadLength /* points and scalars */,
114
+ writes: 3 /* output triplet */,
115
+ addressing,
116
+ });
112
117
  context.machineState.incrementPc();
113
118
  }
114
119
  }
@@ -10,8 +10,8 @@ abstract class BaseStorageInstruction extends Instruction {
10
10
  public static readonly wireFormat: OperandType[] = [
11
11
  OperandType.UINT8,
12
12
  OperandType.UINT8,
13
- OperandType.UINT32,
14
- OperandType.UINT32,
13
+ OperandType.UINT16,
14
+ OperandType.UINT16,
15
15
  ];
16
16
 
17
17
  constructor(protected indirect: number, protected aOffset: number, protected bOffset: number) {
@@ -32,11 +32,12 @@ export class SStore extends BaseStorageInstruction {
32
32
  throw new StaticCallAlterationError();
33
33
  }
34
34
 
35
- const memoryOperations = { reads: 2, indirect: this.indirect };
36
35
  const memory = context.machineState.memory.track(this.type);
37
- context.machineState.consumeGas(this.gasCost({ ...memoryOperations }));
36
+ context.machineState.consumeGas(this.gasCost());
38
37
 
39
- const [srcOffset, slotOffset] = Addressing.fromWire(this.indirect).resolve([this.aOffset, this.bOffset], memory);
38
+ const operands = [this.aOffset, this.bOffset];
39
+ const addressing = Addressing.fromWire(this.indirect, operands.length);
40
+ const [srcOffset, slotOffset] = addressing.resolve(operands, memory);
40
41
  memory.checkTag(TypeTag.FIELD, slotOffset);
41
42
  memory.checkTag(TypeTag.FIELD, srcOffset);
42
43
 
@@ -44,7 +45,7 @@ export class SStore extends BaseStorageInstruction {
44
45
  const value = memory.get(srcOffset).toFr();
45
46
  context.persistableState.writeStorage(context.environment.storageAddress, slot, value);
46
47
 
47
- memory.assert(memoryOperations);
48
+ memory.assert({ reads: 2, addressing });
48
49
  context.machineState.incrementPc();
49
50
  }
50
51
  }
@@ -58,11 +59,12 @@ export class SLoad extends BaseStorageInstruction {
58
59
  }
59
60
 
60
61
  public async execute(context: AvmContext): Promise<void> {
61
- const memoryOperations = { writes: 1, reads: 1, indirect: this.indirect };
62
62
  const memory = context.machineState.memory.track(this.type);
63
- context.machineState.consumeGas(this.gasCost({ ...memoryOperations }));
63
+ context.machineState.consumeGas(this.gasCost());
64
64
 
65
- const [slotOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.aOffset, this.bOffset], memory);
65
+ const operands = [this.aOffset, this.bOffset];
66
+ const addressing = Addressing.fromWire(this.indirect, operands.length);
67
+ const [slotOffset, dstOffset] = addressing.resolve(operands, memory);
66
68
  memory.checkTag(TypeTag.FIELD, slotOffset);
67
69
 
68
70
  const slot = memory.get(slotOffset).toFr();
@@ -70,6 +72,6 @@ export class SLoad extends BaseStorageInstruction {
70
72
  memory.set(dstOffset, new Field(value));
71
73
 
72
74
  context.machineState.incrementPc();
73
- memory.assert(memoryOperations);
75
+ memory.assert({ writes: 1, reads: 1, addressing });
74
76
  }
75
77
  }
@@ -1,7 +1,6 @@
1
1
  import {
2
2
  Add,
3
3
  And,
4
- CMov,
5
4
  Call,
6
5
  CalldataCopy,
7
6
  Cast,
@@ -114,7 +113,6 @@ const INSTRUCTION_SET = () =>
114
113
  [Opcode.SET_FF, Set.as(Set.wireFormatFF).deserialize],
115
114
  [Opcode.MOV_8, Mov.as(Mov.wireFormat8).deserialize],
116
115
  [Opcode.MOV_16, Mov.as(Mov.wireFormat16).deserialize],
117
- [CMov.opcode, Instruction.deserialize.bind(CMov)],
118
116
 
119
117
  // World State
120
118
  [SLoad.opcode, Instruction.deserialize.bind(SLoad)], // Public Storage
@@ -55,7 +55,6 @@ export enum Opcode {
55
55
  SET_FF,
56
56
  MOV_8,
57
57
  MOV_16,
58
- CMOV,
59
58
  // World state
60
59
  SLOAD,
61
60
  SSTORE,
@@ -94,7 +93,6 @@ export enum Opcode {
94
93
  // Note that cpp code introduced an additional enum value TAG to express the instruction tag. In TS,
95
94
  // this one is parsed as UINT8.
96
95
  export enum OperandType {
97
- UINT1,
98
96
  UINT8,
99
97
  UINT16,
100
98
  UINT32,
@@ -108,7 +106,6 @@ type OperandWriter = (value: any) => void;
108
106
 
109
107
  // Specifies how to read and write each operand type.
110
108
  const OPERAND_SPEC = new Map<OperandType, [number, () => OperandNativeType, OperandWriter]>([
111
- [OperandType.UINT1, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
112
109
  [OperandType.UINT8, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
113
110
  [OperandType.UINT16, [2, Buffer.prototype.readUint16BE, Buffer.prototype.writeUint16BE]],
114
111
  [OperandType.UINT32, [4, Buffer.prototype.readUint32BE, Buffer.prototype.writeUint32BE]],
@@ -161,7 +158,7 @@ function writeBigInt128BE(this: Buffer, value: bigint): void {
161
158
  */
162
159
  export function deserialize(cursor: BufferCursor | Buffer, operands: OperandType[]): (number | bigint)[] {
163
160
  const argValues = [];
164
- if (cursor instanceof Buffer) {
161
+ if (Buffer.isBuffer(cursor)) {
165
162
  cursor = new BufferCursor(cursor);
166
163
  }
167
164
 
@@ -1,5 +1,4 @@
1
- import { Fr } from '@aztec/circuits.js';
2
- import { type ContractInstanceWithAddress } from '@aztec/types/contracts';
1
+ import { type ContractInstanceWithAddress, Fr } from '@aztec/circuits.js';
3
2
 
4
3
  import { type jest } from '@jest/globals';
5
4
  import { mock } from 'jest-mock-extended';
@@ -1,50 +1,38 @@
1
1
  import {
2
2
  type AuthWitness,
3
3
  type AztecNode,
4
+ CountedLog,
5
+ CountedNoteLog,
6
+ CountedPublicExecutionRequest,
4
7
  EncryptedL2Log,
5
8
  EncryptedL2NoteLog,
6
- Event,
7
- L1EventPayload,
8
- L1NotePayload,
9
9
  Note,
10
+ NoteAndSlot,
10
11
  type NoteStatus,
12
+ type PrivateExecutionResult,
11
13
  PublicExecutionRequest,
12
- TaggedLog,
13
14
  type UnencryptedL2Log,
14
15
  } from '@aztec/circuit-types';
15
16
  import {
16
17
  CallContext,
17
18
  FunctionSelector,
18
19
  type Header,
19
- type KeyValidationRequest,
20
+ PRIVATE_CONTEXT_INPUTS_LENGTH,
21
+ PUBLIC_DISPATCH_SELECTOR,
20
22
  PrivateContextInputs,
21
23
  type TxContext,
22
24
  } from '@aztec/circuits.js';
23
- import { Aes128 } from '@aztec/circuits.js/barretenberg';
24
25
  import { computeUniqueNoteHash, siloNoteHash } from '@aztec/circuits.js/hash';
25
- import {
26
- EventSelector,
27
- type FunctionAbi,
28
- type FunctionArtifact,
29
- type NoteSelector,
30
- countArgumentsSize,
31
- } from '@aztec/foundation/abi';
26
+ import { type FunctionAbi, type FunctionArtifact, type NoteSelector, countArgumentsSize } from '@aztec/foundation/abi';
32
27
  import { type AztecAddress } from '@aztec/foundation/aztec-address';
33
28
  import { poseidon2HashWithSeparator } from '@aztec/foundation/crypto';
34
- import { Fr, GrumpkinScalar, type Point } from '@aztec/foundation/fields';
29
+ import { Fr } from '@aztec/foundation/fields';
35
30
  import { applyStringFormatting, createDebugLogger } from '@aztec/foundation/log';
36
31
 
37
32
  import { type NoteData, toACVMWitness } from '../acvm/index.js';
38
33
  import { type PackedValuesCache } from '../common/packed_values_cache.js';
39
34
  import { type DBOracle } from './db_oracle.js';
40
35
  import { type ExecutionNoteCache } from './execution_note_cache.js';
41
- import {
42
- CountedLog,
43
- CountedNoteLog,
44
- CountedPublicExecutionRequest,
45
- type ExecutionResult,
46
- type NoteAndSlot,
47
- } from './execution_result.js';
48
36
  import { pickNotes } from './pick_notes.js';
49
37
  import { executePrivateFunction } from './private_execution.js';
50
38
  import { ViewDataOracle } from './view_data_oracle.js';
@@ -75,7 +63,7 @@ export class ClientExecutionContext extends ViewDataOracle {
75
63
  private noteEncryptedLogs: CountedNoteLog[] = [];
76
64
  private encryptedLogs: CountedLog<EncryptedL2Log>[] = [];
77
65
  private unencryptedLogs: CountedLog<UnencryptedL2Log>[] = [];
78
- private nestedExecutions: ExecutionResult[] = [];
66
+ private nestedExecutions: PrivateExecutionResult[] = [];
79
67
  private enqueuedPublicFunctionCalls: CountedPublicExecutionRequest[] = [];
80
68
  private publicTeardownFunctionCall: PublicExecutionRequest = PublicExecutionRequest.empty();
81
69
 
@@ -121,8 +109,12 @@ export class ClientExecutionContext extends ViewDataOracle {
121
109
  this.txContext,
122
110
  this.sideEffectCounter,
123
111
  );
112
+ const privateContextInputsAsFields = privateContextInputs.toFields();
113
+ if (privateContextInputsAsFields.length !== PRIVATE_CONTEXT_INPUTS_LENGTH) {
114
+ throw new Error('Invalid private context inputs size');
115
+ }
124
116
 
125
- const fields = [...privateContextInputs.toFields(), ...args];
117
+ const fields = [...privateContextInputsAsFields, ...args];
126
118
  return toACVMWitness(0, fields);
127
119
  }
128
120
 
@@ -314,11 +306,7 @@ export class ClientExecutionContext extends ViewDataOracle {
314
306
  },
315
307
  counter,
316
308
  );
317
- this.newNotes.push({
318
- storageSlot,
319
- noteTypeId,
320
- note,
321
- });
309
+ this.newNotes.push(new NoteAndSlot(note, storageSlot, noteTypeId));
322
310
  }
323
311
 
324
312
  /**
@@ -373,62 +361,6 @@ export class ClientExecutionContext extends ViewDataOracle {
373
361
  this.noteEncryptedLogs.push(encryptedLog);
374
362
  }
375
363
 
376
- /**
377
- * Encrypt an event
378
- * @param contractAddress - The contract emitting the encrypted event.
379
- * @param randomness - A value used to mask the contract address we are siloing with.
380
- * @param eventTypeId - The type ID of the event (function selector).
381
- * @param ovKeys - The outgoing viewing keys to use to encrypt.
382
- * @param ivpkM - The master incoming viewing public key.
383
- * @param recipient - The recipient of the encrypted event log.
384
- * @param preimage - The event preimage.
385
- */
386
- public override computeEncryptedEventLog(
387
- contractAddress: AztecAddress,
388
- randomness: Fr,
389
- eventTypeId: Fr,
390
- ovKeys: KeyValidationRequest,
391
- ivpkM: Point,
392
- recipient: AztecAddress,
393
- preimage: Fr[],
394
- ) {
395
- const event = new Event(preimage);
396
- const l1EventPayload = new L1EventPayload(event, contractAddress, randomness, EventSelector.fromField(eventTypeId));
397
- const taggedEvent = new TaggedLog(l1EventPayload);
398
-
399
- const ephSk = GrumpkinScalar.random();
400
-
401
- return taggedEvent.encrypt(ephSk, recipient, ivpkM, ovKeys);
402
- }
403
-
404
- /**
405
- * Encrypt a note
406
- * @param contractAddress - The contract address of the note.
407
- * @param storageSlot - The storage slot the note is at.
408
- * @param noteTypeId - The type ID of the note.
409
- * @param ovKeys - The outgoing viewing keys to use to encrypt.
410
- * @param ivpkM - The master incoming viewing public key.
411
- * @param recipient - The recipient of the encrypted note log.
412
- * @param preimage - The note preimage.
413
- */
414
- public override computeEncryptedNoteLog(
415
- contractAddress: AztecAddress,
416
- storageSlot: Fr,
417
- noteTypeId: NoteSelector,
418
- ovKeys: KeyValidationRequest,
419
- ivpkM: Point,
420
- recipient: AztecAddress,
421
- preimage: Fr[],
422
- ) {
423
- const note = new Note(preimage);
424
- const l1NotePayload = new L1NotePayload(note, contractAddress, storageSlot, noteTypeId);
425
- const taggedNote = new TaggedLog(l1NotePayload);
426
-
427
- const ephSk = GrumpkinScalar.random();
428
-
429
- return taggedNote.encrypt(ephSk, recipient, ivpkM, ovKeys);
430
- }
431
-
432
364
  /**
433
365
  * Emit an unencrypted log.
434
366
  * @param log - The unencrypted log to be emitted.
@@ -457,7 +389,7 @@ export class ClientExecutionContext extends ViewDataOracle {
457
389
  return Fr.fromBuffer(log.hash());
458
390
  }
459
391
 
460
- #checkValidStaticCall(childExecutionResult: ExecutionResult) {
392
+ #checkValidStaticCall(childExecutionResult: PrivateExecutionResult) {
461
393
  if (
462
394
  childExecutionResult.callStackItem.publicInputs.noteHashes.some(item => !item.isEmpty()) ||
463
395
  childExecutionResult.callStackItem.publicInputs.nullifiers.some(item => !item.isEmpty()) ||
@@ -568,7 +500,7 @@ export class ClientExecutionContext extends ViewDataOracle {
568
500
  const args = this.packedValuesCache.unpack(argsHash);
569
501
 
570
502
  this.log.verbose(
571
- `Created PublicExecutionRequest of type [${callType}], side-effect counter [${sideEffectCounter}] to ${targetContractAddress}:${functionSelector}(${targetArtifact.name})`,
503
+ `Created PublicExecutionRequest to ${targetArtifact.name}@${targetContractAddress}, of type [${callType}], side-effect counter [${sideEffectCounter}]`,
572
504
  );
573
505
 
574
506
  const request = PublicExecutionRequest.from({
@@ -602,16 +534,27 @@ export class ClientExecutionContext extends ViewDataOracle {
602
534
  sideEffectCounter: number,
603
535
  isStaticCall: boolean,
604
536
  isDelegateCall: boolean,
605
- ) {
537
+ ): Promise<Fr> {
538
+ // TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Fix this.
539
+ // WARNING: This is insecure and should be temporary!
540
+ // The oracle repacks the arguments and returns a new args_hash.
541
+ // new_args = [selector, ...old_args], so as to make it suitable to call the public dispatch function.
542
+ // We don't validate or compute it in the circuit because a) it's harder to do with slices, and
543
+ // b) this is only temporary.
544
+ const newArgsHash = this.packedValuesCache.pack([
545
+ functionSelector.toField(),
546
+ ...this.packedValuesCache.unpack(argsHash),
547
+ ]);
606
548
  await this.createPublicExecutionRequest(
607
549
  'enqueued',
608
550
  targetContractAddress,
609
- functionSelector,
610
- argsHash,
551
+ FunctionSelector.fromField(new Fr(PUBLIC_DISPATCH_SELECTOR)),
552
+ newArgsHash,
611
553
  sideEffectCounter,
612
554
  isStaticCall,
613
555
  isDelegateCall,
614
556
  );
557
+ return newArgsHash;
615
558
  }
616
559
 
617
560
  /**
@@ -632,16 +575,27 @@ export class ClientExecutionContext extends ViewDataOracle {
632
575
  sideEffectCounter: number,
633
576
  isStaticCall: boolean,
634
577
  isDelegateCall: boolean,
635
- ) {
578
+ ): Promise<Fr> {
579
+ // TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Fix this.
580
+ // WARNING: This is insecure and should be temporary!
581
+ // The oracle repacks the arguments and returns a new args_hash.
582
+ // new_args = [selector, ...old_args], so as to make it suitable to call the public dispatch function.
583
+ // We don't validate or compute it in the circuit because a) it's harder to do with slices, and
584
+ // b) this is only temporary.
585
+ const newArgsHash = this.packedValuesCache.pack([
586
+ functionSelector.toField(),
587
+ ...this.packedValuesCache.unpack(argsHash),
588
+ ]);
636
589
  await this.createPublicExecutionRequest(
637
590
  'teardown',
638
591
  targetContractAddress,
639
- functionSelector,
640
- argsHash,
592
+ FunctionSelector.fromField(new Fr(PUBLIC_DISPATCH_SELECTOR)),
593
+ newArgsHash,
641
594
  sideEffectCounter,
642
595
  isStaticCall,
643
596
  isDelegateCall,
644
597
  );
598
+ return newArgsHash;
645
599
  }
646
600
 
647
601
  public override notifySetMinRevertibleSideEffectCounter(minRevertibleSideEffectCounter: number): void {
@@ -698,11 +652,6 @@ export class ClientExecutionContext extends ViewDataOracle {
698
652
  return values;
699
653
  }
700
654
 
701
- public override aes128Encrypt(input: Buffer, initializationVector: Buffer, key: Buffer): Buffer {
702
- const aes128 = new Aes128();
703
- return aes128.encryptBufferCBC(input, initializationVector, key);
704
- }
705
-
706
655
  public override debugLog(message: string, fields: Fr[]) {
707
656
  this.log.verbose(`debug_log ${applyStringFormatting(message, fields)}`);
708
657
  }
@@ -5,11 +5,15 @@ import {
5
5
  type NullifierMembershipWitness,
6
6
  type PublicDataWitness,
7
7
  } from '@aztec/circuit-types';
8
- import { type CompleteAddress, type Header, type KeyValidationRequest } from '@aztec/circuits.js';
8
+ import {
9
+ type CompleteAddress,
10
+ type ContractInstance,
11
+ type Header,
12
+ type KeyValidationRequest,
13
+ } from '@aztec/circuits.js';
9
14
  import { type FunctionArtifact, type FunctionSelector } from '@aztec/foundation/abi';
10
15
  import { type AztecAddress } from '@aztec/foundation/aztec-address';
11
16
  import { type Fr } from '@aztec/foundation/fields';
12
- import { type ContractInstance } from '@aztec/types/contracts';
13
17
 
14
18
  import { type NoteData } from '../acvm/index.js';
15
19
  import { type CommitmentsDB } from '../public/db_interfaces.js';
@@ -1,5 +1,5 @@
1
1
  export * from './simulator.js';
2
2
  export * from './db_oracle.js';
3
- export * from './execution_result.js';
4
3
  export * from './pick_notes.js';
5
4
  export * from './execution_note_cache.js';
5
+ export { extractPrivateCircuitPublicInputs } from './private_execution.js';
@@ -1,15 +1,22 @@
1
+ import { PrivateExecutionResult } from '@aztec/circuit-types';
1
2
  import { type CircuitWitnessGenerationStats } from '@aztec/circuit-types/stats';
2
- import { Fr, FunctionData, PrivateCallStackItem, PrivateCircuitPublicInputs } from '@aztec/circuits.js';
3
- import type { FunctionArtifact, FunctionSelector } from '@aztec/foundation/abi';
3
+ import {
4
+ Fr,
5
+ FunctionData,
6
+ PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH,
7
+ PRIVATE_CONTEXT_INPUTS_LENGTH,
8
+ PrivateCallStackItem,
9
+ PrivateCircuitPublicInputs,
10
+ } from '@aztec/circuits.js';
11
+ import { type FunctionArtifact, type FunctionSelector, countArgumentsSize } from '@aztec/foundation/abi';
4
12
  import { type AztecAddress } from '@aztec/foundation/aztec-address';
5
13
  import { createDebugLogger } from '@aztec/foundation/log';
6
14
  import { Timer } from '@aztec/foundation/timer';
7
15
 
8
- import { witnessMapToFields } from '../acvm/deserialize.js';
9
- import { Oracle, acvm, extractCallStack } from '../acvm/index.js';
16
+ import { fromACVMField, witnessMapToFields } from '../acvm/deserialize.js';
17
+ import { type ACVMWitness, Oracle, acvm, extractCallStack } from '../acvm/index.js';
10
18
  import { ExecutionError } from '../common/errors.js';
11
19
  import { type ClientExecutionContext } from './client_execution_context.js';
12
- import { type ExecutionResult } from './execution_result.js';
13
20
 
14
21
  /**
15
22
  * Execute a private function and return the execution result.
@@ -19,10 +26,10 @@ export async function executePrivateFunction(
19
26
  artifact: FunctionArtifact,
20
27
  contractAddress: AztecAddress,
21
28
  functionSelector: FunctionSelector,
22
- log = createDebugLogger('aztec:simulator:secret_execution'),
23
- ): Promise<ExecutionResult> {
29
+ log = createDebugLogger('aztec:simulator:private_execution'),
30
+ ): Promise<PrivateExecutionResult> {
24
31
  const functionName = await context.getDebugFunctionName();
25
- log.verbose(`Executing external function ${contractAddress}:${functionSelector}(${functionName})`);
32
+ log.verbose(`Executing external function ${functionName}@${contractAddress}`);
26
33
  const acir = artifact.bytecode;
27
34
  const initialWitness = context.getInitialWitness(artifact);
28
35
  const acvmCallback = new Oracle(context);
@@ -40,8 +47,7 @@ export async function executePrivateFunction(
40
47
  });
41
48
  const duration = timer.ms();
42
49
  const partialWitness = acirExecutionResult.partialWitness;
43
- const returnWitness = witnessMapToFields(acirExecutionResult.returnWitness);
44
- const publicInputs = PrivateCircuitPublicInputs.fromFields(returnWitness);
50
+ const publicInputs = extractPrivateCircuitPublicInputs(artifact, partialWitness);
45
51
 
46
52
  // TODO (alexg) estimate this size
47
53
  const initialWitnessSize = witnessMapToFields(initialWitness).length * Fr.SIZE_IN_BYTES;
@@ -75,20 +81,44 @@ export async function executePrivateFunction(
75
81
 
76
82
  log.debug(`Returning from call to ${contractAddress.toString()}:${functionSelector}`);
77
83
 
78
- return {
84
+ return new PrivateExecutionResult(
79
85
  acir,
86
+ Buffer.from(artifact.verificationKey!, 'hex'),
80
87
  partialWitness,
81
88
  callStackItem,
82
- returnValues: rawReturnValues,
83
89
  noteHashLeafIndexMap,
84
90
  newNotes,
85
91
  noteHashNullifierCounterMap,
86
- vk: Buffer.from(artifact.verificationKey!, 'hex'),
92
+ rawReturnValues,
87
93
  nestedExecutions,
88
94
  enqueuedPublicFunctionCalls,
89
- noteEncryptedLogs,
90
95
  publicTeardownFunctionCall,
96
+ noteEncryptedLogs,
91
97
  encryptedLogs,
92
98
  unencryptedLogs,
93
- };
99
+ );
100
+ }
101
+
102
+ /**
103
+ * Get the private circuit public inputs from the partial witness.
104
+ * @param artifact - The function artifact
105
+ * @param partialWitness - The partial witness, result of simulating the function.
106
+ * @returns - The public inputs.
107
+ */
108
+ export function extractPrivateCircuitPublicInputs(
109
+ artifact: FunctionArtifact,
110
+ partialWitness: ACVMWitness,
111
+ ): PrivateCircuitPublicInputs {
112
+ const parametersSize = countArgumentsSize(artifact) + PRIVATE_CONTEXT_INPUTS_LENGTH;
113
+ const returnsSize = PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH;
114
+ const returnData = [];
115
+ // Return values always appear in the witness after arguments.
116
+ for (let i = parametersSize; i < parametersSize + returnsSize; i++) {
117
+ const returnedField = partialWitness.get(i);
118
+ if (returnedField === undefined) {
119
+ throw new Error(`Missing return value for index ${i}`);
120
+ }
121
+ returnData.push(fromACVMField(returnedField));
122
+ }
123
+ return PrivateCircuitPublicInputs.fromFields(returnData);
94
124
  }
@@ -1,4 +1,4 @@
1
- import { type AztecNode, type FunctionCall, type Note, type TxExecutionRequest } from '@aztec/circuit-types';
1
+ import type { AztecNode, FunctionCall, Note, PrivateExecutionResult, TxExecutionRequest } from '@aztec/circuit-types';
2
2
  import { CallContext } from '@aztec/circuits.js';
3
3
  import {
4
4
  type ArrayType,
@@ -17,7 +17,6 @@ import { PackedValuesCache } from '../common/packed_values_cache.js';
17
17
  import { ClientExecutionContext } from './client_execution_context.js';
18
18
  import { type DBOracle } from './db_oracle.js';
19
19
  import { ExecutionNoteCache } from './execution_note_cache.js';
20
- import { type ExecutionResult } from './execution_result.js';
21
20
  import { executePrivateFunction } from './private_execution.js';
22
21
  import { executeUnconstrainedFunction } from './unconstrained_execution.js';
23
22
  import { ViewDataOracle } from './view_data_oracle.js';
@@ -47,7 +46,7 @@ export class AcirSimulator {
47
46
  contractAddress: AztecAddress,
48
47
  msgSender = AztecAddress.fromField(Fr.MAX_FIELD_VALUE),
49
48
  scopes?: AztecAddress[],
50
- ): Promise<ExecutionResult> {
49
+ ): Promise<PrivateExecutionResult> {
51
50
  if (entryPointArtifact.functionType !== FunctionType.PRIVATE) {
52
51
  throw new Error(`Cannot run ${entryPointArtifact.functionType} function as private`);
53
52
  }