@aztec/stdlib 0.77.0-testnet-ignition.17 → 0.77.0-testnet-ignition.21

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 (56) hide show
  1. package/dest/avm/avm.d.ts +3559 -414
  2. package/dest/avm/avm.d.ts.map +1 -1
  3. package/dest/avm/avm.js +117 -662
  4. package/dest/avm/avm_accumulated_data.d.ts +94 -0
  5. package/dest/avm/avm_accumulated_data.d.ts.map +1 -1
  6. package/dest/avm/avm_accumulated_data.js +12 -1
  7. package/dest/avm/avm_circuit_public_inputs.d.ts +1023 -0
  8. package/dest/avm/avm_circuit_public_inputs.d.ts.map +1 -1
  9. package/dest/avm/avm_circuit_public_inputs.js +30 -1
  10. package/dest/avm/avm_proving_request.d.ts +2567 -2
  11. package/dest/avm/avm_proving_request.d.ts.map +1 -1
  12. package/dest/avm/message_pack.d.ts +4 -0
  13. package/dest/avm/message_pack.d.ts.map +1 -0
  14. package/dest/avm/message_pack.js +47 -0
  15. package/dest/block/l2_block_source.d.ts +17 -0
  16. package/dest/block/l2_block_source.d.ts.map +1 -1
  17. package/dest/block/l2_block_source.js +4 -0
  18. package/dest/interfaces/proving-job.d.ts +2567 -2
  19. package/dest/interfaces/proving-job.d.ts.map +1 -1
  20. package/dest/kernel/private_to_avm_accumulated_data.d.ts +85 -0
  21. package/dest/kernel/private_to_avm_accumulated_data.d.ts.map +1 -1
  22. package/dest/kernel/private_to_avm_accumulated_data.js +17 -1
  23. package/dest/kernel/public_call_request.d.ts +26 -0
  24. package/dest/kernel/public_call_request.d.ts.map +1 -1
  25. package/dest/kernel/public_call_request.js +13 -0
  26. package/dest/messaging/l2_to_l1_message.d.ts +55 -0
  27. package/dest/messaging/l2_to_l1_message.d.ts.map +1 -1
  28. package/dest/messaging/l2_to_l1_message.js +15 -0
  29. package/dest/p2p/consensus_payload.d.ts.map +1 -1
  30. package/dest/p2p/consensus_payload.js +2 -1
  31. package/dest/tests/factories.d.ts +4 -4
  32. package/dest/tests/factories.d.ts.map +1 -1
  33. package/dest/tests/factories.js +20 -19
  34. package/dest/trees/nullifier_membership_witness.d.ts +7 -7
  35. package/dest/trees/public_data_witness.d.ts +7 -7
  36. package/dest/tx/private_execution_result.js +1 -1
  37. package/dest/tx/processed_tx.d.ts +4 -0
  38. package/dest/tx/processed_tx.d.ts.map +1 -1
  39. package/dest/tx/processed_tx.js +2 -0
  40. package/dest/tx/tree_snapshots.d.ts +94 -0
  41. package/dest/tx/tree_snapshots.d.ts.map +1 -1
  42. package/dest/tx/tree_snapshots.js +9 -0
  43. package/package.json +6 -6
  44. package/src/avm/avm.ts +208 -906
  45. package/src/avm/avm_accumulated_data.ts +27 -1
  46. package/src/avm/avm_circuit_public_inputs.ts +73 -1
  47. package/src/avm/message_pack.ts +48 -0
  48. package/src/block/l2_block_source.ts +18 -0
  49. package/src/kernel/private_to_avm_accumulated_data.ts +33 -0
  50. package/src/kernel/public_call_request.ts +16 -0
  51. package/src/messaging/l2_to_l1_message.ts +22 -0
  52. package/src/p2p/consensus_payload.ts +2 -1
  53. package/src/tests/factories.ts +38 -28
  54. package/src/tx/private_execution_result.ts +1 -1
  55. package/src/tx/processed_tx.ts +6 -0
  56. package/src/tx/tree_snapshots.ts +15 -0
package/dest/avm/avm.js CHANGED
@@ -1,70 +1,25 @@
1
- import { Fq, Fr, Point } from '@aztec/foundation/fields';
2
- import { bufferSchemaFor } from '@aztec/foundation/schemas';
3
- import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
4
- import { bufferToHex, hexToBuffer } from '@aztec/foundation/string';
5
- import { strict as assert } from 'assert';
6
- import { Encoder, addExtension } from 'msgpackr';
1
+ import { Fr } from '@aztec/foundation/fields';
2
+ import { jsonParseWithSchema, jsonStringify } from '@aztec/foundation/json-rpc';
3
+ import { schemas } from '@aztec/foundation/schemas';
4
+ import { z } from 'zod';
7
5
  import { AztecAddress } from '../aztec-address/index.js';
8
6
  import { PublicKeys } from '../keys/public_keys.js';
9
7
  import { NullifierLeafPreimage } from '../trees/nullifier_leaf.js';
10
8
  import { PublicDataTreeLeafPreimage } from '../trees/public_data_leaf.js';
11
- import { Vector } from '../types/shared.js';
12
9
  import { AvmCircuitPublicInputs } from './avm_circuit_public_inputs.js';
10
+ import { serializeWithMessagePack } from './message_pack.js';
13
11
  export class AvmEnqueuedCallHint {
14
12
  contractAddress;
15
13
  calldata;
16
14
  constructor(contractAddress, calldata){
17
15
  this.contractAddress = contractAddress;
18
- this.calldata = new Vector(calldata);
19
- }
20
- /* Serializes the inputs to a buffer.
21
- * @returns - The inputs serialized to a buffer.
22
- */ toBuffer() {
23
- return serializeToBuffer(...AvmEnqueuedCallHint.getFields(this));
24
- }
25
- /**
26
- * Serializes the inputs to a hex string.
27
- * @returns The instance serialized to a hex string.
28
- */ toString() {
29
- return bufferToHex(this.toBuffer());
30
- }
31
- /**
32
- * Is the struct empty?
33
- * @returns whether all members are empty.
34
- */ isEmpty() {
35
- return this.contractAddress.isZero() && this.calldata.items.length == 0;
36
- }
37
- /**
38
- * Creates a new instance from fields.
39
- * @param fields - Fields to create the instance from.
40
- * @returns A new AvmExecutionHints instance.
41
- */ static from(fields) {
42
- return new AvmEnqueuedCallHint(fields.contractAddress, fields.calldata.items);
43
- }
44
- /**
45
- * Extracts fields from an instance.
46
- * @param fields - Fields to create the instance from.
47
- * @returns An array of fields.
48
- */ static getFields(fields) {
49
- return [
50
- fields.contractAddress,
51
- fields.calldata
52
- ];
53
- }
54
- /**
55
- * Deserializes from a buffer or reader.
56
- * @param buffer - Buffer or reader to read from.
57
- * @returns The deserialized instance.
58
- */ static fromBuffer(buff) {
59
- const reader = BufferReader.asReader(buff);
60
- return new AvmEnqueuedCallHint(AztecAddress.fromBuffer(reader), reader.readVector(Fr));
16
+ this.calldata = calldata;
61
17
  }
62
- /**
63
- * Deserializes from a hex string.
64
- * @param str - Hex string to read from.
65
- * @returns The deserialized instance.
66
- */ static fromString(str) {
67
- return AvmEnqueuedCallHint.fromBuffer(hexToBuffer(str));
18
+ static get schema() {
19
+ return z.object({
20
+ contractAddress: AztecAddress.schema,
21
+ calldata: schemas.Fr.array()
22
+ }).transform(({ contractAddress, calldata })=>new AvmEnqueuedCallHint(contractAddress, calldata));
68
23
  }
69
24
  }
70
25
  export class AvmContractClassHint {
@@ -82,53 +37,15 @@ export class AvmContractClassHint {
82
37
  this.publicBytecodeCommitment = publicBytecodeCommitment;
83
38
  this.packedBytecode = packedBytecode;
84
39
  }
85
- /**
86
- * Serializes the inputs to a buffer.
87
- * @returns - The inputs serialized to a buffer.
88
- */ toBuffer() {
89
- return serializeToBuffer(...AvmContractClassHint.getFields(this));
90
- }
91
- /**
92
- * Serializes the inputs to a hex string.
93
- * @returns The instance serialized to a hex string.
94
- */ toString() {
95
- return bufferToHex(this.toBuffer());
96
- }
97
- /**
98
- * Is the struct empty?
99
- * @returns whether all members are empty.
100
- */ isEmpty() {
101
- return this.classId.isZero() && !this.exists && this.artifactHash.isZero() && this.privateFunctionsRoot.isZero() && this.publicBytecodeCommitment.isZero() && this.packedBytecode.length == 0;
102
- }
103
- /**
104
- * Extracts fields from an instance.
105
- * @param fields - Fields to create the instance from.
106
- * @returns An array of fields.
107
- */ static getFields(fields) {
108
- return [
109
- fields.classId,
110
- fields.exists,
111
- fields.artifactHash,
112
- fields.privateFunctionsRoot,
113
- fields.publicBytecodeCommitment,
114
- fields.packedBytecode.length,
115
- fields.packedBytecode
116
- ];
117
- }
118
- /**
119
- * Deserializes from a buffer or reader.
120
- * @param buffer - Buffer or reader to read from.
121
- * @returns The deserialized instance.
122
- */ static fromBuffer(buff) {
123
- const reader = BufferReader.asReader(buff);
124
- return new AvmContractClassHint(Fr.fromBuffer(reader), reader.readBoolean(), Fr.fromBuffer(reader), Fr.fromBuffer(reader), Fr.fromBuffer(reader), reader.readBuffer());
125
- }
126
- /**
127
- * Deserializes from a hex string.
128
- * @param str - Hex string to read from.
129
- * @returns The deserialized instance.
130
- */ static fromString(str) {
131
- return AvmContractClassHint.fromBuffer(hexToBuffer(str));
40
+ static get schema() {
41
+ return z.object({
42
+ classId: schemas.Fr,
43
+ exists: z.boolean(),
44
+ artifactHash: schemas.Fr,
45
+ privateFunctionsRoot: schemas.Fr,
46
+ publicBytecodeCommitment: schemas.Fr,
47
+ packedBytecode: schemas.Buffer
48
+ }).transform(({ classId, exists, artifactHash, privateFunctionsRoot, publicBytecodeCommitment, packedBytecode })=>new AvmContractClassHint(classId, exists, artifactHash, privateFunctionsRoot, publicBytecodeCommitment, packedBytecode));
132
49
  }
133
50
  }
134
51
  export class AvmContractInstanceHint {
@@ -142,7 +59,8 @@ export class AvmContractInstanceHint {
142
59
  publicKeys;
143
60
  updateMembershipHint;
144
61
  updatePreimage;
145
- constructor(address, exists, salt, deployer, currentContractClassId, originalContractClassId, initializationHash, publicKeys, updateMembershipHint = AvmPublicDataReadTreeHint.empty(), updatePreimage){
62
+ constructor(address, exists, salt, deployer, currentContractClassId, originalContractClassId, initializationHash, publicKeys, // public readonly updateMembershipHint: AvmPublicDataReadTreeHint = AvmPublicDataReadTreeHint.empty(),
63
+ updateMembershipHint, updatePreimage = []){
146
64
  this.address = address;
147
65
  this.exists = exists;
148
66
  this.salt = salt;
@@ -152,403 +70,107 @@ export class AvmContractInstanceHint {
152
70
  this.initializationHash = initializationHash;
153
71
  this.publicKeys = publicKeys;
154
72
  this.updateMembershipHint = updateMembershipHint;
155
- this.updatePreimage = new Vector(updatePreimage);
156
- }
157
- /**
158
- * Serializes the inputs to a buffer.
159
- * @returns - The inputs serialized to a buffer.
160
- */ toBuffer() {
161
- return serializeToBuffer(...AvmContractInstanceHint.getFields(this));
162
- }
163
- /**
164
- * Serializes the inputs to a hex string.
165
- * @returns The instance serialized to a hex string.
166
- */ toString() {
167
- return bufferToHex(this.toBuffer());
168
- }
169
- /**
170
- * Is the struct empty?
171
- * @returns whether all members are empty.
172
- */ isEmpty() {
173
- return this.address.isZero() && !this.exists && this.salt.isZero() && this.deployer.isZero() && this.currentContractClassId.isZero() && this.originalContractClassId.isZero() && this.initializationHash.isZero() && this.publicKeys.isEmpty() && this.updateMembershipHint.isEmpty() && this.updatePreimage.items.length == 0;
174
- }
175
- /**
176
- * Extracts fields from an instance.
177
- * @param fields - Fields to create the instance from.
178
- * @returns An array of fields.
179
- */ static getFields(fields) {
180
- return [
181
- fields.address,
182
- fields.exists,
183
- fields.salt,
184
- fields.deployer,
185
- fields.currentContractClassId,
186
- fields.originalContractClassId,
187
- fields.initializationHash,
188
- fields.publicKeys,
189
- fields.updateMembershipHint,
190
- fields.updatePreimage
191
- ];
192
- }
193
- /**
194
- * Deserializes from a buffer or reader.
195
- * @param buffer - Buffer or reader to read from.
196
- * @returns The deserialized instance.
197
- */ static fromBuffer(buff) {
198
- const reader = BufferReader.asReader(buff);
199
- return new AvmContractInstanceHint(AztecAddress.fromBuffer(reader), reader.readBoolean(), Fr.fromBuffer(reader), AztecAddress.fromBuffer(reader), Fr.fromBuffer(reader), Fr.fromBuffer(reader), Fr.fromBuffer(reader), PublicKeys.fromBuffer(reader), AvmPublicDataReadTreeHint.fromBuffer(reader), reader.readVector(Fr));
200
- }
201
- /**
202
- * Deserializes from a hex string.
203
- * @param str - Hex string to read from.
204
- * @returns The deserialized instance.
205
- */ static fromString(str) {
206
- return AvmContractInstanceHint.fromBuffer(hexToBuffer(str));
73
+ this.updatePreimage = updatePreimage;
74
+ }
75
+ static get schema() {
76
+ return z.object({
77
+ address: AztecAddress.schema,
78
+ exists: z.boolean(),
79
+ salt: schemas.Fr,
80
+ deployer: AztecAddress.schema,
81
+ currentContractClassId: schemas.Fr,
82
+ originalContractClassId: schemas.Fr,
83
+ initializationHash: schemas.Fr,
84
+ publicKeys: PublicKeys.schema,
85
+ updateMembershipHint: AvmPublicDataReadTreeHint.schema,
86
+ updatePreimage: schemas.Fr.array()
87
+ }).transform(({ address, exists, salt, deployer, currentContractClassId, originalContractClassId, initializationHash, publicKeys, updateMembershipHint, updatePreimage })=>new AvmContractInstanceHint(address, exists, salt, deployer, currentContractClassId, originalContractClassId, initializationHash, publicKeys, updateMembershipHint, updatePreimage));
207
88
  }
208
89
  }
209
90
  export class AvmAppendTreeHint {
210
91
  leafIndex;
211
92
  value;
212
- _siblingPath;
213
93
  siblingPath;
214
- /*
215
- * @param bytecode the contract bytecode
216
- * @param contractInstance the contract instance of the nested call, used to derive the contract address
217
- * @param contractClassPreimage the contract class preimage of the nested call, used to derive the class id
218
- * */ constructor(leafIndex, value, _siblingPath){
94
+ constructor(leafIndex, value, siblingPath){
219
95
  this.leafIndex = leafIndex;
220
96
  this.value = value;
221
- this._siblingPath = _siblingPath;
222
- this.siblingPath = new Vector(_siblingPath);
223
- }
224
- /**
225
- * Serializes the inputs to a buffer.
226
- * @returns - The inputs serialized to a buffer.
227
- */ toBuffer() {
228
- return serializeToBuffer(...AvmAppendTreeHint.getFields(this));
229
- }
230
- /**
231
- * Serializes the inputs to a hex string.
232
- * @returns The instance serialized to a hex string.
233
- */ toString() {
234
- return this.toBuffer().toString('hex');
235
- }
236
- /**
237
- * Is the struct empty?
238
- * @returns whether all members are empty.
239
- */ isEmpty() {
240
- return this.value.isZero() && this.siblingPath.items.length == 0;
241
- }
242
- /**
243
- * Creates a new instance from fields.
244
- * @param fields - Fields to create the instance from.
245
- * @returns A new AvmHint instance.
246
- */ static from(fields) {
247
- return new AvmAppendTreeHint(fields.leafIndex, fields.value, fields.siblingPath.items);
248
- }
249
- /**
250
- * Extracts fields from an instance.
251
- * @param fields - Fields to create the instance from.
252
- * @returns An array of fields.
253
- */ static getFields(fields) {
254
- return [
255
- fields.leafIndex,
256
- fields.value,
257
- fields.siblingPath
258
- ];
259
- }
260
- /**
261
- * Deserializes from a buffer or reader.
262
- * @param buffer - Buffer or reader to read from.
263
- * @returns The deserialized instance.
264
- */ static fromBuffer(buff) {
265
- return new AvmAppendTreeHint(Fr.fromBuffer(buff), Fr.fromBuffer(buff), BufferReader.asReader(buff).readVector(Fr));
97
+ this.siblingPath = siblingPath;
266
98
  }
267
- /**
268
- * Deserializes from a hex string.
269
- * @param str - Hex string to read from.
270
- * @returns The deserialized instance.
271
- */ static fromString(str) {
272
- return AvmAppendTreeHint.fromBuffer(Buffer.from(str, 'hex'));
99
+ static get schema() {
100
+ return z.object({
101
+ leafIndex: schemas.Fr,
102
+ value: schemas.Fr,
103
+ siblingPath: schemas.Fr.array()
104
+ }).transform(({ leafIndex, value, siblingPath })=>new AvmAppendTreeHint(leafIndex, value, siblingPath));
273
105
  }
274
106
  }
275
107
  export class AvmNullifierWriteTreeHint {
276
108
  lowLeafRead;
277
- _insertionPath;
278
109
  insertionPath;
279
- /*
280
- * @param bytecode the contract bytecode
281
- * @param contractInstance the contract instance of the nested call, used to derive the contract address
282
- * @param contractClassPreimage the contract class preimage of the nested call, used to derive the class id
283
- * */ constructor(lowLeafRead, _insertionPath){
110
+ constructor(lowLeafRead, insertionPath){
284
111
  this.lowLeafRead = lowLeafRead;
285
- this._insertionPath = _insertionPath;
286
- this.insertionPath = new Vector(_insertionPath);
112
+ this.insertionPath = insertionPath;
287
113
  }
288
- /**
289
- * Serializes the inputs to a buffer.
290
- * @returns - The inputs serialized to a buffer.
291
- */ toBuffer() {
292
- return serializeToBuffer(...AvmNullifierWriteTreeHint.getFields(this));
293
- }
294
- /**
295
- * Serializes the inputs to a hex string.
296
- * @returns The instance serialized to a hex string.
297
- */ toString() {
298
- return this.toBuffer().toString('hex');
299
- }
300
- /**
301
- * Is the struct empty?
302
- * @returns whether all members are empty.
303
- */ isEmpty() {
304
- return this.insertionPath.items.length == 0;
305
- }
306
- /**
307
- * Creates a new instance from fields.
308
- * @param fields - Fields to create the instance from.
309
- * @returns A new AvmHint instance.
310
- */ static from(fields) {
311
- return new AvmNullifierWriteTreeHint(fields.lowLeafRead, fields.insertionPath.items);
312
- }
313
- /**
314
- * Extracts fields from an instance.
315
- * @param fields - Fields to create the instance from.
316
- * @returns An array of fields.
317
- */ static getFields(fields) {
318
- return [
319
- ...AvmNullifierReadTreeHint.getFields(fields.lowLeafRead),
320
- fields.insertionPath
321
- ];
322
- }
323
- /**
324
- * Deserializes from a buffer or reader.
325
- * @param buffer - Buffer or reader to read from.
326
- * @returns The deserialized instance.
327
- */ static fromBuffer(buff) {
328
- const reader = BufferReader.asReader(buff);
329
- const lowLeafRead = AvmNullifierReadTreeHint.fromBuffer(reader);
330
- const insertionPath = reader.readVector(Fr);
331
- return new AvmNullifierWriteTreeHint(lowLeafRead, insertionPath);
332
- }
333
- /**
334
- * Deserializes from a hex string.
335
- * @param str - Hex string to read from.
336
- * @returns The deserialized instance.
337
- */ static fromString(str) {
338
- return AvmNullifierWriteTreeHint.fromBuffer(Buffer.from(str, 'hex'));
114
+ static get schema() {
115
+ return z.object({
116
+ lowLeafRead: AvmNullifierReadTreeHint.schema,
117
+ insertionPath: schemas.Fr.array()
118
+ }).transform(({ lowLeafRead, insertionPath })=>new AvmNullifierWriteTreeHint(lowLeafRead, insertionPath));
339
119
  }
340
120
  }
341
121
  export class AvmNullifierReadTreeHint {
342
122
  lowLeafPreimage;
343
123
  lowLeafIndex;
344
- _lowLeafSiblingPath;
345
124
  lowLeafSiblingPath;
346
- constructor(lowLeafPreimage, lowLeafIndex, _lowLeafSiblingPath){
125
+ constructor(lowLeafPreimage, lowLeafIndex, lowLeafSiblingPath){
347
126
  this.lowLeafPreimage = lowLeafPreimage;
348
127
  this.lowLeafIndex = lowLeafIndex;
349
- this._lowLeafSiblingPath = _lowLeafSiblingPath;
350
- this.lowLeafSiblingPath = new Vector(_lowLeafSiblingPath);
351
- }
352
- /**
353
- * Serializes the inputs to a buffer.
354
- * @returns - The inputs serialized to a buffer.
355
- */ toBuffer() {
356
- return serializeToBuffer(...AvmNullifierReadTreeHint.getFields(this));
357
- }
358
- /**
359
- * Serializes the inputs to a hex string.
360
- * @returns The instance serialized to a hex string.
361
- */ toString() {
362
- return this.toBuffer().toString('hex');
128
+ this.lowLeafSiblingPath = lowLeafSiblingPath;
363
129
  }
364
- /**
365
- * Is the struct empty?
366
- * @returns whether all members are empty.
367
- */ isEmpty() {
368
- return this.lowLeafSiblingPath.items.length == 0;
369
- }
370
- /**
371
- * Creates a new instance from fields.
372
- * @param fields - Fields to create the instance from.
373
- * @returns A new AvmHint instance.
374
- */ static from(fields) {
375
- return new AvmNullifierReadTreeHint(fields.lowLeafPreimage, fields.lowLeafIndex, fields.lowLeafSiblingPath.items);
376
- }
377
- static empty() {
378
- return new AvmNullifierReadTreeHint(NullifierLeafPreimage.empty(), Fr.ZERO, []);
379
- }
380
- /**
381
- * Extracts fields from an instance.
382
- * @param fields - Fields to create the instance from.
383
- * @returns An array of fields.
384
- */ static getFields(fields) {
385
- return [
386
- fields.lowLeafPreimage.nullifier,
387
- fields.lowLeafPreimage.nextNullifier,
388
- new Fr(fields.lowLeafPreimage.nextIndex),
389
- fields.lowLeafIndex,
390
- fields.lowLeafSiblingPath
391
- ];
392
- }
393
- /**
394
- * Deserializes from a buffer or reader.
395
- * @param buffer - Buffer or reader to read from.
396
- * @returns The deserialized instance.
397
- */ static fromBuffer(buff) {
398
- const reader = BufferReader.asReader(buff);
399
- const lowLeafPreimage = reader.readObject(NullifierLeafPreimage);
400
- const lowLeafIndex = Fr.fromBuffer(reader);
401
- const lowSiblingPath = reader.readVector(Fr);
402
- return new AvmNullifierReadTreeHint(lowLeafPreimage, lowLeafIndex, lowSiblingPath);
403
- }
404
- /**
405
- * Deserializes from a hex string.
406
- * @param str - Hex string to read from.
407
- * @returns The deserialized instance.
408
- */ static fromString(str) {
409
- return AvmNullifierReadTreeHint.fromBuffer(Buffer.from(str, 'hex'));
130
+ static get schema() {
131
+ return z.object({
132
+ lowLeafPreimage: NullifierLeafPreimage.schema,
133
+ lowLeafIndex: schemas.Fr,
134
+ lowLeafSiblingPath: schemas.Fr.array()
135
+ }).transform(({ lowLeafPreimage, lowLeafIndex, lowLeafSiblingPath })=>new AvmNullifierReadTreeHint(lowLeafPreimage, lowLeafIndex, lowLeafSiblingPath));
410
136
  }
411
137
  }
412
138
  export class AvmPublicDataReadTreeHint {
413
139
  leafPreimage;
414
140
  leafIndex;
415
- _siblingPath;
416
141
  siblingPath;
417
- constructor(leafPreimage, leafIndex, _siblingPath){
142
+ constructor(leafPreimage, leafIndex, siblingPath){
418
143
  this.leafPreimage = leafPreimage;
419
144
  this.leafIndex = leafIndex;
420
- this._siblingPath = _siblingPath;
421
- this.siblingPath = new Vector(_siblingPath);
422
- }
423
- /**
424
- * Serializes the inputs to a buffer.
425
- * @returns - The inputs serialized to a buffer.
426
- */ toBuffer() {
427
- return serializeToBuffer(...AvmPublicDataReadTreeHint.getFields(this));
428
- }
429
- /**
430
- * Serializes the inputs to a hex string.
431
- * @returns The instance serialized to a hex string.
432
- */ toString() {
433
- return this.toBuffer().toString('hex');
145
+ this.siblingPath = siblingPath;
434
146
  }
435
147
  static empty() {
436
148
  return new AvmPublicDataReadTreeHint(PublicDataTreeLeafPreimage.empty(), Fr.ZERO, []);
437
149
  }
438
- /**
439
- * Is the struct empty?
440
- * @returns whether all members are empty.
441
- */ isEmpty() {
442
- return this.siblingPath.items.length == 0;
443
- }
444
- /**
445
- * Creates a new instance from fields.
446
- * @param fields - Fields to create the instance from.
447
- * @returns A new AvmHint instance.
448
- */ static from(fields) {
449
- return new AvmPublicDataReadTreeHint(fields.leafPreimage, fields.leafIndex, fields.siblingPath.items);
450
- }
451
- /**
452
- * Extracts fields from an instance.
453
- * @param fields - Fields to create the instance from.
454
- * @returns An array of fields.
455
- */ static getFields(fields) {
456
- return [
457
- fields.leafPreimage.slot,
458
- fields.leafPreimage.value,
459
- new Fr(fields.leafPreimage.nextIndex),
460
- fields.leafPreimage.nextSlot,
461
- fields.leafIndex,
462
- fields.siblingPath
463
- ];
464
- }
465
- /**
466
- * Deserializes from a buffer or reader.
467
- * @param buffer - Buffer or reader to read from.
468
- * @returns The deserialized instance.
469
- */ static fromBuffer(buff) {
470
- const reader = BufferReader.asReader(buff);
471
- const lowLeafPreimage = reader.readObject(PublicDataTreeLeafPreimage);
472
- const lowLeafIndex = Fr.fromBuffer(reader);
473
- const lowSiblingPath = reader.readVector(Fr);
474
- return new AvmPublicDataReadTreeHint(lowLeafPreimage, lowLeafIndex, lowSiblingPath);
475
- }
476
- /**
477
- * Deserializes from a hex string.
478
- * @param str - Hex string to read from.
479
- * @returns The deserialized instance.
480
- */ static fromString(str) {
481
- return AvmPublicDataReadTreeHint.fromBuffer(Buffer.from(str, 'hex'));
150
+ static get schema() {
151
+ return z.object({
152
+ leafPreimage: PublicDataTreeLeafPreimage.schema,
153
+ leafIndex: schemas.Fr,
154
+ siblingPath: schemas.Fr.array()
155
+ }).transform(({ leafPreimage, leafIndex, siblingPath })=>new AvmPublicDataReadTreeHint(leafPreimage, leafIndex, siblingPath));
482
156
  }
483
157
  }
484
158
  export class AvmPublicDataWriteTreeHint {
485
159
  lowLeafRead;
486
160
  newLeafPreimage;
487
- _insertionPath;
488
161
  insertionPath;
489
162
  constructor(// To check the current slot has been written to
490
- lowLeafRead, newLeafPreimage, _insertionPath){
163
+ lowLeafRead, newLeafPreimage, insertionPath){
491
164
  this.lowLeafRead = lowLeafRead;
492
165
  this.newLeafPreimage = newLeafPreimage;
493
- this._insertionPath = _insertionPath;
494
- this.insertionPath = new Vector(_insertionPath);
495
- }
496
- /**
497
- * Serializes the inputs to a buffer.
498
- * @returns - The inputs serialized to a buffer.
499
- */ toBuffer() {
500
- return serializeToBuffer(...AvmPublicDataWriteTreeHint.getFields(this));
501
- }
502
- /**
503
- * Serializes the inputs to a hex string.
504
- * @returns The instance serialized to a hex string.
505
- */ toString() {
506
- return this.toBuffer().toString('hex');
507
- }
508
- /**
509
- * Is the struct empty?
510
- * @returns whether all members are empty.
511
- */ isEmpty() {
512
- return this.insertionPath.items.length == 0;
513
- }
514
- /**
515
- * Creates a new instance from fields.
516
- * @param fields - Fields to create the instance from.
517
- * @returns A new AvmHint instance.
518
- */ static from(fields) {
519
- return new AvmPublicDataWriteTreeHint(fields.lowLeafRead, fields.newLeafPreimage, fields.insertionPath.items);
166
+ this.insertionPath = insertionPath;
520
167
  }
521
- /**
522
- * Extracts fields from an instance.
523
- * @param fields - Fields to create the instance from.
524
- * @returns An array of fields.
525
- */ static getFields(fields) {
526
- return [
527
- ...AvmPublicDataReadTreeHint.getFields(fields.lowLeafRead),
528
- fields.newLeafPreimage.slot,
529
- fields.newLeafPreimage.value,
530
- new Fr(fields.newLeafPreimage.nextIndex),
531
- fields.newLeafPreimage.nextSlot,
532
- fields.insertionPath
533
- ];
534
- }
535
- /**
536
- * Deserializes from a buffer or reader.
537
- * @param buffer - Buffer or reader to read from.
538
- * @returns The deserialized instance.
539
- */ static fromBuffer(buff) {
540
- const reader = BufferReader.asReader(buff);
541
- const lowLeafPreimage = reader.readObject(AvmPublicDataReadTreeHint);
542
- const newLeafPreimage = reader.readObject(PublicDataTreeLeafPreimage);
543
- const lowSiblingPath = reader.readVector(Fr);
544
- return new AvmPublicDataWriteTreeHint(lowLeafPreimage, newLeafPreimage, lowSiblingPath);
545
- }
546
- /**
547
- * Deserializes from a hex string.
548
- * @param str - Hex string to read from.
549
- * @returns The deserialized instance.
550
- */ static fromString(str) {
551
- return AvmPublicDataWriteTreeHint.fromBuffer(Buffer.from(str, 'hex'));
168
+ static get schema() {
169
+ return z.object({
170
+ lowLeafRead: AvmPublicDataReadTreeHint.schema,
171
+ newLeafPreimage: PublicDataTreeLeafPreimage.schema,
172
+ insertionPath: schemas.Fr.array()
173
+ }).transform(({ lowLeafRead, newLeafPreimage, insertionPath })=>new AvmPublicDataWriteTreeHint(lowLeafRead, newLeafPreimage, insertionPath));
552
174
  }
553
175
  }
554
176
  export class AvmExecutionHints {
@@ -563,232 +185,65 @@ export class AvmExecutionHints {
563
185
  noteHashWrites;
564
186
  l1ToL2MessageReads;
565
187
  constructor(enqueuedCalls, contractInstances, contractClasses, publicDataReads, publicDataWrites, nullifierReads, nullifierWrites, noteHashReads, noteHashWrites, l1ToL2MessageReads){
566
- this.enqueuedCalls = new Vector(enqueuedCalls);
567
- this.contractInstances = new Vector(contractInstances);
568
- this.contractClasses = new Vector(contractClasses);
569
- this.publicDataReads = new Vector(publicDataReads);
570
- this.publicDataWrites = new Vector(publicDataWrites);
571
- this.nullifierReads = new Vector(nullifierReads);
572
- this.nullifierWrites = new Vector(nullifierWrites);
573
- this.noteHashReads = new Vector(noteHashReads);
574
- this.noteHashWrites = new Vector(noteHashWrites);
575
- this.l1ToL2MessageReads = new Vector(l1ToL2MessageReads);
188
+ this.enqueuedCalls = enqueuedCalls;
189
+ this.contractInstances = contractInstances;
190
+ this.contractClasses = contractClasses;
191
+ this.publicDataReads = publicDataReads;
192
+ this.publicDataWrites = publicDataWrites;
193
+ this.nullifierReads = nullifierReads;
194
+ this.nullifierWrites = nullifierWrites;
195
+ this.noteHashReads = noteHashReads;
196
+ this.noteHashWrites = noteHashWrites;
197
+ this.l1ToL2MessageReads = l1ToL2MessageReads;
576
198
  }
577
- /**
578
- * Return an empty instance.
579
- * @returns an empty instance.
580
- */ static empty() {
199
+ static empty() {
581
200
  return new AvmExecutionHints([], [], [], [], [], [], [], [], [], []);
582
201
  }
583
- /**
584
- * Serializes the inputs to a buffer.
585
- * @returns - The inputs serialized to a buffer.
586
- */ toBuffer() {
587
- return serializeToBuffer(...AvmExecutionHints.getFields(this));
588
- }
589
- /**
590
- * Serializes the inputs to a hex string.
591
- * @returns The instance serialized to a hex string.
592
- */ toString() {
593
- return bufferToHex(this.toBuffer());
594
- }
595
- /**
596
- * Is the struct empty?
597
- * @returns whether all members are empty.
598
- */ isEmpty() {
599
- return this.enqueuedCalls.items.length == 0 && this.contractInstances.items.length == 0 && this.contractClasses.items.length == 0 && this.publicDataReads.items.length == 0 && this.publicDataWrites.items.length == 0 && this.nullifierReads.items.length == 0 && this.nullifierWrites.items.length == 0 && this.noteHashReads.items.length == 0 && this.noteHashWrites.items.length == 0 && this.l1ToL2MessageReads.items.length == 0;
600
- }
601
- /**
602
- * Creates a new instance from fields.
603
- * @param fields - Fields to create the instance from.
604
- * @returns A new AvmExecutionHints instance.
605
- */ static from(fields) {
606
- return new AvmExecutionHints(fields.enqueuedCalls.items, fields.contractInstances.items, fields.contractClasses.items, fields.publicDataReads.items, fields.publicDataWrites.items, fields.nullifierReads.items, fields.nullifierWrites.items, fields.noteHashReads.items, fields.noteHashWrites.items, fields.l1ToL2MessageReads.items);
607
- }
608
- /**
609
- * Extracts fields from an instance.
610
- * @param fields - Fields to create the instance from.
611
- * @returns An array of fields.
612
- */ static getFields(fields) {
613
- return [
614
- fields.enqueuedCalls,
615
- fields.contractInstances,
616
- fields.contractClasses,
617
- fields.publicDataReads,
618
- fields.publicDataWrites,
619
- fields.nullifierReads,
620
- fields.nullifierWrites,
621
- fields.noteHashReads,
622
- fields.noteHashWrites,
623
- fields.l1ToL2MessageReads
624
- ];
625
- }
626
- /**
627
- * Deserializes from a buffer or reader.
628
- * @param buffer - Buffer or reader to read from.
629
- * @returns The deserialized instance.
630
- */ static fromBuffer(buff) {
631
- const reader = BufferReader.asReader(buff);
632
- return new AvmExecutionHints(reader.readVector(AvmEnqueuedCallHint), reader.readVector(AvmContractInstanceHint), reader.readVector(AvmContractClassHint), reader.readVector(AvmPublicDataReadTreeHint), reader.readVector(AvmPublicDataWriteTreeHint), reader.readVector(AvmNullifierReadTreeHint), reader.readVector(AvmNullifierWriteTreeHint), reader.readVector(AvmAppendTreeHint), reader.readVector(AvmAppendTreeHint), reader.readVector(AvmAppendTreeHint));
633
- }
634
- /**
635
- * Deserializes from a hex string.
636
- * @param str - Hex string to read from.
637
- * @returns The deserialized instance.
638
- */ static fromString(str) {
639
- return AvmCircuitInputs.fromBuffer(hexToBuffer(str));
202
+ static get schema() {
203
+ return z.object({
204
+ enqueuedCalls: AvmEnqueuedCallHint.schema.array(),
205
+ contractInstances: AvmContractInstanceHint.schema.array(),
206
+ contractClasses: AvmContractClassHint.schema.array(),
207
+ publicDataReads: AvmPublicDataReadTreeHint.schema.array(),
208
+ publicDataWrites: AvmPublicDataWriteTreeHint.schema.array(),
209
+ nullifierReads: AvmNullifierReadTreeHint.schema.array(),
210
+ nullifierWrites: AvmNullifierWriteTreeHint.schema.array(),
211
+ noteHashReads: AvmAppendTreeHint.schema.array(),
212
+ noteHashWrites: AvmAppendTreeHint.schema.array(),
213
+ l1ToL2MessageReads: AvmAppendTreeHint.schema.array()
214
+ }).transform(({ enqueuedCalls, contractInstances, contractClasses, publicDataReads, publicDataWrites, nullifierReads, nullifierWrites, noteHashReads, noteHashWrites, l1ToL2MessageReads })=>new AvmExecutionHints(enqueuedCalls, contractInstances, contractClasses, publicDataReads, publicDataWrites, nullifierReads, nullifierWrites, noteHashReads, noteHashWrites, l1ToL2MessageReads));
640
215
  }
641
216
  }
642
217
  export class AvmCircuitInputs {
643
218
  functionName;
644
219
  calldata;
645
- avmHints;
220
+ hints;
646
221
  publicInputs;
647
- constructor(functionName, calldata, avmHints, publicInputs){
222
+ constructor(functionName, calldata, hints, publicInputs){
648
223
  this.functionName = functionName;
649
224
  this.calldata = calldata;
650
- this.avmHints = avmHints;
225
+ this.hints = hints;
651
226
  this.publicInputs = publicInputs;
652
227
  }
653
- /**
654
- * Serializes the inputs to a buffer.
655
- * @returns - The inputs serialized to a buffer.
656
- */ toBuffer() {
657
- const functionNameBuffer = Buffer.from(this.functionName);
658
- return serializeToBuffer(functionNameBuffer.length, functionNameBuffer, this.calldata.length, this.calldata, this.avmHints.toBuffer(), this.publicInputs);
659
- }
660
- /**
661
- * Serializes the inputs to a hex string.
662
- * @returns The instance serialized to a hex string.
663
- */ toString() {
664
- return bufferToHex(this.toBuffer());
665
- }
666
228
  static empty() {
667
229
  return new AvmCircuitInputs('', [], AvmExecutionHints.empty(), AvmCircuitPublicInputs.empty());
668
230
  }
669
- /**
670
- * Creates a new instance from fields.
671
- * @param fields - Fields to create the instance from.
672
- * @returns A new AvmCircuitInputs instance.
673
- */ static from(fields) {
674
- return new AvmCircuitInputs(...AvmCircuitInputs.getFields(fields));
675
- }
676
- /**
677
- * Extracts fields from an instance.
678
- * @param fields - Fields to create the instance from.
679
- * @returns An array of fields.
680
- */ static getFields(fields) {
681
- return [
682
- fields.functionName,
683
- fields.calldata,
684
- fields.avmHints,
685
- fields.publicInputs
686
- ];
231
+ static get schema() {
232
+ return z.object({
233
+ functionName: z.string(),
234
+ calldata: schemas.Fr.array(),
235
+ hints: AvmExecutionHints.schema,
236
+ publicInputs: AvmCircuitPublicInputs.schema
237
+ }).transform(({ functionName, calldata, hints, publicInputs })=>new AvmCircuitInputs(functionName, calldata, hints, publicInputs));
687
238
  }
688
- /**
689
- * Deserializes from a buffer or reader.
690
- * @param buffer - Buffer or reader to read from.
691
- * @returns The deserialized instance.
692
- */ static fromBuffer(buff) {
693
- const reader = BufferReader.asReader(buff);
694
- return new AvmCircuitInputs(/*functionName=*/ reader.readBuffer().toString(), /*calldata=*/ reader.readVector(Fr), AvmExecutionHints.fromBuffer(reader), AvmCircuitPublicInputs.fromBuffer(reader));
239
+ serializeWithMessagePack() {
240
+ return serializeWithMessagePack(this);
695
241
  }
696
- /**
697
- * Deserializes from a hex string.
698
- * @param str - Hex string to read from.
699
- * @returns The deserialized instance.
700
- */ static fromString(str) {
701
- return AvmCircuitInputs.fromBuffer(hexToBuffer(str));
242
+ // These are used by the prover to generate an id, and also gcs_proof_store.ts.
243
+ toBuffer() {
244
+ return Buffer.from(jsonStringify(this));
702
245
  }
703
- /** Returns a buffer representation for JSON serialization. */ toJSON() {
704
- return this.toBuffer();
246
+ static fromBuffer(buf) {
247
+ return jsonParseWithSchema(buf.toString(), this.schema);
705
248
  }
706
- /** Creates an instance from a hex string. */ static get schema() {
707
- return bufferSchemaFor(AvmCircuitInputs);
708
- }
709
- /** Serializes in format for the Avm2 */ serializeForAvm2() {
710
- // logger(`original: ${inspect(input)}`);
711
- // logger.verbose(`original: ${inspect(this.avmHints.enqueuedCalls.items)}`);
712
- // Convert the inputs to something that works with vm2 and messagepack.
713
- // const inputSubset = {
714
- // ffs: [new Fr(0x123456789), new Fr(0x987654321)],
715
- // affine: new Point(new Fr(0x123456789), new Fr(0x987654321), false),
716
- // fq: new Fq(0x123456789),
717
- // addr: AztecAddress.fromBigInt(0x123456789n),
718
- // contract_instance_hints: this.avmHints.contractInstances,
719
- // };
720
- const hints = {
721
- contractInstances: [],
722
- contractClasses: [],
723
- initialTreeRoots: {
724
- publicDataTree: this.publicInputs.startTreeSnapshots.publicDataTree.root,
725
- nullifierTree: this.publicInputs.startTreeSnapshots.nullifierTree.root,
726
- noteHashTree: this.publicInputs.startTreeSnapshots.noteHashTree.root,
727
- l1ToL2MessageTree: this.publicInputs.startTreeSnapshots.l1ToL2MessageTree.root
728
- }
729
- };
730
- const inputs = {
731
- hints: hints,
732
- enqueuedCalls: [],
733
- // Placeholder for now.
734
- publicInputs: {
735
- dummy: []
736
- }
737
- };
738
- hints.contractInstances = this.avmHints.contractInstances.items;
739
- hints.contractClasses = this.avmHints.contractClasses.items;
740
- // TODO: for now I only convert app logic requests?
741
- for (const enqueuedCall of this.avmHints.enqueuedCalls.items){
742
- inputs.enqueuedCalls.push({
743
- contractAddress: enqueuedCall.contractAddress,
744
- sender: new Fr(0),
745
- args: enqueuedCall.calldata.items,
746
- isStatic: false
747
- });
748
- }
749
- const inputsBuffer = serializeWithMessagePack(inputs);
750
- return inputsBuffer;
751
- }
752
- }
753
- export function serializeWithMessagePack(obj) {
754
- setUpMessagePackExtensions();
755
- const encoder = new Encoder({
756
- // always encode JS objects as MessagePack maps
757
- // this makes it compatible with other MessagePack decoders
758
- useRecords: false,
759
- int64AsType: 'bigint'
760
- });
761
- return encoder.encode(obj);
762
- }
763
- function setUpMessagePackExtensions() {
764
- // C++ Fr and Fq classes work well with the buffer serialization.
765
- addExtension({
766
- Class: Fr,
767
- write: (fr)=>fr.toBuffer()
768
- });
769
- addExtension({
770
- Class: Fq,
771
- write: (fq)=>fq.toBuffer()
772
- });
773
- // AztecAddress is a class that has a field in TS, but just a field in C++.
774
- addExtension({
775
- Class: AztecAddress,
776
- write: (addr)=>addr.toField()
777
- });
778
- // If we find a vector, we just use the underlying list.
779
- addExtension({
780
- Class: Vector,
781
- write: (v)=>v.items
782
- });
783
- // Affine points are a mess, we do our best.
784
- addExtension({
785
- Class: Point,
786
- write: (p)=>{
787
- assert(!p.inf, 'Cannot serialize infinity');
788
- return {
789
- x: new Fq(p.x.toBigInt()),
790
- y: new Fq(p.y.toBigInt())
791
- };
792
- }
793
- });
794
249
  }