@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.
- package/README.md +3 -0
- package/dest/async_map/index.d.ts +10 -0
- package/dest/async_map/index.d.ts.map +1 -0
- package/dest/async_map/index.js +16 -0
- package/dest/barretenberg-threads.wasm +0 -0
- package/dest/barretenberg.wasm +0 -0
- package/dest/barretenberg_api/blake2s.test.d.ts +2 -0
- package/dest/barretenberg_api/blake2s.test.d.ts.map +1 -0
- package/dest/barretenberg_api/blake2s.test.js +30 -0
- package/dest/barretenberg_api/common.test.d.ts +2 -0
- package/dest/barretenberg_api/common.test.d.ts.map +1 -0
- package/dest/barretenberg_api/common.test.js +18 -0
- package/dest/barretenberg_api/index.d.ts +101 -0
- package/dest/barretenberg_api/index.d.ts.map +1 -0
- package/dest/barretenberg_api/index.js +371 -0
- package/dest/barretenberg_api/pedersen.test.d.ts +2 -0
- package/dest/barretenberg_api/pedersen.test.d.ts.map +1 -0
- package/dest/barretenberg_api/pedersen.test.js +69 -0
- package/dest/barretenberg_api/schnorr.test.d.ts +2 -0
- package/dest/barretenberg_api/schnorr.test.d.ts.map +1 -0
- package/dest/barretenberg_api/schnorr.test.js +113 -0
- package/dest/barretenberg_binder/heap_allocator.d.ts +22 -0
- package/dest/barretenberg_binder/heap_allocator.d.ts.map +1 -0
- package/dest/barretenberg_binder/heap_allocator.js +59 -0
- package/dest/barretenberg_binder/heap_allocator_sync.d.ts +22 -0
- package/dest/barretenberg_binder/heap_allocator_sync.d.ts.map +1 -0
- package/dest/barretenberg_binder/heap_allocator_sync.js +58 -0
- package/dest/barretenberg_binder/index.d.ts +32 -0
- package/dest/barretenberg_binder/index.d.ts.map +1 -0
- package/dest/barretenberg_binder/index.js +73 -0
- package/dest/barretenberg_wasm/barretenberg_wasm.d.ts +49 -0
- package/dest/barretenberg_wasm/barretenberg_wasm.d.ts.map +1 -0
- package/dest/barretenberg_wasm/barretenberg_wasm.js +190 -0
- package/dest/barretenberg_wasm/barretenberg_wasm.test.d.ts +2 -0
- package/dest/barretenberg_wasm/barretenberg_wasm.test.d.ts.map +1 -0
- package/dest/barretenberg_wasm/barretenberg_wasm.test.js +43 -0
- package/dest/barretenberg_wasm/browser/index.d.ts +8 -0
- package/dest/barretenberg_wasm/browser/index.d.ts.map +1 -0
- package/dest/barretenberg_wasm/browser/index.js +26 -0
- package/dest/barretenberg_wasm/browser/worker.d.ts +2 -0
- package/dest/barretenberg_wasm/browser/worker.d.ts.map +1 -0
- package/dest/barretenberg_wasm/browser/worker.js +11 -0
- package/dest/barretenberg_wasm/index.d.ts +2 -0
- package/dest/barretenberg_wasm/index.d.ts.map +1 -0
- package/dest/barretenberg_wasm/index.js +2 -0
- package/dest/barretenberg_wasm/node/index.d.ts +17 -0
- package/dest/barretenberg_wasm/node/index.d.ts.map +1 -0
- package/dest/barretenberg_wasm/node/index.js +40 -0
- package/dest/barretenberg_wasm/node/node_endpoint.d.ts +8 -0
- package/dest/barretenberg_wasm/node/node_endpoint.d.ts.map +1 -0
- package/dest/barretenberg_wasm/node/node_endpoint.js +28 -0
- package/dest/barretenberg_wasm/node/worker.d.ts +2 -0
- package/dest/barretenberg_wasm/node/worker.d.ts.map +1 -0
- package/dest/barretenberg_wasm/node/worker.js +9 -0
- package/dest/barretenberg_wasm.js +2 -0
- package/dest/barretenberg_wasm.js.LICENSE.txt +5 -0
- package/dest/bigint-array/index.d.ts +3 -0
- package/dest/bigint-array/index.d.ts.map +1 -0
- package/dest/bigint-array/index.js +21 -0
- package/dest/bindgen/function_declaration.d.ts +11 -0
- package/dest/bindgen/function_declaration.d.ts.map +1 -0
- package/dest/bindgen/function_declaration.js +2 -0
- package/dest/bindgen/index.d.ts +2 -0
- package/dest/bindgen/index.d.ts.map +1 -0
- package/dest/bindgen/index.js +15 -0
- package/dest/bindgen/mappings.d.ts +4 -0
- package/dest/bindgen/mappings.d.ts.map +1 -0
- package/dest/bindgen/mappings.js +63 -0
- package/dest/bindgen/rust.d.ts +2 -0
- package/dest/bindgen/rust.d.ts.map +1 -0
- package/dest/bindgen/rust.js +43 -0
- package/dest/bindgen/to_camel_case.d.ts +2 -0
- package/dest/bindgen/to_camel_case.d.ts.map +1 -0
- package/dest/bindgen/to_camel_case.js +11 -0
- package/dest/bindgen/typescript.d.ts +2 -0
- package/dest/bindgen/typescript.d.ts.map +1 -0
- package/dest/bindgen/typescript.js +80 -0
- package/dest/crs/browser/cached_net_crs.d.ts +25 -0
- package/dest/crs/browser/cached_net_crs.d.ts.map +1 -0
- package/dest/crs/browser/cached_net_crs.js +54 -0
- package/dest/crs/browser/index.d.ts +2 -0
- package/dest/crs/browser/index.d.ts.map +1 -0
- package/dest/crs/browser/index.js +2 -0
- package/dest/crs/index.d.ts +2 -0
- package/dest/crs/index.d.ts.map +1 -0
- package/dest/crs/index.js +2 -0
- package/dest/crs/net_crs.d.ts +36 -0
- package/dest/crs/net_crs.d.ts.map +1 -0
- package/dest/crs/net_crs.js +59 -0
- package/dest/crs/node/file_crs.d.ts +37 -0
- package/dest/crs/node/file_crs.d.ts.map +1 -0
- package/dest/crs/node/file_crs.js +51 -0
- package/dest/crs/node/index.d.ts +31 -0
- package/dest/crs/node/index.d.ts.map +1 -0
- package/dest/crs/node/index.js +41 -0
- package/dest/examples/simple.rawtest.d.ts +2 -0
- package/dest/examples/simple.rawtest.d.ts.map +1 -0
- package/dest/examples/simple.rawtest.js +24 -0
- package/dest/examples/simple.test.d.ts +2 -0
- package/dest/examples/simple.test.d.ts.map +1 -0
- package/dest/examples/simple.test.js +22 -0
- package/dest/factory/index.d.ts +21 -0
- package/dest/factory/index.d.ts.map +1 -0
- package/dest/factory/index.js +34 -0
- package/dest/index.d.ts +4 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.html +1 -0
- package/dest/index.js +4 -0
- package/dest/main-dev.d.ts +3 -0
- package/dest/main-dev.d.ts.map +1 -0
- package/dest/main-dev.js +3 -0
- package/dest/main.d.ts +10 -0
- package/dest/main.d.ts.map +1 -0
- package/dest/main.js +288 -0
- package/dest/random/browser/index.d.ts +2 -0
- package/dest/random/browser/index.d.ts.map +1 -0
- package/dest/random/browser/index.js +31 -0
- package/dest/random/index.d.ts +2 -0
- package/dest/random/index.d.ts.map +1 -0
- package/dest/random/index.js +2 -0
- package/dest/random/node/index.d.ts +2 -0
- package/dest/random/node/index.d.ts.map +1 -0
- package/dest/random/node/index.js +5 -0
- package/dest/serialize/buffer_reader.d.ts +28 -0
- package/dest/serialize/buffer_reader.d.ts.map +1 -0
- package/dest/serialize/buffer_reader.js +66 -0
- package/dest/serialize/index.d.ts +4 -0
- package/dest/serialize/index.d.ts.map +1 -0
- package/dest/serialize/index.js +4 -0
- package/dest/serialize/output_type.d.ts +11 -0
- package/dest/serialize/output_type.d.ts.map +1 -0
- package/dest/serialize/output_type.js +44 -0
- package/dest/serialize/serialize.d.ts +53 -0
- package/dest/serialize/serialize.d.ts.map +1 -0
- package/dest/serialize/serialize.js +139 -0
- package/dest/simple_test.js +2 -0
- package/dest/simple_test.js.LICENSE.txt +14 -0
- package/dest/types/fields.d.ts +33 -0
- package/dest/types/fields.d.ts.map +1 -0
- package/dest/types/fields.js +86 -0
- package/dest/types/fixed_size_buffer.d.ts +26 -0
- package/dest/types/fixed_size_buffer.d.ts.map +1 -0
- package/dest/types/fixed_size_buffer.js +54 -0
- package/dest/types/index.d.ts +6 -0
- package/dest/types/index.d.ts.map +1 -0
- package/dest/types/index.js +6 -0
- package/dest/types/point.d.ts +17 -0
- package/dest/types/point.d.ts.map +1 -0
- package/dest/types/point.js +32 -0
- package/dest/types/ptr.d.ts +13 -0
- package/dest/types/ptr.d.ts.map +1 -0
- package/dest/types/ptr.js +20 -0
- package/dest/types/raw_buffer.d.ts +3 -0
- package/dest/types/raw_buffer.d.ts.map +1 -0
- package/dest/types/raw_buffer.js +5 -0
- package/package.json +79 -0
- package/src/async_map/index.ts +15 -0
- package/src/barretenberg_api/blake2s.test.ts +39 -0
- package/src/barretenberg_api/common.test.ts +21 -0
- package/src/barretenberg_api/index.ts +461 -0
- package/src/barretenberg_api/pedersen.test.ts +84 -0
- package/src/barretenberg_api/schnorr.test.ts +169 -0
- package/src/barretenberg_binder/heap_allocator.ts +62 -0
- package/src/barretenberg_binder/heap_allocator_sync.ts +61 -0
- package/src/barretenberg_binder/index.ts +76 -0
- package/src/barretenberg_wasm/barretenberg_wasm.test.ts +52 -0
- package/src/barretenberg_wasm/barretenberg_wasm.ts +222 -0
- package/src/barretenberg_wasm/browser/index.ts +32 -0
- package/src/barretenberg_wasm/browser/worker.ts +13 -0
- package/src/barretenberg_wasm/index.ts +1 -0
- package/src/barretenberg_wasm/node/index.ts +46 -0
- package/src/barretenberg_wasm/node/node_endpoint.ts +28 -0
- package/src/barretenberg_wasm/node/worker.ts +10 -0
- package/src/bigint-array/index.ts +21 -0
- package/src/bindgen/function_declaration.ts +11 -0
- package/src/bindgen/index.ts +17 -0
- package/src/bindgen/mappings.ts +66 -0
- package/src/bindgen/rust.ts +52 -0
- package/src/bindgen/to_camel_case.ts +10 -0
- package/src/bindgen/typescript.ts +91 -0
- package/src/crs/browser/cached_net_crs.ts +60 -0
- package/src/crs/browser/index.ts +1 -0
- package/src/crs/index.ts +1 -0
- package/src/crs/net_crs.ts +69 -0
- package/src/crs/node/file_crs.ts +60 -0
- package/src/crs/node/index.ts +48 -0
- package/src/examples/simple.rawtest.ts +32 -0
- package/src/examples/simple.test.ts +27 -0
- package/src/factory/index.ts +36 -0
- package/src/index.html +9 -0
- package/src/index.ts +3 -0
- package/src/main-dev.ts +2 -0
- package/src/main.ts +329 -0
- package/src/random/browser/index.ts +32 -0
- package/src/random/index.ts +1 -0
- package/src/random/node/index.ts +5 -0
- package/src/serialize/buffer_reader.ts +82 -0
- package/src/serialize/index.ts +3 -0
- package/src/serialize/output_type.ts +53 -0
- package/src/serialize/serialize.ts +157 -0
- package/src/types/fields.ts +98 -0
- package/src/types/fixed_size_buffer.ts +59 -0
- package/src/types/index.ts +5 -0
- package/src/types/point.ts +35 -0
- package/src/types/ptr.ts +20 -0
- package/src/types/raw_buffer.ts +3 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Worker } from 'worker_threads';
|
|
2
|
+
import { dirname } from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { readFile } from 'fs/promises';
|
|
5
|
+
import os from 'os';
|
|
6
|
+
import { type BarretenbergWasm, type BarretenbergWasmWorker } from '../barretenberg_wasm.js';
|
|
7
|
+
import { wrap } from 'comlink';
|
|
8
|
+
import { nodeEndpoint } from './node_endpoint.js';
|
|
9
|
+
import { writeSync } from 'fs';
|
|
10
|
+
|
|
11
|
+
export async function fetchCode(name: string) {
|
|
12
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
return await readFile(__dirname + '/../../' + name);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function createWorker() {
|
|
17
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
18
|
+
return new Worker(__dirname + `/worker.js`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getRemoteBarretenbergWasm(worker: Worker): BarretenbergWasmWorker {
|
|
22
|
+
return wrap<BarretenbergWasm>(nodeEndpoint(worker)) as BarretenbergWasmWorker;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getNumCpu() {
|
|
26
|
+
return os.cpus().length;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* In node, the message passing is different to the browser. When using 'debug' in the browser, we seemingly always
|
|
31
|
+
* get our logs, but in node it looks like it's dependent on the chain of workers from child to main thread be
|
|
32
|
+
* unblocked. If one of our threads aborts, we can't see it as the parent is blocked waiting on threads to join.
|
|
33
|
+
* To work around this in node, threads will by default write directly to stdout.
|
|
34
|
+
*/
|
|
35
|
+
export function threadLogger(): ((msg: string) => void) | undefined {
|
|
36
|
+
return (msg: string) => {
|
|
37
|
+
writeSync(1, msg + '\n');
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function killSelf(): never {
|
|
42
|
+
// Extordinarily hard process termination. Due to how parent threads block on child threads etc, even process.exit
|
|
43
|
+
// doesn't seem to be able to abort the process. The following does.
|
|
44
|
+
process.kill(process.pid);
|
|
45
|
+
throw new Error();
|
|
46
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { NodeEndpoint } from 'comlink/dist/esm/node-adapter.js';
|
|
2
|
+
|
|
3
|
+
export function nodeEndpoint(nep: NodeEndpoint) {
|
|
4
|
+
const listeners = new WeakMap();
|
|
5
|
+
return {
|
|
6
|
+
postMessage: nep.postMessage.bind(nep),
|
|
7
|
+
addEventListener: (_: any, eh: any) => {
|
|
8
|
+
const l = (data: any) => {
|
|
9
|
+
if ('handleEvent' in eh) {
|
|
10
|
+
eh.handleEvent({ data });
|
|
11
|
+
} else {
|
|
12
|
+
eh({ data });
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
nep.on('message', l);
|
|
16
|
+
listeners.set(eh, l);
|
|
17
|
+
},
|
|
18
|
+
removeEventListener: (_: any, eh: any) => {
|
|
19
|
+
const l = listeners.get(eh);
|
|
20
|
+
if (!l) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
nep.off('message', l);
|
|
24
|
+
listeners.delete(eh);
|
|
25
|
+
},
|
|
26
|
+
start: nep.start && nep.start.bind(nep),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { parentPort } from 'worker_threads';
|
|
2
|
+
import { expose } from 'comlink';
|
|
3
|
+
import { BarretenbergWasm } from '../index.js';
|
|
4
|
+
import { nodeEndpoint } from './node_endpoint.js';
|
|
5
|
+
|
|
6
|
+
if (!parentPort) {
|
|
7
|
+
throw new Error('No parentPort');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
expose(new BarretenbergWasm(), nodeEndpoint(parentPort));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function toBigIntBE(bytes: Uint8Array) {
|
|
2
|
+
// A Buffer in node, *is* a Uint8Array. We can't refuse it's type.
|
|
3
|
+
// However the algo below only works on an actual Uint8Array, hence we make a new one to be safe.
|
|
4
|
+
bytes = new Uint8Array(bytes);
|
|
5
|
+
let bigint = BigInt(0);
|
|
6
|
+
const view = new DataView(bytes.buffer);
|
|
7
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
8
|
+
bigint = (bigint << BigInt(8)) + BigInt(view.getUint8(i));
|
|
9
|
+
}
|
|
10
|
+
return bigint;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function toBufferBE(value: bigint, byteLength = 32) {
|
|
14
|
+
const bytes = new Uint8Array(byteLength);
|
|
15
|
+
const view = new DataView(bytes.buffer);
|
|
16
|
+
for (let i = 0; i < byteLength; i++) {
|
|
17
|
+
view.setUint8(byteLength - i - 1, Number(value & BigInt(0xff)));
|
|
18
|
+
value >>= BigInt(8);
|
|
19
|
+
}
|
|
20
|
+
return bytes;
|
|
21
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { generateRustCode } from './rust.js';
|
|
2
|
+
import { generateTypeScriptCode } from './typescript.js';
|
|
3
|
+
|
|
4
|
+
const [, , exports = '../exports.json', lang = 'ts'] = process.argv;
|
|
5
|
+
|
|
6
|
+
function generateCode(exports: string, lang: string) {
|
|
7
|
+
switch (lang) {
|
|
8
|
+
case 'ts':
|
|
9
|
+
return generateTypeScriptCode(exports);
|
|
10
|
+
case 'rust':
|
|
11
|
+
return generateRustCode(exports);
|
|
12
|
+
default:
|
|
13
|
+
throw new Error(`Unknown lang: ${lang}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
console.log(generateCode(exports, lang));
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
const typeMap: { [key: string]: string } = {
|
|
3
|
+
in_ptr: 'Ptr',
|
|
4
|
+
out_ptr: 'Ptr',
|
|
5
|
+
'barretenberg::fr::in_buf': 'Fr',
|
|
6
|
+
'barretenberg::fr::vec_in_buf': 'Fr[]',
|
|
7
|
+
'fr::in_buf': 'Fr',
|
|
8
|
+
'fr::out_buf': 'Fr',
|
|
9
|
+
'fr::vec_in_buf': 'Fr[]',
|
|
10
|
+
'fr::vec_out_buf': 'Fr[]',
|
|
11
|
+
'fq::in_buf': 'Fq',
|
|
12
|
+
'fq::out_buf': 'Fq',
|
|
13
|
+
'fq::vec_in_buf': 'Fq[]',
|
|
14
|
+
'fq::vec_out_buf': 'Fq[]',
|
|
15
|
+
'const uint8_t *': 'Uint8Array',
|
|
16
|
+
'uint8_t **': 'Uint8Array',
|
|
17
|
+
in_str_buf: 'string',
|
|
18
|
+
out_str_buf: 'string',
|
|
19
|
+
in_buf32: 'Buffer32',
|
|
20
|
+
out_buf32: 'Buffer32',
|
|
21
|
+
'uint32_t *': 'number',
|
|
22
|
+
'const uint32_t *': 'number',
|
|
23
|
+
'affine_element::in_buf': 'Point',
|
|
24
|
+
'affine_element::out_buf': 'Point',
|
|
25
|
+
'const bool *': 'boolean',
|
|
26
|
+
'bool *': 'boolean',
|
|
27
|
+
'multisig::MultiSigPublicKey::vec_in_buf': 'Buffer128[]',
|
|
28
|
+
'multisig::MultiSigPublicKey::out_buf': 'Buffer128',
|
|
29
|
+
'multisig::RoundOnePublicOutput::vec_in_buf': 'Buffer128[]',
|
|
30
|
+
'multisig::RoundOnePublicOutput::out_buf': 'Buffer128',
|
|
31
|
+
'multisig::RoundOnePrivateOutput::in_buf': 'Buffer128',
|
|
32
|
+
'multisig::RoundOnePrivateOutput::out_buf': 'Buffer128',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const deserializerMap: { [key: string]: string } = {
|
|
36
|
+
out_ptr: 'Ptr',
|
|
37
|
+
'fr::out_buf': 'Fr',
|
|
38
|
+
'fr::vec_out_buf': 'VectorDeserializer(Fr)',
|
|
39
|
+
'fq::out_buf': 'Fq',
|
|
40
|
+
'fq::vec_out_buf': 'VectorDeserializer(Fq)',
|
|
41
|
+
'uint8_t **': 'BufferDeserializer()',
|
|
42
|
+
out_str_buf: 'StringDeserializer()',
|
|
43
|
+
out_buf32: 'Buffer32',
|
|
44
|
+
'uint32_t *': 'NumberDeserializer()',
|
|
45
|
+
'affine_element::out_buf': 'Point',
|
|
46
|
+
'bool *': 'BoolDeserializer()',
|
|
47
|
+
'multisig::MultiSigPublicKey::out_buf': 'Buffer128',
|
|
48
|
+
'multisig::RoundOnePublicOutput::out_buf': 'Buffer128',
|
|
49
|
+
'multisig::RoundOnePrivateOutput::out_buf': 'Buffer128',
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export function mapType(type: string) {
|
|
53
|
+
if (typeMap[type]) {
|
|
54
|
+
return typeMap[type];
|
|
55
|
+
}
|
|
56
|
+
throw new Error(`Unknown type: ${type}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const mapRustType = mapType;
|
|
60
|
+
|
|
61
|
+
export function mapDeserializer(type: string) {
|
|
62
|
+
if (deserializerMap[type]) {
|
|
63
|
+
return deserializerMap[type];
|
|
64
|
+
}
|
|
65
|
+
throw new Error(`Unknown deserializer for type: ${type}`);
|
|
66
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { Arg, FunctionDeclaration } from './function_declaration.js';
|
|
3
|
+
import { mapDeserializer, mapRustType } from './mappings.js';
|
|
4
|
+
|
|
5
|
+
export function generateRustCode(filename: string) {
|
|
6
|
+
const fileContent = fs.readFileSync(filename, 'utf-8');
|
|
7
|
+
const functionDeclarations: FunctionDeclaration[] = JSON.parse(fileContent);
|
|
8
|
+
|
|
9
|
+
let output = `
|
|
10
|
+
// WARNING: FILE CODE GENERATED BY BINDGEN UTILITY. DO NOT EDIT!
|
|
11
|
+
use crate::call_wasm_export::call_wasm_export;
|
|
12
|
+
use crate::serialize::{BufferDeserializer, NumberDeserializer, VectorDeserializer, BoolDeserializer};
|
|
13
|
+
use crate::types::{Fr, Fq, Point, Buffer32, Buffer128};
|
|
14
|
+
`;
|
|
15
|
+
|
|
16
|
+
for (const { functionName, inArgs, outArgs } of functionDeclarations) {
|
|
17
|
+
const parameters = inArgs.map(({ name, type }) => `${name}: ${mapRustType(type)}`).join(', ');
|
|
18
|
+
const inArgsVar = `let in_args = vec![${inArgs.map(arg => arg.name).join(', ')}];`;
|
|
19
|
+
const outTypesVar = `let out_types = vec![${outArgs.map(arg => mapDeserializer(arg.type)).join(', ')}];`;
|
|
20
|
+
const wasmCall = `let result = call_wasm_export(&"${functionName}", &in_args, &out_types)?;`;
|
|
21
|
+
|
|
22
|
+
const returnStmt = getReturnStmt(outArgs);
|
|
23
|
+
const returnType =
|
|
24
|
+
outArgs.length === 0
|
|
25
|
+
? '-> Result<(), Box<dyn std::error::Error>>'
|
|
26
|
+
: `-> Result<(${outArgs.map(a => mapRustType(a.type)).join(', ')}), Box<dyn std::error::Error>>`;
|
|
27
|
+
|
|
28
|
+
const functionDecl = `
|
|
29
|
+
pub fn ${functionName}(${parameters})${returnType} {
|
|
30
|
+
${inArgsVar}
|
|
31
|
+
${outTypesVar}
|
|
32
|
+
${wasmCall}
|
|
33
|
+
${returnStmt}
|
|
34
|
+
}
|
|
35
|
+
`;
|
|
36
|
+
|
|
37
|
+
output += functionDecl;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return output;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function getReturnStmt(outArgs: Arg[]) {
|
|
44
|
+
switch (outArgs.length) {
|
|
45
|
+
case 0:
|
|
46
|
+
return 'Ok(())';
|
|
47
|
+
case 1:
|
|
48
|
+
return `Ok(result[0].clone())`;
|
|
49
|
+
default:
|
|
50
|
+
return `Ok((${outArgs.map((_, idx) => `result[${idx}].clone()`).join(', ')}))`;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function toCamelCase(input: string): string {
|
|
2
|
+
const words = input.split('_');
|
|
3
|
+
const camelCasedWords = words.map((word, index) => {
|
|
4
|
+
if (index === 0) {
|
|
5
|
+
return word;
|
|
6
|
+
}
|
|
7
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
8
|
+
});
|
|
9
|
+
return camelCasedWords.join('');
|
|
10
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { mapDeserializer, mapType } from './mappings.js';
|
|
3
|
+
import { toCamelCase } from './to_camel_case.js';
|
|
4
|
+
import { FunctionDeclaration } from './function_declaration.js';
|
|
5
|
+
|
|
6
|
+
export function generateTypeScriptCode(filename: string) {
|
|
7
|
+
const fileContent = fs.readFileSync(filename, 'utf-8');
|
|
8
|
+
const functionDeclarations: FunctionDeclaration[] = JSON.parse(fileContent);
|
|
9
|
+
|
|
10
|
+
let output = `// WARNING: FILE CODE GENERATED BY BINDGEN UTILITY. DO NOT EDIT!
|
|
11
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
12
|
+
import { BarretenbergBinder, BarretenbergBinderSync } from '../barretenberg_binder/index.js';
|
|
13
|
+
import { BufferDeserializer, NumberDeserializer, VectorDeserializer, BoolDeserializer, StringDeserializer } from '../serialize/index.js';
|
|
14
|
+
import { Fr, Fq, Point, Buffer32, Buffer128, Ptr } from '../types/index.js';
|
|
15
|
+
|
|
16
|
+
export class BarretenbergApi {
|
|
17
|
+
constructor(public binder: BarretenbergBinder) {}
|
|
18
|
+
|
|
19
|
+
async destroy() {
|
|
20
|
+
await this.binder.wasm.destroy();
|
|
21
|
+
}
|
|
22
|
+
`;
|
|
23
|
+
|
|
24
|
+
for (const { functionName, inArgs, outArgs } of functionDeclarations) {
|
|
25
|
+
try {
|
|
26
|
+
const parameters = inArgs.map(({ name, type }) => `${toCamelCase(name)}: ${mapType(type)}`).join(', ');
|
|
27
|
+
const inArgsVar = `[${inArgs.map(arg => toCamelCase(arg.name)).join(', ')}]`;
|
|
28
|
+
const outTypesVar = `[${outArgs.map(arg => mapDeserializer(arg.type)).join(', ')}]`;
|
|
29
|
+
const wasmCall = `const result = await this.binder.callWasmExport('${functionName}', ${inArgsVar}, ${outTypesVar});`;
|
|
30
|
+
|
|
31
|
+
const n = outArgs.length;
|
|
32
|
+
const returnStmt = n === 0 ? 'return;' : n === 1 ? 'return result[0];' : 'return result as any;';
|
|
33
|
+
const returnType =
|
|
34
|
+
outArgs.length === 0
|
|
35
|
+
? 'void'
|
|
36
|
+
: outArgs.length === 1
|
|
37
|
+
? `${mapType(outArgs[0].type)}`
|
|
38
|
+
: `[${outArgs.map(a => mapType(a.type)).join(', ')}]`;
|
|
39
|
+
|
|
40
|
+
output += `
|
|
41
|
+
async ${toCamelCase(functionName)}(${parameters}): Promise<${returnType}> {
|
|
42
|
+
${wasmCall}
|
|
43
|
+
${returnStmt}
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
} catch (err: any) {
|
|
47
|
+
throw new Error(`Function ${functionName}: ${err.message}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
output += `}
|
|
52
|
+
|
|
53
|
+
export class BarretenbergApiSync {
|
|
54
|
+
constructor(public binder: BarretenbergBinderSync) {}
|
|
55
|
+
|
|
56
|
+
async destroy() {
|
|
57
|
+
await this.binder.wasm.destroy();
|
|
58
|
+
}
|
|
59
|
+
`;
|
|
60
|
+
|
|
61
|
+
for (const { functionName, inArgs, outArgs } of functionDeclarations) {
|
|
62
|
+
try {
|
|
63
|
+
const parameters = inArgs.map(({ name, type }) => `${toCamelCase(name)}: ${mapType(type)}`).join(', ');
|
|
64
|
+
const inArgsVar = `[${inArgs.map(arg => toCamelCase(arg.name)).join(', ')}]`;
|
|
65
|
+
const outTypesVar = `[${outArgs.map(arg => mapDeserializer(arg.type)).join(', ')}]`;
|
|
66
|
+
const wasmCall = `const result = this.binder.callWasmExport('${functionName}', ${inArgsVar}, ${outTypesVar});`;
|
|
67
|
+
|
|
68
|
+
const n = outArgs.length;
|
|
69
|
+
const returnStmt = n === 0 ? 'return;' : n === 1 ? 'return result[0];' : 'return result as any;';
|
|
70
|
+
const returnType =
|
|
71
|
+
outArgs.length === 0
|
|
72
|
+
? 'void'
|
|
73
|
+
: outArgs.length === 1
|
|
74
|
+
? `${mapType(outArgs[0].type)}`
|
|
75
|
+
: `[${outArgs.map(a => mapType(a.type)).join(', ')}]`;
|
|
76
|
+
|
|
77
|
+
output += `
|
|
78
|
+
${toCamelCase(functionName)}(${parameters}): ${returnType} {
|
|
79
|
+
${wasmCall}
|
|
80
|
+
${returnStmt}
|
|
81
|
+
}
|
|
82
|
+
`;
|
|
83
|
+
} catch (err: any) {
|
|
84
|
+
throw new Error(`Function ${functionName}: ${err.message}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
output += '}';
|
|
89
|
+
|
|
90
|
+
return output;
|
|
91
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { concatenateUint8Arrays, numToUInt32BE } from '../../serialize/index.js';
|
|
2
|
+
import { NetCrs } from '../net_crs.js';
|
|
3
|
+
import { get, set } from 'idb-keyval';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Downloader for CRS from the web or local.
|
|
7
|
+
*/
|
|
8
|
+
export class CachedNetCrs {
|
|
9
|
+
private g1Data!: Uint8Array;
|
|
10
|
+
private g2Data!: Uint8Array;
|
|
11
|
+
|
|
12
|
+
constructor(public readonly numPoints: number) {}
|
|
13
|
+
|
|
14
|
+
static async new(numPoints: number) {
|
|
15
|
+
const crs = new CachedNetCrs(numPoints);
|
|
16
|
+
await crs.init();
|
|
17
|
+
return crs;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Download the data.
|
|
22
|
+
*/
|
|
23
|
+
async init() {
|
|
24
|
+
// Check if data is in IndexedDB
|
|
25
|
+
const g1Data = await get('g1Data');
|
|
26
|
+
const g2Data = await get('g2Data');
|
|
27
|
+
const netCrs = new NetCrs(this.numPoints);
|
|
28
|
+
const g1DataLength = this.numPoints * 64;
|
|
29
|
+
|
|
30
|
+
if (!g1Data || g1Data.length < g1DataLength) {
|
|
31
|
+
this.g1Data = await netCrs.downloadG1Data();
|
|
32
|
+
await set('g1Data', this.g1Data);
|
|
33
|
+
} else {
|
|
34
|
+
this.g1Data = g1Data;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!g2Data) {
|
|
38
|
+
this.g2Data = await netCrs.downloadG2Data();
|
|
39
|
+
await set('g2Data', this.g2Data);
|
|
40
|
+
} else {
|
|
41
|
+
this.g2Data = g2Data;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* G1 points data for prover key.
|
|
47
|
+
* @returns The points data.
|
|
48
|
+
*/
|
|
49
|
+
getG1Data(): Uint8Array {
|
|
50
|
+
return this.g1Data;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* G2 points data for verification key.
|
|
55
|
+
* @returns The points data.
|
|
56
|
+
*/
|
|
57
|
+
getG2Data(): Uint8Array {
|
|
58
|
+
return this.g2Data;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CachedNetCrs as Crs } from './cached_net_crs.js';
|
package/src/crs/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Crs } from './node/index.js';
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Downloader for CRS from the web or local.
|
|
3
|
+
*/
|
|
4
|
+
export class NetCrs {
|
|
5
|
+
private data!: Uint8Array;
|
|
6
|
+
private g2Data!: Uint8Array;
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
/**
|
|
10
|
+
* The number of circuit gates.
|
|
11
|
+
*/
|
|
12
|
+
public readonly numPoints: number,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Download the data.
|
|
17
|
+
*/
|
|
18
|
+
async init() {
|
|
19
|
+
await this.downloadG1Data();
|
|
20
|
+
await this.downloadG2Data();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async downloadG1Data() {
|
|
24
|
+
const g1Start = 28;
|
|
25
|
+
const g1End = g1Start + this.numPoints * 64 - 1;
|
|
26
|
+
|
|
27
|
+
const response = await fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/monomial/transcript00.dat', {
|
|
28
|
+
headers: {
|
|
29
|
+
Range: `bytes=${g1Start}-${g1End}`,
|
|
30
|
+
},
|
|
31
|
+
cache: 'force-cache',
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return (this.data = new Uint8Array(await response.arrayBuffer()));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Download the G2 points data.
|
|
39
|
+
*/
|
|
40
|
+
async downloadG2Data() {
|
|
41
|
+
const g2Start = 28 + 5040000 * 64;
|
|
42
|
+
const g2End = g2Start + 128 - 1;
|
|
43
|
+
|
|
44
|
+
const response2 = await fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/sealed/transcript00.dat', {
|
|
45
|
+
headers: {
|
|
46
|
+
Range: `bytes=${g2Start}-${g2End}`,
|
|
47
|
+
},
|
|
48
|
+
cache: 'force-cache',
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return (this.g2Data = new Uint8Array(await response2.arrayBuffer()));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* G1 points data for prover key.
|
|
56
|
+
* @returns The points data.
|
|
57
|
+
*/
|
|
58
|
+
getG1Data(): Uint8Array {
|
|
59
|
+
return this.data;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* G2 points data for verification key.
|
|
64
|
+
* @returns The points data.
|
|
65
|
+
*/
|
|
66
|
+
getG2Data(): Uint8Array {
|
|
67
|
+
return this.g2Data;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import { readFile } from 'fs/promises';
|
|
3
|
+
import { dirname } from 'path';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The path to our SRS object, assuming that we are in barretenberg/ts folder.
|
|
8
|
+
*/
|
|
9
|
+
export const SRS_DEV_PATH = dirname(fileURLToPath(import.meta.url)) + '/../../../cpp/srs_db/ignition/monomial';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Downloader for CRS from a local file (for Node).
|
|
13
|
+
*/
|
|
14
|
+
export class FileCrs {
|
|
15
|
+
private data!: Uint8Array;
|
|
16
|
+
private g2Data!: Uint8Array;
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
/**
|
|
20
|
+
* The number of circuit gates.
|
|
21
|
+
*/
|
|
22
|
+
public readonly numPoints: number,
|
|
23
|
+
private path = SRS_DEV_PATH
|
|
24
|
+
) {}
|
|
25
|
+
|
|
26
|
+
static defaultExists() {
|
|
27
|
+
return existsSync(SRS_DEV_PATH);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Read the data file.
|
|
32
|
+
*/
|
|
33
|
+
async init() {
|
|
34
|
+
// We need this.numPoints number of g1 points.
|
|
35
|
+
// numPoints should be circuitSize + 1.
|
|
36
|
+
const g1Start = 28;
|
|
37
|
+
const g1End = g1Start + this.numPoints * 64;
|
|
38
|
+
|
|
39
|
+
const data = await readFile(this.path + '/transcript00.dat');
|
|
40
|
+
this.data = data.subarray(g1Start, g1End);
|
|
41
|
+
|
|
42
|
+
this.g2Data = await readFile(this.path + '/g2.dat');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* G1 points data for prover key.
|
|
47
|
+
* @returns The points data.
|
|
48
|
+
*/
|
|
49
|
+
getG1Data(): Uint8Array {
|
|
50
|
+
return this.data;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* G2 points data for verification key.
|
|
55
|
+
* @returns The points data.
|
|
56
|
+
*/
|
|
57
|
+
getG2Data(): Uint8Array {
|
|
58
|
+
return this.g2Data;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { concatenateUint8Arrays, numToUInt32BE } from '../../serialize/serialize.js';
|
|
2
|
+
import { NetCrs } from '../net_crs.js';
|
|
3
|
+
import { FileCrs } from './file_crs.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generic CRS finder utility class.
|
|
7
|
+
*/
|
|
8
|
+
export class Crs {
|
|
9
|
+
private crs: FileCrs | NetCrs;
|
|
10
|
+
|
|
11
|
+
constructor(
|
|
12
|
+
/**
|
|
13
|
+
* The number of circuit gates.
|
|
14
|
+
*/
|
|
15
|
+
public readonly numPoints: number,
|
|
16
|
+
) {
|
|
17
|
+
this.crs = FileCrs.defaultExists() ? new FileCrs(numPoints) : new NetCrs(numPoints);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static async new(numPoints: number) {
|
|
21
|
+
const crs = new Crs(numPoints);
|
|
22
|
+
await crs.init();
|
|
23
|
+
return crs;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Read CRS from our chosen source.
|
|
28
|
+
*/
|
|
29
|
+
async init() {
|
|
30
|
+
await this.crs.init();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* G1 points data for prover key.
|
|
35
|
+
* @returns The points data.
|
|
36
|
+
*/
|
|
37
|
+
getG1Data(): Uint8Array {
|
|
38
|
+
return this.crs.getG1Data();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* G2 points data for verification key.
|
|
43
|
+
* @returns The points data.
|
|
44
|
+
*/
|
|
45
|
+
getG2Data(): Uint8Array {
|
|
46
|
+
return this.crs.getG2Data();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Crs } from '../crs/index.js';
|
|
2
|
+
import createDebug from 'debug';
|
|
3
|
+
import { newBarretenbergApiAsync } from '../factory/index.js';
|
|
4
|
+
import { RawBuffer } from '../types/index.js';
|
|
5
|
+
|
|
6
|
+
createDebug.enable('*');
|
|
7
|
+
const debug = createDebug('simple_test');
|
|
8
|
+
|
|
9
|
+
async function main() {
|
|
10
|
+
const CIRCUIT_SIZE = 2 ** 19;
|
|
11
|
+
|
|
12
|
+
debug('starting test...');
|
|
13
|
+
const api = await newBarretenbergApiAsync();
|
|
14
|
+
|
|
15
|
+
// Important to init slab allocator as first thing, to ensure maximum memory efficiency.
|
|
16
|
+
await api.commonInitSlabAllocator(CIRCUIT_SIZE);
|
|
17
|
+
|
|
18
|
+
// Plus 1 needed!
|
|
19
|
+
const crs = await Crs.new(CIRCUIT_SIZE + 1);
|
|
20
|
+
await api.srsInitSrs(new RawBuffer(crs.getG1Data()), crs.numPoints, new RawBuffer(crs.getG2Data()));
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < 10; ++i) {
|
|
23
|
+
debug(`iteration ${i} starting...`);
|
|
24
|
+
await api.examplesSimpleCreateAndVerifyProof();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
await api.destroy();
|
|
28
|
+
|
|
29
|
+
debug('test complete.');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
void main();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Crs } from '../index.js';
|
|
2
|
+
import { BarretenbergApiAsync, newBarretenbergApiAsync } from '../factory/index.js';
|
|
3
|
+
import { RawBuffer } from '../types/index.js';
|
|
4
|
+
|
|
5
|
+
describe('simple', () => {
|
|
6
|
+
let api: BarretenbergApiAsync;
|
|
7
|
+
|
|
8
|
+
beforeAll(async () => {
|
|
9
|
+
api = await newBarretenbergApiAsync();
|
|
10
|
+
|
|
11
|
+
// Important to init slab allocator as first thing, to ensure maximum memory efficiency.
|
|
12
|
+
const CIRCUIT_SIZE = 2 ** 19;
|
|
13
|
+
await api.commonInitSlabAllocator(CIRCUIT_SIZE);
|
|
14
|
+
|
|
15
|
+
const crs = await Crs.new(2 ** 19 + 1);
|
|
16
|
+
await api.srsInitSrs(new RawBuffer(crs.getG1Data()), crs.numPoints, new RawBuffer(crs.getG2Data()));
|
|
17
|
+
}, 20000);
|
|
18
|
+
|
|
19
|
+
afterAll(async () => {
|
|
20
|
+
await api.destroy();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should construct 512k gate proof', async () => {
|
|
24
|
+
const valid = await api.examplesSimpleCreateAndVerifyProof();
|
|
25
|
+
expect(valid).toBe(true);
|
|
26
|
+
}, 60000);
|
|
27
|
+
});
|