@aztec/bb.js 0.73.0 → 0.75.0-commit.8a71f57856e217a77b6e50cbc8833c1cd5395b96

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 (55) hide show
  1. package/README.md +10 -0
  2. package/dest/browser/barretenberg/index.d.ts.map +1 -1
  3. package/dest/browser/barretenberg-threads.wasm.gz +0 -0
  4. package/dest/browser/barretenberg.wasm.gz +0 -0
  5. package/dest/browser/barretenberg_wasm/index.d.ts.map +1 -1
  6. package/dest/browser/crs/net_crs.d.ts.map +1 -1
  7. package/dest/browser/index.js +76 -22
  8. package/dest/browser/retry/index.d.ts +26 -0
  9. package/dest/browser/retry/index.d.ts.map +1 -0
  10. package/dest/node/barretenberg/__snapshots__/pedersen.test.js.snap +156 -0
  11. package/dest/node/barretenberg/__snapshots__/poseidon.test.js.snap +40 -0
  12. package/dest/node/barretenberg/index.d.ts.map +1 -1
  13. package/dest/node/barretenberg/index.js +3 -4
  14. package/dest/node/barretenberg_wasm/barretenberg-threads.wasm.gz +0 -0
  15. package/dest/node/barretenberg_wasm/barretenberg_wasm_base/index.js +5 -5
  16. package/dest/node/barretenberg_wasm/barretenberg_wasm_main/index.js +2 -2
  17. package/dest/node/barretenberg_wasm/index.d.ts.map +1 -1
  18. package/dest/node/barretenberg_wasm/index.js +7 -4
  19. package/dest/node/barretenberg_wasm/index.test.js +3 -3
  20. package/dest/node/crs/net_crs.d.ts.map +1 -1
  21. package/dest/node/crs/net_crs.js +6 -5
  22. package/dest/node/main.d.ts.map +1 -1
  23. package/dest/node/main.js +9 -9
  24. package/dest/node/retry/index.d.ts +26 -0
  25. package/dest/node/retry/index.d.ts.map +1 -0
  26. package/dest/node/retry/index.js +50 -0
  27. package/dest/node-cjs/barretenberg/index.d.ts.map +1 -1
  28. package/dest/node-cjs/barretenberg/index.js +3 -4
  29. package/dest/node-cjs/barretenberg_wasm/barretenberg-threads.wasm.gz +0 -0
  30. package/dest/node-cjs/barretenberg_wasm/barretenberg_wasm_base/index.js +5 -5
  31. package/dest/node-cjs/barretenberg_wasm/barretenberg_wasm_main/index.js +2 -2
  32. package/dest/node-cjs/barretenberg_wasm/index.d.ts.map +1 -1
  33. package/dest/node-cjs/barretenberg_wasm/index.js +7 -4
  34. package/dest/node-cjs/barretenberg_wasm/index.test.js +3 -3
  35. package/dest/node-cjs/crs/net_crs.d.ts.map +1 -1
  36. package/dest/node-cjs/crs/net_crs.js +6 -5
  37. package/dest/node-cjs/main.d.ts.map +1 -1
  38. package/dest/node-cjs/main.js +9 -9
  39. package/dest/node-cjs/retry/index.d.ts +26 -0
  40. package/dest/node-cjs/retry/index.d.ts.map +1 -0
  41. package/dest/node-cjs/retry/index.js +56 -0
  42. package/package.json +6 -4
  43. package/src/barretenberg/index.ts +8 -4
  44. package/src/barretenberg_wasm/barretenberg_wasm_base/index.ts +4 -4
  45. package/src/barretenberg_wasm/barretenberg_wasm_main/index.ts +1 -1
  46. package/src/barretenberg_wasm/fetch_code/browser/barretenberg-threads.ts +3 -0
  47. package/src/barretenberg_wasm/fetch_code/browser/barretenberg.ts +3 -0
  48. package/src/barretenberg_wasm/fetch_code/browser/index.ts +4 -4
  49. package/src/barretenberg_wasm/index.test.ts +2 -2
  50. package/src/barretenberg_wasm/index.ts +6 -3
  51. package/src/crs/net_crs.ts +18 -9
  52. package/src/crs/node/index.ts +3 -1
  53. package/src/main.ts +13 -10
  54. package/src/retry/index.ts +50 -0
  55. /package/src/barretenberg_wasm/fetch_code/{browser/wasm-module.d.ts → wasm-module.d.ts} +0 -0
@@ -1,3 +1,4 @@
1
+ import { retry, makeBackoff } from '../retry/index.js';
1
2
  /**
2
3
  * Downloader for CRS from the web or local.
3
4
  */
@@ -28,12 +29,16 @@ export class NetCrs {
28
29
 
29
30
  const g1End = this.numPoints * 64 - 1;
30
31
 
31
- const response = await fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/flat/g1.dat', {
32
- headers: {
33
- Range: `bytes=0-${g1End}`,
34
- },
35
- cache: 'force-cache',
36
- });
32
+ const response = await retry(
33
+ () =>
34
+ fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/flat/g1.dat', {
35
+ headers: {
36
+ Range: `bytes=0-${g1End}`,
37
+ },
38
+ cache: 'force-cache',
39
+ }),
40
+ makeBackoff([5, 5, 5]),
41
+ );
37
42
 
38
43
  return (this.data = new Uint8Array(await response.arrayBuffer()));
39
44
  }
@@ -42,9 +47,13 @@ export class NetCrs {
42
47
  * Download the G2 points data.
43
48
  */
44
49
  async downloadG2Data() {
45
- const response2 = await fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/flat/g2.dat', {
46
- cache: 'force-cache',
47
- });
50
+ const response2 = await retry(
51
+ () =>
52
+ fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/flat/g2.dat', {
53
+ cache: 'force-cache',
54
+ }),
55
+ makeBackoff([5, 5, 5]),
56
+ );
48
57
 
49
58
  return (this.g2Data = new Uint8Array(await response2.arrayBuffer()));
50
59
  }
@@ -45,7 +45,9 @@ export class Crs {
45
45
  * @returns The points data.
46
46
  */
47
47
  getG1Data(): Uint8Array {
48
- const length = this.numPoints * 64;
48
+ // Ensure length > 0, otherwise we might read a huge file.
49
+ // This is a backup.
50
+ const length = Math.max(this.numPoints, 1) * 64;
49
51
  const fd = openSync(this.path + '/bn254_g1.dat', 'r');
50
52
  const buffer = new Uint8Array(length);
51
53
  readSync(fd, buffer, 0, length, 0);
package/src/main.ts CHANGED
@@ -22,18 +22,18 @@ const debug = createDebug('bb.js');
22
22
  const MAX_ULTRAPLONK_CIRCUIT_SIZE_IN_WASM = 2 ** 19;
23
23
  const threads = +process.env.HARDWARE_CONCURRENCY! || undefined;
24
24
 
25
- function getBytecode(bytecodePath: string) {
25
+ function getBytecode(bytecodePath: string): Uint8Array {
26
26
  const extension = bytecodePath.substring(bytecodePath.lastIndexOf('.') + 1);
27
27
 
28
28
  if (extension == 'json') {
29
29
  const encodedCircuit = JSON.parse(readFileSync(bytecodePath, 'utf8'));
30
30
  const decompressed = gunzipSync(Buffer.from(encodedCircuit.bytecode, 'base64'));
31
- return decompressed;
31
+ return Uint8Array.from(decompressed);
32
32
  }
33
33
 
34
34
  const encodedCircuit = readFileSync(bytecodePath);
35
35
  const decompressed = gunzipSync(encodedCircuit);
36
- return decompressed;
36
+ return Uint8Array.from(decompressed);
37
37
  }
38
38
 
39
39
  function base64ToUint8Array(base64: string) {
@@ -62,10 +62,10 @@ async function getGatesUltra(bytecodePath: string, recursive: boolean, honkRecur
62
62
  return total;
63
63
  }
64
64
 
65
- function getWitness(witnessPath: string) {
65
+ function getWitness(witnessPath: string): Uint8Array {
66
66
  const data = readFileSync(witnessPath);
67
67
  const decompressed = gunzipSync(data);
68
- return decompressed;
68
+ return Uint8Array.from(decompressed);
69
69
  }
70
70
 
71
71
  async function computeCircuitSize(bytecodePath: string, recursive: boolean, honkRecursion: boolean, api: Barretenberg) {
@@ -300,7 +300,7 @@ export async function gateCountUltra(bytecodePath: string, recursive: boolean, h
300
300
  const buffer = Buffer.alloc(8);
301
301
  buffer.writeBigInt64LE(BigInt(numberOfGates));
302
302
 
303
- process.stdout.write(buffer);
303
+ process.stdout.write(Uint8Array.from(buffer));
304
304
  } finally {
305
305
  await api.destroy();
306
306
  }
@@ -310,7 +310,7 @@ export async function verify(proofPath: string, vkPath: string, crsPath: string)
310
310
  const { api, acirComposer } = await initLite(crsPath);
311
311
  try {
312
312
  await api.acirLoadVerificationKey(acirComposer, new RawBuffer(readFileSync(vkPath)));
313
- const verified = await api.acirVerifyProof(acirComposer, readFileSync(proofPath));
313
+ const verified = await api.acirVerifyProof(acirComposer, Uint8Array.from(readFileSync(proofPath)));
314
314
  debug(`verified: ${verified}`);
315
315
  return verified;
316
316
  } finally {
@@ -406,7 +406,7 @@ export async function proofAsFields(proofPath: string, vkPath: string, outputPat
406
406
  const numPublicInputs = readFileSync(vkPath).readUint32BE(8);
407
407
  const proofAsFields = await api.acirSerializeProofIntoFields(
408
408
  acirComposer,
409
- readFileSync(proofPath),
409
+ Uint8Array.from(readFileSync(proofPath)),
410
410
  numPublicInputs,
411
411
  );
412
412
  const jsonProofAsFields = JSON.stringify(proofAsFields.map(f => f.toString()));
@@ -521,7 +521,10 @@ export async function verifyUltraHonk(
521
521
  const acirVerifyUltraHonk = options?.keccak
522
522
  ? api.acirVerifyUltraKeccakHonk.bind(api)
523
523
  : api.acirVerifyUltraHonk.bind(api);
524
- const verified = await acirVerifyUltraHonk(readFileSync(proofPath), new RawBuffer(readFileSync(vkPath)));
524
+ const verified = await acirVerifyUltraHonk(
525
+ Uint8Array.from(readFileSync(proofPath)),
526
+ new RawBuffer(readFileSync(vkPath)),
527
+ );
525
528
 
526
529
  debug(`verified: ${verified}`);
527
530
  return verified;
@@ -534,7 +537,7 @@ export async function proofAsFieldsUltraHonk(proofPath: string, outputPath: stri
534
537
  const { api } = await initLite(crsPath);
535
538
  try {
536
539
  debug('outputting proof as vector of fields');
537
- const proofAsFields = await api.acirProofAsFieldsUltraHonk(readFileSync(proofPath));
540
+ const proofAsFields = await api.acirProofAsFieldsUltraHonk(Uint8Array.from(readFileSync(proofPath)));
538
541
  const jsonProofAsFields = JSON.stringify(proofAsFields.map(f => f.toString()));
539
542
 
540
543
  if (outputPath === '-') {
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Generates a backoff sequence for retrying operations with an increasing delay.
3
+ * The backoff sequence follows this pattern: 1, 1, 1, 2, 4, 8, 16, 32, 64, ...
4
+ * This generator can be used in combination with the `retry` function to perform
5
+ * retries with exponential backoff and capped at 64 seconds between attempts.
6
+ *
7
+ * @returns A generator that yields the next backoff value in seconds as an integer.
8
+ */
9
+ export function* backoffGenerator() {
10
+ const v = [1, 1, 1, 2, 4, 8, 16, 32, 64];
11
+ let i = 0;
12
+ while (true) {
13
+ yield v[Math.min(i++, v.length - 1)];
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Generates a backoff sequence based on the array of retry intervals to use with the `retry` function.
19
+ * @param retries - Intervals to retry (in seconds).
20
+ * @returns A generator sequence.
21
+ */
22
+ export function* makeBackoff(retries: number[]) {
23
+ for (const retry of retries) {
24
+ yield retry;
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Retry a given asynchronous function with a specific backoff strategy, until it succeeds or backoff generator ends.
30
+ * It logs the error and retry interval in case an error is caught. The function can be named for better log output.
31
+ *
32
+ * @param fn - The asynchronous function to be retried.
33
+ * @param backoff - The optional backoff generator providing the intervals in seconds between retries. Defaults to a predefined series.
34
+ * @returns A Promise that resolves with the successful result of the provided function, or rejects if backoff generator ends.
35
+ * @throws If `NoRetryError` is thrown by the `fn`, it is rethrown.
36
+ */
37
+ export async function retry<Result>(fn: () => Promise<Result>, backoff = backoffGenerator()) {
38
+ while (true) {
39
+ try {
40
+ return await fn();
41
+ } catch (err: any) {
42
+ const s = backoff.next().value;
43
+ if (s === undefined) {
44
+ throw err;
45
+ }
46
+ await new Promise(resolve => setTimeout(resolve, s * 1000));
47
+ continue;
48
+ }
49
+ }
50
+ }