@aztec/foundation 2.1.0-rc.9 → 3.0.0-devnet.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 (87) hide show
  1. package/dest/config/env_var.d.ts +1 -1
  2. package/dest/config/env_var.d.ts.map +1 -1
  3. package/dest/config/network_name.d.ts +1 -1
  4. package/dest/config/network_name.d.ts.map +1 -1
  5. package/dest/config/network_name.js +6 -2
  6. package/dest/crypto/aes128/index.d.ts.map +1 -1
  7. package/dest/crypto/aes128/index.js +23 -6
  8. package/dest/crypto/ecdsa/index.d.ts.map +1 -1
  9. package/dest/crypto/ecdsa/index.js +66 -48
  10. package/dest/crypto/grumpkin/index.d.ts.map +1 -1
  11. package/dest/crypto/grumpkin/index.js +64 -43
  12. package/dest/crypto/keys/index.js +9 -4
  13. package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
  14. package/dest/crypto/pedersen/pedersen.wasm.js +29 -13
  15. package/dest/crypto/poseidon/index.d.ts.map +1 -1
  16. package/dest/crypto/poseidon/index.js +42 -17
  17. package/dest/crypto/schnorr/index.d.ts.map +1 -1
  18. package/dest/crypto/schnorr/index.js +35 -37
  19. package/dest/crypto/secp256k1/index.d.ts.map +1 -1
  20. package/dest/crypto/secp256k1/index.js +29 -18
  21. package/dest/crypto/secp256k1-signer/utils.d.ts +8 -0
  22. package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
  23. package/dest/crypto/secp256k1-signer/utils.js +14 -0
  24. package/dest/crypto/sync/index.js +3 -1
  25. package/dest/crypto/sync/pedersen/index.d.ts.map +1 -1
  26. package/dest/crypto/sync/pedersen/index.js +17 -10
  27. package/dest/crypto/sync/poseidon/index.d.ts.map +1 -1
  28. package/dest/crypto/sync/poseidon/index.js +27 -12
  29. package/dest/fields/bls12_point.d.ts +7 -7
  30. package/dest/fields/bls12_point.js +7 -7
  31. package/dest/fields/fields.d.ts.map +1 -1
  32. package/dest/fields/fields.js +9 -10
  33. package/dest/index.d.ts +1 -0
  34. package/dest/index.d.ts.map +1 -1
  35. package/dest/index.js +1 -0
  36. package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
  37. package/dest/json-rpc/client/safe_json_rpc_client.js +9 -0
  38. package/dest/log/pino-logger.d.ts.map +1 -1
  39. package/dest/log/pino-logger.js +0 -1
  40. package/dest/profiler/index.d.ts +2 -0
  41. package/dest/profiler/index.d.ts.map +1 -0
  42. package/dest/profiler/index.js +1 -0
  43. package/dest/profiler/profiler.d.ts +8 -0
  44. package/dest/profiler/profiler.d.ts.map +1 -0
  45. package/dest/profiler/profiler.js +97 -0
  46. package/dest/testing/formatting.d.ts +4 -0
  47. package/dest/testing/formatting.d.ts.map +1 -0
  48. package/dest/testing/formatting.js +3 -0
  49. package/dest/testing/index.d.ts +1 -0
  50. package/dest/testing/index.d.ts.map +1 -1
  51. package/dest/testing/index.js +1 -0
  52. package/dest/trees/unbalanced_merkle_tree.d.ts +0 -1
  53. package/dest/trees/unbalanced_merkle_tree.d.ts.map +1 -1
  54. package/dest/trees/unbalanced_merkle_tree.js +1 -1
  55. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +25 -22
  56. package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
  57. package/dest/trees/unbalanced_merkle_tree_calculator.js +124 -94
  58. package/dest/trees/unbalanced_tree_store.d.ts +1 -0
  59. package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
  60. package/dest/trees/unbalanced_tree_store.js +6 -0
  61. package/package.json +4 -3
  62. package/src/config/env_var.ts +2 -1
  63. package/src/config/network_name.ts +14 -3
  64. package/src/crypto/aes128/index.ts +19 -10
  65. package/src/crypto/ecdsa/index.ts +40 -37
  66. package/src/crypto/grumpkin/index.ts +29 -31
  67. package/src/crypto/keys/index.ts +5 -5
  68. package/src/crypto/pedersen/pedersen.wasm.ts +22 -18
  69. package/src/crypto/poseidon/index.ts +32 -24
  70. package/src/crypto/schnorr/index.ts +20 -17
  71. package/src/crypto/secp256k1/index.ts +15 -11
  72. package/src/crypto/secp256k1-signer/utils.ts +16 -0
  73. package/src/crypto/sync/index.ts +1 -1
  74. package/src/crypto/sync/pedersen/index.ts +16 -15
  75. package/src/crypto/sync/poseidon/index.ts +27 -22
  76. package/src/fields/bls12_point.ts +7 -7
  77. package/src/fields/fields.ts +5 -6
  78. package/src/index.ts +1 -0
  79. package/src/json-rpc/client/safe_json_rpc_client.ts +9 -0
  80. package/src/log/pino-logger.ts +0 -1
  81. package/src/profiler/index.ts +1 -0
  82. package/src/profiler/profiler.ts +125 -0
  83. package/src/testing/formatting.ts +3 -0
  84. package/src/testing/index.ts +1 -0
  85. package/src/trees/unbalanced_merkle_tree.ts +1 -1
  86. package/src/trees/unbalanced_merkle_tree_calculator.ts +140 -92
  87. package/src/trees/unbalanced_tree_store.ts +5 -1
@@ -1,4 +1,4 @@
1
- import { BarretenbergSync, RawBuffer } from '@aztec/bb.js';
1
+ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { Buffer } from 'buffer';
4
4
 
@@ -22,10 +22,15 @@ export class Aes128 {
22
22
  paddingBuffer.fill(numPaddingBytes);
23
23
  const input = Buffer.concat([data, paddingBuffer]);
24
24
 
25
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
26
- return Buffer.from(
27
- api.aesEncryptBufferCbc(new RawBuffer(input), new RawBuffer(iv), new RawBuffer(key), input.length),
28
- );
25
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
26
+ const api = BarretenbergSync.getSingleton();
27
+ const response = api.aesEncrypt({
28
+ plaintext: input,
29
+ iv,
30
+ key,
31
+ length: input.length,
32
+ });
33
+ return Buffer.from(response.ciphertext);
29
34
  }
30
35
 
31
36
  /**
@@ -37,11 +42,15 @@ export class Aes128 {
37
42
  * @returns Decrypted data.
38
43
  */
39
44
  public async decryptBufferCBCKeepPadding(data: Uint8Array, iv: Uint8Array, key: Uint8Array): Promise<Buffer> {
40
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
41
- const paddedBuffer = Buffer.from(
42
- api.aesDecryptBufferCbc(new RawBuffer(data), new RawBuffer(iv), new RawBuffer(key), data.length),
43
- );
44
- return paddedBuffer;
45
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
46
+ const api = BarretenbergSync.getSingleton();
47
+ const response = api.aesDecrypt({
48
+ ciphertext: data,
49
+ iv,
50
+ key,
51
+ length: data.length,
52
+ });
53
+ return Buffer.from(response.plaintext);
45
54
  }
46
55
 
47
56
  /**
@@ -1,7 +1,5 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
- import { numToInt32BE } from '@aztec/foundation/serialize';
3
2
 
4
- import { concatenateUint8Arrays } from '../serialize.js';
5
3
  import { EcdsaSignature } from './signature.js';
6
4
 
7
5
  export * from './signature.js';
@@ -18,11 +16,13 @@ export class Ecdsa {
18
16
  * @returns A secp256k1 public key.
19
17
  */
20
18
  public async computePublicKey(privateKey: Buffer): Promise<Buffer> {
21
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
22
- const [result] = api
23
- .getWasm()
24
- .callWasmExport(`ecdsa_${this.curve === 'secp256r1' ? 'r' : ''}_compute_public_key`, [privateKey], [64]);
25
- return Buffer.from(result);
19
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
20
+ const api = BarretenbergSync.getSingleton();
21
+ const response =
22
+ this.curve === 'secp256r1'
23
+ ? api.ecdsaSecp256r1ComputePublicKey({ privateKey })
24
+ : api.ecdsaSecp256k1ComputePublicKey({ privateKey });
25
+ return Buffer.concat([Buffer.from(response.publicKey.x), Buffer.from(response.publicKey.y)]);
26
26
  }
27
27
 
28
28
  /**
@@ -32,16 +32,13 @@ export class Ecdsa {
32
32
  * @returns An ECDSA signature of the form (r, s, v).
33
33
  */
34
34
  public async constructSignature(msg: Uint8Array, privateKey: Buffer) {
35
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
36
- const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]);
37
- const [r, s, v] = api
38
- .getWasm()
39
- .callWasmExport(
40
- `ecdsa_${this.curve === 'secp256r1' ? 'r' : ''}_construct_signature_`,
41
- [messageArray, privateKey],
42
- [32, 32, 1],
43
- );
44
- return new EcdsaSignature(Buffer.from(r), Buffer.from(s), Buffer.from(v));
35
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
36
+ const api = BarretenbergSync.getSingleton();
37
+ const response =
38
+ this.curve === 'secp256r1'
39
+ ? api.ecdsaSecp256r1ConstructSignature({ message: msg, privateKey })
40
+ : api.ecdsaSecp256k1ConstructSignature({ message: msg, privateKey });
41
+ return new EcdsaSignature(Buffer.from(response.r), Buffer.from(response.s), Buffer.from([response.v]));
45
42
  }
46
43
 
47
44
  /**
@@ -51,16 +48,13 @@ export class Ecdsa {
51
48
  * @returns The secp256k1 public key of the signer.
52
49
  */
53
50
  public async recoverPublicKey(msg: Uint8Array, sig: EcdsaSignature): Promise<Buffer> {
54
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
55
- const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]);
56
- const [result] = api
57
- .getWasm()
58
- .callWasmExport(
59
- `ecdsa_${this.curve === 'secp256r1' ? 'r' : ''}_recover_public_key_from_signature_`,
60
- [messageArray, sig.r, sig.s, sig.v],
61
- [64],
62
- );
63
- return Buffer.from(result);
51
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
52
+ const api = BarretenbergSync.getSingleton();
53
+ const response =
54
+ this.curve === 'secp256r1'
55
+ ? api.ecdsaSecp256r1RecoverPublicKey({ message: msg, r: sig.r, s: sig.s, v: sig.v[0] })
56
+ : api.ecdsaSecp256k1RecoverPublicKey({ message: msg, r: sig.r, s: sig.s, v: sig.v[0] });
57
+ return Buffer.concat([Buffer.from(response.publicKey.x), Buffer.from(response.publicKey.y)]);
64
58
  }
65
59
 
66
60
  /**
@@ -71,15 +65,24 @@ export class Ecdsa {
71
65
  * @returns True or false.
72
66
  */
73
67
  public async verifySignature(msg: Uint8Array, pubKey: Buffer, sig: EcdsaSignature) {
74
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
75
- const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]);
76
- const [result] = api
77
- .getWasm()
78
- .callWasmExport(
79
- `ecdsa_${this.curve === 'secp256r1' ? 'r' : ''}_verify_signature_`,
80
- [messageArray, pubKey, sig.r, sig.s, sig.v],
81
- [1],
82
- );
83
- return result[0] === 1;
68
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
69
+ const api = BarretenbergSync.getSingleton();
70
+ const response =
71
+ this.curve === 'secp256r1'
72
+ ? api.ecdsaSecp256r1VerifySignature({
73
+ message: msg,
74
+ publicKey: { x: pubKey.subarray(0, 32), y: pubKey.subarray(32, 64) },
75
+ r: sig.r,
76
+ s: sig.s,
77
+ v: sig.v[0],
78
+ })
79
+ : api.ecdsaSecp256k1VerifySignature({
80
+ message: msg,
81
+ publicKey: { x: pubKey.subarray(0, 32), y: pubKey.subarray(32, 64) },
82
+ r: sig.r,
83
+ s: sig.s,
84
+ v: sig.v[0],
85
+ });
86
+ return response.verified;
84
87
  }
85
88
  }
@@ -28,9 +28,13 @@ export class Grumpkin {
28
28
  * @returns Result of the multiplication.
29
29
  */
30
30
  public async mul(point: Point, scalar: GrumpkinScalar): Promise<Point> {
31
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
32
- const [result] = api.getWasm().callWasmExport('ecc_grumpkin__mul', [point.toBuffer(), scalar.toBuffer()], [64]);
33
- return Point.fromBuffer(Buffer.from(result));
31
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
32
+ const api = BarretenbergSync.getSingleton();
33
+ const response = api.grumpkinMul({
34
+ point: { x: point.x.toBuffer(), y: point.y.toBuffer() },
35
+ scalar: scalar.toBuffer(),
36
+ });
37
+ return Point.fromBuffer(Buffer.concat([Buffer.from(response.point.x), Buffer.from(response.point.y)]));
34
38
  }
35
39
 
36
40
  /**
@@ -40,9 +44,13 @@ export class Grumpkin {
40
44
  * @returns Result of the addition.
41
45
  */
42
46
  public async add(a: Point, b: Point): Promise<Point> {
43
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
44
- const [result] = api.getWasm().callWasmExport('ecc_grumpkin__add', [a.toBuffer(), b.toBuffer()], [64]);
45
- return Point.fromBuffer(Buffer.from(result));
47
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
48
+ const api = BarretenbergSync.getSingleton();
49
+ const response = api.grumpkinAdd({
50
+ pointA: { x: a.x.toBuffer(), y: a.y.toBuffer() },
51
+ pointB: { x: b.x.toBuffer(), y: b.y.toBuffer() },
52
+ });
53
+ return Point.fromBuffer(Buffer.concat([Buffer.from(response.point.x), Buffer.from(response.point.y)]));
46
54
  }
47
55
 
48
56
  /**
@@ -52,24 +60,14 @@ export class Grumpkin {
52
60
  * @returns Points multiplied by the scalar.
53
61
  */
54
62
  public async batchMul(points: Point[], scalar: GrumpkinScalar) {
55
- const concatenatedPoints: Buffer = Buffer.concat(points.map(point => point.toBuffer()));
63
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
64
+ const api = BarretenbergSync.getSingleton();
65
+ const response = api.grumpkinBatchMul({
66
+ points: points.map(p => ({ x: p.x.toBuffer(), y: p.y.toBuffer() })),
67
+ scalar: scalar.toBuffer(),
68
+ });
56
69
 
57
- const pointsByteLength = points.length * Point.SIZE_IN_BYTES;
58
-
59
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
60
- const [result] = api
61
- .getWasm()
62
- .callWasmExport(
63
- 'ecc_grumpkin__batch_mul',
64
- [concatenatedPoints, scalar.toBuffer(), points.length],
65
- [pointsByteLength],
66
- );
67
-
68
- const parsedResult: Point[] = [];
69
- for (let i = 0; i < pointsByteLength; i += 64) {
70
- parsedResult.push(Point.fromBuffer(Buffer.from(result.subarray(i, i + 64))));
71
- }
72
- return parsedResult;
70
+ return response.points.map(p => Point.fromBuffer(Buffer.concat([Buffer.from(p.x), Buffer.from(p.y)])));
73
71
  }
74
72
 
75
73
  /**
@@ -77,9 +75,10 @@ export class Grumpkin {
77
75
  * @returns Random field element.
78
76
  */
79
77
  public async getRandomFr(): Promise<Fr> {
80
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
81
- const [result] = api.getWasm().callWasmExport('ecc_grumpkin__get_random_scalar_mod_circuit_modulus', [], [32]);
82
- return Fr.fromBuffer(Buffer.from(result));
78
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
79
+ const api = BarretenbergSync.getSingleton();
80
+ const response = api.grumpkinGetRandomFr({ dummy: 0 });
81
+ return Fr.fromBuffer(Buffer.from(response.value));
83
82
  }
84
83
 
85
84
  /**
@@ -88,10 +87,9 @@ export class Grumpkin {
88
87
  * @returns Buffer representation of the field element.
89
88
  */
90
89
  public async reduce512BufferToFr(uint512Buf: Buffer): Promise<Fr> {
91
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
92
- const [result] = api
93
- .getWasm()
94
- .callWasmExport('ecc_grumpkin__reduce512_buffer_mod_circuit_modulus', [uint512Buf], [32]);
95
- return Fr.fromBuffer(Buffer.from(result));
90
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
91
+ const api = BarretenbergSync.getSingleton();
92
+ const response = api.grumpkinReduce512({ input: uint512Buf });
93
+ return Fr.fromBuffer(Buffer.from(response.value));
96
94
  }
97
95
  }
@@ -1,10 +1,10 @@
1
- import { BarretenbergSync, RawBuffer } from '@aztec/bb.js';
1
+ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { Fr } from '../../fields/fields.js';
4
4
 
5
5
  export async function vkAsFieldsMegaHonk(input: Buffer): Promise<Fr[]> {
6
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
7
- const result = api.acirVkAsFieldsMegaHonk(new RawBuffer(input));
8
-
9
- return result.map(bbFr => Fr.fromBuffer(Buffer.from(bbFr.toBuffer()))); // TODO(#4189): remove this conversion
6
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
7
+ const api = BarretenbergSync.getSingleton();
8
+ const response = api.megaVkAsFields({ verificationKey: input });
9
+ return response.fields.map(field => Fr.fromBuffer(Buffer.from(field)));
10
10
  }
@@ -1,4 +1,4 @@
1
- import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';
1
+ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { Fr } from '../../fields/fields.js';
4
4
  import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
@@ -12,14 +12,13 @@ export async function pedersenCommit(input: Buffer[], offset = 0) {
12
12
  throw new Error('All Pedersen Commit input buffers must be <= 32 bytes.');
13
13
  }
14
14
  input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
15
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
16
- const point = api.pedersenCommit(
17
- input.map(i => new FrBarretenberg(i)),
18
- offset,
19
- );
20
- // toBuffer returns Uint8Arrays (browser/worker-boundary friendly).
21
- // TODO: rename toTypedArray()?
22
- return [Buffer.from(point.x.toBuffer()), Buffer.from(point.y.toBuffer())];
15
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
16
+ const api = BarretenbergSync.getSingleton();
17
+ const response = api.pedersenCommit({
18
+ inputs: input,
19
+ hashIndex: offset,
20
+ });
21
+ return [Buffer.from(response.point.x), Buffer.from(response.point.y)];
23
22
  }
24
23
 
25
24
  /**
@@ -30,19 +29,24 @@ export async function pedersenCommit(input: Buffer[], offset = 0) {
30
29
  */
31
30
  export async function pedersenHash(input: Fieldable[], index = 0): Promise<Fr> {
32
31
  const inputFields = serializeToFields(input);
33
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
34
- const hash = api.pedersenHash(
35
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
36
- index,
37
- );
38
- return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
32
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
33
+ const api = BarretenbergSync.getSingleton();
34
+ const response = api.pedersenHash({
35
+ inputs: inputFields.map(i => i.toBuffer()),
36
+ hashIndex: index,
37
+ });
38
+ return Fr.fromBuffer(Buffer.from(response.hash));
39
39
  }
40
40
 
41
41
  /**
42
42
  * Create a pedersen hash from an arbitrary length buffer.
43
43
  */
44
44
  export async function pedersenHashBuffer(input: Buffer, index = 0) {
45
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
46
- const result = api.pedersenHashBuffer(input, index);
47
- return Buffer.from(result.toBuffer());
45
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
46
+ const api = BarretenbergSync.getSingleton();
47
+ const response = api.pedersenHashBuffer({
48
+ input,
49
+ hashIndex: index,
50
+ });
51
+ return Buffer.from(response.hash);
48
52
  }
@@ -1,4 +1,4 @@
1
- import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';
1
+ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { Fr } from '../../fields/fields.js';
4
4
  import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
@@ -10,11 +10,12 @@ import { type Fieldable, serializeToFields } from '../../serialize/serialize.js'
10
10
  */
11
11
  export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
12
12
  const inputFields = serializeToFields(input);
13
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
14
- const hash = api.poseidon2Hash(
15
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
16
- );
17
- return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
13
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
14
+ const api = BarretenbergSync.getSingleton();
15
+ const response = api.poseidon2Hash({
16
+ inputs: inputFields.map(i => i.toBuffer()),
17
+ });
18
+ return Fr.fromBuffer(Buffer.from(response.hash));
18
19
  }
19
20
 
20
21
  /**
@@ -26,19 +27,22 @@ export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
26
27
  export async function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Promise<Fr> {
27
28
  const inputFields = serializeToFields(input);
28
29
  inputFields.unshift(new Fr(separator));
29
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
30
-
31
- const hash = api.poseidon2Hash(
32
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
33
- );
34
- return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
30
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
31
+ const api = BarretenbergSync.getSingleton();
32
+ const response = api.poseidon2Hash({
33
+ inputs: inputFields.map(i => i.toBuffer()),
34
+ });
35
+ return Fr.fromBuffer(Buffer.from(response.hash));
35
36
  }
36
37
 
37
38
  export async function poseidon2HashAccumulate(input: Fieldable[]): Promise<Fr> {
38
39
  const inputFields = serializeToFields(input);
39
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
40
- const result = api.poseidon2HashAccumulate(inputFields.map(i => new FrBarretenberg(i.toBuffer())));
41
- return Fr.fromBuffer(Buffer.from(result.toBuffer()));
40
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
41
+ const api = BarretenbergSync.getSingleton();
42
+ const response = api.poseidon2HashAccumulate({
43
+ inputs: inputFields.map(i => i.toBuffer()),
44
+ });
45
+ return Fr.fromBuffer(Buffer.from(response.hash));
42
46
  }
43
47
 
44
48
  /**
@@ -50,11 +54,14 @@ export async function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]> {
50
54
  const inputFields = serializeToFields(input);
51
55
  // We'd like this assertion but it's not possible to use it in the browser.
52
56
  // assert(input.length === 4, 'Input state must be of size 4');
53
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
54
- const res = api.poseidon2Permutation(inputFields.map(i => new FrBarretenberg(i.toBuffer())));
57
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
58
+ const api = BarretenbergSync.getSingleton();
59
+ const response = api.poseidon2Permutation({
60
+ inputs: inputFields.map(i => i.toBuffer()),
61
+ });
55
62
  // We'd like this assertion but it's not possible to use it in the browser.
56
- // assert(res.length === 4, 'Output state must be of size 4');
57
- return res.map(o => Fr.fromBuffer(Buffer.from(o.toBuffer())));
63
+ // assert(response.outputs.length === 4, 'Output state must be of size 4');
64
+ return response.outputs.map(o => Fr.fromBuffer(Buffer.from(o)));
58
65
  }
59
66
 
60
67
  export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
@@ -68,10 +75,11 @@ export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
68
75
  inputFields.push(Fr.fromBuffer(fieldBytes));
69
76
  }
70
77
 
71
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
72
- const res = api.poseidon2Hash(
73
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
74
- );
78
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
79
+ const api = BarretenbergSync.getSingleton();
80
+ const response = api.poseidon2Hash({
81
+ inputs: inputFields.map(i => i.toBuffer()),
82
+ });
75
83
 
76
- return Fr.fromBuffer(Buffer.from(res.toBuffer()));
84
+ return Fr.fromBuffer(Buffer.from(response.hash));
77
85
  }
@@ -1,8 +1,6 @@
1
1
  import { BarretenbergSync } from '@aztec/bb.js';
2
2
  import { type GrumpkinScalar, Point } from '@aztec/foundation/fields';
3
- import { numToInt32BE } from '@aztec/foundation/serialize';
4
3
 
5
- import { concatenateUint8Arrays } from '../serialize.js';
6
4
  import { SchnorrSignature } from './signature.js';
7
5
 
8
6
  export * from './signature.js';
@@ -17,9 +15,10 @@ export class Schnorr {
17
15
  * @returns A grumpkin public key.
18
16
  */
19
17
  public async computePublicKey(privateKey: GrumpkinScalar): Promise<Point> {
20
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
21
- const [result] = api.getWasm().callWasmExport('schnorr_compute_public_key', [privateKey.toBuffer()], [64]);
22
- return Point.fromBuffer(Buffer.from(result));
18
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
19
+ const api = BarretenbergSync.getSingleton();
20
+ const response = api.schnorrComputePublicKey({ privateKey: privateKey.toBuffer() });
21
+ return Point.fromBuffer(Buffer.concat([Buffer.from(response.publicKey.x), Buffer.from(response.publicKey.y)]));
23
22
  }
24
23
 
25
24
  /**
@@ -29,12 +28,13 @@ export class Schnorr {
29
28
  * @returns A Schnorr signature of the form (s, e).
30
29
  */
31
30
  public async constructSignature(msg: Uint8Array, privateKey: GrumpkinScalar) {
32
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
33
- const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]);
34
- const [s, e] = api
35
- .getWasm()
36
- .callWasmExport('schnorr_construct_signature', [messageArray, privateKey.toBuffer()], [32, 32]);
37
- return new SchnorrSignature(Buffer.from([...s, ...e]));
31
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
32
+ const api = BarretenbergSync.getSingleton();
33
+ const response = api.schnorrConstructSignature({
34
+ message: msg,
35
+ privateKey: privateKey.toBuffer(),
36
+ });
37
+ return new SchnorrSignature(Buffer.from([...response.s, ...response.e]));
38
38
  }
39
39
 
40
40
  /**
@@ -45,11 +45,14 @@ export class Schnorr {
45
45
  * @returns True or false.
46
46
  */
47
47
  public async verifySignature(msg: Uint8Array, pubKey: Point, sig: SchnorrSignature) {
48
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
49
- const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]);
50
- const [result] = api
51
- .getWasm()
52
- .callWasmExport('schnorr_verify_signature', [messageArray, pubKey.toBuffer(), sig.s, sig.e], [1]);
53
- return result[0] === 1;
48
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
49
+ const api = BarretenbergSync.getSingleton();
50
+ const response = api.schnorrVerifySignature({
51
+ message: msg,
52
+ publicKey: { x: pubKey.x.toBuffer(), y: pubKey.y.toBuffer() },
53
+ s: sig.s,
54
+ e: sig.e,
55
+ });
56
+ return response.verified;
54
57
  }
55
58
  }
@@ -27,9 +27,13 @@ export class Secp256k1 {
27
27
  * @returns Result of the multiplication.
28
28
  */
29
29
  public async mul(point: Uint8Array, scalar: Uint8Array) {
30
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
31
- const [result] = api.getWasm().callWasmExport('ecc_secp256k1__mul', [point, scalar], [64]);
32
- return Buffer.from(result);
30
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
31
+ const api = BarretenbergSync.getSingleton();
32
+ const response = api.secp256k1Mul({
33
+ point: { x: point.subarray(0, 32), y: point.subarray(32, 64) },
34
+ scalar,
35
+ });
36
+ return Buffer.concat([Buffer.from(response.point.x), Buffer.from(response.point.y)]);
33
37
  }
34
38
 
35
39
  /**
@@ -37,9 +41,10 @@ export class Secp256k1 {
37
41
  * @returns Random field element.
38
42
  */
39
43
  public async getRandomFr() {
40
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
41
- const [result] = api.getWasm().callWasmExport('ecc_secp256k1__get_random_scalar_mod_circuit_modulus', [], [32]);
42
- return Buffer.from(result);
44
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
45
+ const api = BarretenbergSync.getSingleton();
46
+ const response = api.secp256k1GetRandomFr({ dummy: 0 });
47
+ return Buffer.from(response.value);
43
48
  }
44
49
 
45
50
  /**
@@ -48,10 +53,9 @@ export class Secp256k1 {
48
53
  * @returns Buffer representation of the field element.
49
54
  */
50
55
  public async reduce512BufferToFr(uint512Buf: Buffer) {
51
- const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
52
- const [result] = api
53
- .getWasm()
54
- .callWasmExport('ecc_secp256k1__reduce512_buffer_mod_circuit_modulus', [uint512Buf], [32]);
55
- return Buffer.from(result);
56
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
57
+ const api = BarretenbergSync.getSingleton();
58
+ const response = api.secp256k1Reduce512({ input: uint512Buf });
59
+ return Buffer.from(response.value);
56
60
  }
57
61
  }
@@ -47,6 +47,7 @@ export function addressFromPrivateKey(privateKey: Buffer): EthAddress {
47
47
  * @param hash - The hash to recover the address from.
48
48
  * @param signature - The signature to recover the address from.
49
49
  * @returns The address.
50
+ * @throws Error if signature recovery fails.
50
51
  */
51
52
  export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress {
52
53
  try {
@@ -59,6 +60,21 @@ export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress
59
60
  }
60
61
  }
61
62
 
63
+ /**
64
+ * Safely attempts to recover an address from a hash and a signature.
65
+ * @param hash - The hash to recover the address from.
66
+ * @param signature - The signature to recover the address from.
67
+ * @returns The address if recovery succeeds, undefined otherwise.
68
+ */
69
+ export function tryRecoverAddress(hash: Buffer32, signature: Signature): EthAddress | undefined {
70
+ try {
71
+ const publicKey = recoverPublicKey(hash, signature);
72
+ return publicKeyToAddress(publicKey);
73
+ } catch {
74
+ return undefined;
75
+ }
76
+ }
77
+
62
78
  /**
63
79
  * @attribution - viem
64
80
  * Converts a yParityOrV value to a recovery bit.
@@ -3,4 +3,4 @@ import { BarretenbergSync } from '@aztec/bb.js';
3
3
  export * from './poseidon/index.js';
4
4
  export * from './pedersen/index.js';
5
5
 
6
- await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
6
+ await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
@@ -1,4 +1,4 @@
1
- import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';
1
+ import { BarretenbergSync } from '@aztec/bb.js';
2
2
 
3
3
  import { Fr } from '../../../fields/fields.js';
4
4
  import { type Fieldable, serializeToFields } from '../../../serialize/serialize.js';
@@ -12,13 +12,11 @@ export function pedersenCommit(input: Buffer[], offset = 0) {
12
12
  throw new Error('All Pedersen Commit input buffers must be <= 32 bytes.');
13
13
  }
14
14
  input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
15
- const point = BarretenbergSync.getSingleton().pedersenCommit(
16
- input.map(i => new FrBarretenberg(i)),
17
- offset,
18
- );
19
- // toBuffer returns Uint8Arrays (browser/worker-boundary friendly).
20
- // TODO: rename toTypedArray()?
21
- return [Buffer.from(point.x.toBuffer()), Buffer.from(point.y.toBuffer())];
15
+ const response = BarretenbergSync.getSingleton().pedersenCommit({
16
+ inputs: input,
17
+ hashIndex: offset,
18
+ });
19
+ return [Buffer.from(response.point.x), Buffer.from(response.point.y)];
22
20
  }
23
21
 
24
22
  /**
@@ -29,17 +27,20 @@ export function pedersenCommit(input: Buffer[], offset = 0) {
29
27
  */
30
28
  export function pedersenHash(input: Fieldable[], index = 0): Fr {
31
29
  const inputFields = serializeToFields(input);
32
- const hash = BarretenbergSync.getSingleton().pedersenHash(
33
- inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
34
- index,
35
- );
36
- return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
30
+ const response = BarretenbergSync.getSingleton().pedersenHash({
31
+ inputs: inputFields.map(i => i.toBuffer()),
32
+ hashIndex: index,
33
+ });
34
+ return Fr.fromBuffer(Buffer.from(response.hash));
37
35
  }
38
36
 
39
37
  /**
40
38
  * Create a pedersen hash from an arbitrary length buffer.
41
39
  */
42
40
  export function pedersenHashBuffer(input: Buffer, index = 0) {
43
- const result = BarretenbergSync.getSingleton().pedersenHashBuffer(input, index);
44
- return Buffer.from(result.toBuffer());
41
+ const response = BarretenbergSync.getSingleton().pedersenHashBuffer({
42
+ input,
43
+ hashIndex: index,
44
+ });
45
+ return Buffer.from(response.hash);
45
46
  }