@aztec/bb.js 0.0.1-alpha.6 → 0.3.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 (39) hide show
  1. package/README.md +3 -4
  2. package/dest/barretenberg-threads.wasm +0 -0
  3. package/dest/barretenberg.wasm +0 -0
  4. package/dest/barretenberg_api/common.test.js +2 -2
  5. package/dest/barretenberg_api/index.d.ts.map +1 -1
  6. package/dest/barretenberg_api/index.js +2 -2
  7. package/dest/barretenberg_api/pedersen.test.js +1 -1
  8. package/dest/barretenberg_wasm/barretenberg_wasm.js +6 -6
  9. package/dest/barretenberg_wasm/barretenberg_wasm.test.js +6 -2
  10. package/dest/barretenberg_wasm/index.d.ts +1 -1
  11. package/dest/barretenberg_wasm/index.js +1 -1
  12. package/dest/barretenberg_wasm.js +1 -1
  13. package/dest/crs/browser/cached_net_crs.d.ts.map +1 -1
  14. package/dest/crs/browser/cached_net_crs.js +1 -1
  15. package/dest/crs/node/{file_crs.d.ts → ignition_files_crs.d.ts} +2 -2
  16. package/dest/crs/node/ignition_files_crs.d.ts.map +1 -0
  17. package/dest/crs/node/ignition_files_crs.js +51 -0
  18. package/dest/crs/node/index.d.ts +3 -13
  19. package/dest/crs/node/index.d.ts.map +1 -1
  20. package/dest/crs/node/index.js +29 -16
  21. package/dest/examples/simple.test.js +2 -2
  22. package/dest/main.d.ts +5 -5
  23. package/dest/main.d.ts.map +1 -1
  24. package/dest/main.js +46 -50
  25. package/dest/simple_test.js +1 -1
  26. package/package.json +7 -3
  27. package/src/barretenberg_api/common.test.ts +1 -1
  28. package/src/barretenberg_api/index.ts +220 -42
  29. package/src/barretenberg_api/pedersen.test.ts +1 -1
  30. package/src/barretenberg_wasm/barretenberg_wasm.test.ts +6 -1
  31. package/src/barretenberg_wasm/barretenberg_wasm.ts +5 -5
  32. package/src/barretenberg_wasm/index.ts +1 -1
  33. package/src/crs/browser/cached_net_crs.ts +0 -1
  34. package/src/crs/node/{file_crs.ts → ignition_files_crs.ts} +2 -2
  35. package/src/crs/node/index.ts +28 -20
  36. package/src/examples/simple.test.ts +2 -2
  37. package/src/main.ts +47 -53
  38. package/dest/crs/node/file_crs.d.ts.map +0 -1
  39. package/dest/crs/node/file_crs.js +0 -51
package/src/main.ts CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env -S node --no-warnings
1
+ #!/usr/bin/env node
2
2
  import { Crs, BarretenbergApiAsync, newBarretenbergApiAsync, RawBuffer } from './index.js';
3
3
  import createDebug from 'debug';
4
4
  import { readFileSync, writeFileSync } from 'fs';
@@ -12,50 +12,57 @@ const debug = createDebug('bb.js');
12
12
  // Maximum we support.
13
13
  const MAX_CIRCUIT_SIZE = 2 ** 19;
14
14
 
15
- function getBytecode(jsonPath: string) {
15
+ function getJsonData(jsonPath: string) {
16
16
  const json = readFileSync(jsonPath, 'utf-8');
17
17
  const parsed = JSON.parse(json);
18
+ return parsed;
19
+ }
20
+
21
+ function getBytecode(jsonPath: string) {
22
+ const parsed = getJsonData(jsonPath);
18
23
  const buffer = Buffer.from(parsed.bytecode, 'base64');
19
24
  const decompressed = gunzipSync(buffer);
20
25
  return decompressed;
21
26
  }
22
27
 
28
+ async function getGates(jsonPath: string, api: BarretenbergApiAsync) {
29
+ const parsed = getJsonData(jsonPath);
30
+ if (parsed.gates) {
31
+ return +parsed.gates;
32
+ }
33
+ const { total } = await computeCircuitSize(jsonPath, api);
34
+ const jsonData = getJsonData(jsonPath);
35
+ jsonData.gates = total;
36
+ writeFileSync(jsonPath, JSON.stringify(jsonData));
37
+ return total;
38
+ }
39
+
23
40
  function getWitness(witnessPath: string) {
24
41
  const data = readFileSync(witnessPath);
25
42
  return Buffer.concat([numToUInt32BE(data.length / 32), data]);
26
43
  }
27
44
 
28
- async function getCircuitSize(jsonPath: string, api: BarretenbergApiAsync) {
45
+ async function computeCircuitSize(jsonPath: string, api: BarretenbergApiAsync) {
46
+ debug(`computing circuit size...`);
29
47
  const bytecode = getBytecode(jsonPath);
30
48
  const [exact, total, subgroup] = await api.acirGetCircuitSizes(new RawBuffer(bytecode));
31
49
  return { exact, total, subgroup };
32
50
  }
33
51
 
34
- async function init(jsonPath: string, sizeHint?: number) {
52
+ async function init(jsonPath: string, crsPath: string) {
35
53
  const api = await newBarretenbergApiAsync();
36
54
 
37
- const subgroupSize = await (async () => {
38
- if (sizeHint) {
39
- // Round to subgroup size.
40
- return Math.pow(2, Math.ceil(Math.log2(sizeHint)));
41
- }
42
-
43
- // First compute circuit size.
44
- debug(`computing circuit size (use size hint to skip)...`);
45
- const circuitSizes = await getCircuitSize(jsonPath, api);
46
- debug(`circuit size: ${circuitSizes.total}`);
47
-
48
- if (circuitSizes.subgroup > MAX_CIRCUIT_SIZE) {
49
- throw new Error(`Circuit size of ${circuitSizes.subgroup} exceeds max supported of ${MAX_CIRCUIT_SIZE}`);
50
- }
51
-
52
- return circuitSizes.subgroup;
53
- })();
55
+ const circuitSize = await getGates(jsonPath, api);
56
+ const subgroupSize = Math.pow(2, Math.ceil(Math.log2(circuitSize)));
57
+ if (subgroupSize > MAX_CIRCUIT_SIZE) {
58
+ throw new Error(`Circuit size of ${subgroupSize} exceeds max supported of ${MAX_CIRCUIT_SIZE}`);
59
+ }
54
60
 
61
+ debug(`circuit size: ${circuitSize}`);
55
62
  debug(`subgroup size: ${subgroupSize}`);
56
63
  debug('loading crs...');
57
64
  // Plus 1 needed! (Move +1 into Crs?)
58
- const crs = await Crs.new(subgroupSize + 1);
65
+ const crs = await Crs.new(subgroupSize + 1, crsPath);
59
66
 
60
67
  // Important to init slab allocator as first thing, to ensure maximum memory efficiency.
61
68
  await api.commonInitSlabAllocator(subgroupSize);
@@ -81,8 +88,8 @@ async function initLite() {
81
88
  return { api, acirComposer };
82
89
  }
83
90
 
84
- export async function proveAndVerify(jsonPath: string, witnessPath: string, isRecursive: boolean, sizeHint?: number) {
85
- const { api, acirComposer } = await init(jsonPath, sizeHint);
91
+ export async function proveAndVerify(jsonPath: string, witnessPath: string, crsPath: string, isRecursive: boolean) {
92
+ const { api, acirComposer } = await init(jsonPath, crsPath);
86
93
  try {
87
94
  debug(`creating proof...`);
88
95
  const bytecode = getBytecode(jsonPath);
@@ -101,11 +108,11 @@ export async function proveAndVerify(jsonPath: string, witnessPath: string, isRe
101
108
  export async function prove(
102
109
  jsonPath: string,
103
110
  witnessPath: string,
111
+ crsPath: string,
104
112
  isRecursive: boolean,
105
113
  outputPath: string,
106
- sizeHint?: number,
107
114
  ) {
108
- const { api, acirComposer } = await init(jsonPath, sizeHint);
115
+ const { api, acirComposer } = await init(jsonPath, crsPath);
109
116
  try {
110
117
  debug(`creating proof...`);
111
118
  const bytecode = getBytecode(jsonPath);
@@ -123,14 +130,13 @@ export async function prove(
123
130
  export async function gateCount(jsonPath: string) {
124
131
  const api = await newBarretenbergApiAsync(1);
125
132
  try {
126
- const circuitSizes = await getCircuitSize(jsonPath, api);
127
- console.log(`${circuitSizes.exact}`);
133
+ console.log(`gates: ${await getGates(jsonPath, api)}`);
128
134
  } finally {
129
135
  await api.destroy();
130
136
  }
131
137
  }
132
138
 
133
- export async function verify(jsonPath: string, proofPath: string, isRecursive: boolean, vkPath: string) {
139
+ export async function verify(proofPath: string, isRecursive: boolean, vkPath: string) {
134
140
  const { api, acirComposer } = await initLite();
135
141
  try {
136
142
  await api.acirLoadVerificationKey(acirComposer, new RawBuffer(readFileSync(vkPath)));
@@ -158,8 +164,8 @@ export async function contract(outputPath: string, vkPath: string) {
158
164
  }
159
165
  }
160
166
 
161
- export async function writeVk(jsonPath: string, outputPath: string, sizeHint?: number) {
162
- const { api, acirComposer } = await init(jsonPath, sizeHint);
167
+ export async function writeVk(jsonPath: string, crsPath: string, outputPath: string) {
168
+ const { api, acirComposer } = await init(jsonPath, crsPath);
163
169
  try {
164
170
  debug('initing proving key...');
165
171
  const bytecode = getBytecode(jsonPath);
@@ -211,19 +217,10 @@ export async function vkAsFields(vkPath: string, vkeyOutputPath: string) {
211
217
  }
212
218
  }
213
219
 
214
- // nargo use bb.js: backend -> bb.js
215
- // backend prove --data-dir data --witness /foo/bar/witness.tr --json /foo/bar/main.json
216
- // backend verify ...
217
- // backend get_total_num_gates --data-dir data --json /foo/bar/main.json
218
- // backend get_sol_contract --data-dir data --json /foo/bar/main.json --output
219
- // backend get_features
220
- // OPTIONAL stateful backend:
221
- // backend start
222
- // backend stop
223
-
224
220
  const program = new Command();
225
221
 
226
222
  program.option('-v, --verbose', 'enable verbose logging', false);
223
+ program.option('-c, --crs-path <path>', 'set crs path', './crs');
227
224
 
228
225
  function handleGlobalOptions() {
229
226
  if (program.opts().verbose) {
@@ -237,10 +234,9 @@ program
237
234
  .option('-j, --json-path <path>', 'Specify the JSON path', './target/main.json')
238
235
  .option('-w, --witness-path <path>', 'Specify the witness path', './target/witness.tr')
239
236
  .option('-r, --recursive', 'prove and verify using recursive prover and verifier', false)
240
- .option('-s, --size-hint <gates>', 'provide a circuit size hint to skip calculation phase')
241
- .action(async ({ jsonPath, witnessPath, recursive, sizeHint }) => {
237
+ .action(async ({ jsonPath, witnessPath, recursive, crsPath }) => {
242
238
  handleGlobalOptions();
243
- const result = await proveAndVerify(jsonPath, witnessPath, recursive, sizeHint);
239
+ const result = await proveAndVerify(jsonPath, witnessPath, crsPath, recursive);
244
240
  process.exit(result ? 0 : 1);
245
241
  });
246
242
 
@@ -251,10 +247,9 @@ program
251
247
  .option('-w, --witness-path <path>', 'Specify the witness path', './target/witness.tr')
252
248
  .option('-r, --recursive', 'prove using recursive prover', false)
253
249
  .option('-o, --output-path <path>', 'Specify the proof output path', './proofs/proof')
254
- .option('-s, --size-hint <gates>', 'provide a circuit size hint to skip calculation phase')
255
- .action(async ({ jsonPath, witnessPath, recursive, outputPath, sizeHint }) => {
250
+ .action(async ({ jsonPath, witnessPath, recursive, outputPath, crsPath }) => {
256
251
  handleGlobalOptions();
257
- await prove(jsonPath, witnessPath, recursive, outputPath, sizeHint);
252
+ await prove(jsonPath, witnessPath, crsPath, recursive, outputPath);
258
253
  });
259
254
 
260
255
  program
@@ -269,13 +264,13 @@ program
269
264
  program
270
265
  .command('verify')
271
266
  .description('Verify a proof. Process exists with success or failure code.')
272
- .option('-j, --json-path <path>', 'Specify the JSON path', './target/main.json')
273
267
  .requiredOption('-p, --proof-path <path>', 'Specify the path to the proof')
274
268
  .option('-r, --recursive', 'prove using recursive prover', false)
275
269
  .requiredOption('-k, --vk <path>', 'path to a verification key. avoids recomputation.')
276
- .action(async ({ jsonPath, proofPath, recursive, vk }) => {
270
+ .action(async ({ proofPath, recursive, vk }) => {
277
271
  handleGlobalOptions();
278
- await verify(jsonPath, proofPath, recursive, vk);
272
+ const result = await verify(proofPath, recursive, vk);
273
+ process.exit(result ? 0 : 1);
279
274
  });
280
275
 
281
276
  program
@@ -294,10 +289,9 @@ program
294
289
  .description('Output verification key.')
295
290
  .option('-j, --json-path <path>', 'Specify the JSON path', './target/main.json')
296
291
  .requiredOption('-o, --output-path <path>', 'Specify the path to write the key')
297
- .option('-s, --size-hint <gates>', 'provide a circuit size hint to skip calculation phase')
298
- .action(async ({ jsonPath, outputPath, sizeHint }) => {
292
+ .action(async ({ jsonPath, outputPath, crsPath }) => {
299
293
  handleGlobalOptions();
300
- await writeVk(jsonPath, outputPath, sizeHint);
294
+ await writeVk(jsonPath, crsPath, outputPath);
301
295
  });
302
296
 
303
297
  program
@@ -1 +0,0 @@
1
- {"version":3,"file":"file_crs.d.ts","sourceRoot":"","sources":["../../../src/crs/node/file_crs.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,YAAY,QAAqF,CAAC;AAE/G;;GAEG;AACH,qBAAa,OAAO;IAKhB;;OAEG;aACa,SAAS,EAAE,MAAM;IACjC,OAAO,CAAC,IAAI;IARd,OAAO,CAAC,IAAI,CAAc;IAC1B,OAAO,CAAC,MAAM,CAAc;;IAG1B;;OAEG;IACa,SAAS,EAAE,MAAM,EACzB,IAAI,SAAe;IAG7B,MAAM,CAAC,aAAa;IAIpB;;OAEG;IACG,IAAI;IAYV;;;OAGG;IACH,SAAS,IAAI,UAAU;IAIvB;;;OAGG;IACH,SAAS,IAAI,UAAU;CAGxB"}
@@ -1,51 +0,0 @@
1
- import { existsSync } from 'fs';
2
- import { readFile } from 'fs/promises';
3
- import { dirname } from 'path';
4
- import { fileURLToPath } from 'url';
5
- /**
6
- * The path to our SRS object, assuming that we are in barretenberg/ts folder.
7
- */
8
- export const SRS_DEV_PATH = dirname(fileURLToPath(import.meta.url)) + '/../../../cpp/srs_db/ignition/monomial';
9
- /**
10
- * Downloader for CRS from a local file (for Node).
11
- */
12
- export class FileCrs {
13
- constructor(
14
- /**
15
- * The number of circuit gates.
16
- */
17
- numPoints, path = SRS_DEV_PATH) {
18
- this.numPoints = numPoints;
19
- this.path = path;
20
- }
21
- static defaultExists() {
22
- return existsSync(SRS_DEV_PATH);
23
- }
24
- /**
25
- * Read the data file.
26
- */
27
- async init() {
28
- // We need this.numPoints number of g1 points.
29
- // numPoints should be circuitSize + 1.
30
- const g1Start = 28;
31
- const g1End = g1Start + this.numPoints * 64;
32
- const data = await readFile(this.path + '/transcript00.dat');
33
- this.data = data.subarray(g1Start, g1End);
34
- this.g2Data = await readFile(this.path + '/g2.dat');
35
- }
36
- /**
37
- * G1 points data for prover key.
38
- * @returns The points data.
39
- */
40
- getG1Data() {
41
- return this.data;
42
- }
43
- /**
44
- * G2 points data for verification key.
45
- * @returns The points data.
46
- */
47
- getG2Data() {
48
- return this.g2Data;
49
- }
50
- }
51
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmlsZV9jcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvY3JzL25vZGUvZmlsZV9jcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLElBQUksQ0FBQztBQUNoQyxPQUFPLEVBQUUsUUFBUSxFQUFF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