@aztec/bb.js 0.85.0-nightly.20250417 → 0.85.0-nightly.20250419

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 (48) hide show
  1. package/dest/browser/barretenberg/backend.d.ts +12 -8
  2. package/dest/browser/barretenberg/backend.d.ts.map +1 -1
  3. package/dest/browser/barretenberg-threads.js +1 -1
  4. package/dest/browser/barretenberg.js +1 -1
  5. package/dest/browser/barretenberg_api/index.d.ts +18 -27
  6. package/dest/browser/barretenberg_api/index.d.ts.map +1 -1
  7. package/dest/browser/index.js +2226 -254
  8. package/dest/node/barretenberg/backend.d.ts +12 -8
  9. package/dest/node/barretenberg/backend.d.ts.map +1 -1
  10. package/dest/node/barretenberg/backend.js +64 -15
  11. package/dest/node/barretenberg_api/index.d.ts +18 -27
  12. package/dest/node/barretenberg_api/index.d.ts.map +1 -1
  13. package/dest/node/barretenberg_api/index.js +110 -190
  14. package/dest/node/barretenberg_wasm/barretenberg-threads.wasm.gz +0 -0
  15. package/dest/node/bindgen/mappings.d.ts.map +1 -1
  16. package/dest/node/bindgen/mappings.js +2 -1
  17. package/dest/node/bindgen/typescript.d.ts.map +1 -1
  18. package/dest/node/bindgen/typescript.js +3 -2
  19. package/dest/node/main.d.ts +0 -1
  20. package/dest/node/main.d.ts.map +1 -1
  21. package/dest/node/main.js +41 -38
  22. package/dest/node-cjs/barretenberg/backend.d.ts +12 -8
  23. package/dest/node-cjs/barretenberg/backend.d.ts.map +1 -1
  24. package/dest/node-cjs/barretenberg/backend.js +64 -15
  25. package/dest/node-cjs/barretenberg_api/index.d.ts +18 -27
  26. package/dest/node-cjs/barretenberg_api/index.d.ts.map +1 -1
  27. package/dest/node-cjs/barretenberg_api/index.js +109 -189
  28. package/dest/node-cjs/barretenberg_wasm/barretenberg-threads.wasm.gz +0 -0
  29. package/dest/node-cjs/bindgen/mappings.d.ts.map +1 -1
  30. package/dest/node-cjs/bindgen/mappings.js +2 -1
  31. package/dest/node-cjs/bindgen/typescript.d.ts.map +1 -1
  32. package/dest/node-cjs/bindgen/typescript.js +3 -2
  33. package/dest/node-cjs/main.d.ts +0 -1
  34. package/dest/node-cjs/main.d.ts.map +1 -1
  35. package/dest/node-cjs/main.js +42 -40
  36. package/package.json +2 -2
  37. package/src/barretenberg/backend.ts +86 -18
  38. package/src/barretenberg_api/index.ts +171 -338
  39. package/src/bindgen/mappings.ts +1 -0
  40. package/src/bindgen/typescript.ts +2 -1
  41. package/src/main.ts +43 -42
  42. package/dest/node/barretenberg/schnorr.test.d.ts +0 -2
  43. package/dest/node/barretenberg/schnorr.test.d.ts.map +0 -1
  44. package/dest/node/barretenberg/schnorr.test.js +0 -113
  45. package/dest/node-cjs/barretenberg/schnorr.test.d.ts +0 -2
  46. package/dest/node-cjs/barretenberg/schnorr.test.d.ts.map +0 -1
  47. package/dest/node-cjs/barretenberg/schnorr.test.js +0 -115
  48. package/src/barretenberg/schnorr.test.ts +0 -182
@@ -10,6 +10,7 @@ import {
10
10
  splitHonkProof,
11
11
  AGGREGATION_OBJECT_LENGTH,
12
12
  } from '../proof/index.js';
13
+ import { Encoder } from 'msgpackr/pack';
13
14
 
14
15
  export class AztecClientBackendError extends Error {
15
16
  constructor(message: string) {
@@ -17,6 +18,24 @@ export class AztecClientBackendError extends Error {
17
18
  }
18
19
  }
19
20
 
21
+ // Utility for parsing gate counts from buffer
22
+ // TODO: Where should this logic live? Should go away with move to msgpack.
23
+ function parseBigEndianU32Array(buffer: Uint8Array): number[] {
24
+ const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
25
+
26
+ let offset = 0;
27
+ const count = buffer.byteLength >>> 2; // default is entire buffer length / 4
28
+ console.log(buffer);
29
+
30
+ const out: number[] = new Array(count);
31
+ for (let i = 0; i < count; i++) {
32
+ out[i] = dv.getUint32(offset, false);
33
+ offset += 4;
34
+ }
35
+
36
+ return out;
37
+ }
38
+
20
39
  export class UltraPlonkBackend {
21
40
  // These type assertions are used so that we don't
22
41
  // have to initialize `api` and `acirComposer` in the constructor.
@@ -159,11 +178,16 @@ export class UltraPlonkBackend {
159
178
  * Options for the UltraHonkBackend.
160
179
  */
161
180
  export type UltraHonkBackendOptions = {
162
- /**Selecting this option will use the keccak hash function instead of poseidon
181
+ /** Selecting this option will use the keccak hash function instead of poseidon
163
182
  * when generating challenges in the proof.
164
183
  * Use this when you want to verify the created proof on an EVM chain.
165
184
  */
166
- keccak: boolean;
185
+ keccak?: boolean;
186
+ /**S electing this option will use the poseidon/stark252 hash function instead of poseidon
187
+ * when generating challenges in the proof.
188
+ * Use this when you want to verify the created proof on an Starknet chain with Garaga.
189
+ */
190
+ starknet?: boolean;
167
191
  };
168
192
 
169
193
  export class UltraHonkBackend {
@@ -183,7 +207,7 @@ export class UltraHonkBackend {
183
207
  this.acirUncompressedBytecode = acirToUint8Array(acirBytecode);
184
208
  }
185
209
  /** @ignore */
186
- async instantiate(): Promise<void> {
210
+ private async instantiate(): Promise<void> {
187
211
  if (!this.api) {
188
212
  const api = await Barretenberg.new(this.backendOptions);
189
213
  const honkRecursion = true;
@@ -200,14 +224,18 @@ export class UltraHonkBackend {
200
224
 
201
225
  const proveUltraHonk = options?.keccak
202
226
  ? this.api.acirProveUltraKeccakHonk.bind(this.api)
203
- : this.api.acirProveUltraHonk.bind(this.api);
227
+ : options?.starknet
228
+ ? this.api.acirProveUltraStarknetHonk.bind(this.api)
229
+ : this.api.acirProveUltraHonk.bind(this.api);
204
230
 
205
231
  const proofWithPublicInputs = await proveUltraHonk(this.acirUncompressedBytecode, gunzip(compressedWitness));
206
232
 
207
233
  // Write VK to get the number of public inputs
208
234
  const writeVKUltraHonk = options?.keccak
209
235
  ? this.api.acirWriteVkUltraKeccakHonk.bind(this.api)
210
- : this.api.acirWriteVkUltraHonk.bind(this.api);
236
+ : options?.starknet
237
+ ? this.api.acirWriteVkUltraStarknetHonk.bind(this.api)
238
+ : this.api.acirWriteVkUltraHonk.bind(this.api);
211
239
 
212
240
  const vk = await writeVKUltraHonk(this.acirUncompressedBytecode);
213
241
  const vkAsFields = await this.api.acirVkAsFieldsUltraHonk(new RawBuffer(vk));
@@ -229,10 +257,14 @@ export class UltraHonkBackend {
229
257
 
230
258
  const writeVkUltraHonk = options?.keccak
231
259
  ? this.api.acirWriteVkUltraKeccakHonk.bind(this.api)
232
- : this.api.acirWriteVkUltraHonk.bind(this.api);
260
+ : options?.starknet
261
+ ? this.api.acirWriteVkUltraStarknetHonk.bind(this.api)
262
+ : this.api.acirWriteVkUltraHonk.bind(this.api);
233
263
  const verifyUltraHonk = options?.keccak
234
264
  ? this.api.acirVerifyUltraKeccakHonk.bind(this.api)
235
- : this.api.acirVerifyUltraHonk.bind(this.api);
265
+ : options?.starknet
266
+ ? this.api.acirVerifyUltraStarknetHonk.bind(this.api)
267
+ : this.api.acirVerifyUltraHonk.bind(this.api);
236
268
 
237
269
  const vkBuf = await writeVkUltraHonk(this.acirUncompressedBytecode);
238
270
  return await verifyUltraHonk(proof, new RawBuffer(vkBuf));
@@ -242,7 +274,9 @@ export class UltraHonkBackend {
242
274
  await this.instantiate();
243
275
  return options?.keccak
244
276
  ? await this.api.acirWriteVkUltraKeccakHonk(this.acirUncompressedBytecode)
245
- : await this.api.acirWriteVkUltraHonk(this.acirUncompressedBytecode);
277
+ : options?.starknet
278
+ ? await this.api.acirWriteVkUltraStarknetHonk(this.acirUncompressedBytecode)
279
+ : await this.api.acirWriteVkUltraHonk(this.acirUncompressedBytecode);
246
280
  }
247
281
 
248
282
  /** @description Returns a solidity verifier */
@@ -291,6 +325,35 @@ export class UltraHonkBackend {
291
325
  await this.api.destroy();
292
326
  }
293
327
  }
328
+ interface AztecClientExecutionStep {
329
+ functionName: string;
330
+ gateCount?: number;
331
+ // Note: not gzipped like in native code
332
+ bytecode: Uint8Array;
333
+ // Note: not gzipped like in native code. Already bincoded.
334
+ witness: Uint8Array;
335
+ /* TODO(https://github.com/AztecProtocol/barretenberg/issues/1328) this should get its own proper class. */
336
+ vk: Uint8Array;
337
+ }
338
+
339
+ function serializeAztecClientExecutionSteps(acirBuf: Uint8Array[], witnessBuf: Uint8Array[], vksBuf: Uint8Array[]): Uint8Array {
340
+ const steps: AztecClientExecutionStep[] = [];
341
+ for (let i = 0; i < acirBuf.length; i++) {
342
+ const bytecode = acirBuf[i];
343
+ // Witnesses are not provided at all for gates info.
344
+ const witness = witnessBuf[i] || Buffer.from([]);
345
+ // VKs are optional for proving (deprecated feature) or not provided at all for gates info.
346
+ const vk = vksBuf[i] || Buffer.from([]);
347
+ const functionName = `unknown_wasm_${i}`;
348
+ steps.push({
349
+ bytecode,
350
+ witness,
351
+ vk,
352
+ functionName,
353
+ });
354
+ }
355
+ return new Encoder({ useRecords: false }).pack(steps);
356
+ }
294
357
 
295
358
  export class AztecClientBackend {
296
359
  // These type assertions are used so that we don't
@@ -300,10 +363,10 @@ export class AztecClientBackend {
300
363
 
301
364
  protected api!: Barretenberg;
302
365
 
303
- constructor(protected acirMsgpack: Uint8Array[], protected options: BackendOptions = { threads: 1 }) {}
366
+ constructor(protected acirBuf: Uint8Array[], protected options: BackendOptions = { threads: 1 }) {}
304
367
 
305
368
  /** @ignore */
306
- async instantiate(): Promise<void> {
369
+ private async instantiate(): Promise<void> {
307
370
  if (!this.api) {
308
371
  const api = await Barretenberg.new(this.options);
309
372
  await api.initSRSClientIVC();
@@ -311,9 +374,17 @@ export class AztecClientBackend {
311
374
  }
312
375
  }
313
376
 
314
- async prove(witnessMsgpack: Uint8Array[]): Promise<[Uint8Array, Uint8Array]> {
377
+ async prove(witnessBuf: Uint8Array[], vksBuf: Uint8Array[] = []): Promise<[Uint8Array, Uint8Array]> {
378
+ if (vksBuf.length !== 0 && this.acirBuf.length !== witnessBuf.length) {
379
+ throw new AztecClientBackendError('Witness and bytecodes must have the same stack depth!');
380
+ }
381
+ if (vksBuf.length !== 0 && vksBuf.length !== witnessBuf.length) {
382
+ // NOTE: we allow 0 as an explicit 'I have no VKs'. This is a deprecated feature.
383
+ throw new AztecClientBackendError('Witness and VKs must have the same stack depth!');
384
+ }
315
385
  await this.instantiate();
316
- const proofAndVk = await this.api.acirProveAztecClient(this.acirMsgpack, witnessMsgpack);
386
+ const ivcInputsBuf = serializeAztecClientExecutionSteps(this.acirBuf, witnessBuf, vksBuf);
387
+ const proofAndVk = await this.api.acirProveAztecClient(ivcInputsBuf);
317
388
  const [proof, vk] = proofAndVk;
318
389
  if (!(await this.verify(proof, vk))) {
319
390
  throw new AztecClientBackendError('Failed to verify the private (ClientIVC) transaction proof!');
@@ -326,15 +397,12 @@ export class AztecClientBackend {
326
397
  return this.api.acirVerifyAztecClient(proof, vk);
327
398
  }
328
399
 
329
- async proveAndVerify(witnessMsgpack: Uint8Array[]): Promise<boolean> {
330
- await this.instantiate();
331
- return this.api.acirProveAndVerifyAztecClient(this.acirMsgpack, witnessMsgpack);
332
- }
333
-
334
400
  async gates(): Promise<number[]> {
335
401
  // call function on API
336
402
  await this.instantiate();
337
- return this.api.acirGatesAztecClient(this.acirMsgpack);
403
+ const ivcInputsBuf = serializeAztecClientExecutionSteps(this.acirBuf, [], []);
404
+ const resultBuffer = await this.api.acirGatesAztecClient(ivcInputsBuf);
405
+ return parseBigEndianU32Array(resultBuffer);
338
406
  }
339
407
 
340
408
  async destroy(): Promise<void> {