@aztec/bb.js 0.0.1-alpha.6 → 0.0.1-fake-ceab37513c

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 (227) hide show
  1. package/README.md +69 -33
  2. package/package.json +56 -41
  3. package/src/barretenberg/__snapshots__/pedersen.test.ts.snap +156 -0
  4. package/src/barretenberg/__snapshots__/poseidon.test.ts.snap +40 -0
  5. package/src/barretenberg/backend.ts +378 -0
  6. package/src/barretenberg/blake2s.test.ts +70 -0
  7. package/src/{barretenberg_api → barretenberg}/common.test.ts +7 -5
  8. package/src/barretenberg/index.ts +204 -0
  9. package/src/barretenberg/pedersen.test.ts +62 -0
  10. package/src/barretenberg/poseidon.test.ts +39 -0
  11. package/src/barretenberg_api/index.ts +1128 -383
  12. package/src/barretenberg_wasm/barretenberg_wasm_base/index.ts +138 -0
  13. package/src/barretenberg_wasm/barretenberg_wasm_main/factory/browser/index.ts +11 -0
  14. package/src/barretenberg_wasm/barretenberg_wasm_main/factory/browser/main.worker.ts +13 -0
  15. package/src/barretenberg_wasm/barretenberg_wasm_main/factory/node/index.ts +21 -0
  16. package/src/barretenberg_wasm/barretenberg_wasm_main/factory/node/main.worker.ts +19 -0
  17. package/src/{barretenberg_binder/heap_allocator_sync.ts → barretenberg_wasm/barretenberg_wasm_main/heap_allocator.ts} +20 -17
  18. package/src/barretenberg_wasm/barretenberg_wasm_main/index.ts +167 -0
  19. package/src/barretenberg_wasm/barretenberg_wasm_thread/factory/browser/index.ts +11 -0
  20. package/src/barretenberg_wasm/barretenberg_wasm_thread/factory/browser/thread.worker.ts +13 -0
  21. package/src/barretenberg_wasm/barretenberg_wasm_thread/factory/node/index.ts +21 -0
  22. package/src/barretenberg_wasm/barretenberg_wasm_thread/factory/node/thread.worker.ts +19 -0
  23. package/src/barretenberg_wasm/barretenberg_wasm_thread/index.ts +47 -0
  24. package/src/barretenberg_wasm/fetch_code/browser/barretenberg-threads.ts +3 -0
  25. package/src/barretenberg_wasm/fetch_code/browser/barretenberg.ts +3 -0
  26. package/src/barretenberg_wasm/fetch_code/browser/index.ts +34 -0
  27. package/src/barretenberg_wasm/fetch_code/index.ts +1 -0
  28. package/src/barretenberg_wasm/fetch_code/node/index.ts +34 -0
  29. package/src/barretenberg_wasm/fetch_code/wasm-module.d.ts +4 -0
  30. package/src/barretenberg_wasm/helpers/browser/index.ts +47 -0
  31. package/src/barretenberg_wasm/helpers/index.ts +1 -0
  32. package/src/barretenberg_wasm/{node → helpers/node}/index.ts +24 -15
  33. package/src/barretenberg_wasm/index.test.ts +45 -0
  34. package/src/barretenberg_wasm/index.ts +22 -1
  35. package/src/benchmark/index.ts +26 -0
  36. package/src/benchmark/timer.ts +45 -0
  37. package/src/bigint-array/index.ts +39 -17
  38. package/src/bindgen/index.ts +2 -2
  39. package/src/bindgen/mappings.ts +3 -2
  40. package/src/bindgen/typescript.ts +50 -25
  41. package/src/cbind/README.md +1 -0
  42. package/src/cbind/generate.ts +89 -0
  43. package/src/cbind/schema_compiler.ts +833 -0
  44. package/src/crs/browser/cached_net_crs.ts +41 -2
  45. package/src/crs/browser/index.ts +1 -1
  46. package/src/crs/index.ts +1 -1
  47. package/src/crs/net_crs.ts +114 -19
  48. package/src/crs/node/index.ts +98 -20
  49. package/src/index.html +1 -1
  50. package/src/index.ts +13 -5
  51. package/src/log/browser/index.ts +35 -0
  52. package/src/log/index.ts +1 -0
  53. package/src/log/node/index.ts +52 -0
  54. package/src/log/types.ts +6 -0
  55. package/src/main.ts +395 -185
  56. package/src/proof/index.ts +94 -0
  57. package/src/random/browser/index.ts +1 -1
  58. package/src/retry/index.ts +50 -0
  59. package/src/serialize/buffer_reader.ts +4 -1
  60. package/src/types/fields.ts +46 -18
  61. package/src/types/point.ts +4 -1
  62. package/dest/async_map/index.d.ts +0 -10
  63. package/dest/async_map/index.d.ts.map +0 -1
  64. package/dest/async_map/index.js +0 -16
  65. package/dest/barretenberg-threads.wasm +0 -0
  66. package/dest/barretenberg.wasm +0 -0
  67. package/dest/barretenberg_api/blake2s.test.d.ts +0 -2
  68. package/dest/barretenberg_api/blake2s.test.d.ts.map +0 -1
  69. package/dest/barretenberg_api/blake2s.test.js +0 -30
  70. package/dest/barretenberg_api/common.test.d.ts +0 -2
  71. package/dest/barretenberg_api/common.test.d.ts.map +0 -1
  72. package/dest/barretenberg_api/common.test.js +0 -18
  73. package/dest/barretenberg_api/index.d.ts +0 -103
  74. package/dest/barretenberg_api/index.d.ts.map +0 -1
  75. package/dest/barretenberg_api/index.js +0 -379
  76. package/dest/barretenberg_api/pedersen.test.d.ts +0 -2
  77. package/dest/barretenberg_api/pedersen.test.d.ts.map +0 -1
  78. package/dest/barretenberg_api/pedersen.test.js +0 -69
  79. package/dest/barretenberg_api/schnorr.test.d.ts +0 -2
  80. package/dest/barretenberg_api/schnorr.test.d.ts.map +0 -1
  81. package/dest/barretenberg_api/schnorr.test.js +0 -113
  82. package/dest/barretenberg_binder/heap_allocator.d.ts +0 -22
  83. package/dest/barretenberg_binder/heap_allocator.d.ts.map +0 -1
  84. package/dest/barretenberg_binder/heap_allocator.js +0 -59
  85. package/dest/barretenberg_binder/heap_allocator_sync.d.ts +0 -22
  86. package/dest/barretenberg_binder/heap_allocator_sync.d.ts.map +0 -1
  87. package/dest/barretenberg_binder/heap_allocator_sync.js +0 -58
  88. package/dest/barretenberg_binder/index.d.ts +0 -32
  89. package/dest/barretenberg_binder/index.d.ts.map +0 -1
  90. package/dest/barretenberg_binder/index.js +0 -73
  91. package/dest/barretenberg_wasm/barretenberg_wasm.d.ts +0 -50
  92. package/dest/barretenberg_wasm/barretenberg_wasm.d.ts.map +0 -1
  93. package/dest/barretenberg_wasm/barretenberg_wasm.js +0 -212
  94. package/dest/barretenberg_wasm/barretenberg_wasm.test.d.ts +0 -2
  95. package/dest/barretenberg_wasm/barretenberg_wasm.test.d.ts.map +0 -1
  96. package/dest/barretenberg_wasm/barretenberg_wasm.test.js +0 -43
  97. package/dest/barretenberg_wasm/browser/index.d.ts +0 -8
  98. package/dest/barretenberg_wasm/browser/index.d.ts.map +0 -1
  99. package/dest/barretenberg_wasm/browser/index.js +0 -26
  100. package/dest/barretenberg_wasm/browser/worker.d.ts +0 -2
  101. package/dest/barretenberg_wasm/browser/worker.d.ts.map +0 -1
  102. package/dest/barretenberg_wasm/browser/worker.js +0 -11
  103. package/dest/barretenberg_wasm/index.d.ts +0 -2
  104. package/dest/barretenberg_wasm/index.d.ts.map +0 -1
  105. package/dest/barretenberg_wasm/index.js +0 -2
  106. package/dest/barretenberg_wasm/node/index.d.ts +0 -17
  107. package/dest/barretenberg_wasm/node/index.d.ts.map +0 -1
  108. package/dest/barretenberg_wasm/node/index.js +0 -40
  109. package/dest/barretenberg_wasm/node/node_endpoint.d.ts +0 -8
  110. package/dest/barretenberg_wasm/node/node_endpoint.d.ts.map +0 -1
  111. package/dest/barretenberg_wasm/node/node_endpoint.js +0 -28
  112. package/dest/barretenberg_wasm/node/worker.d.ts +0 -2
  113. package/dest/barretenberg_wasm/node/worker.d.ts.map +0 -1
  114. package/dest/barretenberg_wasm/node/worker.js +0 -9
  115. package/dest/barretenberg_wasm.js +0 -2
  116. package/dest/barretenberg_wasm.js.LICENSE.txt +0 -5
  117. package/dest/bigint-array/index.d.ts +0 -3
  118. package/dest/bigint-array/index.d.ts.map +0 -1
  119. package/dest/bigint-array/index.js +0 -21
  120. package/dest/bindgen/function_declaration.d.ts +0 -11
  121. package/dest/bindgen/function_declaration.d.ts.map +0 -1
  122. package/dest/bindgen/function_declaration.js +0 -2
  123. package/dest/bindgen/index.d.ts +0 -2
  124. package/dest/bindgen/index.d.ts.map +0 -1
  125. package/dest/bindgen/index.js +0 -15
  126. package/dest/bindgen/mappings.d.ts +0 -4
  127. package/dest/bindgen/mappings.d.ts.map +0 -1
  128. package/dest/bindgen/mappings.js +0 -63
  129. package/dest/bindgen/rust.d.ts +0 -2
  130. package/dest/bindgen/rust.d.ts.map +0 -1
  131. package/dest/bindgen/rust.js +0 -43
  132. package/dest/bindgen/to_camel_case.d.ts +0 -2
  133. package/dest/bindgen/to_camel_case.d.ts.map +0 -1
  134. package/dest/bindgen/to_camel_case.js +0 -11
  135. package/dest/bindgen/typescript.d.ts +0 -2
  136. package/dest/bindgen/typescript.d.ts.map +0 -1
  137. package/dest/bindgen/typescript.js +0 -80
  138. package/dest/crs/browser/cached_net_crs.d.ts +0 -25
  139. package/dest/crs/browser/cached_net_crs.d.ts.map +0 -1
  140. package/dest/crs/browser/cached_net_crs.js +0 -54
  141. package/dest/crs/browser/index.d.ts +0 -2
  142. package/dest/crs/browser/index.d.ts.map +0 -1
  143. package/dest/crs/browser/index.js +0 -2
  144. package/dest/crs/index.d.ts +0 -2
  145. package/dest/crs/index.d.ts.map +0 -1
  146. package/dest/crs/index.js +0 -2
  147. package/dest/crs/net_crs.d.ts +0 -36
  148. package/dest/crs/net_crs.d.ts.map +0 -1
  149. package/dest/crs/net_crs.js +0 -59
  150. package/dest/crs/node/file_crs.d.ts +0 -37
  151. package/dest/crs/node/file_crs.d.ts.map +0 -1
  152. package/dest/crs/node/file_crs.js +0 -51
  153. package/dest/crs/node/index.d.ts +0 -31
  154. package/dest/crs/node/index.d.ts.map +0 -1
  155. package/dest/crs/node/index.js +0 -41
  156. package/dest/examples/simple.rawtest.d.ts +0 -2
  157. package/dest/examples/simple.rawtest.d.ts.map +0 -1
  158. package/dest/examples/simple.rawtest.js +0 -29
  159. package/dest/examples/simple.test.d.ts +0 -2
  160. package/dest/examples/simple.test.d.ts.map +0 -1
  161. package/dest/examples/simple.test.js +0 -22
  162. package/dest/factory/index.d.ts +0 -21
  163. package/dest/factory/index.d.ts.map +0 -1
  164. package/dest/factory/index.js +0 -34
  165. package/dest/index.d.ts +0 -6
  166. package/dest/index.d.ts.map +0 -1
  167. package/dest/index.html +0 -1
  168. package/dest/index.js +0 -6
  169. package/dest/main.d.ts +0 -10
  170. package/dest/main.d.ts.map +0 -1
  171. package/dest/main.js +0 -280
  172. package/dest/random/browser/index.d.ts +0 -2
  173. package/dest/random/browser/index.d.ts.map +0 -1
  174. package/dest/random/browser/index.js +0 -31
  175. package/dest/random/index.d.ts +0 -2
  176. package/dest/random/index.d.ts.map +0 -1
  177. package/dest/random/index.js +0 -2
  178. package/dest/random/node/index.d.ts +0 -2
  179. package/dest/random/node/index.d.ts.map +0 -1
  180. package/dest/random/node/index.js +0 -5
  181. package/dest/serialize/buffer_reader.d.ts +0 -28
  182. package/dest/serialize/buffer_reader.d.ts.map +0 -1
  183. package/dest/serialize/buffer_reader.js +0 -66
  184. package/dest/serialize/index.d.ts +0 -4
  185. package/dest/serialize/index.d.ts.map +0 -1
  186. package/dest/serialize/index.js +0 -4
  187. package/dest/serialize/output_type.d.ts +0 -11
  188. package/dest/serialize/output_type.d.ts.map +0 -1
  189. package/dest/serialize/output_type.js +0 -44
  190. package/dest/serialize/serialize.d.ts +0 -53
  191. package/dest/serialize/serialize.d.ts.map +0 -1
  192. package/dest/serialize/serialize.js +0 -139
  193. package/dest/simple_test.js +0 -2
  194. package/dest/simple_test.js.LICENSE.txt +0 -14
  195. package/dest/types/fields.d.ts +0 -33
  196. package/dest/types/fields.d.ts.map +0 -1
  197. package/dest/types/fields.js +0 -86
  198. package/dest/types/fixed_size_buffer.d.ts +0 -26
  199. package/dest/types/fixed_size_buffer.d.ts.map +0 -1
  200. package/dest/types/fixed_size_buffer.js +0 -54
  201. package/dest/types/index.d.ts +0 -6
  202. package/dest/types/index.d.ts.map +0 -1
  203. package/dest/types/index.js +0 -6
  204. package/dest/types/point.d.ts +0 -17
  205. package/dest/types/point.d.ts.map +0 -1
  206. package/dest/types/point.js +0 -32
  207. package/dest/types/ptr.d.ts +0 -13
  208. package/dest/types/ptr.d.ts.map +0 -1
  209. package/dest/types/ptr.js +0 -20
  210. package/dest/types/raw_buffer.d.ts +0 -3
  211. package/dest/types/raw_buffer.d.ts.map +0 -1
  212. package/dest/types/raw_buffer.js +0 -5
  213. package/src/barretenberg_api/blake2s.test.ts +0 -39
  214. package/src/barretenberg_api/pedersen.test.ts +0 -84
  215. package/src/barretenberg_api/schnorr.test.ts +0 -169
  216. package/src/barretenberg_binder/heap_allocator.ts +0 -62
  217. package/src/barretenberg_binder/index.ts +0 -76
  218. package/src/barretenberg_wasm/barretenberg_wasm.test.ts +0 -52
  219. package/src/barretenberg_wasm/barretenberg_wasm.ts +0 -246
  220. package/src/barretenberg_wasm/browser/index.ts +0 -32
  221. package/src/barretenberg_wasm/browser/worker.ts +0 -13
  222. package/src/barretenberg_wasm/node/worker.ts +0 -10
  223. package/src/crs/node/file_crs.ts +0 -60
  224. package/src/examples/simple.rawtest.ts +0 -37
  225. package/src/examples/simple.test.ts +0 -27
  226. package/src/factory/index.ts +0 -36
  227. /package/src/barretenberg_wasm/{node → helpers/node}/node_endpoint.ts +0 -0
@@ -1,5 +1,4 @@
1
- import { concatenateUint8Arrays, numToUInt32BE } from '../../serialize/index.js';
2
- import { NetCrs } from '../net_crs.js';
1
+ import { NetCrs, NetGrumpkinCrs } from '../net_crs.js';
3
2
  import { get, set } from 'idb-keyval';
4
3
 
5
4
  /**
@@ -58,3 +57,43 @@ export class CachedNetCrs {
58
57
  return this.g2Data;
59
58
  }
60
59
  }
60
+
61
+ /**
62
+ * Downloader for CRS from the web or local.
63
+ */
64
+ export class CachedNetGrumpkinCrs {
65
+ private g1Data!: Uint8Array;
66
+
67
+ constructor(public readonly numPoints: number) {}
68
+
69
+ static async new(numPoints: number) {
70
+ const crs = new CachedNetGrumpkinCrs(numPoints);
71
+ await crs.init();
72
+ return crs;
73
+ }
74
+
75
+ /**
76
+ * Download the data.
77
+ */
78
+ async init() {
79
+ // Check if data is in IndexedDB
80
+ const g1Data = await get('grumpkinG1Data');
81
+ const netGrumpkinCrs = new NetGrumpkinCrs(this.numPoints);
82
+ const g1DataLength = this.numPoints * 64;
83
+
84
+ if (!g1Data || g1Data.length < g1DataLength) {
85
+ this.g1Data = await netGrumpkinCrs.downloadG1Data();
86
+ await set('grumpkinG1Data', this.g1Data);
87
+ } else {
88
+ this.g1Data = g1Data;
89
+ }
90
+ }
91
+
92
+ /**
93
+ * G1 points data for prover key.
94
+ * @returns The points data.
95
+ */
96
+ getG1Data(): Uint8Array {
97
+ return this.g1Data;
98
+ }
99
+ }
@@ -1 +1 @@
1
- export { CachedNetCrs as Crs } from './cached_net_crs.js';
1
+ export { CachedNetCrs as Crs, CachedNetGrumpkinCrs as GrumpkinCrs } from './cached_net_crs.js';
package/src/crs/index.ts CHANGED
@@ -1 +1 @@
1
- export { Crs } from './node/index.js';
1
+ export { Crs, GrumpkinCrs } from './node/index.js';
@@ -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
  */
@@ -20,17 +21,24 @@ export class NetCrs {
20
21
  await this.downloadG2Data();
21
22
  }
22
23
 
23
- async downloadG1Data() {
24
- const g1Start = 28;
25
- const g1End = g1Start + this.numPoints * 64 - 1;
24
+ /**
25
+ * Opens up a ReadableStream to the points data
26
+ */
27
+ async streamG1Data(): Promise<ReadableStream<Uint8Array>> {
28
+ const response = await this.fetchG1Data();
29
+ return response.body!;
30
+ }
26
31
 
27
- const response = await fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/monomial/transcript00.dat', {
28
- headers: {
29
- Range: `bytes=${g1Start}-${g1End}`,
30
- },
31
- cache: 'force-cache',
32
- });
32
+ /**
33
+ * Opens up a ReadableStream to the points data
34
+ */
35
+ async streamG2Data(): Promise<ReadableStream<Uint8Array>> {
36
+ const response = await this.fetchG2Data();
37
+ return response.body!;
38
+ }
33
39
 
40
+ async downloadG1Data() {
41
+ const response = await this.fetchG1Data();
34
42
  return (this.data = new Uint8Array(await response.arrayBuffer()));
35
43
  }
36
44
 
@@ -38,16 +46,7 @@ export class NetCrs {
38
46
  * Download the G2 points data.
39
47
  */
40
48
  async downloadG2Data() {
41
- const g2Start = 28 + 5040000 * 64;
42
- const g2End = g2Start + 128 - 1;
43
-
44
- const response2 = await fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/sealed/transcript00.dat', {
45
- headers: {
46
- Range: `bytes=${g2Start}-${g2End}`,
47
- },
48
- cache: 'force-cache',
49
- });
50
-
49
+ const response2 = await this.fetchG2Data();
51
50
  return (this.g2Data = new Uint8Array(await response2.arrayBuffer()));
52
51
  }
53
52
 
@@ -66,4 +65,100 @@ export class NetCrs {
66
65
  getG2Data(): Uint8Array {
67
66
  return this.g2Data;
68
67
  }
68
+
69
+ /**
70
+ * Fetches the appropriate range of points from a remote source
71
+ */
72
+ private async fetchG1Data(): Promise<Response> {
73
+ // Skip the download if numPoints is 0 (would download the entire file due to bad range header otherwise)
74
+ if (this.numPoints === 0) {
75
+ return new Response(new Uint8Array([]));
76
+ }
77
+
78
+ const g1End = this.numPoints * 64 - 1;
79
+ return await retry(
80
+ () =>
81
+ fetch('https://crs.aztec.network/g1.dat', {
82
+ headers: {
83
+ Range: `bytes=0-${g1End}`,
84
+ },
85
+ cache: 'force-cache',
86
+ }),
87
+ makeBackoff([5, 5, 5]),
88
+ );
89
+ }
90
+
91
+ /**
92
+ * Fetches the appropriate range of points from a remote source
93
+ */
94
+ private async fetchG2Data(): Promise<Response> {
95
+ return await retry(
96
+ () =>
97
+ fetch('https://crs.aztec.network/g2.dat', {
98
+ cache: 'force-cache',
99
+ }),
100
+ makeBackoff([5, 5, 5]),
101
+ );
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Downloader for CRS from the web or local.
107
+ */
108
+ export class NetGrumpkinCrs {
109
+ private data!: Uint8Array;
110
+
111
+ constructor(
112
+ /**
113
+ * The number of circuit gates.
114
+ */
115
+ public readonly numPoints: number,
116
+ ) {}
117
+
118
+ /**
119
+ * Download the data.
120
+ */
121
+ async init() {
122
+ await this.downloadG1Data();
123
+ }
124
+
125
+ async downloadG1Data() {
126
+ const response = await this.fetchG1Data();
127
+ return (this.data = new Uint8Array(await response.arrayBuffer()));
128
+ }
129
+
130
+ /**
131
+ * Opens up a ReadableStream to the points data
132
+ */
133
+ async streamG1Data(): Promise<ReadableStream<Uint8Array>> {
134
+ const response = await this.fetchG1Data();
135
+ return response.body!;
136
+ }
137
+
138
+ /**
139
+ * G1 points data for prover key.
140
+ * @returns The points data.
141
+ */
142
+ getG1Data(): Uint8Array {
143
+ return this.data;
144
+ }
145
+
146
+ /**
147
+ * Fetches the appropriate range of points from a remote source
148
+ */
149
+ private async fetchG1Data(): Promise<Response> {
150
+ // Skip the download if numPoints is 0 (would download the entire file due to bad range header otherwise)
151
+ if (this.numPoints === 0) {
152
+ return new Response(new Uint8Array([]));
153
+ }
154
+
155
+ const g1End = this.numPoints * 64 - 1;
156
+
157
+ return await fetch('https://crs.aztec.network/grumpkin_g1.dat', {
158
+ headers: {
159
+ Range: `bytes=0-${g1End}`,
160
+ },
161
+ cache: 'force-cache',
162
+ });
163
+ }
69
164
  }
@@ -1,33 +1,55 @@
1
- import { concatenateUint8Arrays, numToUInt32BE } from '../../serialize/serialize.js';
2
- import { NetCrs } from '../net_crs.js';
3
- import { FileCrs } from './file_crs.js';
1
+ import { NetCrs, NetGrumpkinCrs } from '../net_crs.js';
2
+ import { closeSync, mkdirSync, openSync, readFileSync, readSync, writeFileSync, createWriteStream } from 'fs';
3
+ import { stat } from 'fs/promises';
4
+ import { Readable } from 'stream';
5
+ import { homedir } from 'os';
6
+ import { finished } from 'stream/promises';
7
+ import { createDebugLogger } from '../../log/index.js';
8
+ import { join } from 'path';
4
9
 
5
10
  /**
6
11
  * Generic CRS finder utility class.
7
12
  */
8
13
  export class Crs {
9
- private crs: FileCrs | NetCrs;
10
-
11
14
  constructor(
12
- /**
13
- * The number of circuit gates.
14
- */
15
15
  public readonly numPoints: number,
16
- ) {
17
- this.crs = FileCrs.defaultExists() ? new FileCrs(numPoints) : new NetCrs(numPoints);
18
- }
16
+ public readonly path: string,
17
+ private readonly logger: (msg: string) => void = createDebugLogger('crs'),
18
+ ) {}
19
19
 
20
- static async new(numPoints: number) {
21
- const crs = new Crs(numPoints);
20
+ static async new(
21
+ numPoints: number,
22
+ crsPath = process.env.CRS_PATH ?? join(homedir(), '.bb-crs'),
23
+ logger: (msg: string) => void = createDebugLogger('crs'),
24
+ ) {
25
+ const crs = new Crs(numPoints, crsPath, logger);
22
26
  await crs.init();
23
27
  return crs;
24
28
  }
25
29
 
26
- /**
27
- * Read CRS from our chosen source.
28
- */
29
- async init() {
30
- await this.crs.init();
30
+ async init(): Promise<void> {
31
+ mkdirSync(this.path, { recursive: true });
32
+
33
+ const g1FileSize = await stat(this.path + '/bn254_g1.dat')
34
+ .then(stats => stats.size)
35
+ .catch(() => 0);
36
+ const g2FileSize = await stat(this.path + '/bn254_g2.dat')
37
+ .then(stats => stats.size)
38
+ .catch(() => 0);
39
+
40
+ if (g1FileSize >= this.numPoints * 64 && g1FileSize % 64 == 0 && g2FileSize == 128) {
41
+ this.logger(`Using cached CRS of size ${g1FileSize / 64}`);
42
+ return;
43
+ }
44
+
45
+ this.logger(`Downloading CRS of size ${this.numPoints} into ${this.path}`);
46
+ const crs = new NetCrs(this.numPoints);
47
+ const [g1, g2] = await Promise.all([crs.streamG1Data(), crs.streamG2Data()]);
48
+
49
+ await Promise.all([
50
+ finished(Readable.fromWeb(g1 as any).pipe(createWriteStream(this.path + '/bn254_g1.dat'))),
51
+ finished(Readable.fromWeb(g2 as any).pipe(createWriteStream(this.path + '/bn254_g2.dat'))),
52
+ ]);
31
53
  }
32
54
 
33
55
  /**
@@ -35,7 +57,14 @@ export class Crs {
35
57
  * @returns The points data.
36
58
  */
37
59
  getG1Data(): Uint8Array {
38
- return this.crs.getG1Data();
60
+ // Ensure length > 0, otherwise we might read a huge file.
61
+ // This is a backup.
62
+ const length = Math.max(this.numPoints, 1) * 64;
63
+ const fd = openSync(this.path + '/bn254_g1.dat', 'r');
64
+ const buffer = new Uint8Array(length);
65
+ readSync(fd, buffer, 0, length, 0);
66
+ closeSync(fd);
67
+ return buffer;
39
68
  }
40
69
 
41
70
  /**
@@ -43,6 +72,55 @@ export class Crs {
43
72
  * @returns The points data.
44
73
  */
45
74
  getG2Data(): Uint8Array {
46
- return this.crs.getG2Data();
75
+ return readFileSync(this.path + '/bn254_g2.dat');
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Generic Grumpkin CRS finder utility class.
81
+ */
82
+ export class GrumpkinCrs {
83
+ constructor(
84
+ public readonly numPoints: number,
85
+ public readonly path: string,
86
+ private readonly logger: (msg: string) => void = createDebugLogger('crs'),
87
+ ) {}
88
+
89
+ static async new(
90
+ numPoints: number,
91
+ crsPath = process.env.CRS_PATH ?? join(homedir(), '.bb-crs'),
92
+ logger: (msg: string) => void = createDebugLogger('crs'),
93
+ ) {
94
+ const crs = new GrumpkinCrs(numPoints, crsPath, logger);
95
+ await crs.init();
96
+ return crs;
97
+ }
98
+
99
+ async init(): Promise<void> {
100
+ mkdirSync(this.path, { recursive: true });
101
+
102
+ const g1FileSize = await stat(this.path + '/grumpkin_g1.flat.dat')
103
+ .then(stats => stats.size)
104
+ .catch(() => 0);
105
+
106
+ if (g1FileSize >= this.numPoints * 64 && g1FileSize % 64 == 0) {
107
+ this.logger(`Using cached Grumpkin CRS of size ${g1FileSize / 64}`);
108
+ return;
109
+ }
110
+
111
+ this.logger(`Downloading Grumpkin CRS of size ${this.numPoints} into ${this.path}`);
112
+ const crs = new NetGrumpkinCrs(this.numPoints);
113
+ const stream = await crs.streamG1Data();
114
+
115
+ await finished(Readable.fromWeb(stream as any).pipe(createWriteStream(this.path + '/grumpkin_g1.flat.dat')));
116
+ writeFileSync(this.path + '/grumpkin_size', String(crs.numPoints));
117
+ }
118
+
119
+ /**
120
+ * G1 points data for prover key.
121
+ * @returns The points data.
122
+ */
123
+ getG1Data(): Uint8Array {
124
+ return readFileSync(this.path + '/grumpkin_g1.flat.dat');
47
125
  }
48
126
  }
package/src/index.html CHANGED
@@ -1,4 +1,4 @@
1
- <!DOCTYPE html>
1
+ <!doctype html>
2
2
  <html>
3
3
  <head>
4
4
  <title>My App</title>
package/src/index.ts CHANGED
@@ -1,5 +1,13 @@
1
- export * from './crs/index.js';
2
- export * from './barretenberg_wasm/index.js';
3
- export * from './barretenberg_api/index.js';
4
- export * from './factory/index.js';
5
- export { RawBuffer } from './types/index.js';
1
+ export { Crs, GrumpkinCrs } from './crs/index.js';
2
+ export {
3
+ type BackendOptions,
4
+ Barretenberg,
5
+ BarretenbergSync,
6
+ UltraHonkVerifierBackend,
7
+ UltraHonkBackend,
8
+ AztecClientBackend,
9
+ } from './barretenberg/index.js';
10
+
11
+ export { randomBytes } from './random/index.js';
12
+ export { RawBuffer, Fr } from './types/index.js';
13
+ export { splitHonkProof, reconstructHonkProof, deflattenFields, type ProofData } from './proof/index.js';
@@ -0,0 +1,35 @@
1
+ import { pino } from 'pino';
2
+ import { LogOptions } from '../types.js';
3
+ import { Logger } from 'pino';
4
+
5
+ const defaultOptions = {
6
+ name: 'bb.js',
7
+ useOnlyCustomLevels: false,
8
+ customLevels: { verbose: 25 },
9
+ browser: { asObject: false },
10
+ };
11
+
12
+ // Options must be exposed so they can be provided to threads upon creation
13
+ // This way we ensure all loggers are spawned with the same options
14
+ export let logOptions: LogOptions | undefined;
15
+
16
+ let logger: Logger<'verbose'> | undefined;
17
+
18
+ export function initLogger({ level = 'info' }: LogOptions = { level: 'info', useStdErr: false }) {
19
+ if (logger) {
20
+ return logger;
21
+ }
22
+ logOptions = { level, useStdErr: false };
23
+ logger = pino({ ...defaultOptions, level });
24
+ }
25
+
26
+ export function createDebugLogger(name: string) {
27
+ initLogger();
28
+
29
+ const sublogger = logger!.child({
30
+ name,
31
+ });
32
+ return (msg: string) => {
33
+ sublogger.debug(msg);
34
+ };
35
+ }
@@ -0,0 +1 @@
1
+ export { initLogger, createDebugLogger, logOptions } from './node/index.js';
@@ -0,0 +1,52 @@
1
+ import { Logger, pino } from 'pino';
2
+ import { LogLevels, LogOptions } from '../types.js';
3
+
4
+ const defaultOptions = {
5
+ name: 'bb.js',
6
+ useOnlyCustomLevels: false,
7
+ customLevels: { verbose: 25 },
8
+ };
9
+
10
+ const defaultLevel = parseLogLevel(process.env.LOG_LEVEL);
11
+
12
+ // Options must be exposed so they can be provided to threads upon creation
13
+ // This way we ensure all loggers are spawned with the same options
14
+ export let logOptions: LogOptions | undefined;
15
+
16
+ let logger: Logger<'verbose'> | undefined;
17
+
18
+ export function initLogger(
19
+ { level = defaultLevel, useStdErr = false }: LogOptions = { level: defaultLevel, useStdErr: false },
20
+ ) {
21
+ if (logger) {
22
+ return logger;
23
+ }
24
+ logOptions = { level, useStdErr };
25
+ const transport = pino.transport({
26
+ target: 'pino/file',
27
+ options: { destination: useStdErr ? 2 : 1 },
28
+ });
29
+ logger = pino({ ...defaultOptions, level }, transport);
30
+ }
31
+
32
+ export function createDebugLogger(name: string) {
33
+ initLogger();
34
+
35
+ const sublogger = logger!.child({
36
+ name,
37
+ });
38
+ return (msg: string) => {
39
+ sublogger.debug(msg);
40
+ };
41
+ }
42
+
43
+ function parseLogLevel(logLevel?: string): LogLevels {
44
+ if (!logLevel) {
45
+ return 'info';
46
+ }
47
+
48
+ const knownLogLevels: LogLevels[] = ['info', 'debug', 'warn', 'error', 'trace', 'silent', 'verbose'];
49
+ const [defaultLogLevel] = logLevel.split(';');
50
+
51
+ return knownLogLevels.indexOf(defaultLogLevel as LogLevels) !== -1 ? (defaultLogLevel as LogLevels) : 'info';
52
+ }
@@ -0,0 +1,6 @@
1
+ export type LogLevels = 'silent' | 'info' | 'debug' | 'warn' | 'error' | 'trace' | 'verbose';
2
+
3
+ export type LogOptions = {
4
+ level: LogLevels;
5
+ useStdErr: boolean;
6
+ };