@aztec/bb.js 0.0.1-alpha.7 → 0.0.1-fake-ceab37513c
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 +69 -32
- package/package.json +56 -45
- package/src/barretenberg/__snapshots__/pedersen.test.ts.snap +156 -0
- package/src/barretenberg/__snapshots__/poseidon.test.ts.snap +40 -0
- package/src/barretenberg/backend.ts +378 -0
- package/src/barretenberg/blake2s.test.ts +70 -0
- package/src/{barretenberg_api → barretenberg}/common.test.ts +7 -5
- package/src/barretenberg/index.ts +204 -0
- package/src/barretenberg/pedersen.test.ts +62 -0
- package/src/barretenberg/poseidon.test.ts +39 -0
- package/src/barretenberg_api/index.ts +982 -415
- package/src/barretenberg_wasm/barretenberg_wasm_base/index.ts +138 -0
- package/src/barretenberg_wasm/barretenberg_wasm_main/factory/browser/index.ts +11 -0
- package/src/barretenberg_wasm/barretenberg_wasm_main/factory/browser/main.worker.ts +13 -0
- package/src/barretenberg_wasm/barretenberg_wasm_main/factory/node/index.ts +21 -0
- package/src/barretenberg_wasm/barretenberg_wasm_main/factory/node/main.worker.ts +19 -0
- package/src/{barretenberg_binder/heap_allocator_sync.ts → barretenberg_wasm/barretenberg_wasm_main/heap_allocator.ts} +20 -17
- package/src/barretenberg_wasm/barretenberg_wasm_main/index.ts +167 -0
- package/src/barretenberg_wasm/barretenberg_wasm_thread/factory/browser/index.ts +11 -0
- package/src/barretenberg_wasm/barretenberg_wasm_thread/factory/browser/thread.worker.ts +13 -0
- package/src/barretenberg_wasm/barretenberg_wasm_thread/factory/node/index.ts +21 -0
- package/src/barretenberg_wasm/barretenberg_wasm_thread/factory/node/thread.worker.ts +19 -0
- package/src/barretenberg_wasm/barretenberg_wasm_thread/index.ts +47 -0
- package/src/barretenberg_wasm/fetch_code/browser/barretenberg-threads.ts +3 -0
- package/src/barretenberg_wasm/fetch_code/browser/barretenberg.ts +3 -0
- package/src/barretenberg_wasm/fetch_code/browser/index.ts +34 -0
- package/src/barretenberg_wasm/fetch_code/index.ts +1 -0
- package/src/barretenberg_wasm/fetch_code/node/index.ts +34 -0
- package/src/barretenberg_wasm/fetch_code/wasm-module.d.ts +4 -0
- package/src/barretenberg_wasm/helpers/browser/index.ts +47 -0
- package/src/barretenberg_wasm/helpers/index.ts +1 -0
- package/src/barretenberg_wasm/{node → helpers/node}/index.ts +24 -15
- package/src/barretenberg_wasm/index.test.ts +45 -0
- package/src/barretenberg_wasm/index.ts +22 -1
- package/src/benchmark/index.ts +26 -0
- package/src/benchmark/timer.ts +45 -0
- package/src/bigint-array/index.ts +39 -17
- package/src/bindgen/index.ts +2 -2
- package/src/bindgen/mappings.ts +3 -2
- package/src/bindgen/typescript.ts +50 -25
- package/src/cbind/README.md +1 -0
- package/src/cbind/generate.ts +89 -0
- package/src/cbind/schema_compiler.ts +833 -0
- package/src/crs/browser/cached_net_crs.ts +41 -1
- package/src/crs/browser/index.ts +1 -1
- package/src/crs/index.ts +1 -1
- package/src/crs/net_crs.ts +114 -19
- package/src/crs/node/index.ts +96 -26
- package/src/index.html +1 -1
- package/src/index.ts +13 -5
- package/src/log/browser/index.ts +35 -0
- package/src/log/index.ts +1 -0
- package/src/log/node/index.ts +52 -0
- package/src/log/types.ts +6 -0
- package/src/main.ts +391 -168
- package/src/proof/index.ts +94 -0
- package/src/random/browser/index.ts +1 -1
- package/src/retry/index.ts +50 -0
- package/src/serialize/buffer_reader.ts +4 -1
- package/src/types/fields.ts +46 -18
- package/src/types/point.ts +4 -1
- package/dest/async_map/index.d.ts +0 -10
- package/dest/async_map/index.d.ts.map +0 -1
- package/dest/async_map/index.js +0 -16
- package/dest/barretenberg-threads.wasm +0 -0
- package/dest/barretenberg.wasm +0 -0
- package/dest/barretenberg_api/blake2s.test.d.ts +0 -2
- package/dest/barretenberg_api/blake2s.test.d.ts.map +0 -1
- package/dest/barretenberg_api/blake2s.test.js +0 -30
- package/dest/barretenberg_api/common.test.d.ts +0 -2
- package/dest/barretenberg_api/common.test.d.ts.map +0 -1
- package/dest/barretenberg_api/common.test.js +0 -18
- package/dest/barretenberg_api/index.d.ts +0 -103
- package/dest/barretenberg_api/index.d.ts.map +0 -1
- package/dest/barretenberg_api/index.js +0 -379
- package/dest/barretenberg_api/pedersen.test.d.ts +0 -2
- package/dest/barretenberg_api/pedersen.test.d.ts.map +0 -1
- package/dest/barretenberg_api/pedersen.test.js +0 -69
- package/dest/barretenberg_api/schnorr.test.d.ts +0 -2
- package/dest/barretenberg_api/schnorr.test.d.ts.map +0 -1
- package/dest/barretenberg_api/schnorr.test.js +0 -113
- package/dest/barretenberg_binder/heap_allocator.d.ts +0 -22
- package/dest/barretenberg_binder/heap_allocator.d.ts.map +0 -1
- package/dest/barretenberg_binder/heap_allocator.js +0 -59
- package/dest/barretenberg_binder/heap_allocator_sync.d.ts +0 -22
- package/dest/barretenberg_binder/heap_allocator_sync.d.ts.map +0 -1
- package/dest/barretenberg_binder/heap_allocator_sync.js +0 -58
- package/dest/barretenberg_binder/index.d.ts +0 -32
- package/dest/barretenberg_binder/index.d.ts.map +0 -1
- package/dest/barretenberg_binder/index.js +0 -73
- package/dest/barretenberg_wasm/barretenberg_wasm.d.ts +0 -50
- package/dest/barretenberg_wasm/barretenberg_wasm.d.ts.map +0 -1
- package/dest/barretenberg_wasm/barretenberg_wasm.js +0 -212
- package/dest/barretenberg_wasm/barretenberg_wasm.test.d.ts +0 -2
- package/dest/barretenberg_wasm/barretenberg_wasm.test.d.ts.map +0 -1
- package/dest/barretenberg_wasm/barretenberg_wasm.test.js +0 -43
- package/dest/barretenberg_wasm/browser/index.d.ts +0 -8
- package/dest/barretenberg_wasm/browser/index.d.ts.map +0 -1
- package/dest/barretenberg_wasm/browser/index.js +0 -26
- package/dest/barretenberg_wasm/browser/worker.d.ts +0 -2
- package/dest/barretenberg_wasm/browser/worker.d.ts.map +0 -1
- package/dest/barretenberg_wasm/browser/worker.js +0 -11
- package/dest/barretenberg_wasm/index.d.ts +0 -2
- package/dest/barretenberg_wasm/index.d.ts.map +0 -1
- package/dest/barretenberg_wasm/index.js +0 -2
- package/dest/barretenberg_wasm/node/index.d.ts +0 -17
- package/dest/barretenberg_wasm/node/index.d.ts.map +0 -1
- package/dest/barretenberg_wasm/node/index.js +0 -40
- package/dest/barretenberg_wasm/node/node_endpoint.d.ts +0 -8
- package/dest/barretenberg_wasm/node/node_endpoint.d.ts.map +0 -1
- package/dest/barretenberg_wasm/node/node_endpoint.js +0 -28
- package/dest/barretenberg_wasm/node/worker.d.ts +0 -2
- package/dest/barretenberg_wasm/node/worker.d.ts.map +0 -1
- package/dest/barretenberg_wasm/node/worker.js +0 -9
- package/dest/barretenberg_wasm.js +0 -2
- package/dest/barretenberg_wasm.js.LICENSE.txt +0 -5
- package/dest/bigint-array/index.d.ts +0 -3
- package/dest/bigint-array/index.d.ts.map +0 -1
- package/dest/bigint-array/index.js +0 -21
- package/dest/bindgen/function_declaration.d.ts +0 -11
- package/dest/bindgen/function_declaration.d.ts.map +0 -1
- package/dest/bindgen/function_declaration.js +0 -2
- package/dest/bindgen/index.d.ts +0 -2
- package/dest/bindgen/index.d.ts.map +0 -1
- package/dest/bindgen/index.js +0 -15
- package/dest/bindgen/mappings.d.ts +0 -4
- package/dest/bindgen/mappings.d.ts.map +0 -1
- package/dest/bindgen/mappings.js +0 -63
- package/dest/bindgen/rust.d.ts +0 -2
- package/dest/bindgen/rust.d.ts.map +0 -1
- package/dest/bindgen/rust.js +0 -43
- package/dest/bindgen/to_camel_case.d.ts +0 -2
- package/dest/bindgen/to_camel_case.d.ts.map +0 -1
- package/dest/bindgen/to_camel_case.js +0 -11
- package/dest/bindgen/typescript.d.ts +0 -2
- package/dest/bindgen/typescript.d.ts.map +0 -1
- package/dest/bindgen/typescript.js +0 -80
- package/dest/crs/browser/cached_net_crs.d.ts +0 -25
- package/dest/crs/browser/cached_net_crs.d.ts.map +0 -1
- package/dest/crs/browser/cached_net_crs.js +0 -54
- package/dest/crs/browser/index.d.ts +0 -2
- package/dest/crs/browser/index.d.ts.map +0 -1
- package/dest/crs/browser/index.js +0 -2
- package/dest/crs/index.d.ts +0 -2
- package/dest/crs/index.d.ts.map +0 -1
- package/dest/crs/index.js +0 -2
- package/dest/crs/net_crs.d.ts +0 -36
- package/dest/crs/net_crs.d.ts.map +0 -1
- package/dest/crs/net_crs.js +0 -59
- package/dest/crs/node/ignition_files_crs.d.ts +0 -37
- package/dest/crs/node/ignition_files_crs.d.ts.map +0 -1
- package/dest/crs/node/ignition_files_crs.js +0 -51
- package/dest/crs/node/index.d.ts +0 -21
- package/dest/crs/node/index.d.ts.map +0 -1
- package/dest/crs/node/index.js +0 -54
- package/dest/examples/simple.rawtest.d.ts +0 -2
- package/dest/examples/simple.rawtest.d.ts.map +0 -1
- package/dest/examples/simple.rawtest.js +0 -29
- package/dest/examples/simple.test.d.ts +0 -2
- package/dest/examples/simple.test.d.ts.map +0 -1
- package/dest/examples/simple.test.js +0 -22
- package/dest/factory/index.d.ts +0 -21
- package/dest/factory/index.d.ts.map +0 -1
- package/dest/factory/index.js +0 -34
- package/dest/index.d.ts +0 -6
- package/dest/index.d.ts.map +0 -1
- package/dest/index.html +0 -1
- package/dest/index.js +0 -6
- package/dest/main.d.ts +0 -10
- package/dest/main.d.ts.map +0 -1
- package/dest/main.js +0 -275
- package/dest/random/browser/index.d.ts +0 -2
- package/dest/random/browser/index.d.ts.map +0 -1
- package/dest/random/browser/index.js +0 -31
- package/dest/random/index.d.ts +0 -2
- package/dest/random/index.d.ts.map +0 -1
- package/dest/random/index.js +0 -2
- package/dest/random/node/index.d.ts +0 -2
- package/dest/random/node/index.d.ts.map +0 -1
- package/dest/random/node/index.js +0 -5
- package/dest/serialize/buffer_reader.d.ts +0 -28
- package/dest/serialize/buffer_reader.d.ts.map +0 -1
- package/dest/serialize/buffer_reader.js +0 -66
- package/dest/serialize/index.d.ts +0 -4
- package/dest/serialize/index.d.ts.map +0 -1
- package/dest/serialize/index.js +0 -4
- package/dest/serialize/output_type.d.ts +0 -11
- package/dest/serialize/output_type.d.ts.map +0 -1
- package/dest/serialize/output_type.js +0 -44
- package/dest/serialize/serialize.d.ts +0 -53
- package/dest/serialize/serialize.d.ts.map +0 -1
- package/dest/serialize/serialize.js +0 -139
- package/dest/simple_test.js +0 -2
- package/dest/simple_test.js.LICENSE.txt +0 -14
- package/dest/types/fields.d.ts +0 -33
- package/dest/types/fields.d.ts.map +0 -1
- package/dest/types/fields.js +0 -86
- package/dest/types/fixed_size_buffer.d.ts +0 -26
- package/dest/types/fixed_size_buffer.d.ts.map +0 -1
- package/dest/types/fixed_size_buffer.js +0 -54
- package/dest/types/index.d.ts +0 -6
- package/dest/types/index.d.ts.map +0 -1
- package/dest/types/index.js +0 -6
- package/dest/types/point.d.ts +0 -17
- package/dest/types/point.d.ts.map +0 -1
- package/dest/types/point.js +0 -32
- package/dest/types/ptr.d.ts +0 -13
- package/dest/types/ptr.d.ts.map +0 -1
- package/dest/types/ptr.js +0 -20
- package/dest/types/raw_buffer.d.ts +0 -3
- package/dest/types/raw_buffer.d.ts.map +0 -1
- package/dest/types/raw_buffer.js +0 -5
- package/src/barretenberg_api/blake2s.test.ts +0 -39
- package/src/barretenberg_api/pedersen.test.ts +0 -84
- package/src/barretenberg_api/schnorr.test.ts +0 -169
- package/src/barretenberg_binder/heap_allocator.ts +0 -62
- package/src/barretenberg_binder/index.ts +0 -76
- package/src/barretenberg_wasm/barretenberg_wasm.test.ts +0 -52
- package/src/barretenberg_wasm/barretenberg_wasm.ts +0 -246
- package/src/barretenberg_wasm/browser/index.ts +0 -32
- package/src/barretenberg_wasm/browser/worker.ts +0 -13
- package/src/barretenberg_wasm/node/worker.ts +0 -10
- package/src/crs/node/ignition_files_crs.ts +0 -60
- package/src/examples/simple.rawtest.ts +0 -37
- package/src/examples/simple.test.ts +0 -27
- package/src/factory/index.ts +0 -36
- /package/src/barretenberg_wasm/{node → helpers/node}/node_endpoint.ts +0 -0
package/dest/types/point.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
import { Fr } from './index.js';
|
|
3
|
-
import { BufferReader } from '../serialize/buffer_reader.js';
|
|
4
|
-
export declare class Point {
|
|
5
|
-
readonly x: Fr;
|
|
6
|
-
readonly y: Fr;
|
|
7
|
-
static SIZE_IN_BYTES: number;
|
|
8
|
-
static EMPTY: Point;
|
|
9
|
-
constructor(x: Fr, y: Fr);
|
|
10
|
-
static random(): Point;
|
|
11
|
-
static fromBuffer(buffer: Uint8Array | BufferReader): Point;
|
|
12
|
-
static fromString(address: string): Point;
|
|
13
|
-
toBuffer(): Buffer;
|
|
14
|
-
toString(): string;
|
|
15
|
-
equals(rhs: Point): boolean;
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=point.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"point.d.ts","sourceRoot":"","sources":["../../src/types/point.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,YAAY,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAE7D,qBAAa,KAAK;aAIY,CAAC,EAAE,EAAE;aAAkB,CAAC,EAAE,EAAE;IAHxD,MAAM,CAAC,aAAa,SAAM;IAC1B,MAAM,CAAC,KAAK,QAA+B;gBAEf,CAAC,EAAE,EAAE,EAAkB,CAAC,EAAE,EAAE;IAExD,MAAM,CAAC,MAAM;IAKb,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,YAAY;IAKnD,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM;IAIjC,QAAQ;IAIR,QAAQ;IAIR,MAAM,CAAC,GAAG,EAAE,KAAK;CAGlB"}
|
package/dest/types/point.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Fr } from './index.js';
|
|
2
|
-
import { BufferReader } from '../serialize/buffer_reader.js';
|
|
3
|
-
class Point {
|
|
4
|
-
constructor(x, y) {
|
|
5
|
-
this.x = x;
|
|
6
|
-
this.y = y;
|
|
7
|
-
}
|
|
8
|
-
static random() {
|
|
9
|
-
// TODO: This is not a point on the curve!
|
|
10
|
-
return new Point(Fr.random(), Fr.random());
|
|
11
|
-
}
|
|
12
|
-
static fromBuffer(buffer) {
|
|
13
|
-
const reader = BufferReader.asReader(buffer);
|
|
14
|
-
return new this(Fr.fromBuffer(reader), Fr.fromBuffer(reader));
|
|
15
|
-
}
|
|
16
|
-
static fromString(address) {
|
|
17
|
-
return Point.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
|
|
18
|
-
}
|
|
19
|
-
toBuffer() {
|
|
20
|
-
return Buffer.concat([this.x.toBuffer(), this.y.toBuffer()]);
|
|
21
|
-
}
|
|
22
|
-
toString() {
|
|
23
|
-
return '0x' + this.toBuffer().toString('hex');
|
|
24
|
-
}
|
|
25
|
-
equals(rhs) {
|
|
26
|
-
return this.x.equals(rhs.x) && this.y.equals(rhs.y);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
Point.SIZE_IN_BYTES = 64;
|
|
30
|
-
Point.EMPTY = new Point(Fr.ZERO, Fr.ZERO);
|
|
31
|
-
export { Point };
|
|
32
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9pbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdHlwZXMvcG9pbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLEVBQUUsRUFBRSxNQUFNLFlBQVksQ0FBQztBQUNoQyxPQUFPLEVBQUUsWUFBWSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFFN0QsTUFBYSxLQUFLO0lBSWhCLFlBQTRCLENBQUssRUFBa0IsQ0FBSztRQUE1QixNQUFDLEdBQUQsQ0FBQyxDQUFJO1FBQWtCLE1BQUMsR0FBRCxDQUFDLENBQUk7SUFBRyxDQUFDO0lBRTVELE1BQU0sQ0FBQyxNQUFNO1FBQ1gsMENBQTBDO1FBQzFDLE9BQU8sSUFBSSxLQUFLLENBQUMsRUFBRSxDQUFDLE1BQU0sRUFBRSxFQUFFLEVBQUUsQ0FBQyxNQUFNLEVBQUUsQ0FBQyxDQUFDO0lBQzdDLENBQUM7SUFFRCxNQUFNLENBQUMsVUFBVSxDQUFDLE1BQWlDO1FBQ2pELE1BQU0sTUFBTSxHQUFHLFlBQVksQ0FBQyxRQUFRLENBQUMsTUFBTSxDQUFDLENBQUM7UUFDN0MsT0FBTyxJQUFJLElBQUksQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLE1BQU0sQ0FBQyxFQUFFLEVBQUUsQ0FBQyxVQUFVLENBQUMsTUFBTSxDQUFDLENBQUMsQ0FBQztJQUNoRSxDQUFDO0lBRUQsTUFBTSxDQUFDLFVBQVUsQ0FBQyxPQUFlO1FBQy9CLE9BQU8sS0FBSyxDQUFDLFVBQVUsQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsTUFBTSxFQUFFLEVBQUUsQ0FBQyxFQUFFLEtBQUssQ0FBQyxDQUFDLENBQUM7SUFDM0UsQ0FBQztJQUVELFFBQVE7UUFDTixPQUFPLE1BQU0sQ0FBQyxNQUFNLENBQUMsQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLFFBQVEsRUFBRSxFQUFFLElBQUksQ0FBQyxDQUFDLENBQUMsUUFBUSxFQUFFLENBQUMsQ0FBQyxDQUFDO0lBQy9ELENBQUM7SUFFRCxRQUFRO1FBQ04sT0FBTyxJQUFJLEdBQUcsSUFBSSxDQUFDLFFBQVEsRUFBRSxDQUFDLFFBQVEsQ0FBQyxLQUFLLENBQUMsQ0FBQztJQUNoRCxDQUFDO0lBRUQsTUFBTSxDQUFDLEdBQVU7UUFDZixPQUFPLElBQUksQ0FBQyxDQUFDLENBQUMsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUMsSUFBSSxJQUFJLENBQUMsQ0FBQyxDQUFDLE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDLENBQUM7SUFDdEQsQ0FBQzs7QUE3Qk0sbUJBQWEsR0FBRyxFQUFFLENBQUM7QUFDbkIsV0FBSyxHQUFHLElBQUksS0FBSyxDQUFDLEVBQUUsQ0FBQyxJQUFJLEVBQUUsRUFBRSxDQUFDLElBQUksQ0FBQyxDQUFDO1NBRmhDLEtBQUsifQ==
|
package/dest/types/ptr.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { BufferReader } from '../serialize/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* Holds an opaque pointer into WASM memory.
|
|
4
|
-
* Currently only 4 bytes, but could grow to 8 bytes with wasm64.
|
|
5
|
-
*/
|
|
6
|
-
export declare class Ptr {
|
|
7
|
-
readonly value: Uint8Array;
|
|
8
|
-
static SIZE_IN_BYTES: number;
|
|
9
|
-
constructor(value: Uint8Array);
|
|
10
|
-
static fromBuffer(buffer: Uint8Array | BufferReader): Ptr;
|
|
11
|
-
toBuffer(): Uint8Array;
|
|
12
|
-
}
|
|
13
|
-
//# sourceMappingURL=ptr.d.ts.map
|
package/dest/types/ptr.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ptr.d.ts","sourceRoot":"","sources":["../../src/types/ptr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD;;;GAGG;AACH,qBAAa,GAAG;aAGc,KAAK,EAAE,UAAU;IAF7C,MAAM,CAAC,aAAa,SAAK;gBAEG,KAAK,EAAE,UAAU;IAE7C,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,YAAY;IAKnD,QAAQ;CAGT"}
|
package/dest/types/ptr.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { BufferReader } from '../serialize/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* Holds an opaque pointer into WASM memory.
|
|
4
|
-
* Currently only 4 bytes, but could grow to 8 bytes with wasm64.
|
|
5
|
-
*/
|
|
6
|
-
class Ptr {
|
|
7
|
-
constructor(value) {
|
|
8
|
-
this.value = value;
|
|
9
|
-
}
|
|
10
|
-
static fromBuffer(buffer) {
|
|
11
|
-
const reader = BufferReader.asReader(buffer);
|
|
12
|
-
return new this(reader.readBytes(this.SIZE_IN_BYTES));
|
|
13
|
-
}
|
|
14
|
-
toBuffer() {
|
|
15
|
-
return this.value;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
Ptr.SIZE_IN_BYTES = 4;
|
|
19
|
-
export { Ptr };
|
|
20
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHRyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3R5cGVzL3B0ci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsWUFBWSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFFckQ7OztHQUdHO0FBQ0gsTUFBYSxHQUFHO0lBR2QsWUFBNEIsS0FBaUI7UUFBakIsVUFBSyxHQUFMLEtBQUssQ0FBWTtJQUFHLENBQUM7SUFFakQsTUFBTSxDQUFDLFVBQVUsQ0FBQyxNQUFpQztRQUNqRCxNQUFNLE1BQU0sR0FBRyxZQUFZLENBQUMsUUFBUSxDQUFDLE1BQU0sQ0FBQyxDQUFDO1FBQzdDLE9BQU8sSUFBSSxJQUFJLENBQUMsTUFBTSxDQUFDLFNBQVMsQ0FBQyxJQUFJLENBQUMsYUFBYSxDQUFDLENBQUMsQ0FBQztJQUN4RCxDQUFDO0lBRUQsUUFBUTtRQUNOLE9BQU8sSUFBSSxDQUFDLEtBQUssQ0FBQztJQUNwQixDQUFDOztBQVhNLGlCQUFhLEdBQUcsQ0FBQyxDQUFDO1NBRGQsR0FBRyJ9
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"raw_buffer.d.ts","sourceRoot":"","sources":["../../src/types/raw_buffer.ts"],"names":[],"mappings":"AAEA,qBAAa,SAAU,SAAQ,UAAU;CAAG"}
|
package/dest/types/raw_buffer.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
// Used when the data is to be sent exactly as is. i.e. no length prefix will be added.
|
|
2
|
-
// This is useful for sending structured data that can be parsed-as-you-go, as opposed to just an array of bytes.
|
|
3
|
-
export class RawBuffer extends Uint8Array {
|
|
4
|
-
}
|
|
5
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmF3X2J1ZmZlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy90eXBlcy9yYXdfYnVmZmVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLHVGQUF1RjtBQUN2RixpSEFBaUg7QUFDakgsTUFBTSxPQUFPLFNBQVUsU0FBUSxVQUFVO0NBQUcifQ==
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { newBarretenbergApiSync } from '../factory/index.js';
|
|
2
|
-
import { Buffer32, Fr } from '../types/index.js';
|
|
3
|
-
import { BarretenbergApiSync } from './index.js';
|
|
4
|
-
|
|
5
|
-
describe('blake2s', () => {
|
|
6
|
-
let api: BarretenbergApiSync;
|
|
7
|
-
|
|
8
|
-
beforeAll(async () => {
|
|
9
|
-
api = await newBarretenbergApiSync();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
afterAll(async () => {
|
|
13
|
-
await api.destroy();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('blake2s', () => {
|
|
17
|
-
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
|
|
18
|
-
const expected = Buffer32.fromBuffer(
|
|
19
|
-
new Uint8Array([
|
|
20
|
-
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
|
|
21
|
-
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
|
|
22
|
-
]),
|
|
23
|
-
);
|
|
24
|
-
const result = api.blake2s(input);
|
|
25
|
-
expect(result).toEqual(expected);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('blake2sToField', () => {
|
|
29
|
-
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
|
|
30
|
-
const expected = Fr.fromBufferReduce(
|
|
31
|
-
new Uint8Array([
|
|
32
|
-
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
|
|
33
|
-
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
|
|
34
|
-
]),
|
|
35
|
-
);
|
|
36
|
-
const result = api.blake2sToField(input);
|
|
37
|
-
expect(result).toEqual(expected);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { newBarretenbergApiSync } from '../factory/index.js';
|
|
2
|
-
import { Fr } from '../types/index.js';
|
|
3
|
-
import { BarretenbergApiSync } from './index.js';
|
|
4
|
-
|
|
5
|
-
describe('pedersen', () => {
|
|
6
|
-
let api: BarretenbergApiSync;
|
|
7
|
-
|
|
8
|
-
beforeAll(async () => {
|
|
9
|
-
api = await newBarretenbergApiSync();
|
|
10
|
-
api.pedersenHashInit();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
afterAll(async () => {
|
|
14
|
-
await api.destroy();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('pedersenCompressFields', () => {
|
|
18
|
-
const result = api.pedersenCompressFields(new Fr(4n), new Fr(8n));
|
|
19
|
-
expect(result).toEqual(new Fr(16672613430297770667465722499387909817686322516130512258122141976728892914370n));
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('pedersenPlookupCompressFields', () => {
|
|
23
|
-
const result = api.pedersenPlookupCompressFields(new Fr(4n), new Fr(8n));
|
|
24
|
-
expect(result).toEqual(new Fr(21568810706345846819294487214368613840251909831689369685420108292337497444070n));
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('pedersenCompress', () => {
|
|
28
|
-
const result = api.pedersenCompress([new Fr(4n), new Fr(8n), new Fr(12n)]);
|
|
29
|
-
expect(result).toEqual(new Fr(20749503715308760529311051818180468653739005441229560405092292242074298877245n));
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('pedersenPlookupCompress', () => {
|
|
33
|
-
const result = api.pedersenPlookupCompress([new Fr(4n), new Fr(8n), new Fr(12n)]);
|
|
34
|
-
expect(result).toEqual(new Fr(4213911891650716450883144878301329379460622830501147795631256054071351353887n));
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it('pedersenCompressWithHashIndex', () => {
|
|
38
|
-
const result = api.pedersenCompressWithHashIndex([new Fr(4n), new Fr(8n)], 7);
|
|
39
|
-
expect(result).toEqual(new Fr(12675961871866002745031098923411501942277744385859978302365013982702509949754n));
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('pedersenCommit', () => {
|
|
43
|
-
const result = api.pedersenCommit([new Fr(4n), new Fr(8n), new Fr(12n)]);
|
|
44
|
-
expect(result).toEqual(new Fr(20749503715308760529311051818180468653739005441229560405092292242074298877245n));
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('pedersenPlookupCommit', () => {
|
|
48
|
-
const result = api.pedersenPlookupCommit([new Fr(4n), new Fr(8n)]);
|
|
49
|
-
expect(result).toEqual(new Fr(21568810706345846819294487214368613840251909831689369685420108292337497444070n));
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('pedersenBufferToField', () => {
|
|
53
|
-
const result = api.pedersenBufferToField(Buffer.from('Hello world! I am a buffer to be converted to a field!'));
|
|
54
|
-
expect(result).toEqual(new Fr(4923399520610513632896240312051201308554838580477778325691012985962614653619n));
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('pedersenHashPair', () => {
|
|
58
|
-
const result = api.pedersenHashPair(new Fr(4n), new Fr(8n));
|
|
59
|
-
expect(result).toEqual(new Fr(7508407170365331152493586290597472346478280823936748458450026785528968221772n));
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('pedersenHashMultiple', () => {
|
|
63
|
-
const result = api.pedersenHashMultiple([new Fr(4n), new Fr(8n), new Fr(12n)]);
|
|
64
|
-
expect(result).toEqual(new Fr(641613987782189905475142047603559162464012327378197326488471789040703504911n));
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('pedersenHashMultipleWithHashIndex', () => {
|
|
68
|
-
const result = api.pedersenHashMultipleWithHashIndex([new Fr(4n), new Fr(8n)], 7);
|
|
69
|
-
expect(result).toEqual(new Fr(14181105996307540196932058280391669339364159586581375348016341320932872505408n));
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('pedersenHashToTree', () => {
|
|
73
|
-
const result = api.pedersenHashToTree([new Fr(4n), new Fr(8n), new Fr(12n), new Fr(16n)]);
|
|
74
|
-
expect(result).toEqual([
|
|
75
|
-
new Fr(4n),
|
|
76
|
-
new Fr(8n),
|
|
77
|
-
new Fr(12n),
|
|
78
|
-
new Fr(16n),
|
|
79
|
-
new Fr(7508407170365331152493586290597472346478280823936748458450026785528968221772n),
|
|
80
|
-
new Fr(61370238324203854110612958249832030753990119715269709182131929073387209477n),
|
|
81
|
-
new Fr(7696240979753031171651958947943309270095593128155855154123615677953596407768n),
|
|
82
|
-
]);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
@@ -1,169 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,62 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
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
|
-
}, 20000);
|
|
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
|
-
});
|