@aztec/foundation 0.34.0 → 0.35.1

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/abi/abi.d.ts +2 -2
  2. package/dest/abi/abi.d.ts.map +1 -1
  3. package/dest/abi/abi.js +11 -11
  4. package/dest/abi/event_selector.js +3 -3
  5. package/dest/abi/function_selector.js +3 -3
  6. package/dest/aztec-address/index.d.ts +4 -0
  7. package/dest/aztec-address/index.d.ts.map +1 -1
  8. package/dest/aztec-address/index.js +10 -1
  9. package/dest/crypto/index.d.ts +1 -0
  10. package/dest/crypto/index.d.ts.map +1 -1
  11. package/dest/crypto/index.js +2 -1
  12. package/dest/crypto/keccak/index.d.ts +1 -1
  13. package/dest/crypto/keccak/index.d.ts.map +1 -1
  14. package/dest/crypto/keccak/index.js +2 -2
  15. package/dest/crypto/pedersen/pedersen.wasm.d.ts +5 -3
  16. package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
  17. package/dest/crypto/pedersen/pedersen.wasm.js +8 -9
  18. package/dest/crypto/poseidon/index.d.ts +10 -3
  19. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  20. package/dest/crypto/poseidon/index.js +21 -4
  21. package/dest/crypto/sha512/index.d.ts +10 -0
  22. package/dest/crypto/sha512/index.d.ts.map +1 -0
  23. package/dest/crypto/sha512/index.js +14 -0
  24. package/dest/eth-address/index.d.ts +4 -0
  25. package/dest/eth-address/index.d.ts.map +1 -1
  26. package/dest/eth-address/index.js +10 -1
  27. package/dest/fields/fields.d.ts +10 -1
  28. package/dest/fields/fields.d.ts.map +1 -1
  29. package/dest/fields/fields.js +19 -1
  30. package/dest/log/logger.js +2 -2
  31. package/dest/noir/noir_package_config.d.ts +2 -2
  32. package/dest/serialize/index.d.ts +1 -0
  33. package/dest/serialize/index.d.ts.map +1 -1
  34. package/dest/serialize/index.js +2 -1
  35. package/dest/serialize/serialize.d.ts +9 -3
  36. package/dest/serialize/serialize.d.ts.map +1 -1
  37. package/dest/serialize/serialize.js +16 -7
  38. package/dest/serialize/type_registry.d.ts +23 -0
  39. package/dest/serialize/type_registry.d.ts.map +1 -0
  40. package/dest/serialize/type_registry.js +38 -0
  41. package/package.json +2 -2
  42. package/src/abi/abi.ts +10 -10
  43. package/src/abi/event_selector.ts +2 -2
  44. package/src/abi/function_selector.ts +2 -2
  45. package/src/aztec-address/index.ts +11 -0
  46. package/src/crypto/index.ts +1 -0
  47. package/src/crypto/keccak/index.ts +1 -1
  48. package/src/crypto/pedersen/pedersen.wasm.ts +7 -9
  49. package/src/crypto/poseidon/index.ts +25 -3
  50. package/src/crypto/sha512/index.ts +16 -0
  51. package/src/eth-address/index.ts +11 -0
  52. package/src/fields/fields.ts +23 -1
  53. package/src/log/logger.ts +1 -1
  54. package/src/serialize/index.ts +1 -0
  55. package/src/serialize/serialize.ts +23 -9
  56. package/src/serialize/type_registry.ts +43 -0
@@ -3,6 +3,7 @@ import { inspect } from 'util';
3
3
  import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
4
4
  import { randomBytes } from '../crypto/random/index.js';
5
5
  import { BufferReader } from '../serialize/buffer_reader.js';
6
+ import { TypeRegistry } from '../serialize/type_registry.js';
6
7
 
7
8
  const ZERO_BUFFER = Buffer.alloc(32);
8
9
 
@@ -187,6 +188,7 @@ export interface Fr {
187
188
  */
188
189
  export class Fr extends BaseField {
189
190
  static ZERO = new Fr(0n);
191
+ static ONE = new Fr(1n);
190
192
  static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
191
193
 
192
194
  constructor(value: number | bigint | boolean | Fr | Buffer) {
@@ -257,8 +259,18 @@ export class Fr extends BaseField {
257
259
 
258
260
  return new Fr(this.toBigInt() / rhs.toBigInt());
259
261
  }
262
+
263
+ toJSON() {
264
+ return {
265
+ type: 'Fr',
266
+ value: this.toString(),
267
+ };
268
+ }
260
269
  }
261
270
 
271
+ // For deserializing JSON.
272
+ TypeRegistry.register('Fr', Fr);
273
+
262
274
  /**
263
275
  * Branding to ensure fields are not interchangeable types.
264
276
  */
@@ -319,8 +331,18 @@ export class Fq extends BaseField {
319
331
  static fromHighLow(high: Fr, low: Fr): Fq {
320
332
  return new Fq((high.toBigInt() << Fq.HIGH_SHIFT) + low.toBigInt());
321
333
  }
334
+
335
+ toJSON() {
336
+ return {
337
+ type: 'Fq',
338
+ value: this.toString(),
339
+ };
340
+ }
322
341
  }
323
342
 
343
+ // For deserializing JSON.
344
+ TypeRegistry.register('Fq', Fq);
345
+
324
346
  // Beware: Performance bottleneck below
325
347
 
326
348
  /**
@@ -351,7 +373,7 @@ function extendedEuclidean(a: bigint, modulus: bigint): [bigint, bigint, bigint]
351
373
  /**
352
374
  * GrumpkinScalar is an Fq.
353
375
  * @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
354
- * the Aztec Yellow Paper.
376
+ * the Aztec Protocol Specs.
355
377
  */
356
378
  export type GrumpkinScalar = Fq;
357
379
  export const GrumpkinScalar = Fq;
package/src/log/logger.ts CHANGED
@@ -97,7 +97,7 @@ function logWithDebug(debug: debug.Debugger, level: LogLevel, msg: string, data?
97
97
  function getPrefix(debugLogger: debug.Debugger, level: LogLevel) {
98
98
  const levelLabel = currentLevel !== level ? ` ${level.toUpperCase()}` : '';
99
99
  const prefix = `${debugLogger.namespace.replace(/^aztec:/, '')}${levelLabel}`;
100
- if (!isNode || !isatty(process.stderr.fd)) {
100
+ if ((!isNode || !isatty(process.stderr.fd)) && !process.env.DEBUG_COLORS) {
101
101
  return prefix;
102
102
  }
103
103
  const colorIndex = debug.selectColor(debugLogger.namespace) as number;
@@ -3,3 +3,4 @@ export * from './buffer_reader.js';
3
3
  export * from './field_reader.js';
4
4
  export * from './types.js';
5
5
  export * from './serialize.js';
6
+ export * from './type_registry.js';
@@ -121,11 +121,19 @@ export type Bufferable =
121
121
  | Bufferable[];
122
122
 
123
123
  /** A type that can be converted to a Field or a Field array. */
124
- export type Fieldeable =
124
+ export type Fieldable =
125
125
  | Fr
126
126
  | boolean
127
127
  | number
128
128
  | bigint
129
+ | Buffer
130
+ | {
131
+ /**
132
+ * Serialize to a field.
133
+ * @dev Duplicate to `toField` but left as is as it is used in AVM codebase.
134
+ */
135
+ toFr: () => Fr;
136
+ }
129
137
  | {
130
138
  /** Serialize to a field. */
131
139
  toField: () => Fr;
@@ -134,7 +142,7 @@ export type Fieldeable =
134
142
  /** Serialize to an array of fields. */
135
143
  toFields: () => Fr[];
136
144
  }
137
- | Fieldeable[];
145
+ | Fieldable[];
138
146
 
139
147
  /**
140
148
  * Serializes a list of objects contiguously.
@@ -142,10 +150,10 @@ export type Fieldeable =
142
150
  * @returns A buffer list with the concatenation of all fields.
143
151
  */
144
152
  export function serializeToBufferArray(...objs: Bufferable[]): Buffer[] {
145
- let ret: Buffer[] = [];
153
+ const ret: Buffer[] = [];
146
154
  for (const obj of objs) {
147
155
  if (Array.isArray(obj)) {
148
- ret = [...ret, ...serializeToBufferArray(...obj)];
156
+ ret.push(...serializeToBufferArray(...obj));
149
157
  } else if (Buffer.isBuffer(obj)) {
150
158
  ret.push(obj);
151
159
  } else if (typeof obj === 'boolean') {
@@ -176,19 +184,25 @@ export function serializeToBufferArray(...objs: Bufferable[]): Buffer[] {
176
184
  * @param objs - Objects to serialize.
177
185
  * @returns An array of fields with the concatenation of all fields.
178
186
  */
179
- export function serializeToFields(...objs: Fieldeable[]): Fr[] {
180
- let ret: Fr[] = [];
187
+ export function serializeToFields(...objs: Fieldable[]): Fr[] {
188
+ const ret: Fr[] = [];
181
189
  for (const obj of objs) {
182
190
  if (Array.isArray(obj)) {
183
- ret = [...ret, ...serializeToFields(...obj)];
191
+ ret.push(...serializeToFields(...obj));
184
192
  } else if (obj instanceof Fr) {
185
193
  ret.push(obj);
186
194
  } else if (typeof obj === 'boolean' || typeof obj === 'number' || typeof obj === 'bigint') {
187
195
  ret.push(new Fr(obj));
188
196
  } else if ('toFields' in obj) {
189
- ret = [...ret, ...obj.toFields()];
190
- } else {
197
+ ret.push(...obj.toFields());
198
+ } else if ('toFr' in obj) {
199
+ ret.push(obj.toFr());
200
+ } else if ('toField' in obj) {
191
201
  ret.push(obj.toField());
202
+ } else if (Buffer.isBuffer(obj)) {
203
+ ret.push(Fr.fromBuffer(obj));
204
+ } else {
205
+ throw new Error(`Cannot serialize input to field: ${typeof obj} ${(obj as any).constructor?.name}`);
192
206
  }
193
207
  }
194
208
  return ret;
@@ -0,0 +1,43 @@
1
+ type Deserializable = { fromString(str: string): object };
2
+
3
+ /**
4
+ * Register a class here that has a toJSON method that returns:
5
+ * ```
6
+ * {
7
+ * "type": "ExampleClassName",
8
+ * "value": <result of ExampleClassName.toString()>
9
+ * }
10
+ * ```
11
+ * and has an e.g. ExampleClassName.fromString(string) method.
12
+ * This means you can then easily serialize/deserialize the type using JSON.stringify and JSON.parse.
13
+ */
14
+ export class TypeRegistry {
15
+ private static registry: Map<string, Deserializable> = new Map();
16
+
17
+ public static register(typeName: string, constructor: Deserializable): void {
18
+ this.registry.set(typeName, constructor);
19
+ }
20
+
21
+ public static getConstructor(typeName: string): Deserializable | undefined {
22
+ return this.registry.get(typeName);
23
+ }
24
+ }
25
+
26
+ // Resolver function that enables JSON serialization of BigInts.
27
+ export function resolver(_: any, value: any) {
28
+ return typeof value === 'bigint' ? value.toString() + 'n' : value;
29
+ }
30
+
31
+ // Reviver function that uses TypeRegistry to instantiate objects.
32
+ export function reviver(key: string, value: any) {
33
+ if (typeof value === 'string' && /^\d+n$/.test(value)) {
34
+ return BigInt(value.slice(0, -1));
35
+ }
36
+ if (value && typeof value === 'object' && 'type' in value && 'value' in value) {
37
+ const Constructor = TypeRegistry.getConstructor(value.type);
38
+ if (Constructor) {
39
+ return Constructor.fromString(value.value);
40
+ }
41
+ }
42
+ return value;
43
+ }