@aztec/foundation 0.0.1-commit.f650c0a5c → 0.0.1-commit.f7ea82942
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/branded-types/buffer32_hash.d.ts +24 -0
- package/dest/branded-types/buffer32_hash.d.ts.map +1 -0
- package/dest/branded-types/buffer32_hash.js +19 -0
- package/dest/branded-types/checkpoint_number.d.ts +4 -1
- package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
- package/dest/branded-types/checkpoint_number.js +3 -0
- package/dest/branded-types/index.d.ts +2 -1
- package/dest/branded-types/index.d.ts.map +1 -1
- package/dest/branded-types/index.js +1 -0
- package/dest/buffer/buffer16.d.ts +3 -3
- package/dest/buffer/buffer16.d.ts.map +1 -1
- package/dest/buffer/buffer32.d.ts +28 -63
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/buffer/buffer32.js +26 -60
- package/dest/config/env_var.d.ts +2 -2
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/config/index.d.ts +48 -30
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +21 -30
- package/dest/curves/bls12/field.d.ts +1 -1
- package/dest/curves/bls12/field.d.ts.map +1 -1
- package/dest/curves/bls12/field.js +0 -5
- package/dest/curves/bn254/field.d.ts +17 -16
- package/dest/curves/bn254/field.d.ts.map +1 -1
- package/dest/curves/bn254/field.js +18 -13
- package/dest/curves/grumpkin/point.d.ts +3 -3
- package/dest/curves/grumpkin/point.d.ts.map +1 -1
- package/dest/curves/grumpkin/point.js +6 -3
- package/dest/eth-address/index.d.ts +1 -1
- package/dest/eth-address/index.d.ts.map +1 -1
- package/dest/eth-address/index.js +0 -3
- package/dest/trees/balanced_merkle_tree_root.d.ts +2 -3
- package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -1
- package/dest/trees/balanced_merkle_tree_root.js +2 -3
- package/dest/trees/hasher.d.ts +3 -2
- package/dest/trees/hasher.d.ts.map +1 -1
- package/dest/trees/hasher.js +5 -5
- package/dest/trees/merkle_tree_calculator.d.ts +4 -2
- package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/merkle_tree_calculator.js +1 -5
- package/dest/trees/sibling_path.d.ts +2 -2
- package/dest/trees/sibling_path.d.ts.map +1 -1
- package/dest/trees/sibling_path.js +1 -5
- package/dest/trees/unbalanced_merkle_tree_root.d.ts +2 -3
- package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_root.js +2 -3
- package/package.json +2 -2
- package/src/branded-types/buffer32_hash.ts +42 -0
- package/src/branded-types/checkpoint_number.ts +5 -0
- package/src/branded-types/index.ts +1 -0
- package/src/buffer/buffer16.ts +2 -2
- package/src/buffer/buffer32.ts +36 -68
- package/src/config/env_var.ts +3 -1
- package/src/config/index.ts +96 -84
- package/src/curves/bls12/field.ts +0 -7
- package/src/curves/bn254/field.ts +25 -31
- package/src/curves/grumpkin/point.ts +5 -2
- package/src/eth-address/index.ts +0 -4
- package/src/trees/balanced_merkle_tree_root.ts +2 -5
- package/src/trees/hasher.ts +6 -3
- package/src/trees/merkle_tree_calculator.ts +4 -10
- package/src/trees/sibling_path.ts +1 -2
- package/src/trees/unbalanced_merkle_tree_root.ts +2 -5
package/src/config/index.ts
CHANGED
|
@@ -10,14 +10,15 @@ export type { EnvVar, NetworkNames };
|
|
|
10
10
|
export type { NetworkConfig, NetworkConfigMap } from './network_config.js';
|
|
11
11
|
export { NetworkConfigMapSchema, NetworkConfigSchema } from './network_config.js';
|
|
12
12
|
|
|
13
|
-
export interface ConfigMapping {
|
|
13
|
+
export interface ConfigMapping<T> {
|
|
14
14
|
env?: EnvVar;
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
/** Parse an env-var string into `T`. Throws on invalid input. */
|
|
16
|
+
parseEnv?: (val: string) => T;
|
|
17
|
+
defaultValue?: T;
|
|
17
18
|
printDefault?: (val: any) => string;
|
|
18
19
|
description: string;
|
|
19
20
|
isBoolean?: boolean;
|
|
20
|
-
nested?: Record<string, ConfigMapping
|
|
21
|
+
nested?: Record<string, ConfigMapping<any>>;
|
|
21
22
|
fallback?: EnvVar[];
|
|
22
23
|
/**
|
|
23
24
|
* List of deprecated env vars that are still supported but will log a warning.
|
|
@@ -30,7 +31,9 @@ export function isBooleanConfigValue<T>(obj: T, key: keyof T): boolean {
|
|
|
30
31
|
return typeof obj[key] === 'boolean';
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
export type ConfigMappingsType<T> =
|
|
34
|
+
export type ConfigMappingsType<T> = {
|
|
35
|
+
[K in keyof T]-?: ConfigMapping<Required<T>[K]>;
|
|
36
|
+
};
|
|
34
37
|
|
|
35
38
|
/**
|
|
36
39
|
* Shared utility function to get a value from environment variables with fallback support.
|
|
@@ -66,8 +69,8 @@ export function getValueFromEnvWithFallback<T>(
|
|
|
66
69
|
}
|
|
67
70
|
}
|
|
68
71
|
|
|
69
|
-
// Parse the value if needed
|
|
70
|
-
if (value !== undefined) {
|
|
72
|
+
// Parse the value if needed. Empty strings are treated as "not set".
|
|
73
|
+
if (value !== undefined && value !== '') {
|
|
71
74
|
return parseFunc ? parseFunc(value) : (value as unknown as T);
|
|
72
75
|
}
|
|
73
76
|
|
|
@@ -84,7 +87,11 @@ export function getConfigFromMappings<T>(configMappings: ConfigMappingsType<T>):
|
|
|
84
87
|
(config as any)[key] = getConfigFromMappings(nested);
|
|
85
88
|
} else {
|
|
86
89
|
// Use the shared utility function
|
|
87
|
-
|
|
90
|
+
try {
|
|
91
|
+
(config as any)[key] = getValueFromEnvWithFallback(env, parseEnv, defaultValue, fallback);
|
|
92
|
+
} catch (e: any) {
|
|
93
|
+
throw new Error(`Failed to parse config '${key}' (env: ${env ?? 'none'}): ${e.message}`);
|
|
94
|
+
}
|
|
88
95
|
|
|
89
96
|
// Check for deprecated env vars and warn if logger is set
|
|
90
97
|
if (deprecatedFallback?.length) {
|
|
@@ -123,7 +130,7 @@ export function omitConfigMappings<T, K extends keyof T>(
|
|
|
123
130
|
* @param defaultVal - The default numerical value to use if the environment variable is not set or is invalid
|
|
124
131
|
* @returns Object with parseEnv and default values for a numerical config value
|
|
125
132
|
*/
|
|
126
|
-
export function numberConfigHelper(defaultVal: number): Pick<ConfigMapping
|
|
133
|
+
export function numberConfigHelper(defaultVal: number): Pick<ConfigMapping<number>, 'parseEnv' | 'defaultValue'> {
|
|
127
134
|
return {
|
|
128
135
|
parseEnv: (val: string) => safeParseNumber(val, defaultVal),
|
|
129
136
|
defaultValue: defaultVal,
|
|
@@ -138,7 +145,7 @@ export function numberConfigHelper(defaultVal: number): Pick<ConfigMapping, 'par
|
|
|
138
145
|
export function floatConfigHelper(
|
|
139
146
|
defaultVal: number,
|
|
140
147
|
validationFn?: (val: number) => void,
|
|
141
|
-
): Pick<ConfigMapping
|
|
148
|
+
): Pick<ConfigMapping<number>, 'parseEnv' | 'defaultValue'> {
|
|
142
149
|
return {
|
|
143
150
|
parseEnv: (val: string): number => {
|
|
144
151
|
const parsed = safeParseFloat(val, defaultVal);
|
|
@@ -152,7 +159,7 @@ export function floatConfigHelper(
|
|
|
152
159
|
/**
|
|
153
160
|
* Parses an environment variable to a 0-1 percentage value
|
|
154
161
|
*/
|
|
155
|
-
export function percentageConfigHelper(defaultVal: number): Pick<ConfigMapping
|
|
162
|
+
export function percentageConfigHelper(defaultVal: number): Pick<ConfigMapping<number>, 'parseEnv' | 'defaultValue'> {
|
|
156
163
|
return {
|
|
157
164
|
parseEnv: (val: string): number => {
|
|
158
165
|
const parsed = safeParseFloat(val, defaultVal);
|
|
@@ -171,12 +178,13 @@ export function percentageConfigHelper(defaultVal: number): Pick<ConfigMapping,
|
|
|
171
178
|
* @param defaultVal - The default numerical value to use if the environment variable is not set or is invalid
|
|
172
179
|
* @returns Object with parseEnv and default values for a numerical config value
|
|
173
180
|
*/
|
|
174
|
-
export function bigintConfigHelper(defaultVal
|
|
181
|
+
export function bigintConfigHelper(defaultVal: bigint): Pick<ConfigMapping<bigint>, 'parseEnv' | 'defaultValue'>;
|
|
182
|
+
export function bigintConfigHelper(): Pick<ConfigMapping<bigint | undefined>, 'parseEnv' | 'defaultValue'>;
|
|
183
|
+
export function bigintConfigHelper(
|
|
184
|
+
defaultVal?: bigint,
|
|
185
|
+
): Pick<ConfigMapping<bigint | undefined>, 'parseEnv' | 'defaultValue'> {
|
|
175
186
|
return {
|
|
176
187
|
parseEnv: (val: string) => {
|
|
177
|
-
if (val === '') {
|
|
178
|
-
return defaultVal;
|
|
179
|
-
}
|
|
180
188
|
// Handle scientific notation (e.g. "1e+23", "2E23") which BigInt() doesn't accept directly.
|
|
181
189
|
// We parse it losslessly using bigint arithmetic instead of going through float64.
|
|
182
190
|
if (/[eE]/.test(val)) {
|
|
@@ -200,32 +208,37 @@ export function bigintConfigHelper(defaultVal?: bigint): Pick<ConfigMapping, 'pa
|
|
|
200
208
|
|
|
201
209
|
/**
|
|
202
210
|
* Generates parseEnv for an optional numerical config value.
|
|
211
|
+
* Empty strings are already handled by getValueFromEnvWithFallback.
|
|
203
212
|
*/
|
|
204
|
-
export function optionalNumberConfigHelper(): Pick<ConfigMapping
|
|
213
|
+
export function optionalNumberConfigHelper(): Pick<ConfigMapping<number>, 'parseEnv'> {
|
|
205
214
|
return {
|
|
206
|
-
parseEnv: (val: string
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
215
|
+
parseEnv: (val: string) => {
|
|
216
|
+
const parsedValue = parseInt(val);
|
|
217
|
+
if (!Number.isSafeInteger(parsedValue)) {
|
|
218
|
+
throw new Error(`Invalid number: ${val}`);
|
|
210
219
|
}
|
|
211
|
-
return
|
|
220
|
+
return parsedValue;
|
|
212
221
|
},
|
|
213
222
|
};
|
|
214
223
|
}
|
|
215
224
|
|
|
216
225
|
/** Generates parseEnv for an enum-like config value. */
|
|
226
|
+
export function enumConfigHelper<T extends string>(
|
|
227
|
+
values: T[],
|
|
228
|
+
defaultValue: NoInfer<T>,
|
|
229
|
+
): Pick<ConfigMapping<T>, 'parseEnv' | 'defaultValue'>;
|
|
230
|
+
export function enumConfigHelper<T extends string>(
|
|
231
|
+
values: T[],
|
|
232
|
+
): Pick<ConfigMapping<T | undefined>, 'parseEnv' | 'defaultValue'>;
|
|
217
233
|
export function enumConfigHelper<T extends string>(
|
|
218
234
|
values: T[],
|
|
219
235
|
defaultValue?: NoInfer<T>,
|
|
220
|
-
): Pick<ConfigMapping
|
|
236
|
+
): Pick<ConfigMapping<T | undefined>, 'parseEnv' | 'defaultValue'> {
|
|
221
237
|
return {
|
|
222
238
|
parseEnv: (val: string) => {
|
|
223
|
-
const sanitizedVal =
|
|
224
|
-
if (values.
|
|
225
|
-
return
|
|
226
|
-
}
|
|
227
|
-
if (!val && defaultValue) {
|
|
228
|
-
return defaultValue;
|
|
239
|
+
const sanitizedVal = val.trim().toLowerCase();
|
|
240
|
+
if (values.some(v => v.toLowerCase() === sanitizedVal)) {
|
|
241
|
+
return values.find(v => v.toLowerCase() === sanitizedVal)!;
|
|
229
242
|
}
|
|
230
243
|
throw new Error(`Invalid config value '${val}' (must be one of ${values.join(', ')})`);
|
|
231
244
|
},
|
|
@@ -240,7 +253,9 @@ export function enumConfigHelper<T extends string>(
|
|
|
240
253
|
*/
|
|
241
254
|
export function booleanConfigHelper(
|
|
242
255
|
defaultVal = false,
|
|
243
|
-
): Required<
|
|
256
|
+
): Required<
|
|
257
|
+
Pick<ConfigMapping<boolean>, 'parseEnv' | 'defaultValue' | 'isBoolean'> & { parseVal: (val: string) => boolean }
|
|
258
|
+
> {
|
|
244
259
|
const parse = (val: string | boolean) => (typeof val === 'boolean' ? val : parseBooleanEnv(val));
|
|
245
260
|
return {
|
|
246
261
|
parseEnv: parse,
|
|
@@ -250,91 +265,88 @@ export function booleanConfigHelper(
|
|
|
250
265
|
};
|
|
251
266
|
}
|
|
252
267
|
|
|
253
|
-
export function secretValueConfigHelper<T>(parse: (val: string | undefined) => T):
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
268
|
+
export function secretValueConfigHelper<T>(parse: (val: string | undefined) => T): Pick<
|
|
269
|
+
ConfigMapping<SecretValue<T>>,
|
|
270
|
+
'parseEnv' | 'defaultValue'
|
|
271
|
+
> & {
|
|
272
|
+
parseVal: (val: string) => SecretValue<T>;
|
|
273
|
+
} {
|
|
258
274
|
const wrap = (val: string) => new SecretValue(parse(val));
|
|
259
275
|
return {
|
|
260
276
|
parseEnv: wrap,
|
|
261
277
|
parseVal: wrap,
|
|
262
278
|
defaultValue: new SecretValue(parse(undefined)),
|
|
263
|
-
isBoolean: true,
|
|
264
279
|
};
|
|
265
280
|
}
|
|
266
281
|
|
|
267
282
|
export { parseBooleanEnv } from './parse-env.js';
|
|
268
283
|
|
|
269
|
-
export function secretStringConfigHelper():
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
export function secretStringConfigHelper(defaultValue: string):
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
export function secretStringConfigHelper(defaultValue?: string):
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
+
export function secretStringConfigHelper(): {
|
|
285
|
+
parseEnv: (val: string) => SecretValue<string>;
|
|
286
|
+
parseVal: (val: string) => SecretValue<string>;
|
|
287
|
+
defaultValue: undefined;
|
|
288
|
+
};
|
|
289
|
+
export function secretStringConfigHelper(defaultValue: string): {
|
|
290
|
+
parseEnv: (val: string) => SecretValue<string>;
|
|
291
|
+
parseVal: (val: string) => SecretValue<string>;
|
|
292
|
+
defaultValue: SecretValue<string>;
|
|
293
|
+
};
|
|
294
|
+
export function secretStringConfigHelper(defaultValue?: string): {
|
|
295
|
+
parseEnv: (val: string) => SecretValue<string>;
|
|
296
|
+
parseVal: (val: string) => SecretValue<string>;
|
|
297
|
+
defaultValue: SecretValue<string> | undefined;
|
|
298
|
+
} {
|
|
284
299
|
const parse = (val: string) => new SecretValue(val);
|
|
285
300
|
return {
|
|
286
301
|
parseEnv: parse,
|
|
287
302
|
parseVal: parse,
|
|
288
303
|
defaultValue: defaultValue !== undefined ? new SecretValue(defaultValue) : undefined,
|
|
289
|
-
isBoolean: true,
|
|
290
304
|
};
|
|
291
305
|
}
|
|
292
306
|
|
|
293
|
-
export function secretFrConfigHelper():
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
export function secretFrConfigHelper(defaultValue: Fr):
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
export function secretFrConfigHelper(defaultValue?: Fr):
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
307
|
+
export function secretFrConfigHelper(): {
|
|
308
|
+
parseEnv: (val: string) => SecretValue<Fr>;
|
|
309
|
+
parseVal: (val: string) => SecretValue<Fr>;
|
|
310
|
+
defaultValue: undefined;
|
|
311
|
+
};
|
|
312
|
+
export function secretFrConfigHelper(defaultValue: Fr): {
|
|
313
|
+
parseEnv: (val: string) => SecretValue<Fr>;
|
|
314
|
+
parseVal: (val: string) => SecretValue<Fr>;
|
|
315
|
+
defaultValue: SecretValue<Fr>;
|
|
316
|
+
};
|
|
317
|
+
export function secretFrConfigHelper(defaultValue?: Fr): {
|
|
318
|
+
parseEnv: (val: string) => SecretValue<Fr>;
|
|
319
|
+
parseVal: (val: string) => SecretValue<Fr>;
|
|
320
|
+
defaultValue: SecretValue<Fr> | undefined;
|
|
321
|
+
} {
|
|
308
322
|
const parse = (val: string) => new SecretValue(Fr.fromHexString(val));
|
|
309
323
|
return {
|
|
310
324
|
parseEnv: parse,
|
|
311
325
|
parseVal: parse,
|
|
312
326
|
defaultValue: defaultValue !== undefined ? new SecretValue(defaultValue) : undefined,
|
|
313
|
-
isBoolean: true,
|
|
314
327
|
};
|
|
315
328
|
}
|
|
316
329
|
|
|
317
|
-
export function secretFqConfigHelper(
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
export function secretFqConfigHelper():
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
export function secretFqConfigHelper(defaultValue?: Fq):
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
330
|
+
export function secretFqConfigHelper(): {
|
|
331
|
+
parseEnv: (val: string) => SecretValue<Fq>;
|
|
332
|
+
parseVal: (val: string) => SecretValue<Fq>;
|
|
333
|
+
defaultValue: undefined;
|
|
334
|
+
};
|
|
335
|
+
export function secretFqConfigHelper(defaultValue: Fq): {
|
|
336
|
+
parseEnv: (val: string) => SecretValue<Fq>;
|
|
337
|
+
parseVal: (val: string) => SecretValue<Fq>;
|
|
338
|
+
defaultValue: SecretValue<Fq>;
|
|
339
|
+
};
|
|
340
|
+
export function secretFqConfigHelper(defaultValue?: Fq): {
|
|
341
|
+
parseEnv: (val: string) => SecretValue<Fq>;
|
|
342
|
+
parseVal: (val: string) => SecretValue<Fq>;
|
|
343
|
+
defaultValue: SecretValue<Fq> | undefined;
|
|
344
|
+
} {
|
|
332
345
|
const parse = (val: string) => new SecretValue(Fq.fromHexString(val));
|
|
333
346
|
return {
|
|
334
347
|
parseEnv: parse,
|
|
335
348
|
parseVal: parse,
|
|
336
|
-
defaultValue:
|
|
337
|
-
isBoolean: true,
|
|
349
|
+
defaultValue: defaultValue !== undefined ? new SecretValue(defaultValue) : undefined,
|
|
338
350
|
};
|
|
339
351
|
}
|
|
340
352
|
|
|
@@ -6,7 +6,6 @@ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
|
|
|
6
6
|
import { randomBytes } from '../../crypto/random/index.js';
|
|
7
7
|
import { hexSchemaFor } from '../../schemas/utils.js';
|
|
8
8
|
import { BufferReader } from '../../serialize/buffer_reader.js';
|
|
9
|
-
import { TypeRegistry } from '../../serialize/type_registry.js';
|
|
10
9
|
import { Fr } from '../bn254/field.js';
|
|
11
10
|
|
|
12
11
|
/**
|
|
@@ -323,9 +322,6 @@ export class BLS12Fr extends BLS12Field {
|
|
|
323
322
|
}
|
|
324
323
|
}
|
|
325
324
|
|
|
326
|
-
// For deserializing JSON.
|
|
327
|
-
TypeRegistry.register('BLS12Fr', BLS12Fr);
|
|
328
|
-
|
|
329
325
|
/**
|
|
330
326
|
* Fq field class.
|
|
331
327
|
* @dev This class is used to represent elements of BLS12-381 base field.
|
|
@@ -458,6 +454,3 @@ export class BLS12Fq extends BLS12Field {
|
|
|
458
454
|
return hexSchemaFor(BLS12Fq);
|
|
459
455
|
}
|
|
460
456
|
}
|
|
461
|
-
|
|
462
|
-
// For deserializing JSON.
|
|
463
|
-
TypeRegistry.register('BLS12Fq', BLS12Fq);
|
|
@@ -6,9 +6,6 @@ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
|
|
|
6
6
|
import { randomBytes } from '../../crypto/random/index.js';
|
|
7
7
|
import { hexSchemaFor } from '../../schemas/utils.js';
|
|
8
8
|
import { BufferReader } from '../../serialize/buffer_reader.js';
|
|
9
|
-
import { TypeRegistry } from '../../serialize/type_registry.js';
|
|
10
|
-
|
|
11
|
-
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
|
|
12
9
|
|
|
13
10
|
/**
|
|
14
11
|
* Represents a field derived from BaseField.
|
|
@@ -187,10 +184,25 @@ function fromHexString<T extends BaseField>(buf: string, f: DerivedField<T>) {
|
|
|
187
184
|
return new f(toBigIntBE(buffer));
|
|
188
185
|
}
|
|
189
186
|
|
|
190
|
-
/**
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Abstract unbranded base class for BN254 scalar field elements.
|
|
189
|
+
* Extend this instead of Fr when defining a branded subtype (e.g. BlockHash)
|
|
190
|
+
* to avoid inheriting Fr's `_branding`.
|
|
191
|
+
*/
|
|
192
|
+
export abstract class BaseFr extends BaseField {
|
|
193
|
+
static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
194
|
+
|
|
195
|
+
constructor(value: number | bigint | boolean | BaseField | Buffer) {
|
|
196
|
+
super(value);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
protected modulus() {
|
|
200
|
+
return BaseFr.MODULUS;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
toJSON() {
|
|
204
|
+
return this.toString();
|
|
205
|
+
}
|
|
194
206
|
}
|
|
195
207
|
|
|
196
208
|
/**
|
|
@@ -198,10 +210,12 @@ export interface Fr {
|
|
|
198
210
|
* @dev This class is used to represent elements of BN254 scalar field or elements in the base field of Grumpkin.
|
|
199
211
|
* (Grumpkin's scalar field corresponds to BN254's base field and vice versa.)
|
|
200
212
|
*/
|
|
201
|
-
export class Fr extends
|
|
213
|
+
export class Fr extends BaseFr {
|
|
214
|
+
/** Branding for nominal typing. */
|
|
215
|
+
declare private readonly _branding: 'Fr';
|
|
216
|
+
static override MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
202
217
|
static ZERO = new Fr(0n);
|
|
203
218
|
static ONE = new Fr(1n);
|
|
204
|
-
static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
205
219
|
static MAX_FIELD_VALUE = new Fr(this.MODULUS - 1n);
|
|
206
220
|
|
|
207
221
|
constructor(value: number | bigint | boolean | Fr | Buffer) {
|
|
@@ -212,10 +226,6 @@ export class Fr extends BaseField {
|
|
|
212
226
|
return `Fr<${this.toString()}>`;
|
|
213
227
|
}
|
|
214
228
|
|
|
215
|
-
protected modulus() {
|
|
216
|
-
return Fr.MODULUS;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
229
|
static random() {
|
|
220
230
|
return random(Fr);
|
|
221
231
|
}
|
|
@@ -320,10 +330,6 @@ export class Fr extends BaseField {
|
|
|
320
330
|
return Fr.fromBuffer(Buffer.from(response.value));
|
|
321
331
|
}
|
|
322
332
|
|
|
323
|
-
toJSON() {
|
|
324
|
-
return this.toString();
|
|
325
|
-
}
|
|
326
|
-
|
|
327
333
|
/**
|
|
328
334
|
* Creates an Fr instance from a plain object without Zod validation.
|
|
329
335
|
* This method is optimized for performance and skips validation, making it suitable
|
|
@@ -345,23 +351,14 @@ export class Fr extends BaseField {
|
|
|
345
351
|
}
|
|
346
352
|
}
|
|
347
353
|
|
|
348
|
-
// For deserializing JSON.
|
|
349
|
-
TypeRegistry.register('Fr', Fr);
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Branding to ensure fields are not interchangeable types.
|
|
353
|
-
*/
|
|
354
|
-
export interface Fq {
|
|
355
|
-
/** Brand. */
|
|
356
|
-
_branding: 'Fq';
|
|
357
|
-
}
|
|
358
|
-
|
|
359
354
|
/**
|
|
360
355
|
* Fq field class.
|
|
361
356
|
* @dev This class is used to represent elements of BN254 base field or elements in the scalar field of Grumpkin.
|
|
362
357
|
* (Grumpkin's scalar field corresponds to BN254's base field and vice versa.)
|
|
363
358
|
*/
|
|
364
359
|
export class Fq extends BaseField {
|
|
360
|
+
/** Branding for nominal typing. */
|
|
361
|
+
declare private readonly _branding: 'Fq';
|
|
365
362
|
static ZERO = new Fq(0n);
|
|
366
363
|
static MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
|
|
367
364
|
private static HIGH_SHIFT = BigInt((BaseField.SIZE_IN_BYTES / 2) * 8);
|
|
@@ -470,9 +467,6 @@ export class Fq extends BaseField {
|
|
|
470
467
|
}
|
|
471
468
|
}
|
|
472
469
|
|
|
473
|
-
// For deserializing JSON.
|
|
474
|
-
TypeRegistry.register('Fq', Fq);
|
|
475
|
-
|
|
476
470
|
// Beware: Performance bottleneck below
|
|
477
471
|
|
|
478
472
|
/**
|
|
@@ -125,9 +125,12 @@ export class Point {
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
/**
|
|
128
|
-
* Returns the contents of the point as an array of
|
|
129
|
-
* @returns The point as an array of
|
|
128
|
+
* Returns the contents of the point as an array of 3 fields.
|
|
129
|
+
* @returns The point as an array of 3 fields
|
|
130
130
|
*/
|
|
131
|
+
// TODO(F-553): Once we take the breaking change and drop the custom Noir `Point` wrapper in
|
|
132
|
+
// `noir-projects/noir-protocol-circuits/crates/types/src/point.nr`, revert this to serialize as `[x, y]` (2 fields)
|
|
133
|
+
// and drop the `is_infinite` flag from `fromFields` / `toNoirStruct` below.
|
|
131
134
|
toFields() {
|
|
132
135
|
return [this.x, this.y, new Fr(this.isInfinite)];
|
|
133
136
|
}
|
package/src/eth-address/index.ts
CHANGED
|
@@ -5,7 +5,6 @@ import { randomBytes } from '../crypto/random/index.js';
|
|
|
5
5
|
import { Fr } from '../curves/bn254/index.js';
|
|
6
6
|
import { hexSchemaFor } from '../schemas/utils.js';
|
|
7
7
|
import { BufferReader, FieldReader } from '../serialize/index.js';
|
|
8
|
-
import { TypeRegistry } from '../serialize/type_registry.js';
|
|
9
8
|
import { bufferToHex } from '../string/index.js';
|
|
10
9
|
|
|
11
10
|
/**
|
|
@@ -268,6 +267,3 @@ export class EthAddress {
|
|
|
268
267
|
return addrA.equals(addrB);
|
|
269
268
|
}
|
|
270
269
|
}
|
|
271
|
-
|
|
272
|
-
// For deserializing JSON.
|
|
273
|
-
TypeRegistry.register('EthAddress', EthAddress);
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { shaMerkleHash } from './hasher.js';
|
|
2
2
|
|
|
3
3
|
export const computeBalancedShaRoot = (leaves: Buffer[]) => computeBalancedMerkleTreeRoot(leaves);
|
|
4
4
|
|
|
5
|
-
export const computeBalancedPoseidonRoot = async (leaves: Buffer[]) =>
|
|
6
|
-
await computeBalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
|
|
7
|
-
|
|
8
5
|
/**
|
|
9
6
|
* Computes the Merkle root with the provided leaves **synchronously**.
|
|
10
7
|
* This method uses a synchronous hash function (defaults to `sha256Trunc`).
|
|
@@ -36,7 +33,7 @@ export function computeBalancedMerkleTreeRoot(leaves: Buffer[], hasher = shaMerk
|
|
|
36
33
|
*/
|
|
37
34
|
export async function computeBalancedMerkleTreeRootAsync(
|
|
38
35
|
leaves: Buffer[],
|
|
39
|
-
hasher
|
|
36
|
+
hasher: (left: Buffer, right: Buffer) => Promise<Buffer<ArrayBuffer>>,
|
|
40
37
|
): Promise<Buffer> {
|
|
41
38
|
const height = getTreeHeight(leaves);
|
|
42
39
|
let nodes = leaves.slice();
|
package/src/trees/hasher.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { poseidon2HashWithSeparator } from '../crypto/poseidon/index.js';
|
|
2
2
|
import { sha256Trunc } from '../crypto/sha256/index.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -44,5 +44,8 @@ export interface AsyncHasher {
|
|
|
44
44
|
export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
|
|
45
45
|
sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
/** Creates a poseidon2 merkle hasher with the given domain separator. */
|
|
48
|
+
export const makePoseidonMerkleHash =
|
|
49
|
+
(separator: number): AsyncHasher['hash'] =>
|
|
50
|
+
async (left: Buffer, right: Buffer) =>
|
|
51
|
+
(await poseidon2HashWithSeparator([left, right], separator)).toBuffer() as Buffer<ArrayBuffer>;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
|
|
2
|
-
|
|
3
|
-
import type { AsyncHasher } from './hasher.js';
|
|
4
1
|
import { MerkleTree } from './merkle_tree.js';
|
|
5
2
|
|
|
3
|
+
type MerkleHashFn = (left: Buffer, right: Buffer) => Promise<Buffer<ArrayBuffer>>;
|
|
4
|
+
|
|
6
5
|
/**
|
|
7
6
|
* Merkle tree calculator.
|
|
8
7
|
*/
|
|
@@ -10,17 +9,12 @@ export class MerkleTreeCalculator {
|
|
|
10
9
|
private constructor(
|
|
11
10
|
private height: number,
|
|
12
11
|
private zeroHashes: Buffer[],
|
|
13
|
-
private hasher:
|
|
12
|
+
private hasher: MerkleHashFn,
|
|
14
13
|
) {
|
|
15
14
|
this.hasher = hasher;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
static async create(
|
|
19
|
-
height: number,
|
|
20
|
-
zeroLeaf: Buffer = Buffer.alloc(32),
|
|
21
|
-
hasher = async (left: Buffer, right: Buffer) =>
|
|
22
|
-
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>,
|
|
23
|
-
) {
|
|
17
|
+
static async create(height: number, zeroLeaf: Buffer = Buffer.alloc(32), hasher: MerkleHashFn) {
|
|
24
18
|
const zeroHashes = [zeroLeaf];
|
|
25
19
|
for (let i = 0; i < height; i++) {
|
|
26
20
|
zeroHashes.push((await hasher(zeroHashes[i], zeroHashes[i])) as Buffer<ArrayBuffer>);
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { makeTuple } from '../array/array.js';
|
|
2
|
-
import { poseidon2Hash } from '../crypto/poseidon/index.js';
|
|
3
2
|
import { Fr } from '../curves/bn254/index.js';
|
|
4
3
|
import { schemas } from '../schemas/index.js';
|
|
5
4
|
import {
|
|
@@ -172,7 +171,7 @@ export async function computeRootFromSiblingPath(
|
|
|
172
171
|
leaf: Buffer,
|
|
173
172
|
siblingPath: Buffer[],
|
|
174
173
|
index: number,
|
|
175
|
-
hasher
|
|
174
|
+
hasher: (left: Buffer, right: Buffer) => Promise<Buffer>,
|
|
176
175
|
) {
|
|
177
176
|
let result = leaf;
|
|
178
177
|
for (const sibling of siblingPath) {
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { computeBalancedMerkleTreeRoot, computeBalancedMerkleTreeRootAsync } from './balanced_merkle_tree_root.js';
|
|
2
|
-
import {
|
|
2
|
+
import { shaMerkleHash } from './hasher.js';
|
|
3
3
|
import { UnbalancedMerkleTreeCalculator } from './unbalanced_merkle_tree_calculator.js';
|
|
4
4
|
|
|
5
5
|
export const computeUnbalancedShaRoot = (leaves: Buffer[]) => computeUnbalancedMerkleTreeRoot(leaves, shaMerkleHash);
|
|
6
6
|
|
|
7
|
-
export const computeUnbalancedPoseidonRoot = async (leaves: Buffer[]) =>
|
|
8
|
-
await computeUnbalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
|
|
9
|
-
|
|
10
7
|
export const computeWonkyShaRoot = (leaves: Buffer[]) => computeWonkyMerkleTreeRoot(leaves);
|
|
11
8
|
|
|
12
9
|
/**
|
|
@@ -67,7 +64,7 @@ export function computeUnbalancedMerkleTreeRoot(
|
|
|
67
64
|
|
|
68
65
|
export async function computeUnbalancedMerkleTreeRootAsync(
|
|
69
66
|
leaves: Buffer[],
|
|
70
|
-
hasher
|
|
67
|
+
hasher: (left: Uint8Array, right: Uint8Array) => Promise<Buffer<ArrayBuffer>>,
|
|
71
68
|
emptyRoot = Buffer.alloc(32),
|
|
72
69
|
): Promise<Buffer> {
|
|
73
70
|
if (!leaves.length) {
|