@aztec/bb.js 2.1.0-rc.9 → 2.1.2

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 (98) hide show
  1. package/dest/browser/barretenberg/index.d.ts +8 -1
  2. package/dest/browser/barretenberg/index.d.ts.map +1 -1
  3. package/dest/browser/barretenberg/index.js +18 -1
  4. package/dest/browser/barretenberg_wasm/fetch_code/browser/barretenberg-threads.js +1 -1
  5. package/dest/browser/barretenberg_wasm/fetch_code/browser/barretenberg.js +1 -1
  6. package/dest/browser/bbapi_exception.d.ts +7 -0
  7. package/dest/browser/bbapi_exception.d.ts.map +1 -0
  8. package/dest/browser/bbapi_exception.js +14 -0
  9. package/dest/browser/cbind/generate.js +93 -1
  10. package/dest/browser/cbind/generated/api_types.d.ts +242 -0
  11. package/dest/browser/cbind/generated/api_types.d.ts.map +1 -1
  12. package/dest/browser/cbind/generated/api_types.js +319 -1
  13. package/dest/browser/cbind/generated/async.d.ts +7 -1
  14. package/dest/browser/cbind/generated/async.d.ts.map +1 -1
  15. package/dest/browser/cbind/generated/async.js +135 -17
  16. package/dest/browser/cbind/generated/curve_constants.d.ts +45 -0
  17. package/dest/browser/cbind/generated/curve_constants.d.ts.map +1 -0
  18. package/dest/browser/cbind/generated/curve_constants.js +45 -0
  19. package/dest/browser/cbind/generated/native.d.ts +7 -1
  20. package/dest/browser/cbind/generated/native.d.ts.map +1 -1
  21. package/dest/browser/cbind/generated/native.js +56 -2
  22. package/dest/browser/cbind/generated/sync.d.ts +7 -1
  23. package/dest/browser/cbind/generated/sync.d.ts.map +1 -1
  24. package/dest/browser/cbind/generated/sync.js +129 -17
  25. package/dest/browser/cbind/schema_compiler.d.ts.map +1 -1
  26. package/dest/browser/cbind/schema_compiler.js +31 -7
  27. package/dest/browser/index.d.ts +4 -1
  28. package/dest/browser/index.d.ts.map +1 -1
  29. package/dest/browser/index.js +5 -2
  30. package/dest/node/barretenberg/index.d.ts +8 -1
  31. package/dest/node/barretenberg/index.d.ts.map +1 -1
  32. package/dest/node/barretenberg/index.js +18 -1
  33. package/dest/node/barretenberg_wasm/barretenberg-threads.wasm.gz +0 -0
  34. package/dest/node/bbapi_exception.d.ts +7 -0
  35. package/dest/node/bbapi_exception.d.ts.map +1 -0
  36. package/dest/node/bbapi_exception.js +14 -0
  37. package/dest/node/cbind/generate.js +93 -1
  38. package/dest/node/cbind/generated/api_types.d.ts +242 -0
  39. package/dest/node/cbind/generated/api_types.d.ts.map +1 -1
  40. package/dest/node/cbind/generated/api_types.js +319 -1
  41. package/dest/node/cbind/generated/async.d.ts +7 -1
  42. package/dest/node/cbind/generated/async.d.ts.map +1 -1
  43. package/dest/node/cbind/generated/async.js +135 -17
  44. package/dest/node/cbind/generated/curve_constants.d.ts +45 -0
  45. package/dest/node/cbind/generated/curve_constants.d.ts.map +1 -0
  46. package/dest/node/cbind/generated/curve_constants.js +45 -0
  47. package/dest/node/cbind/generated/native.d.ts +7 -1
  48. package/dest/node/cbind/generated/native.d.ts.map +1 -1
  49. package/dest/node/cbind/generated/native.js +56 -2
  50. package/dest/node/cbind/generated/sync.d.ts +7 -1
  51. package/dest/node/cbind/generated/sync.d.ts.map +1 -1
  52. package/dest/node/cbind/generated/sync.js +129 -17
  53. package/dest/node/cbind/schema_compiler.d.ts.map +1 -1
  54. package/dest/node/cbind/schema_compiler.js +31 -7
  55. package/dest/node/index.d.ts +4 -1
  56. package/dest/node/index.d.ts.map +1 -1
  57. package/dest/node/index.js +5 -2
  58. package/dest/node/main.d.ts +0 -10
  59. package/dest/node/main.d.ts.map +1 -1
  60. package/dest/node/main.js +469 -457
  61. package/dest/node-cjs/barretenberg/index.d.ts +8 -1
  62. package/dest/node-cjs/barretenberg/index.d.ts.map +1 -1
  63. package/dest/node-cjs/barretenberg/index.js +18 -1
  64. package/dest/node-cjs/barretenberg_wasm/barretenberg-threads.wasm.gz +0 -0
  65. package/dest/node-cjs/bbapi_exception.d.ts +7 -0
  66. package/dest/node-cjs/bbapi_exception.d.ts.map +1 -0
  67. package/dest/node-cjs/bbapi_exception.js +18 -0
  68. package/dest/node-cjs/cbind/generate.js +93 -1
  69. package/dest/node-cjs/cbind/generated/api_types.d.ts +242 -0
  70. package/dest/node-cjs/cbind/generated/api_types.d.ts.map +1 -1
  71. package/dest/node-cjs/cbind/generated/api_types.js +349 -1
  72. package/dest/node-cjs/cbind/generated/async.d.ts +7 -1
  73. package/dest/node-cjs/cbind/generated/async.d.ts.map +1 -1
  74. package/dest/node-cjs/cbind/generated/async.js +134 -16
  75. package/dest/node-cjs/cbind/generated/curve_constants.d.ts +45 -0
  76. package/dest/node-cjs/cbind/generated/curve_constants.d.ts.map +1 -0
  77. package/dest/node-cjs/cbind/generated/curve_constants.js +48 -0
  78. package/dest/node-cjs/cbind/generated/native.d.ts +7 -1
  79. package/dest/node-cjs/cbind/generated/native.d.ts.map +1 -1
  80. package/dest/node-cjs/cbind/generated/native.js +55 -1
  81. package/dest/node-cjs/cbind/generated/sync.d.ts +7 -1
  82. package/dest/node-cjs/cbind/generated/sync.d.ts.map +1 -1
  83. package/dest/node-cjs/cbind/generated/sync.js +128 -16
  84. package/dest/node-cjs/cbind/schema_compiler.d.ts.map +1 -1
  85. package/dest/node-cjs/cbind/schema_compiler.js +31 -7
  86. package/dest/node-cjs/index.d.ts +4 -1
  87. package/dest/node-cjs/index.d.ts.map +1 -1
  88. package/dest/node-cjs/index.js +24 -7
  89. package/dest/node-cjs/main.d.ts +0 -10
  90. package/dest/node-cjs/main.d.ts.map +1 -1
  91. package/dest/node-cjs/main.js +469 -466
  92. package/package.json +1 -1
  93. package/src/barretenberg/index.ts +20 -1
  94. package/src/bbapi_exception.ts +13 -0
  95. package/src/cbind/generate.ts +99 -0
  96. package/src/cbind/schema_compiler.ts +33 -6
  97. package/src/index.ts +26 -1
  98. package/src/main.ts +533 -532
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aztec/bb.js",
3
3
  "packageManager": "yarn@4.5.2",
4
- "version": "2.1.0-rc.9",
4
+ "version": "2.1.2",
5
5
  "homepage": "https://github.com/AztecProtocol/aztec-packages/tree/master/barretenberg/ts",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -8,7 +8,8 @@ import { RawBuffer } from '../types/raw_buffer.js';
8
8
  import { fetchModuleAndThreads } from '../barretenberg_wasm/index.js';
9
9
  import { createDebugLogger } from '../log/index.js';
10
10
  import { AsyncApi } from '../cbind/generated/async.js';
11
- import { BbApiBase, CircuitComputeVk, CircuitProve, CircuitVerify, ClientIvcAccumulate, ClientIvcComputeIvcVk, ClientIvcStats, ClientIvcLoad, ClientIvcProve, ClientIvcStart, ClientIvcVerify, VkAsFields } from '../cbind/generated/api_types.js';
11
+ import { BbApiBase, CircuitComputeVk, CircuitProve, CircuitVerify, ClientIvcAccumulate, ClientIvcComputeIvcVk, ClientIvcStats, ClientIvcLoad, ClientIvcProve, ClientIvcStart, ClientIvcVerify, VkAsFields, Bn254G1Mul, Bn254G2Mul, Bn254G1FromCompressed, Bn254G1IsOnCurve, Bn254FqSqrt } from '../cbind/generated/api_types.js';
12
+ import { SyncApi } from '../cbind/generated/sync.js';
12
13
 
13
14
  export { UltraHonkBackend, UltraHonkVerifierBackend, AztecClientBackend } from './backend.js';
14
15
 
@@ -171,8 +172,10 @@ let barretenbergSyncSingletonPromise: Promise<BarretenbergSync>;
171
172
  let barretenbergSyncSingleton: BarretenbergSync;
172
173
 
173
174
  export class BarretenbergSync extends BarretenbergApiSync {
175
+ api: SyncApi;
174
176
  private constructor(wasm: BarretenbergWasmMain) {
175
177
  super(wasm);
178
+ this.api = new SyncApi(wasm);
176
179
  }
177
180
 
178
181
  private static async new(wasmPath?: string, logger: (msg: string) => void = createDebugLogger('bb_wasm_sync')) {
@@ -198,6 +201,22 @@ export class BarretenbergSync extends BarretenbergApiSync {
198
201
  return barretenbergSyncSingleton;
199
202
  }
200
203
 
204
+ bn254G1Mul(command: Bn254G1Mul) {
205
+ return this.api.bn254G1Mul(command);
206
+ }
207
+ bn254G2Mul(command: Bn254G2Mul) {
208
+ return this.api.bn254G2Mul(command);
209
+ }
210
+ bn254G1IsOnCurve(command: Bn254G1IsOnCurve) {
211
+ return this.api.bn254G1IsOnCurve(command);
212
+ }
213
+ bn254G1FromCompressed(command: Bn254G1FromCompressed) {
214
+ return this.api.bn254G1FromCompressed(command);
215
+ }
216
+ bn254FqSqrt(command: Bn254FqSqrt) {
217
+ return this.api.bn254FqSqrt(command);
218
+ }
219
+
201
220
  getWasm() {
202
221
  return this.wasm;
203
222
  }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Exception thrown when barretenberg API operations fail
3
+ */
4
+ export class BBApiException extends Error {
5
+ constructor(message: string) {
6
+ super(message);
7
+ this.name = 'BBApiException';
8
+ // Maintains proper stack trace for where our error was thrown (only available on V8)
9
+ if (Error.captureStackTrace) {
10
+ Error.captureStackTrace(this, BBApiException);
11
+ }
12
+ }
13
+ }
@@ -7,6 +7,7 @@ import { dirname, join } from 'path';
7
7
  import { exec } from 'child_process';
8
8
  import { promisify } from 'util';
9
9
  import { fileURLToPath } from 'url';
10
+ import { unpack } from 'msgpackr';
10
11
  import {
11
12
  createSharedTypesCompiler,
12
13
  createSyncApiCompiler,
@@ -79,9 +80,107 @@ async function generate() {
79
80
  console.log(`✓ ${config.name}: ${outputPath}`);
80
81
  }
81
82
 
83
+ // Generate curve constants
84
+ console.log('\nGenerating curve constants...');
85
+ await generateCurveConstants(bbBuildPath, outputDir);
86
+
82
87
  console.log('\nGeneration complete!');
83
88
  }
84
89
 
90
+ async function generateCurveConstants(bbBuildPath: string, outputDir: string) {
91
+ // Get curve constants from bb as msgpack binary
92
+ const { stdout: constantsBuffer } = await execAsync(`${bbBuildPath} msgpack curve_constants`, {
93
+ encoding: 'buffer',
94
+ maxBuffer: 10 * 1024 * 1024, // 10MB buffer
95
+ });
96
+
97
+ // Decode msgpack
98
+ const constants = unpack(constantsBuffer as Buffer);
99
+
100
+ // Helper to convert Uint8Array to hex string
101
+ const toHex = (bytes: Uint8Array) => '0x' + Buffer.from(bytes).toString('hex');
102
+
103
+ // Helper to convert Uint8Array to bigint (big-endian)
104
+ const toBigInt = (bytes: Uint8Array) => {
105
+ let result = 0n;
106
+ for (const byte of bytes) {
107
+ result = (result << 8n) | BigInt(byte);
108
+ }
109
+ return result;
110
+ };
111
+
112
+ // Helper to serialize point coordinate (handles both Uint8Array and array of Uint8Array for field2)
113
+ const serializeCoordinate = (coord: Uint8Array | Uint8Array[]) => {
114
+ if (Array.isArray(coord)) {
115
+ // For field2 (like BN254 G2), we have array of two Uint8Arrays
116
+ return `[${coord.map(c => `new Uint8Array([${Array.from(c).join(', ')}])`).join(', ')}]`;
117
+ } else {
118
+ // For regular fields, single Uint8Array
119
+ return `new Uint8Array([${Array.from(coord).join(', ')}])`;
120
+ }
121
+ };
122
+
123
+ // Generate TypeScript file
124
+ const content = `/**
125
+ * Curve constants generated from barretenberg native binary.
126
+ * DO NOT EDIT - This file is auto-generated by generate.ts
127
+ */
128
+
129
+ /**
130
+ * BN254 curve constants
131
+ */
132
+ export const BN254_FR_MODULUS = ${toBigInt(constants.bn254_fr_modulus)}n;
133
+ export const BN254_FQ_MODULUS = ${toBigInt(constants.bn254_fq_modulus)}n;
134
+
135
+ export const BN254_G1_GENERATOR = {
136
+ x: ${serializeCoordinate(constants.bn254_g1_generator.x)},
137
+ y: ${serializeCoordinate(constants.bn254_g1_generator.y)},
138
+ } as const;
139
+
140
+ export const BN254_G2_GENERATOR = {
141
+ x: ${serializeCoordinate(constants.bn254_g2_generator.x)},
142
+ y: ${serializeCoordinate(constants.bn254_g2_generator.y)},
143
+ } as const;
144
+
145
+ /**
146
+ * Grumpkin curve constants
147
+ */
148
+ export const GRUMPKIN_FR_MODULUS = ${toBigInt(constants.grumpkin_fr_modulus)}n;
149
+ export const GRUMPKIN_FQ_MODULUS = ${toBigInt(constants.grumpkin_fq_modulus)}n;
150
+
151
+ export const GRUMPKIN_G1_GENERATOR = {
152
+ x: ${serializeCoordinate(constants.grumpkin_g1_generator.x)},
153
+ y: ${serializeCoordinate(constants.grumpkin_g1_generator.y)},
154
+ } as const;
155
+
156
+ /**
157
+ * Secp256k1 curve constants
158
+ */
159
+ export const SECP256K1_FR_MODULUS = ${toBigInt(constants.secp256k1_fr_modulus)}n;
160
+ export const SECP256K1_FQ_MODULUS = ${toBigInt(constants.secp256k1_fq_modulus)}n;
161
+
162
+ export const SECP256K1_G1_GENERATOR = {
163
+ x: ${serializeCoordinate(constants.secp256k1_g1_generator.x)},
164
+ y: ${serializeCoordinate(constants.secp256k1_g1_generator.y)},
165
+ } as const;
166
+
167
+ /**
168
+ * Secp256r1 curve constants
169
+ */
170
+ export const SECP256R1_FR_MODULUS = ${toBigInt(constants.secp256r1_fr_modulus)}n;
171
+ export const SECP256R1_FQ_MODULUS = ${toBigInt(constants.secp256r1_fq_modulus)}n;
172
+
173
+ export const SECP256R1_G1_GENERATOR = {
174
+ x: ${serializeCoordinate(constants.secp256r1_g1_generator.x)},
175
+ y: ${serializeCoordinate(constants.secp256r1_g1_generator.y)},
176
+ } as const;
177
+ `;
178
+
179
+ const outputPath = join(outputDir, 'curve_constants.ts');
180
+ writeFileSync(outputPath, content);
181
+ console.log(`✓ Curve constants: ${outputPath}`);
182
+ }
183
+
85
184
  // Run the generator
86
185
  generate().catch(error => {
87
186
  console.error('Generation failed:', error);
@@ -80,9 +80,16 @@ export class SchemaCompiler {
80
80
  const commands = commandsSchema[1] as Array<[string, Schema]>;
81
81
  const responses = responsesSchema[1] as Array<[string, Schema]>;
82
82
 
83
+ // Filter out ErrorResponse - it's a special error variant, not a command response
84
+ const normalResponses = responses.filter(([name]) => name !== 'ErrorResponse');
85
+
86
+ if (commands.length !== normalResponses.length) {
87
+ throw new Error(`Command count (${commands.length}) does not match response count (${normalResponses.length})`);
88
+ }
89
+
83
90
  for (let i = 0; i < commands.length; i++) {
84
91
  const [commandName] = commands[i];
85
- const [responseName] = responses[i];
92
+ const [responseName] = normalResponses[i];
86
93
 
87
94
  this.functionMetadata.push({
88
95
  name: camelCase(commandName),
@@ -296,6 +303,9 @@ export class SchemaCompiler {
296
303
  switch (type) {
297
304
  case 'array': {
298
305
  const [subtype, size] = args[0];
306
+ if (subtype === 'unsigned char') {
307
+ return { typeName: 'Uint8Array' };
308
+ }
299
309
  const subtypeInfo = this.processSchema(subtype);
300
310
  return {
301
311
  typeName: `Tuple<${subtypeInfo.typeName}, ${size}>`,
@@ -398,6 +408,13 @@ export class SchemaCompiler {
398
408
  return { typeName: 'string' };
399
409
  case 'bin32':
400
410
  return { typeName: 'Uint8Array' };
411
+ case 'field2':
412
+ // field2 is an extension field type (fq2) represented as a tuple of two Uint8Arrays
413
+ return {
414
+ typeName: 'Field2',
415
+ msgpackTypeName: '[Uint8Array, Uint8Array]',
416
+ declaration: 'export type Field2 = [Uint8Array, Uint8Array];',
417
+ };
401
418
  default:
402
419
  return { typeName: pascalCase(schema) };
403
420
  }
@@ -532,7 +549,7 @@ ${conversions}
532
549
  }
533
550
 
534
551
  // Handle custom types
535
- if (typeInfo.declaration) {
552
+ if (typeInfo.declaration && typeInfo.typeName !== 'Fr' && typeInfo.typeName !== 'Fq' && typeInfo.typeName !== 'Field2') {
536
553
  return `${direction}${typeInfo.typeName}(${value})`;
537
554
  }
538
555
 
@@ -656,8 +673,11 @@ ${methods}
656
673
  return ` ${name}(command: ${commandType}): Promise<${responseType}> {
657
674
  const msgpackCommand = from${commandType}(command);
658
675
  return msgpackCall(this.wasm, 'bbapi', [["${capitalize(name)}", msgpackCommand]]).then(([variantName, result]: [string, any]) => {
676
+ if (variantName === 'ErrorResponse') {
677
+ throw new BBApiException(result.message || 'Unknown error from barretenberg');
678
+ }
659
679
  if (variantName !== '${responseType}') {
660
- throw new Error(\`Expected variant name '${responseType}' but got '\${variantName}'\`);
680
+ throw new BBApiException(\`Expected variant name '${responseType}' but got '\${variantName}'\`);
661
681
  }
662
682
  return to${responseType}(result);
663
683
  });
@@ -668,8 +688,11 @@ ${methods}
668
688
  return ` ${name}(command: ${commandType}): ${responseType} {
669
689
  const msgpackCommand = from${commandType}(command);
670
690
  const [variantName, result] = msgpackCall(this.wasm, 'bbapi', [["${capitalize(name)}", msgpackCommand]]);
691
+ if (variantName === 'ErrorResponse') {
692
+ throw new BBApiException(result.message || 'Unknown error from barretenberg');
693
+ }
671
694
  if (variantName !== '${responseType}') {
672
- throw new Error(\`Expected variant name '${responseType}' but got '\${variantName}'\`);
695
+ throw new BBApiException(\`Expected variant name '${responseType}' but got '\${variantName}'\`);
673
696
  }
674
697
  return to${responseType}(result);
675
698
  }`;
@@ -803,11 +826,13 @@ export function createSharedTypesCompiler(): SchemaCompiler {
803
826
  }
804
827
 
805
828
  export function createSyncApiCompiler(): SchemaCompiler {
829
+
806
830
  return new SchemaCompiler({
807
831
  mode: 'sync',
808
832
  imports: [
809
833
  `import { BarretenbergWasmMain } from "../../barretenberg_wasm/barretenberg_wasm_main/index.js";`,
810
834
  `import { Decoder, Encoder } from 'msgpackr';`,
835
+ `import { BBApiException } from '../../bbapi_exception.js';`,
811
836
  ],
812
837
  });
813
838
  }
@@ -817,7 +842,8 @@ export function createAsyncApiCompiler(): SchemaCompiler {
817
842
  mode: 'async',
818
843
  imports: [
819
844
  `import { BarretenbergWasmMainWorker } from "../../barretenberg_wasm/barretenberg_wasm_main/index.js";`,
820
- `import { Decoder, Encoder } from 'msgpackr';`
845
+ `import { Decoder, Encoder } from 'msgpackr';`,
846
+ `import { BBApiException } from '../../bbapi_exception.js';`
821
847
  ],
822
848
  });
823
849
  }
@@ -827,7 +853,8 @@ export function createNativeApiCompiler(): SchemaCompiler {
827
853
  mode: 'native',
828
854
  imports: [
829
855
  `import { spawn, ChildProcess } from 'child_process';`,
830
- `import { Decoder, Encoder } from 'msgpackr';`
856
+ `import { Decoder, Encoder } from 'msgpackr';`,
857
+ `import { BBApiException } from '../../bbapi_exception.js';`
831
858
  ],
832
859
  });
833
860
  }
package/src/index.ts CHANGED
@@ -8,6 +8,31 @@ export {
8
8
  AztecClientBackend,
9
9
  } from './barretenberg/index.js';
10
10
 
11
- export { randomBytes } from './random/index.js';
12
11
  export { RawBuffer, Fr } from './types/index.js';
12
+ export { randomBytes } from './random/index.js';
13
13
  export { splitHonkProof, reconstructHonkProof, deflattenFields, type ProofData } from './proof/index.js';
14
+ export { BBApiException } from './bbapi_exception.js';
15
+
16
+ // Export Point types for use in foundation and other packages
17
+ export type {
18
+ Bn254G1Point,
19
+ Bn254G2Point,
20
+ Field2,
21
+ } from './cbind/generated/api_types.js';
22
+
23
+ // Export curve constants for use in foundation
24
+ export {
25
+ BN254_FQ_MODULUS,
26
+ BN254_FR_MODULUS,
27
+ BN254_G1_GENERATOR,
28
+ BN254_G2_GENERATOR,
29
+ GRUMPKIN_FR_MODULUS,
30
+ GRUMPKIN_FQ_MODULUS,
31
+ GRUMPKIN_G1_GENERATOR,
32
+ SECP256K1_FR_MODULUS,
33
+ SECP256K1_FQ_MODULUS,
34
+ SECP256K1_G1_GENERATOR,
35
+ SECP256R1_FR_MODULUS,
36
+ SECP256R1_FQ_MODULUS,
37
+ SECP256R1_G1_GENERATOR,
38
+ } from './cbind/generated/curve_constants.js';