@aztec/bb.js 0.0.1-alpha.6 → 0.0.1-alpha.7

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 (35) 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/index.d.ts.map +1 -1
  5. package/dest/barretenberg_api/index.js +2 -2
  6. package/dest/barretenberg_wasm/barretenberg_wasm.js +5 -5
  7. package/dest/barretenberg_wasm/barretenberg_wasm.test.js +2 -2
  8. package/dest/barretenberg_wasm/index.d.ts +1 -1
  9. package/dest/barretenberg_wasm/index.js +1 -1
  10. package/dest/barretenberg_wasm.js +1 -1
  11. package/dest/crs/browser/cached_net_crs.d.ts.map +1 -1
  12. package/dest/crs/browser/cached_net_crs.js +1 -1
  13. package/dest/crs/node/{file_crs.d.ts → ignition_files_crs.d.ts} +2 -2
  14. package/dest/crs/node/ignition_files_crs.d.ts.map +1 -0
  15. package/dest/crs/node/ignition_files_crs.js +51 -0
  16. package/dest/crs/node/index.d.ts +2 -12
  17. package/dest/crs/node/index.d.ts.map +1 -1
  18. package/dest/crs/node/index.js +28 -15
  19. package/dest/examples/simple.test.js +1 -1
  20. package/dest/main.d.ts +4 -4
  21. package/dest/main.d.ts.map +1 -1
  22. package/dest/main.js +43 -48
  23. package/dest/simple_test.js +1 -1
  24. package/package.json +7 -3
  25. package/src/barretenberg_api/index.ts +220 -42
  26. package/src/barretenberg_wasm/barretenberg_wasm.test.ts +1 -1
  27. package/src/barretenberg_wasm/barretenberg_wasm.ts +4 -4
  28. package/src/barretenberg_wasm/index.ts +1 -1
  29. package/src/crs/browser/cached_net_crs.ts +0 -1
  30. package/src/crs/node/{file_crs.ts → ignition_files_crs.ts} +2 -2
  31. package/src/crs/node/index.ts +27 -19
  32. package/src/examples/simple.test.ts +1 -1
  33. package/src/main.ts +44 -57
  34. package/dest/crs/node/file_crs.d.ts.map +0 -1
  35. package/dest/crs/node/file_crs.js +0 -51
package/src/main.ts CHANGED
@@ -12,46 +12,53 @@ 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) {
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?)
@@ -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, isRecursive: boolean) {
92
+ const { api, acirComposer } = await init(jsonPath);
86
93
  try {
87
94
  debug(`creating proof...`);
88
95
  const bytecode = getBytecode(jsonPath);
@@ -98,14 +105,8 @@ export async function proveAndVerify(jsonPath: string, witnessPath: string, isRe
98
105
  }
99
106
  }
100
107
 
101
- export async function prove(
102
- jsonPath: string,
103
- witnessPath: string,
104
- isRecursive: boolean,
105
- outputPath: string,
106
- sizeHint?: number,
107
- ) {
108
- const { api, acirComposer } = await init(jsonPath, sizeHint);
108
+ export async function prove(jsonPath: string, witnessPath: string, isRecursive: boolean, outputPath: string) {
109
+ const { api, acirComposer } = await init(jsonPath);
109
110
  try {
110
111
  debug(`creating proof...`);
111
112
  const bytecode = getBytecode(jsonPath);
@@ -123,14 +124,13 @@ export async function prove(
123
124
  export async function gateCount(jsonPath: string) {
124
125
  const api = await newBarretenbergApiAsync(1);
125
126
  try {
126
- const circuitSizes = await getCircuitSize(jsonPath, api);
127
- console.log(`${circuitSizes.exact}`);
127
+ console.log(`gates: ${await getGates(jsonPath, api)}`);
128
128
  } finally {
129
129
  await api.destroy();
130
130
  }
131
131
  }
132
132
 
133
- export async function verify(jsonPath: string, proofPath: string, isRecursive: boolean, vkPath: string) {
133
+ export async function verify(proofPath: string, isRecursive: boolean, vkPath: string) {
134
134
  const { api, acirComposer } = await initLite();
135
135
  try {
136
136
  await api.acirLoadVerificationKey(acirComposer, new RawBuffer(readFileSync(vkPath)));
@@ -158,8 +158,8 @@ export async function contract(outputPath: string, vkPath: string) {
158
158
  }
159
159
  }
160
160
 
161
- export async function writeVk(jsonPath: string, outputPath: string, sizeHint?: number) {
162
- const { api, acirComposer } = await init(jsonPath, sizeHint);
161
+ export async function writeVk(jsonPath: string, outputPath: string) {
162
+ const { api, acirComposer } = await init(jsonPath);
163
163
  try {
164
164
  debug('initing proving key...');
165
165
  const bytecode = getBytecode(jsonPath);
@@ -211,16 +211,6 @@ export async function vkAsFields(vkPath: string, vkeyOutputPath: string) {
211
211
  }
212
212
  }
213
213
 
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
214
  const program = new Command();
225
215
 
226
216
  program.option('-v, --verbose', 'enable verbose logging', false);
@@ -237,10 +227,9 @@ program
237
227
  .option('-j, --json-path <path>', 'Specify the JSON path', './target/main.json')
238
228
  .option('-w, --witness-path <path>', 'Specify the witness path', './target/witness.tr')
239
229
  .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 }) => {
230
+ .action(async ({ jsonPath, witnessPath, recursive }) => {
242
231
  handleGlobalOptions();
243
- const result = await proveAndVerify(jsonPath, witnessPath, recursive, sizeHint);
232
+ const result = await proveAndVerify(jsonPath, witnessPath, recursive);
244
233
  process.exit(result ? 0 : 1);
245
234
  });
246
235
 
@@ -251,10 +240,9 @@ program
251
240
  .option('-w, --witness-path <path>', 'Specify the witness path', './target/witness.tr')
252
241
  .option('-r, --recursive', 'prove using recursive prover', false)
253
242
  .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 }) => {
243
+ .action(async ({ jsonPath, witnessPath, recursive, outputPath }) => {
256
244
  handleGlobalOptions();
257
- await prove(jsonPath, witnessPath, recursive, outputPath, sizeHint);
245
+ await prove(jsonPath, witnessPath, recursive, outputPath);
258
246
  });
259
247
 
260
248
  program
@@ -269,13 +257,13 @@ program
269
257
  program
270
258
  .command('verify')
271
259
  .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
260
  .requiredOption('-p, --proof-path <path>', 'Specify the path to the proof')
274
261
  .option('-r, --recursive', 'prove using recursive prover', false)
275
262
  .requiredOption('-k, --vk <path>', 'path to a verification key. avoids recomputation.')
276
- .action(async ({ jsonPath, proofPath, recursive, vk }) => {
263
+ .action(async ({ proofPath, recursive, vk }) => {
277
264
  handleGlobalOptions();
278
- await verify(jsonPath, proofPath, recursive, vk);
265
+ const result = await verify(proofPath, recursive, vk);
266
+ process.exit(result ? 0 : 1);
279
267
  });
280
268
 
281
269
  program
@@ -294,10 +282,9 @@ program
294
282
  .description('Output verification key.')
295
283
  .option('-j, --json-path <path>', 'Specify the JSON path', './target/main.json')
296
284
  .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 }) => {
285
+ .action(async ({ jsonPath, outputPath }) => {
299
286
  handleGlobalOptions();
300
- await writeVk(jsonPath, outputPath, sizeHint);
287
+ await writeVk(jsonPath, outputPath);
301
288
  });
302
289
 
303
290
  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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmlsZV9jcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvY3JzL25vZGUvZmlsZV9jcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLElBQUksQ0FBQztBQUNoQyxPQUFPLEVBQUUsUUFBUSxFQUFFLE1BQU0sYUFBYSxDQUFDO0FBQ3ZDLE9BQU8sRUFBRSxPQUFPLEVBQUUsTUFBTSxNQUFNLENBQUM7QUFDL0IsT0FBTyxFQUFFLGFBQWEsRUFBRSxNQUFNLEtBQUssQ0FBQztBQUVwQzs7R0FFRztBQUNILE1BQU0sQ0FBQyxNQUFNLFlBQVksR0FBRyxPQUFPLENBQUMsYUFBYSxDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLENBQUMsR0FBRyx3Q0FBd0MsQ0FBQztBQUUvRzs7R0FFRztBQUNILE1BQU0sT0FBTyxPQUFPO0lBSWxCO0lBQ0U7O09BRUc7SUFDYSxTQUFpQixFQUN6QixPQUFPLFlBQVk7UUFEWCxjQUFTLEdBQVQsU0FBUyxDQUFRO1FBQ3pCLFNBQUksR0FBSixJQUFJLENBQWU7SUFDMUIsQ0FBQztJQUVKLE1BQU0sQ0FBQyxhQUFhO1FBQ2xCLE9BQU8sVUFBVSxDQUFDLFlBQVksQ0FBQyxDQUFDO0lBQ2xDLENBQUM7SUFFRDs7T0FFRztJQUNILEtBQUssQ0FBQyxJQUFJO1FBQ1IsOENBQThDO1FBQzlDLHVDQUF1QztRQUN2QyxNQUFNLE9BQU8sR0FBRyxFQUFFLENBQUM7UUFDbkIsTUFBTSxLQUFLLEdBQUcsT0FBTyxHQUFHLElBQUksQ0FBQyxTQUFTLEdBQUcsRUFBRSxDQUFDO1FBRTVDLE1BQU0sSUFBSSxHQUFHLE1BQU0sUUFBUSxDQUFDLElBQUksQ0FBQyxJQUFJLEdBQUcsbUJBQW1CLENBQUMsQ0FBQztRQUM3RCxJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQyxRQUFRLENBQUMsT0FBTyxFQUFFLEtBQUssQ0FBQyxDQUFDO1FBRTFDLElBQUksQ0FBQyxNQUFNLEdBQUcsTUFBTSxRQUFRLENBQUMsSUFBSSxDQUFDLElBQUksR0FBRyxTQUFTLENBQUMsQ0FBQztJQUN0RCxDQUFDO0lBRUQ7OztPQUdHO0lBQ0gsU0FBUztRQUNQLE9BQU8sSUFBSSxDQUFDLElBQUksQ0FBQztJQUNuQixDQUFDO0lBRUQ7OztPQUdHO0lBQ0gsU0FBUztRQUNQLE9BQU8sSUFBSSxDQUFDLE1BQU0sQ0FBQztJQUNyQixDQUFDO0NBQ0YifQ==