@aztec/bb.js 0.0.1-alpha.0

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 (206) hide show
  1. package/README.md +3 -0
  2. package/dest/async_map/index.d.ts +10 -0
  3. package/dest/async_map/index.d.ts.map +1 -0
  4. package/dest/async_map/index.js +16 -0
  5. package/dest/barretenberg-threads.wasm +0 -0
  6. package/dest/barretenberg.wasm +0 -0
  7. package/dest/barretenberg_api/blake2s.test.d.ts +2 -0
  8. package/dest/barretenberg_api/blake2s.test.d.ts.map +1 -0
  9. package/dest/barretenberg_api/blake2s.test.js +30 -0
  10. package/dest/barretenberg_api/common.test.d.ts +2 -0
  11. package/dest/barretenberg_api/common.test.d.ts.map +1 -0
  12. package/dest/barretenberg_api/common.test.js +18 -0
  13. package/dest/barretenberg_api/index.d.ts +101 -0
  14. package/dest/barretenberg_api/index.d.ts.map +1 -0
  15. package/dest/barretenberg_api/index.js +371 -0
  16. package/dest/barretenberg_api/pedersen.test.d.ts +2 -0
  17. package/dest/barretenberg_api/pedersen.test.d.ts.map +1 -0
  18. package/dest/barretenberg_api/pedersen.test.js +69 -0
  19. package/dest/barretenberg_api/schnorr.test.d.ts +2 -0
  20. package/dest/barretenberg_api/schnorr.test.d.ts.map +1 -0
  21. package/dest/barretenberg_api/schnorr.test.js +113 -0
  22. package/dest/barretenberg_binder/heap_allocator.d.ts +22 -0
  23. package/dest/barretenberg_binder/heap_allocator.d.ts.map +1 -0
  24. package/dest/barretenberg_binder/heap_allocator.js +59 -0
  25. package/dest/barretenberg_binder/heap_allocator_sync.d.ts +22 -0
  26. package/dest/barretenberg_binder/heap_allocator_sync.d.ts.map +1 -0
  27. package/dest/barretenberg_binder/heap_allocator_sync.js +58 -0
  28. package/dest/barretenberg_binder/index.d.ts +32 -0
  29. package/dest/barretenberg_binder/index.d.ts.map +1 -0
  30. package/dest/barretenberg_binder/index.js +73 -0
  31. package/dest/barretenberg_wasm/barretenberg_wasm.d.ts +49 -0
  32. package/dest/barretenberg_wasm/barretenberg_wasm.d.ts.map +1 -0
  33. package/dest/barretenberg_wasm/barretenberg_wasm.js +190 -0
  34. package/dest/barretenberg_wasm/barretenberg_wasm.test.d.ts +2 -0
  35. package/dest/barretenberg_wasm/barretenberg_wasm.test.d.ts.map +1 -0
  36. package/dest/barretenberg_wasm/barretenberg_wasm.test.js +43 -0
  37. package/dest/barretenberg_wasm/browser/index.d.ts +8 -0
  38. package/dest/barretenberg_wasm/browser/index.d.ts.map +1 -0
  39. package/dest/barretenberg_wasm/browser/index.js +26 -0
  40. package/dest/barretenberg_wasm/browser/worker.d.ts +2 -0
  41. package/dest/barretenberg_wasm/browser/worker.d.ts.map +1 -0
  42. package/dest/barretenberg_wasm/browser/worker.js +11 -0
  43. package/dest/barretenberg_wasm/index.d.ts +2 -0
  44. package/dest/barretenberg_wasm/index.d.ts.map +1 -0
  45. package/dest/barretenberg_wasm/index.js +2 -0
  46. package/dest/barretenberg_wasm/node/index.d.ts +17 -0
  47. package/dest/barretenberg_wasm/node/index.d.ts.map +1 -0
  48. package/dest/barretenberg_wasm/node/index.js +40 -0
  49. package/dest/barretenberg_wasm/node/node_endpoint.d.ts +8 -0
  50. package/dest/barretenberg_wasm/node/node_endpoint.d.ts.map +1 -0
  51. package/dest/barretenberg_wasm/node/node_endpoint.js +28 -0
  52. package/dest/barretenberg_wasm/node/worker.d.ts +2 -0
  53. package/dest/barretenberg_wasm/node/worker.d.ts.map +1 -0
  54. package/dest/barretenberg_wasm/node/worker.js +9 -0
  55. package/dest/barretenberg_wasm.js +2 -0
  56. package/dest/barretenberg_wasm.js.LICENSE.txt +5 -0
  57. package/dest/bigint-array/index.d.ts +3 -0
  58. package/dest/bigint-array/index.d.ts.map +1 -0
  59. package/dest/bigint-array/index.js +21 -0
  60. package/dest/bindgen/function_declaration.d.ts +11 -0
  61. package/dest/bindgen/function_declaration.d.ts.map +1 -0
  62. package/dest/bindgen/function_declaration.js +2 -0
  63. package/dest/bindgen/index.d.ts +2 -0
  64. package/dest/bindgen/index.d.ts.map +1 -0
  65. package/dest/bindgen/index.js +15 -0
  66. package/dest/bindgen/mappings.d.ts +4 -0
  67. package/dest/bindgen/mappings.d.ts.map +1 -0
  68. package/dest/bindgen/mappings.js +63 -0
  69. package/dest/bindgen/rust.d.ts +2 -0
  70. package/dest/bindgen/rust.d.ts.map +1 -0
  71. package/dest/bindgen/rust.js +43 -0
  72. package/dest/bindgen/to_camel_case.d.ts +2 -0
  73. package/dest/bindgen/to_camel_case.d.ts.map +1 -0
  74. package/dest/bindgen/to_camel_case.js +11 -0
  75. package/dest/bindgen/typescript.d.ts +2 -0
  76. package/dest/bindgen/typescript.d.ts.map +1 -0
  77. package/dest/bindgen/typescript.js +80 -0
  78. package/dest/crs/browser/cached_net_crs.d.ts +25 -0
  79. package/dest/crs/browser/cached_net_crs.d.ts.map +1 -0
  80. package/dest/crs/browser/cached_net_crs.js +54 -0
  81. package/dest/crs/browser/index.d.ts +2 -0
  82. package/dest/crs/browser/index.d.ts.map +1 -0
  83. package/dest/crs/browser/index.js +2 -0
  84. package/dest/crs/index.d.ts +2 -0
  85. package/dest/crs/index.d.ts.map +1 -0
  86. package/dest/crs/index.js +2 -0
  87. package/dest/crs/net_crs.d.ts +36 -0
  88. package/dest/crs/net_crs.d.ts.map +1 -0
  89. package/dest/crs/net_crs.js +59 -0
  90. package/dest/crs/node/file_crs.d.ts +37 -0
  91. package/dest/crs/node/file_crs.d.ts.map +1 -0
  92. package/dest/crs/node/file_crs.js +51 -0
  93. package/dest/crs/node/index.d.ts +31 -0
  94. package/dest/crs/node/index.d.ts.map +1 -0
  95. package/dest/crs/node/index.js +41 -0
  96. package/dest/examples/simple.rawtest.d.ts +2 -0
  97. package/dest/examples/simple.rawtest.d.ts.map +1 -0
  98. package/dest/examples/simple.rawtest.js +24 -0
  99. package/dest/examples/simple.test.d.ts +2 -0
  100. package/dest/examples/simple.test.d.ts.map +1 -0
  101. package/dest/examples/simple.test.js +22 -0
  102. package/dest/factory/index.d.ts +21 -0
  103. package/dest/factory/index.d.ts.map +1 -0
  104. package/dest/factory/index.js +34 -0
  105. package/dest/index.d.ts +4 -0
  106. package/dest/index.d.ts.map +1 -0
  107. package/dest/index.html +1 -0
  108. package/dest/index.js +4 -0
  109. package/dest/main-dev.d.ts +3 -0
  110. package/dest/main-dev.d.ts.map +1 -0
  111. package/dest/main-dev.js +3 -0
  112. package/dest/main.d.ts +10 -0
  113. package/dest/main.d.ts.map +1 -0
  114. package/dest/main.js +288 -0
  115. package/dest/random/browser/index.d.ts +2 -0
  116. package/dest/random/browser/index.d.ts.map +1 -0
  117. package/dest/random/browser/index.js +31 -0
  118. package/dest/random/index.d.ts +2 -0
  119. package/dest/random/index.d.ts.map +1 -0
  120. package/dest/random/index.js +2 -0
  121. package/dest/random/node/index.d.ts +2 -0
  122. package/dest/random/node/index.d.ts.map +1 -0
  123. package/dest/random/node/index.js +5 -0
  124. package/dest/serialize/buffer_reader.d.ts +28 -0
  125. package/dest/serialize/buffer_reader.d.ts.map +1 -0
  126. package/dest/serialize/buffer_reader.js +66 -0
  127. package/dest/serialize/index.d.ts +4 -0
  128. package/dest/serialize/index.d.ts.map +1 -0
  129. package/dest/serialize/index.js +4 -0
  130. package/dest/serialize/output_type.d.ts +11 -0
  131. package/dest/serialize/output_type.d.ts.map +1 -0
  132. package/dest/serialize/output_type.js +44 -0
  133. package/dest/serialize/serialize.d.ts +53 -0
  134. package/dest/serialize/serialize.d.ts.map +1 -0
  135. package/dest/serialize/serialize.js +139 -0
  136. package/dest/simple_test.js +2 -0
  137. package/dest/simple_test.js.LICENSE.txt +14 -0
  138. package/dest/types/fields.d.ts +33 -0
  139. package/dest/types/fields.d.ts.map +1 -0
  140. package/dest/types/fields.js +86 -0
  141. package/dest/types/fixed_size_buffer.d.ts +26 -0
  142. package/dest/types/fixed_size_buffer.d.ts.map +1 -0
  143. package/dest/types/fixed_size_buffer.js +54 -0
  144. package/dest/types/index.d.ts +6 -0
  145. package/dest/types/index.d.ts.map +1 -0
  146. package/dest/types/index.js +6 -0
  147. package/dest/types/point.d.ts +17 -0
  148. package/dest/types/point.d.ts.map +1 -0
  149. package/dest/types/point.js +32 -0
  150. package/dest/types/ptr.d.ts +13 -0
  151. package/dest/types/ptr.d.ts.map +1 -0
  152. package/dest/types/ptr.js +20 -0
  153. package/dest/types/raw_buffer.d.ts +3 -0
  154. package/dest/types/raw_buffer.d.ts.map +1 -0
  155. package/dest/types/raw_buffer.js +5 -0
  156. package/package.json +79 -0
  157. package/src/async_map/index.ts +15 -0
  158. package/src/barretenberg_api/blake2s.test.ts +39 -0
  159. package/src/barretenberg_api/common.test.ts +21 -0
  160. package/src/barretenberg_api/index.ts +461 -0
  161. package/src/barretenberg_api/pedersen.test.ts +84 -0
  162. package/src/barretenberg_api/schnorr.test.ts +169 -0
  163. package/src/barretenberg_binder/heap_allocator.ts +62 -0
  164. package/src/barretenberg_binder/heap_allocator_sync.ts +61 -0
  165. package/src/barretenberg_binder/index.ts +76 -0
  166. package/src/barretenberg_wasm/barretenberg_wasm.test.ts +52 -0
  167. package/src/barretenberg_wasm/barretenberg_wasm.ts +222 -0
  168. package/src/barretenberg_wasm/browser/index.ts +32 -0
  169. package/src/barretenberg_wasm/browser/worker.ts +13 -0
  170. package/src/barretenberg_wasm/index.ts +1 -0
  171. package/src/barretenberg_wasm/node/index.ts +46 -0
  172. package/src/barretenberg_wasm/node/node_endpoint.ts +28 -0
  173. package/src/barretenberg_wasm/node/worker.ts +10 -0
  174. package/src/bigint-array/index.ts +21 -0
  175. package/src/bindgen/function_declaration.ts +11 -0
  176. package/src/bindgen/index.ts +17 -0
  177. package/src/bindgen/mappings.ts +66 -0
  178. package/src/bindgen/rust.ts +52 -0
  179. package/src/bindgen/to_camel_case.ts +10 -0
  180. package/src/bindgen/typescript.ts +91 -0
  181. package/src/crs/browser/cached_net_crs.ts +60 -0
  182. package/src/crs/browser/index.ts +1 -0
  183. package/src/crs/index.ts +1 -0
  184. package/src/crs/net_crs.ts +69 -0
  185. package/src/crs/node/file_crs.ts +60 -0
  186. package/src/crs/node/index.ts +48 -0
  187. package/src/examples/simple.rawtest.ts +32 -0
  188. package/src/examples/simple.test.ts +27 -0
  189. package/src/factory/index.ts +36 -0
  190. package/src/index.html +9 -0
  191. package/src/index.ts +3 -0
  192. package/src/main-dev.ts +2 -0
  193. package/src/main.ts +329 -0
  194. package/src/random/browser/index.ts +32 -0
  195. package/src/random/index.ts +1 -0
  196. package/src/random/node/index.ts +5 -0
  197. package/src/serialize/buffer_reader.ts +82 -0
  198. package/src/serialize/index.ts +3 -0
  199. package/src/serialize/output_type.ts +53 -0
  200. package/src/serialize/serialize.ts +157 -0
  201. package/src/types/fields.ts +98 -0
  202. package/src/types/fixed_size_buffer.ts +59 -0
  203. package/src/types/index.ts +5 -0
  204. package/src/types/point.ts +35 -0
  205. package/src/types/ptr.ts +20 -0
  206. package/src/types/raw_buffer.ts +3 -0
@@ -0,0 +1,169 @@
1
+ import { TextEncoder } from 'util';
2
+ import { Buffer128, Buffer32, Fr, Point } from '../types/index.js';
3
+ import { BarretenbergApiSync } from './index.js';
4
+ import { newBarretenbergApiSync } from '../factory/index.js';
5
+
6
+ describe('schnorr', () => {
7
+ const msg = Buffer.from(new TextEncoder().encode('The quick brown dog jumped over the lazy fox.'));
8
+ let api: BarretenbergApiSync;
9
+
10
+ beforeAll(async () => {
11
+ api = await newBarretenbergApiSync();
12
+ api.pedersenInit();
13
+ });
14
+
15
+ afterAll(async () => {
16
+ await api.destroy();
17
+ });
18
+
19
+ it('should verify signature', () => {
20
+ const pk = Fr.fromBuffer(
21
+ new Uint8Array([
22
+ 0x0b, 0x9b, 0x3a, 0xde, 0xe6, 0xb3, 0xd8, 0x1b, 0x28, 0xa0, 0x88, 0x6b, 0x2a, 0x84, 0x15, 0xc7, 0xda, 0x31,
23
+ 0x29, 0x1a, 0x5e, 0x96, 0xbb, 0x7a, 0x56, 0x63, 0x9e, 0x17, 0x7d, 0x30, 0x1b, 0xeb,
24
+ ]),
25
+ );
26
+ const pubKey = api.schnorrComputePublicKey(pk);
27
+ const [s, e] = api.schnorrConstructSignature(msg, pk);
28
+ const verified = api.schnorrVerifySignature(msg, pubKey, s, e);
29
+
30
+ expect(verified).toBe(true);
31
+ });
32
+
33
+ it('public key negation should work', () => {
34
+ const publicKeyStr =
35
+ '0x164f01b1011a1b292217acf53eef4d74f625f6e9bd5edfdb74c56fd81aafeebb21912735f9266a3719f61c1eb747ddee0cac9917f5c807485d356709b529b62c';
36
+ const publicKey = Point.fromString(publicKeyStr);
37
+ // hardcoded expected negated public key
38
+ const expectedInvertedStr =
39
+ '0x164f01b1011a1b292217acf53eef4d74f625f6e9bd5edfdb74c56fd81aafeebb0ed3273ce80b35f29e5a2997ca397a6f1b874f3083f16948e6ac8e8a3ad649d5';
40
+ const expectedInverted = Point.fromString(expectedInvertedStr);
41
+
42
+ // negate - should match expected negated key
43
+ const negatedPublicKey = api.schnorrNegatePublicKey(publicKey);
44
+ expect(negatedPublicKey.equals(expectedInverted)).toEqual(true);
45
+ // negate again - should be original public key now
46
+ expect(api.schnorrNegatePublicKey(negatedPublicKey).equals(publicKey)).toEqual(true);
47
+ });
48
+
49
+ it('should create + verify multi signature', () => {
50
+ // set up multisig accounts
51
+ const numSigners = 7;
52
+ const pks = [...Array(numSigners)].map(() => Fr.random());
53
+ const pubKeys = pks.map(pk => api.schnorrMultisigCreateMultisigPublicKey(pk));
54
+
55
+ // round one
56
+ const roundOnePublicOutputs: Buffer128[] = [];
57
+ const roundOnePrivateOutputs: Buffer128[] = [];
58
+ for (let i = 0; i < numSigners; ++i) {
59
+ const [publicOutput, privateOutput] = api.schnorrMultisigConstructSignatureRound1();
60
+ roundOnePublicOutputs.push(publicOutput);
61
+ roundOnePrivateOutputs.push(privateOutput);
62
+ }
63
+
64
+ // round two
65
+ const roundTwoOutputs = pks.map(
66
+ (pk, i) =>
67
+ api.schnorrMultisigConstructSignatureRound2(
68
+ msg,
69
+ pk,
70
+ roundOnePrivateOutputs[i],
71
+ pubKeys,
72
+ roundOnePublicOutputs,
73
+ )[0],
74
+ );
75
+
76
+ // generate signature
77
+ const [s, e] = api.schnorrMultisigCombineSignatures(msg, pubKeys, roundOnePublicOutputs, roundTwoOutputs)!;
78
+ const [combinedKey] = api.schnorrMultisigValidateAndCombineSignerPubkeys(pubKeys);
79
+ expect(combinedKey).not.toEqual(Buffer.alloc(64));
80
+ const verified = api.schnorrVerifySignature(msg, combinedKey, s, e);
81
+ expect(verified).toBe(true);
82
+ });
83
+
84
+ it('should identify invalid multi signature', () => {
85
+ const pks = [...Array(3)].map(() => Fr.random());
86
+ const pubKeys = pks.map(pk => api.schnorrMultisigCreateMultisigPublicKey(pk));
87
+ const [combinedKey] = api.schnorrMultisigValidateAndCombineSignerPubkeys(pubKeys);
88
+
89
+ const verified = api.schnorrVerifySignature(msg, combinedKey, Buffer32.random(), Buffer32.random());
90
+ expect(verified).toBe(false);
91
+ });
92
+
93
+ it('should not construct invalid multi signature', () => {
94
+ // set up multisig accounts
95
+ const numSigners = 7;
96
+ const pks = [...Array(numSigners)].map(() => Fr.random());
97
+ const pubKeys = pks.map(pk => api.schnorrMultisigCreateMultisigPublicKey(pk));
98
+
99
+ // round one
100
+ const roundOnePublicOutputs: Buffer128[] = [];
101
+ const roundOnePrivateOutputs: Buffer128[] = [];
102
+ for (let i = 0; i < numSigners; ++i) {
103
+ const [publicOutput, privateOutput] = api.schnorrMultisigConstructSignatureRound1();
104
+ roundOnePublicOutputs.push(publicOutput);
105
+ roundOnePrivateOutputs.push(privateOutput);
106
+ }
107
+
108
+ // round two
109
+ const roundTwoOutputs = pks.map(
110
+ (pk, i) =>
111
+ api.schnorrMultisigConstructSignatureRound2(
112
+ msg,
113
+ pk,
114
+ roundOnePrivateOutputs[i],
115
+ pubKeys,
116
+ roundOnePublicOutputs,
117
+ )[0],
118
+ );
119
+
120
+ // wrong number of data
121
+ {
122
+ expect(
123
+ api.schnorrMultisigCombineSignatures(
124
+ msg,
125
+ pubKeys.slice(0, -1),
126
+ roundOnePublicOutputs.slice(0, -1),
127
+ roundTwoOutputs.slice(0, -1),
128
+ )[2],
129
+ ).toBe(false);
130
+ }
131
+
132
+ // invalid round two output
133
+ {
134
+ const invalidOutputs = [...roundTwoOutputs];
135
+ invalidOutputs[1] = api.schnorrMultisigConstructSignatureRound2(
136
+ msg,
137
+ pks[2], // <- Wrong private key.
138
+ roundOnePrivateOutputs[1],
139
+ pubKeys,
140
+ roundOnePublicOutputs,
141
+ )[0];
142
+ expect(api.schnorrMultisigCombineSignatures(msg, pubKeys, roundOnePublicOutputs, invalidOutputs)[2]).toBe(false);
143
+ }
144
+
145
+ // contains duplicates
146
+ {
147
+ const invalidOutputs = [...roundTwoOutputs];
148
+ invalidOutputs[1] = roundTwoOutputs[2];
149
+ expect(api.schnorrMultisigCombineSignatures(msg, pubKeys, roundOnePublicOutputs, invalidOutputs)[2]).toBe(false);
150
+ }
151
+ });
152
+
153
+ it('should not create combined key from public keys containing invalid key', () => {
154
+ const pks = [...Array(5)].map(() => Fr.random());
155
+ const pubKeys = pks.map(pk => api.schnorrMultisigCreateMultisigPublicKey(pk));
156
+
157
+ // not a valid point
158
+ {
159
+ pubKeys[1] = new Buffer128(Buffer.alloc(128));
160
+ expect(api.schnorrMultisigValidateAndCombineSignerPubkeys(pubKeys)[1]).toBe(false);
161
+ }
162
+
163
+ // contains duplicates
164
+ {
165
+ pubKeys[1] = pubKeys[2];
166
+ expect(api.schnorrMultisigValidateAndCombineSignerPubkeys(pubKeys)[1]).toBe(false);
167
+ }
168
+ });
169
+ });
@@ -0,0 +1,62 @@
1
+ import { Bufferable, serializeBufferable, OutputType } from '../serialize/index.js';
2
+ import { BarretenbergWasm, BarretenbergWasmWorker } from '../barretenberg_wasm/barretenberg_wasm.js';
3
+ import { asyncMap } from '../async_map/index.js';
4
+
5
+ /**
6
+ * Keeps track of heap allocations so they can be easily freed.
7
+ * The WASM memory layout has 1024 bytes of unused "scratch" space at the start (addresses 0-1023).
8
+ * We can leverage this for IO rather than making expensive bb_malloc bb_free calls.
9
+ * Heap allocations will be created for input/output args that don't fit into the scratch space.
10
+ * Input and output args can use the same scratch space as it's assume all input reads will be performed before any
11
+ * output writes are performed.
12
+ */
13
+ export class HeapAllocator {
14
+ private allocs: number[] = [];
15
+ private inScratchRemaining = 1024;
16
+ private outScratchRemaining = 1024;
17
+
18
+ constructor(private wasm: BarretenbergWasm | BarretenbergWasmWorker) {}
19
+
20
+ async copyToMemory(bufferable: Bufferable[]) {
21
+ return await asyncMap(bufferable.map(serializeBufferable), async buf => {
22
+ if (buf.length <= this.inScratchRemaining) {
23
+ const ptr = (this.inScratchRemaining -= buf.length);
24
+ await this.wasm.writeMemory(ptr, buf);
25
+ return ptr;
26
+ } else {
27
+ const ptr = await this.wasm.call('bbmalloc', buf.length);
28
+ await this.wasm.writeMemory(ptr, buf);
29
+ this.allocs.push(ptr);
30
+ return ptr;
31
+ }
32
+ });
33
+ }
34
+
35
+ async getOutputPtrs(objs: OutputType[]) {
36
+ return await asyncMap(objs, async obj => {
37
+ // If the obj is variable length, we need a 4 byte ptr to write the serialized data address to.
38
+ // WARNING: 4 only works with WASM as it has 32 bit memory.
39
+ const size = obj.SIZE_IN_BYTES || 4;
40
+
41
+ if (size <= this.outScratchRemaining) {
42
+ return (this.outScratchRemaining -= size);
43
+ } else {
44
+ const ptr = await this.wasm.call('bbmalloc', size);
45
+ this.allocs.push(ptr);
46
+ return ptr;
47
+ }
48
+ });
49
+ }
50
+
51
+ addOutputPtr(ptr: number) {
52
+ if (ptr >= 1024) {
53
+ this.allocs.push(ptr);
54
+ }
55
+ }
56
+
57
+ async freeAll() {
58
+ for (const ptr of this.allocs) {
59
+ await this.wasm.call('bbfree', ptr);
60
+ }
61
+ }
62
+ }
@@ -0,0 +1,61 @@
1
+ import { Bufferable, serializeBufferable, OutputType } from '../serialize/index.js';
2
+ import { BarretenbergWasm } from '../barretenberg_wasm/barretenberg_wasm.js';
3
+
4
+ /**
5
+ * Keeps track of heap allocations so they can be easily freed.
6
+ * The WASM memory layout has 1024 bytes of unused "scratch" space at the start (addresses 0-1023).
7
+ * We can leverage this for IO rather than making expensive bb_malloc bb_free calls.
8
+ * Heap allocations will be created for input/output args that don't fit into the scratch space.
9
+ * Input and output args can use the same scratch space as it's assume all input reads will be performed before any
10
+ * output writes are performed.
11
+ */
12
+ export class HeapAllocatorSync {
13
+ private allocs: number[] = [];
14
+ private inScratchRemaining = 1024;
15
+ private outScratchRemaining = 1024;
16
+
17
+ constructor(private wasm: BarretenbergWasm) {}
18
+
19
+ copyToMemory(bufferable: Bufferable[]) {
20
+ return bufferable.map(serializeBufferable).map(buf => {
21
+ if (buf.length <= this.inScratchRemaining) {
22
+ const ptr = (this.inScratchRemaining -= buf.length);
23
+ this.wasm.writeMemory(ptr, buf);
24
+ return ptr;
25
+ } else {
26
+ const ptr = this.wasm.call('bbmalloc', buf.length);
27
+ this.wasm.writeMemory(ptr, buf);
28
+ this.allocs.push(ptr);
29
+ return ptr;
30
+ }
31
+ });
32
+ }
33
+
34
+ getOutputPtrs(objs: OutputType[]) {
35
+ return objs.map(obj => {
36
+ // If the obj is variable length, we need a 4 byte ptr to write the serialized data address to.
37
+ // WARNING: 4 only works with WASM as it has 32 bit memory.
38
+ const size = obj.SIZE_IN_BYTES || 4;
39
+
40
+ if (size <= this.outScratchRemaining) {
41
+ return (this.outScratchRemaining -= size);
42
+ } else {
43
+ const ptr = this.wasm.call('bbmalloc', size);
44
+ this.allocs.push(ptr);
45
+ return ptr;
46
+ }
47
+ });
48
+ }
49
+
50
+ addOutputPtr(ptr: number) {
51
+ if (ptr >= 1024) {
52
+ this.allocs.push(ptr);
53
+ }
54
+ }
55
+
56
+ freeAll() {
57
+ for (const ptr of this.allocs) {
58
+ this.wasm.call('bbfree', ptr);
59
+ }
60
+ }
61
+ }
@@ -0,0 +1,76 @@
1
+ import { BarretenbergWasm, BarretenbergWasmWorker } from '../barretenberg_wasm/barretenberg_wasm.js';
2
+ import { HeapAllocator } from './heap_allocator.js';
3
+ import { Bufferable, OutputType } from '../serialize/index.js';
4
+ import { asyncMap } from '../async_map/index.js';
5
+ import { HeapAllocatorSync } from './heap_allocator_sync.js';
6
+
7
+ /**
8
+ * Calls a WASM export function, handles allocating/freeing of memory, and serializing/deserializing to types.
9
+ *
10
+ * Notes on function binding ABI:
11
+ * All functions can have an arbitrary number of input and output args.
12
+ * All arguments must be pointers.
13
+ * Input args are determined by being const or pointer to const.
14
+ * Output args must come after input args.
15
+ * All input data is big-endian.
16
+ * All output data is big-endian, except output heap alloc pointers.
17
+ * As integer types are converted to/from big-endian form, we shouldn't have to worry about memory alignment. (SURE?)
18
+ * All functions should return void.
19
+ * This binding function is responsible for allocating argument memory (including output memory).
20
+ * Variable length output args are allocated on the heap, and the resulting pointer is written to the output arg ptr,
21
+ * hence the above statement remains true.
22
+ * Binding will free any variable length output args that were allocated on the heap.
23
+ */
24
+ export class BarretenbergBinder {
25
+ constructor(public wasm: BarretenbergWasm | BarretenbergWasmWorker) {}
26
+
27
+ async callWasmExport(funcName: string, inArgs: Bufferable[], outTypes: OutputType[]) {
28
+ const alloc = new HeapAllocator(this.wasm);
29
+ const inPtrs = await alloc.copyToMemory(inArgs);
30
+ const outPtrs = await alloc.getOutputPtrs(outTypes);
31
+ await this.wasm.call(funcName, ...inPtrs, ...outPtrs);
32
+ const outArgs = this.deserializeOutputArgs(outTypes, outPtrs, alloc);
33
+ await alloc.freeAll();
34
+ return outArgs;
35
+ }
36
+
37
+ private deserializeOutputArgs(outTypes: OutputType[], outPtrs: number[], alloc: HeapAllocator) {
38
+ return asyncMap(outTypes, async (t, i) => {
39
+ if (t.SIZE_IN_BYTES) {
40
+ const slice = await this.wasm.getMemorySlice(outPtrs[i], outPtrs[i] + t.SIZE_IN_BYTES);
41
+ return t.fromBuffer(slice);
42
+ }
43
+ const slice = await this.wasm.getMemorySlice(outPtrs[i], outPtrs[i] + 4);
44
+ const ptr = new DataView(slice.buffer, slice.byteOffset, slice.byteLength).getUint32(0, true);
45
+ alloc.addOutputPtr(ptr);
46
+ return t.fromBuffer(await this.wasm.getMemorySlice(ptr));
47
+ });
48
+ }
49
+ }
50
+
51
+ export class BarretenbergBinderSync {
52
+ constructor(public wasm: BarretenbergWasm) {}
53
+
54
+ callWasmExport(funcName: string, inArgs: Bufferable[], outTypes: OutputType[]) {
55
+ const alloc = new HeapAllocatorSync(this.wasm);
56
+ const inPtrs = alloc.copyToMemory(inArgs);
57
+ const outPtrs = alloc.getOutputPtrs(outTypes);
58
+ this.wasm.call(funcName, ...inPtrs, ...outPtrs);
59
+ const outArgs = this.deserializeOutputArgs(outTypes, outPtrs, alloc);
60
+ alloc.freeAll();
61
+ return outArgs;
62
+ }
63
+
64
+ private deserializeOutputArgs(outTypes: OutputType[], outPtrs: number[], alloc: HeapAllocatorSync) {
65
+ return outTypes.map((t, i) => {
66
+ if (t.SIZE_IN_BYTES) {
67
+ const slice = this.wasm.getMemorySlice(outPtrs[i], outPtrs[i] + t.SIZE_IN_BYTES);
68
+ return t.fromBuffer(slice);
69
+ }
70
+ const slice = this.wasm.getMemorySlice(outPtrs[i], outPtrs[i] + 4);
71
+ const ptr = new DataView(slice.buffer, slice.byteOffset, slice.byteLength).getUint32(0, true);
72
+ alloc.addOutputPtr(ptr);
73
+ return t.fromBuffer(this.wasm.getMemorySlice(ptr));
74
+ });
75
+ }
76
+ }
@@ -0,0 +1,52 @@
1
+ import { type Worker } from 'worker_threads';
2
+ import { BarretenbergWasm, BarretenbergWasmWorker } from './barretenberg_wasm.js';
3
+
4
+ describe('barretenberg wasm', () => {
5
+ let wasm!: BarretenbergWasm;
6
+
7
+ beforeAll(async () => {
8
+ wasm = await BarretenbergWasm.new();
9
+ });
10
+
11
+ afterAll(async () => {
12
+ await wasm.destroy();
13
+ });
14
+
15
+ it('should new malloc, transfer and slice mem', () => {
16
+ const length = 1024;
17
+ const ptr = wasm.call('bbmalloc', length);
18
+ const buf = Buffer.alloc(length, 128);
19
+ wasm.writeMemory(ptr, buf);
20
+ const result = Buffer.from(wasm.getMemorySlice(ptr, ptr + length));
21
+ wasm.call('bbfree', ptr);
22
+ expect(result).toStrictEqual(buf);
23
+ });
24
+
25
+ it('test abort', () => {
26
+ expect(() => wasm.call('test_abort')).toThrow();
27
+ });
28
+ });
29
+
30
+ describe('barretenberg wasm worker', () => {
31
+ let worker!: Worker;
32
+ let wasm!: BarretenbergWasmWorker;
33
+
34
+ beforeAll(async () => {
35
+ ({ wasm, worker } = await BarretenbergWasm.newWorker(2));
36
+ });
37
+
38
+ afterAll(async () => {
39
+ await wasm.destroy();
40
+ await worker.terminate();
41
+ });
42
+
43
+ it('should new malloc, transfer and slice mem', async () => {
44
+ const length = 1024;
45
+ const ptr = await wasm.call('bbmalloc', length);
46
+ const buf = Buffer.alloc(length, 128);
47
+ await wasm.writeMemory(ptr, buf);
48
+ const result = Buffer.from(await wasm.getMemorySlice(ptr, ptr + length));
49
+ await wasm.call('bbfree', ptr);
50
+ expect(result).toStrictEqual(buf);
51
+ });
52
+ });
@@ -0,0 +1,222 @@
1
+ import { type Worker } from 'worker_threads';
2
+ import { EventEmitter } from 'events';
3
+ import createDebug from 'debug';
4
+ import { Remote, proxy } from 'comlink';
5
+ import { randomBytes } from '../random/index.js';
6
+ // Webpack config swaps this import with ./browser/index.js
7
+ // You can toggle between these two imports to sanity check the type-safety.
8
+ import { fetchCode, getNumCpu, createWorker, getRemoteBarretenbergWasm, threadLogger, killSelf } from './node/index.js';
9
+ // import { fetchCode, getNumCpu, createWorker, randomBytes } from './browser/index.js';
10
+
11
+ const debug = createDebug('bb.js:wasm');
12
+
13
+ EventEmitter.defaultMaxListeners = 30;
14
+
15
+ export class BarretenbergWasm {
16
+ static MAX_THREADS = 16;
17
+ private memory!: WebAssembly.Memory;
18
+ private instance!: WebAssembly.Instance;
19
+ private workers: Worker[] = [];
20
+ private remoteWasms: BarretenbergWasmWorker[] = [];
21
+ private nextWorker = 0;
22
+ private nextThreadId = 1;
23
+ private isThread = false;
24
+ private logger: (msg: string) => void = debug;
25
+
26
+ public static async new() {
27
+ const barretenberg = new BarretenbergWasm();
28
+ await barretenberg.init(1);
29
+ return barretenberg;
30
+ }
31
+
32
+ /**
33
+ * Construct and initialise BarretenbergWasm within a Worker. Return both the worker and the wasm proxy.
34
+ * Used when running in the browser, because we can't block the main thread.
35
+ */
36
+ public static async newWorker(threads?: number) {
37
+ const worker = createWorker();
38
+ const wasm = getRemoteBarretenbergWasm(worker);
39
+ await wasm.init(threads, proxy(debug));
40
+ return { worker, wasm };
41
+ }
42
+
43
+ public getNumThreads() {
44
+ return this.workers.length + 1;
45
+ }
46
+
47
+ /**
48
+ * Init as main thread. Spawn child threads.
49
+ */
50
+ public async init(
51
+ threads = Math.min(getNumCpu(), BarretenbergWasm.MAX_THREADS),
52
+ logger: (msg: string) => void = debug,
53
+ initial = 25,
54
+ maximum = 2 ** 16,
55
+ ) {
56
+ this.logger = logger;
57
+
58
+ const initialMb = (initial * 2 ** 16) / (1024 * 1024);
59
+ const maxMb = (maximum * 2 ** 16) / (1024 * 1024);
60
+ this.logger(
61
+ `initial mem: ${initial} pages, ${initialMb}MiB. ` +
62
+ `max mem: ${maximum} pages, ${maxMb}MiB. ` +
63
+ `threads: ${threads}`,
64
+ );
65
+
66
+ this.memory = new WebAssembly.Memory({ initial, maximum, shared: threads > 1 });
67
+
68
+ // Annoyingly the wasm declares if it's memory is shared or not. So now we need two wasms if we want to be
69
+ // able to fallback on "non shared memory" situations.
70
+ const code = await fetchCode(threads > 1 ? 'barretenberg-threads.wasm' : 'barretenberg.wasm');
71
+ const { instance, module } = await WebAssembly.instantiate(code, this.getImportObj(this.memory));
72
+
73
+ this.instance = instance;
74
+
75
+ // Init all global/static data.
76
+ this.call('_initialize');
77
+
78
+ // Create worker threads. Create 1 less than requested, as main thread counts as a thread.
79
+ this.logger('creating worker threads...');
80
+ this.workers = await Promise.all(Array.from({ length: threads - 1 }).map(createWorker));
81
+ this.remoteWasms = await Promise.all(this.workers.map(getRemoteBarretenbergWasm));
82
+ await Promise.all(this.remoteWasms.map(w => w.initThread(module, this.memory)));
83
+ this.logger('init complete.');
84
+ }
85
+
86
+ /**
87
+ * Init as worker thread.
88
+ */
89
+ public async initThread(module: WebAssembly.Module, memory: WebAssembly.Memory) {
90
+ this.isThread = true;
91
+ this.logger = threadLogger() || this.logger;
92
+ this.memory = memory;
93
+ this.instance = await WebAssembly.instantiate(module, this.getImportObj(this.memory));
94
+ }
95
+
96
+ /**
97
+ * Called on main thread. Signals child threads to gracefully exit.
98
+ */
99
+ public async destroy() {
100
+ await Promise.all(this.workers.map(w => w.terminate()));
101
+ }
102
+
103
+ private getImportObj(memory: WebAssembly.Memory) {
104
+ /* eslint-disable camelcase */
105
+ const importObj = {
106
+ // We need to implement a part of the wasi api:
107
+ // https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md
108
+ // We literally only need to support random_get, everything else is noop implementated in barretenberg.wasm.
109
+ wasi_snapshot_preview1: {
110
+ random_get: (out: any, length: number) => {
111
+ out = out >>> 0;
112
+ const randomData = randomBytes(length);
113
+ const mem = this.getMemory();
114
+ mem.set(randomData, out);
115
+ },
116
+ clock_time_get: (a1: number, a2: number, out: number) => {
117
+ out = out >>> 0;
118
+ const ts = BigInt(new Date().getTime()) * 1000000n;
119
+ const view = new DataView(this.getMemory().buffer);
120
+ view.setBigUint64(out, ts, true);
121
+ },
122
+ proc_exit: () => {
123
+ this.logger('HUNG: proc_exit was called. This is caused by unstable experimental wasi pthreads. Try again.');
124
+ this.logger(new Error().stack!);
125
+ killSelf();
126
+ },
127
+ },
128
+ wasi: {
129
+ 'thread-spawn': (arg: number) => {
130
+ arg = arg >>> 0;
131
+ const id = this.nextThreadId++;
132
+ const worker = this.nextWorker++ % this.remoteWasms.length;
133
+ // this.logger(`spawning thread ${id} on worker ${worker} with arg ${arg >>> 0}`);
134
+ this.remoteWasms[worker].call('wasi_thread_start', id, arg).catch(this.logger);
135
+ // this.remoteWasms[worker].postMessage({ msg: 'thread', data: { id, arg } });
136
+ return id;
137
+ },
138
+ },
139
+
140
+ // These are functions implementations for imports we've defined are needed.
141
+ // The native C++ build defines these in a module called "env". We must implement TypeScript versions here.
142
+ env: {
143
+ env_hardware_concurrency: () => {
144
+ // If there are no workers (we're already running as a worker, or the main thread requested no workers)
145
+ // then we return 1, which should cause any algos using threading to just not create a thread.
146
+ return this.remoteWasms.length + 1;
147
+ },
148
+ /**
149
+ * The 'info' call we use for logging in C++, calls this under the hood.
150
+ * The native code will just print to std:err (to avoid std::cout which is used for IPC).
151
+ * Here we just emit the log line for the client to decide what to do with.
152
+ */
153
+ logstr: (addr: number) => {
154
+ const str = this.stringFromAddress(addr);
155
+ const m = this.getMemory();
156
+ const str2 = `${str} (mem: ${(m.length / (1024 * 1024)).toFixed(2)}MiB)`;
157
+ this.logger(str2);
158
+ },
159
+
160
+ memory,
161
+ },
162
+ };
163
+ /* eslint-enable camelcase */
164
+
165
+ return importObj;
166
+ }
167
+
168
+ public exports(): any {
169
+ return this.instance.exports;
170
+ }
171
+
172
+ /**
173
+ * When returning values from the WASM, use >>> operator to convert signed representation to unsigned representation.
174
+ */
175
+ public call(name: string, ...args: any) {
176
+ if (!this.exports()[name]) {
177
+ throw new Error(`WASM function ${name} not found.`);
178
+ }
179
+ try {
180
+ return this.exports()[name](...args) >>> 0;
181
+ } catch (err: any) {
182
+ const message = `WASM function ${name} aborted, error: ${err}`;
183
+ this.logger(message);
184
+ this.logger(err.stack);
185
+ if (this.isThread) {
186
+ killSelf();
187
+ } else {
188
+ throw err;
189
+ }
190
+ }
191
+ }
192
+
193
+ public memSize() {
194
+ return this.getMemory().length;
195
+ }
196
+
197
+ public getMemorySlice(start: number, end?: number) {
198
+ return this.getMemory().subarray(start, end);
199
+ }
200
+
201
+ public writeMemory(offset: number, arr: Uint8Array) {
202
+ const mem = this.getMemory();
203
+ mem.set(arr, offset);
204
+ }
205
+
206
+ // PRIVATE METHODS
207
+
208
+ private getMemory() {
209
+ return new Uint8Array(this.memory.buffer);
210
+ }
211
+
212
+ private stringFromAddress(addr: number) {
213
+ addr = addr >>> 0;
214
+ const m = this.getMemory();
215
+ let i = addr;
216
+ for (; m[i] !== 0; ++i);
217
+ const textDecoder = new TextDecoder('ascii');
218
+ return textDecoder.decode(m.slice(addr, i));
219
+ }
220
+ }
221
+
222
+ export type BarretenbergWasmWorker = Remote<BarretenbergWasm>;
@@ -0,0 +1,32 @@
1
+ import { wrap } from 'comlink';
2
+ import { BarretenbergWasmWorker, type BarretenbergWasm } from '../barretenberg_wasm.js';
3
+ import debug from 'debug';
4
+
5
+ export async function fetchCode(name: string) {
6
+ const res = await fetch('/' + name);
7
+ return await res.arrayBuffer();
8
+ }
9
+
10
+ export function createWorker() {
11
+ const worker = new Worker('barretenberg_wasm.js');
12
+ const debugStr = debug.disable();
13
+ debug.enable(debugStr);
14
+ worker.postMessage({ debug: debugStr });
15
+ return worker;
16
+ }
17
+
18
+ export function getRemoteBarretenbergWasm(worker: Worker): BarretenbergWasmWorker {
19
+ return wrap<BarretenbergWasm>(worker);
20
+ }
21
+
22
+ export function getNumCpu() {
23
+ return navigator.hardwareConcurrency;
24
+ }
25
+
26
+ export function threadLogger(): ((msg: string) => void) | undefined {
27
+ return undefined;
28
+ }
29
+
30
+ export function killSelf() {
31
+ self.close();
32
+ }
@@ -0,0 +1,13 @@
1
+ import { expose } from 'comlink';
2
+ import { BarretenbergWasm } from '../index.js';
3
+ import debug from 'debug';
4
+
5
+ self.onmessage = function (e) {
6
+ if (e.data.debug) {
7
+ debug.enable(e.data.debug);
8
+ }
9
+ };
10
+
11
+ expose(new BarretenbergWasm());
12
+
13
+ self.postMessage({ ready: true });
@@ -0,0 +1 @@
1
+ export * from "./barretenberg_wasm.js";