@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
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { BackendOptions, Barretenberg, CircuitOptions } from './index.js';
|
|
2
|
+
import { RawBuffer } from '../types/raw_buffer.js';
|
|
3
|
+
import {
|
|
4
|
+
ProofData,
|
|
5
|
+
reconstructHonkProof,
|
|
6
|
+
splitHonkProof,
|
|
7
|
+
PAIRING_POINTS_SIZE,
|
|
8
|
+
uint8ArrayToHex,
|
|
9
|
+
hexToUint8Array,
|
|
10
|
+
} from '../proof/index.js';
|
|
11
|
+
import { fromClientIVCProof, toClientIVCProof } from '../cbind/generated/api_types.js';
|
|
12
|
+
import { ungzip } from 'pako';
|
|
13
|
+
import { Buffer } from 'buffer';
|
|
14
|
+
import { Decoder, Encoder } from 'msgpackr';
|
|
15
|
+
|
|
16
|
+
export class AztecClientBackendError extends Error {
|
|
17
|
+
constructor(message: string) {
|
|
18
|
+
super(message);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Options for the UltraHonkBackend.
|
|
24
|
+
*/
|
|
25
|
+
export type UltraHonkBackendOptions = {
|
|
26
|
+
/** Selecting this option will use the keccak hash function instead of poseidon
|
|
27
|
+
* when generating challenges in the proof.
|
|
28
|
+
* Use this when you want to verify the created proof on an EVM chain.
|
|
29
|
+
*/
|
|
30
|
+
keccak?: boolean;
|
|
31
|
+
/** Selecting this option will use the keccak hash function instead of poseidon
|
|
32
|
+
* when generating challenges in the proof.
|
|
33
|
+
* Use this when you want to verify the created proof on an EVM chain.
|
|
34
|
+
*/
|
|
35
|
+
keccakZK?: boolean;
|
|
36
|
+
/** Selecting this option will use the poseidon/stark252 hash function instead of poseidon
|
|
37
|
+
* when generating challenges in the proof.
|
|
38
|
+
* Use this when you want to verify the created proof on an Starknet chain with Garaga.
|
|
39
|
+
*/
|
|
40
|
+
starknet?: boolean;
|
|
41
|
+
/** Selecting this option will use the poseidon/stark252 hash function instead of poseidon
|
|
42
|
+
* when generating challenges in the proof.
|
|
43
|
+
* Use this when you want to verify the created proof on an Starknet chain with Garaga.
|
|
44
|
+
*/
|
|
45
|
+
starknetZK?: boolean;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function getProofSettingsFromOptions(
|
|
49
|
+
options?: UltraHonkBackendOptions,
|
|
50
|
+
): { ipaAccumulation: boolean; oracleHashType: string; disableZk: boolean, optimizedSolidityVerifier: boolean } {
|
|
51
|
+
return {
|
|
52
|
+
ipaAccumulation: false,
|
|
53
|
+
oracleHashType: options?.keccak || options?.keccakZK ? 'keccak' : (options?.starknet || options?.starknetZK ? 'starknet' : 'poseidon2'),
|
|
54
|
+
// TODO no current way to target non-zk poseidon2 hash
|
|
55
|
+
disableZk: options?.keccak || options?.starknet ? true : false,
|
|
56
|
+
optimizedSolidityVerifier: false,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class UltraHonkVerifierBackend {
|
|
61
|
+
protected api!: Barretenberg;
|
|
62
|
+
|
|
63
|
+
constructor(
|
|
64
|
+
protected backendOptions: BackendOptions = { threads: 1 },
|
|
65
|
+
protected circuitOptions: CircuitOptions = { recursive: false },
|
|
66
|
+
) {}
|
|
67
|
+
/** @ignore */
|
|
68
|
+
private async instantiate(): Promise<void> {
|
|
69
|
+
if (!this.api) {
|
|
70
|
+
const api = await Barretenberg.new(this.backendOptions);
|
|
71
|
+
const honkRecursion = true;
|
|
72
|
+
await api.initSRSForCircuitSize(0);
|
|
73
|
+
|
|
74
|
+
this.api = api;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async verifyProof(proofData: ProofData & { verificationKey: Uint8Array }, options?: UltraHonkBackendOptions): Promise<boolean> {
|
|
79
|
+
await this.instantiate();
|
|
80
|
+
|
|
81
|
+
const proofFrs: Uint8Array[] = [];
|
|
82
|
+
for (let i = 0; i < proofData.proof.length; i += 32) {
|
|
83
|
+
proofFrs.push(proofData.proof.slice(i, i + 32));
|
|
84
|
+
}
|
|
85
|
+
const { verified } = await this.api.circuitVerify({
|
|
86
|
+
verificationKey: proofData.verificationKey,
|
|
87
|
+
publicInputs: proofData.publicInputs.map(hexToUint8Array),
|
|
88
|
+
proof: proofFrs,
|
|
89
|
+
settings: getProofSettingsFromOptions(options),
|
|
90
|
+
});
|
|
91
|
+
return verified;
|
|
92
|
+
}
|
|
93
|
+
destroy(): Promise<void> {
|
|
94
|
+
if (!this.api) {
|
|
95
|
+
return Promise.resolve();
|
|
96
|
+
}
|
|
97
|
+
return this.api.destroy();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class UltraHonkBackend {
|
|
102
|
+
// These type assertions are used so that we don't
|
|
103
|
+
// have to initialize `api` in the constructor.
|
|
104
|
+
// These are initialized asynchronously in the `init` function,
|
|
105
|
+
// constructors cannot be asynchronous which is why we do this.
|
|
106
|
+
|
|
107
|
+
protected api!: Barretenberg;
|
|
108
|
+
protected acirUncompressedBytecode: Uint8Array;
|
|
109
|
+
|
|
110
|
+
constructor(
|
|
111
|
+
acirBytecode: string,
|
|
112
|
+
protected backendOptions: BackendOptions = { threads: 1 },
|
|
113
|
+
protected circuitOptions: CircuitOptions = { recursive: false },
|
|
114
|
+
) {
|
|
115
|
+
this.acirUncompressedBytecode = acirToUint8Array(acirBytecode);
|
|
116
|
+
}
|
|
117
|
+
/** @ignore */
|
|
118
|
+
private async instantiate(): Promise<void> {
|
|
119
|
+
if (!this.api) {
|
|
120
|
+
const api = await Barretenberg.new(this.backendOptions);
|
|
121
|
+
const honkRecursion = true;
|
|
122
|
+
await api.acirInitSRS(this.acirUncompressedBytecode, this.circuitOptions.recursive, honkRecursion);
|
|
123
|
+
|
|
124
|
+
this.api = api;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async generateProof(compressedWitness: Uint8Array, options?: UltraHonkBackendOptions): Promise<ProofData> {
|
|
129
|
+
await this.instantiate();
|
|
130
|
+
|
|
131
|
+
const witness = ungzip(compressedWitness);
|
|
132
|
+
const { proof, publicInputs } = await this.api.circuitProve({
|
|
133
|
+
witness,
|
|
134
|
+
circuit: {
|
|
135
|
+
name: 'circuit',
|
|
136
|
+
bytecode: Buffer.from(this.acirUncompressedBytecode),
|
|
137
|
+
verificationKey: Buffer.from([]), // Empty VK - lower performance.
|
|
138
|
+
},
|
|
139
|
+
settings: getProofSettingsFromOptions(options)
|
|
140
|
+
});
|
|
141
|
+
console.log(`Generated proof for circuit with ${publicInputs.length} public inputs and ${proof.length} fields.`);
|
|
142
|
+
|
|
143
|
+
// We return ProofData as a flat buffer and an array of strings to match the current ProofData class.
|
|
144
|
+
const flatProof = new Uint8Array(proof.length * 32);
|
|
145
|
+
proof.forEach((fr, i) => {
|
|
146
|
+
flatProof.set(fr, i * 32);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
return { proof: flatProof, publicInputs: publicInputs.map(uint8ArrayToHex) };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async verifyProof(proofData: ProofData, options?: UltraHonkBackendOptions): Promise<boolean> {
|
|
153
|
+
await this.instantiate();
|
|
154
|
+
|
|
155
|
+
const proofFrs: Uint8Array[] = [];
|
|
156
|
+
for (let i = 0; i < proofData.proof.length; i += 32) {
|
|
157
|
+
proofFrs.push(proofData.proof.slice(i, i + 32));
|
|
158
|
+
}
|
|
159
|
+
// TODO reconsider API - computing the VK at this point is not optimal
|
|
160
|
+
const vkResult = await this.api.circuitComputeVk({
|
|
161
|
+
circuit: {
|
|
162
|
+
name: 'circuit',
|
|
163
|
+
bytecode: this.acirUncompressedBytecode,
|
|
164
|
+
},
|
|
165
|
+
settings: getProofSettingsFromOptions(options),
|
|
166
|
+
});
|
|
167
|
+
const {verified} = await this.api.circuitVerify({
|
|
168
|
+
verificationKey: vkResult.bytes,
|
|
169
|
+
publicInputs: proofData.publicInputs.map(hexToUint8Array),
|
|
170
|
+
proof: proofFrs,
|
|
171
|
+
settings: getProofSettingsFromOptions(options),
|
|
172
|
+
});
|
|
173
|
+
return verified;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async getVerificationKey(options?: UltraHonkBackendOptions): Promise<Uint8Array> {
|
|
177
|
+
await this.instantiate();
|
|
178
|
+
|
|
179
|
+
const vkResult = await this.api.circuitComputeVk({
|
|
180
|
+
circuit: {
|
|
181
|
+
name: 'circuit',
|
|
182
|
+
bytecode: Buffer.from(this.acirUncompressedBytecode),
|
|
183
|
+
},
|
|
184
|
+
settings: getProofSettingsFromOptions(options),
|
|
185
|
+
});
|
|
186
|
+
return vkResult.bytes;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** @description Returns a solidity verifier */
|
|
190
|
+
async getSolidityVerifier(vk?: Uint8Array): Promise<string> {
|
|
191
|
+
await this.instantiate();
|
|
192
|
+
const vkBuf = vk ?? (await this.api.acirWriteVkUltraKeccakHonk(this.acirUncompressedBytecode));
|
|
193
|
+
return await this.api.acirHonkSolidityVerifier(this.acirUncompressedBytecode, new RawBuffer(vkBuf));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// TODO(https://github.com/noir-lang/noir/issues/5661): Update this to handle Honk recursive aggregation in the browser once it is ready in the backend itself
|
|
197
|
+
async generateRecursiveProofArtifacts(
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
199
|
+
_proof: Uint8Array,
|
|
200
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
201
|
+
_numOfPublicInputs: number,
|
|
202
|
+
): Promise<{ proofAsFields: string[]; vkAsFields: string[]; vkHash: string }> {
|
|
203
|
+
await this.instantiate();
|
|
204
|
+
// TODO(https://github.com/noir-lang/noir/issues/5661): This needs to be updated to handle recursive aggregation.
|
|
205
|
+
// There is still a proofAsFields method but we could consider getting rid of it as the proof itself
|
|
206
|
+
// is a list of field elements.
|
|
207
|
+
// UltraHonk also does not have public inputs directly prepended to the proof and they are still instead
|
|
208
|
+
// inserted at an offset.
|
|
209
|
+
// const proof = reconstructProofWithPublicInputs(proofData);
|
|
210
|
+
// const proofAsFields = (await this.api.acirProofAsFieldsUltraHonk(proof)).slice(numOfPublicInputs);
|
|
211
|
+
|
|
212
|
+
// TODO: perhaps we should put this in the init function. Need to benchmark
|
|
213
|
+
// TODO how long it takes.
|
|
214
|
+
const vkResult = await this.api.circuitComputeVk({
|
|
215
|
+
circuit: {
|
|
216
|
+
name: 'circuit',
|
|
217
|
+
bytecode: Buffer.from(this.acirUncompressedBytecode),
|
|
218
|
+
},
|
|
219
|
+
settings: getProofSettingsFromOptions({}),
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// Convert VK bytes to field elements (32-byte chunks)
|
|
223
|
+
const vkAsFields: string[] = [];
|
|
224
|
+
for (let i = 0; i < vkResult.bytes.length; i += 32) {
|
|
225
|
+
const chunk = vkResult.bytes.slice(i, i + 32);
|
|
226
|
+
vkAsFields.push(uint8ArrayToHex(chunk));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
// TODO(https://github.com/noir-lang/noir/issues/5661)
|
|
231
|
+
proofAsFields: [],
|
|
232
|
+
vkAsFields,
|
|
233
|
+
// We use an empty string for the vk hash here as it is unneeded as part of the recursive artifacts
|
|
234
|
+
// The user can be expected to hash the vk inside their circuit to check whether the vk is the circuit
|
|
235
|
+
// they expect
|
|
236
|
+
vkHash: uint8ArrayToHex(vkResult.hash)
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async destroy(): Promise<void> {
|
|
241
|
+
if (!this.api) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
await this.api.destroy();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export class AztecClientBackend {
|
|
249
|
+
// These type assertions are used so that we don't
|
|
250
|
+
// have to initialize `api` in the constructor.
|
|
251
|
+
// These are initialized asynchronously in the `init` function,
|
|
252
|
+
// constructors cannot be asynchronous which is why we do this.
|
|
253
|
+
|
|
254
|
+
protected api!: Barretenberg;
|
|
255
|
+
|
|
256
|
+
constructor(
|
|
257
|
+
protected acirBuf: Uint8Array[],
|
|
258
|
+
protected options: BackendOptions = { threads: 1 },
|
|
259
|
+
) {}
|
|
260
|
+
|
|
261
|
+
/** @ignore */
|
|
262
|
+
private async instantiate(): Promise<void> {
|
|
263
|
+
if (!this.api) {
|
|
264
|
+
const api = await Barretenberg.new(this.options);
|
|
265
|
+
await api.initSRSClientIVC();
|
|
266
|
+
this.api = api;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async prove(witnessBuf: Uint8Array[], vksBuf: Uint8Array[] = []): Promise<[Uint8Array, Uint8Array]> {
|
|
271
|
+
if (vksBuf.length !== 0 && this.acirBuf.length !== witnessBuf.length) {
|
|
272
|
+
throw new AztecClientBackendError('Witness and bytecodes must have the same stack depth!');
|
|
273
|
+
}
|
|
274
|
+
if (vksBuf.length !== 0 && vksBuf.length !== witnessBuf.length) {
|
|
275
|
+
// NOTE: we allow 0 as an explicit 'I have no VKs'. This is a deprecated feature.
|
|
276
|
+
throw new AztecClientBackendError('Witness and VKs must have the same stack depth!');
|
|
277
|
+
}
|
|
278
|
+
await this.instantiate();
|
|
279
|
+
|
|
280
|
+
// Queue IVC start with the number of circuits
|
|
281
|
+
this.api.clientIvcStart({ numCircuits: this.acirBuf.length });
|
|
282
|
+
|
|
283
|
+
// Queue load and accumulate for each circuit
|
|
284
|
+
for (let i = 0; i < this.acirBuf.length; i++) {
|
|
285
|
+
const bytecode = this.acirBuf[i];
|
|
286
|
+
const witness = witnessBuf[i] || Buffer.from([]);
|
|
287
|
+
const vk = vksBuf[i] || Buffer.from([]);
|
|
288
|
+
const functionName = `unknown_wasm_${i}`;
|
|
289
|
+
|
|
290
|
+
// Load the circuit
|
|
291
|
+
this.api.clientIvcLoad({
|
|
292
|
+
circuit: {
|
|
293
|
+
name: functionName,
|
|
294
|
+
bytecode: Buffer.from(bytecode),
|
|
295
|
+
verificationKey: Buffer.from(vk),
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Accumulate with witness
|
|
300
|
+
this.api.clientIvcAccumulate({
|
|
301
|
+
witness: Buffer.from(witness),
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
// Generate the proof (and wait for all previous steps to finish)
|
|
309
|
+
const proveResult = await this.api.clientIvcProve({});
|
|
310
|
+
// The API currently expects a msgpack-encoded API.
|
|
311
|
+
const proof = new Encoder({useRecords: false}).encode(fromClientIVCProof(proveResult.proof));
|
|
312
|
+
// Generate the VK
|
|
313
|
+
const vkResult = await this.api.clientIvcComputeIvcVk({ circuit: {
|
|
314
|
+
name: 'hiding',
|
|
315
|
+
bytecode: this.acirBuf[this.acirBuf.length - 1],
|
|
316
|
+
} });
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
// Note: Verification may not work correctly until we properly serialize the proof
|
|
320
|
+
if (!(await this.verify(proof, vkResult.bytes))) {
|
|
321
|
+
throw new AztecClientBackendError('Failed to verify the private (ClientIVC) transaction proof!');
|
|
322
|
+
}
|
|
323
|
+
return [proof, vkResult.bytes];
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async verify(proof: Uint8Array, vk: Uint8Array): Promise<boolean> {
|
|
327
|
+
await this.instantiate();
|
|
328
|
+
const result = await this.api.clientIvcVerify({
|
|
329
|
+
proof: toClientIVCProof(new Decoder({useRecords: false}).decode(proof)),
|
|
330
|
+
vk: Buffer.from(vk),
|
|
331
|
+
});
|
|
332
|
+
return result.valid;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async gates(): Promise<number[]> {
|
|
336
|
+
await this.instantiate();
|
|
337
|
+
const circuitSizes: number[] = [];
|
|
338
|
+
for (const buf of this.acirBuf) {
|
|
339
|
+
const gates = await this.api.clientIvcStats({
|
|
340
|
+
circuit: {
|
|
341
|
+
name: 'circuit',
|
|
342
|
+
bytecode: buf,
|
|
343
|
+
},
|
|
344
|
+
includeGatesPerOpcode: false
|
|
345
|
+
});
|
|
346
|
+
circuitSizes.push(gates.circuitSize);
|
|
347
|
+
}
|
|
348
|
+
return circuitSizes;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async destroy(): Promise<void> {
|
|
352
|
+
if (!this.api) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
await this.api.destroy();
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Converts bytecode from a base64 string to a Uint8Array
|
|
360
|
+
function acirToUint8Array(base64EncodedBytecode: string): Uint8Array {
|
|
361
|
+
const compressedByteCode = base64Decode(base64EncodedBytecode);
|
|
362
|
+
return ungzip(compressedByteCode);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Since this is a simple function, we can use feature detection to
|
|
366
|
+
// see if we are in the nodeJs environment or the browser environment.
|
|
367
|
+
function base64Decode(input: string): Uint8Array {
|
|
368
|
+
if (typeof Buffer !== 'undefined') {
|
|
369
|
+
// Node.js environment
|
|
370
|
+
const b = Buffer.from(input, 'base64');
|
|
371
|
+
return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
|
|
372
|
+
} else if (typeof atob === 'function') {
|
|
373
|
+
// Browser environment
|
|
374
|
+
return Uint8Array.from(atob(input), c => c.charCodeAt(0));
|
|
375
|
+
} else {
|
|
376
|
+
throw new Error('No implementation found for base64 decoding.');
|
|
377
|
+
}
|
|
378
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Barretenberg, BarretenbergSync } from './index.js';
|
|
2
|
+
import { Buffer32, Fr } from '../types/index.js';
|
|
3
|
+
|
|
4
|
+
describe('blake2s async', () => {
|
|
5
|
+
let api: Barretenberg;
|
|
6
|
+
|
|
7
|
+
beforeAll(async () => {
|
|
8
|
+
api = await Barretenberg.new({ threads: 1 });
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
afterAll(async () => {
|
|
12
|
+
await api.destroy();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('blake2s', async () => {
|
|
16
|
+
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
|
|
17
|
+
const expected = Buffer32.fromBuffer(
|
|
18
|
+
new Uint8Array([
|
|
19
|
+
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
|
|
20
|
+
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
|
|
21
|
+
]),
|
|
22
|
+
);
|
|
23
|
+
const result = await api.blake2s(input);
|
|
24
|
+
expect(result).toEqual(expected);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('blake2sToField', async () => {
|
|
28
|
+
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
|
|
29
|
+
const expected = Fr.fromBufferReduce(
|
|
30
|
+
new Uint8Array([
|
|
31
|
+
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
|
|
32
|
+
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
|
|
33
|
+
]),
|
|
34
|
+
);
|
|
35
|
+
const result = await api.blake2sToField(input);
|
|
36
|
+
expect(result).toEqual(expected);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('blake2s sync', () => {
|
|
41
|
+
let api: BarretenbergSync;
|
|
42
|
+
|
|
43
|
+
beforeAll(async () => {
|
|
44
|
+
api = await BarretenbergSync.initSingleton();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('blake2s', () => {
|
|
48
|
+
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
|
|
49
|
+
const expected = Buffer32.fromBuffer(
|
|
50
|
+
new Uint8Array([
|
|
51
|
+
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
|
|
52
|
+
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
|
|
53
|
+
]),
|
|
54
|
+
);
|
|
55
|
+
const result = api.blake2s(input);
|
|
56
|
+
expect(result).toEqual(expected);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('blake2sToField', () => {
|
|
60
|
+
const input = Buffer.from('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
|
|
61
|
+
const expected = Fr.fromBufferReduce(
|
|
62
|
+
new Uint8Array([
|
|
63
|
+
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
|
|
64
|
+
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
|
|
65
|
+
]),
|
|
66
|
+
);
|
|
67
|
+
const result = api.blake2sToField(input);
|
|
68
|
+
expect(result).toEqual(expected);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Barretenberg } from './index.js';
|
|
2
2
|
|
|
3
3
|
describe('env', () => {
|
|
4
|
-
let api:
|
|
4
|
+
let api: Barretenberg;
|
|
5
5
|
|
|
6
6
|
beforeAll(async () => {
|
|
7
|
-
api = await
|
|
8
|
-
});
|
|
7
|
+
api = await Barretenberg.new({ threads: 3 });
|
|
8
|
+
}, 30000);
|
|
9
9
|
|
|
10
10
|
afterAll(async () => {
|
|
11
|
-
|
|
11
|
+
if (api) {
|
|
12
|
+
await api.destroy();
|
|
13
|
+
}
|
|
12
14
|
});
|
|
13
15
|
|
|
14
16
|
it('thread test', async () => {
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { proxy } from 'comlink';
|
|
2
|
+
import { BarretenbergApi, BarretenbergApiSync } from '../barretenberg_api/index.js';
|
|
3
|
+
import { createMainWorker } from '../barretenberg_wasm/barretenberg_wasm_main/factory/node/index.js';
|
|
4
|
+
import { BarretenbergWasmMain, BarretenbergWasmMainWorker } from '../barretenberg_wasm/barretenberg_wasm_main/index.js';
|
|
5
|
+
import { getRemoteBarretenbergWasm } from '../barretenberg_wasm/helpers/index.js';
|
|
6
|
+
import { Crs, GrumpkinCrs } from '../crs/index.js';
|
|
7
|
+
import { RawBuffer } from '../types/raw_buffer.js';
|
|
8
|
+
import { fetchModuleAndThreads } from '../barretenberg_wasm/index.js';
|
|
9
|
+
import { createDebugLogger } from '../log/index.js';
|
|
10
|
+
import { AsyncApi } from '../cbind/generated/async.js';
|
|
11
|
+
import { BbApiBase, CircuitComputeVk, CircuitProve, CircuitVerify, ClientIvcAccumulate, ClientIvcComputeIvcVk, ClientIvcStats, ClientIvcLoad, ClientIvcProve, ClientIvcStart, ClientIvcVerify, VkAsFields } from '../cbind/generated/api_types.js';
|
|
12
|
+
|
|
13
|
+
export { UltraHonkBackend, UltraHonkVerifierBackend, AztecClientBackend } from './backend.js';
|
|
14
|
+
|
|
15
|
+
export type BackendOptions = {
|
|
16
|
+
/** @description Number of threads to run the backend worker on */
|
|
17
|
+
threads?: number;
|
|
18
|
+
|
|
19
|
+
/** @description Initial and Maximum memory to be alloted to the backend worker */
|
|
20
|
+
memory?: { initial?: number; maximum?: number };
|
|
21
|
+
|
|
22
|
+
/** @description Path to download CRS files */
|
|
23
|
+
crsPath?: string;
|
|
24
|
+
|
|
25
|
+
/** @description Path to download WASM files */
|
|
26
|
+
wasmPath?: string;
|
|
27
|
+
|
|
28
|
+
/** @description Logging function */
|
|
29
|
+
logger?: (msg: string) => void;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type CircuitOptions = {
|
|
33
|
+
/** @description Whether to produce SNARK friendly proofs */
|
|
34
|
+
recursive: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The main class library consumers interact with.
|
|
39
|
+
* It extends the generated api, and provides a static constructor "new" to compose components.
|
|
40
|
+
*/
|
|
41
|
+
export class Barretenberg extends BarretenbergApi {
|
|
42
|
+
private options: BackendOptions;
|
|
43
|
+
private bbApi: BbApiBase;
|
|
44
|
+
|
|
45
|
+
private constructor(
|
|
46
|
+
private worker: any,
|
|
47
|
+
wasm: BarretenbergWasmMainWorker,
|
|
48
|
+
options: BackendOptions,
|
|
49
|
+
) {
|
|
50
|
+
super(wasm);
|
|
51
|
+
this.options = options;
|
|
52
|
+
this.bbApi = new AsyncApi(wasm);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Constructs an instance of Barretenberg.
|
|
57
|
+
* Launches it within a worker. This is necessary as it blocks waiting on child threads to complete,
|
|
58
|
+
* and blocking the main thread in the browser is not allowed.
|
|
59
|
+
* It threads > 1 (defaults to hardware availability), child threads will be created on their own workers.
|
|
60
|
+
*/
|
|
61
|
+
static async new(options: BackendOptions = {}) {
|
|
62
|
+
const worker = await createMainWorker();
|
|
63
|
+
const wasm = getRemoteBarretenbergWasm<BarretenbergWasmMainWorker>(worker);
|
|
64
|
+
const { module, threads } = await fetchModuleAndThreads(options.threads, options.wasmPath, options.logger);
|
|
65
|
+
await wasm.init(
|
|
66
|
+
module,
|
|
67
|
+
threads,
|
|
68
|
+
proxy(options.logger ?? createDebugLogger('bb_wasm_async')),
|
|
69
|
+
options.memory?.initial,
|
|
70
|
+
options.memory?.maximum,
|
|
71
|
+
);
|
|
72
|
+
return new Barretenberg(worker, wasm, options);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async getNumThreads() {
|
|
76
|
+
return await this.wasm.getNumThreads();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async initSRSForCircuitSize(circuitSize: number): Promise<void> {
|
|
80
|
+
const minSRSSize = 2 ** 9; // 2**9 is the dyadic size for the SmallSubgroupIPA MSM.
|
|
81
|
+
const crs = await Crs.new(Math.max(circuitSize, minSRSSize) + 1, this.options.crsPath, this.options.logger);
|
|
82
|
+
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1129): Do slab allocator initialization?
|
|
83
|
+
// await this.commonInitSlabAllocator(circuitSize);
|
|
84
|
+
await this.srsInitSrs(new RawBuffer(crs.getG1Data()), crs.numPoints, new RawBuffer(crs.getG2Data()));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async initSRSClientIVC(srsSize = this.getDefaultSrsSize()): Promise<void> {
|
|
88
|
+
// crsPath can be undefined
|
|
89
|
+
const crs = await Crs.new(srsSize + 1, this.options.crsPath, this.options.logger);
|
|
90
|
+
const grumpkinCrs = await GrumpkinCrs.new(2 ** 16 + 1, this.options.crsPath, this.options.logger);
|
|
91
|
+
|
|
92
|
+
// Load CRS into wasm global CRS state.
|
|
93
|
+
// TODO: Make RawBuffer be default behavior, and have a specific Vector type for when wanting length prefixed.
|
|
94
|
+
await this.srsInitSrs(new RawBuffer(crs.getG1Data()), crs.numPoints, new RawBuffer(crs.getG2Data()));
|
|
95
|
+
await this.srsInitGrumpkinSrs(new RawBuffer(grumpkinCrs.getG1Data()), grumpkinCrs.numPoints);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
getDefaultSrsSize(): number {
|
|
99
|
+
// iOS browser is very aggressive with memory. Check if running in browser and on iOS
|
|
100
|
+
// We expect the mobile iOS browser to kill us >=1GB, so no real use in using a larger SRS.
|
|
101
|
+
if (typeof window !== 'undefined' && /iPad|iPhone/.test(navigator.userAgent)) {
|
|
102
|
+
return 2 ** 18;
|
|
103
|
+
}
|
|
104
|
+
return 2 ** 20;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async acirInitSRS(bytecode: Uint8Array, recursive: boolean, honkRecursion: boolean): Promise<void> {
|
|
108
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
109
|
+
const [_total, subgroupSize] = await this.acirGetCircuitSizes(bytecode, recursive, honkRecursion);
|
|
110
|
+
return this.initSRSForCircuitSize(subgroupSize);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async destroy() {
|
|
114
|
+
await this.wasm.destroy();
|
|
115
|
+
await this.worker.terminate();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getWasm() {
|
|
119
|
+
return this.wasm;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Wrap ClientIVC methods used by AztecClientBackend and UltraHonkBackend
|
|
123
|
+
async clientIvcStart(command: ClientIvcStart) {
|
|
124
|
+
return this.bbApi.clientIvcStart(command);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async clientIvcLoad(command: ClientIvcLoad) {
|
|
128
|
+
return this.bbApi.clientIvcLoad(command);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async clientIvcAccumulate(command: ClientIvcAccumulate) {
|
|
132
|
+
return this.bbApi.clientIvcAccumulate(command);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async clientIvcProve(command: ClientIvcProve) {
|
|
136
|
+
return this.bbApi.clientIvcProve(command);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async clientIvcVerify(command: ClientIvcVerify) {
|
|
140
|
+
return this.bbApi.clientIvcVerify(command);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async clientIvcComputeIvcVk(command: ClientIvcComputeIvcVk) {
|
|
144
|
+
return this.bbApi.clientIvcComputeIvcVk(command);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async clientIvcStats(command: ClientIvcStats) {
|
|
148
|
+
return this.bbApi.clientIvcStats(command);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Wrap circuit methods used by BbApiUltraHonkBackend
|
|
152
|
+
async circuitProve(command: CircuitProve) {
|
|
153
|
+
return this.bbApi.circuitProve(command);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async circuitComputeVk(command: CircuitComputeVk) {
|
|
157
|
+
return this.bbApi.circuitComputeVk(command);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async circuitVerify(command: CircuitVerify) {
|
|
161
|
+
return this.bbApi.circuitVerify(command);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async vkAsFields(command: VkAsFields) {
|
|
165
|
+
return this.bbApi.vkAsFields(command);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
let barretenbergSyncSingletonPromise: Promise<BarretenbergSync>;
|
|
171
|
+
let barretenbergSyncSingleton: BarretenbergSync;
|
|
172
|
+
|
|
173
|
+
export class BarretenbergSync extends BarretenbergApiSync {
|
|
174
|
+
private constructor(wasm: BarretenbergWasmMain) {
|
|
175
|
+
super(wasm);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private static async new(wasmPath?: string, logger: (msg: string) => void = createDebugLogger('bb_wasm_sync')) {
|
|
179
|
+
const wasm = new BarretenbergWasmMain();
|
|
180
|
+
const { module, threads } = await fetchModuleAndThreads(1, wasmPath, logger);
|
|
181
|
+
await wasm.init(module, threads, logger);
|
|
182
|
+
return new BarretenbergSync(wasm);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
static async initSingleton(wasmPath?: string, logger: (msg: string) => void = createDebugLogger('bb_wasm_sync')) {
|
|
186
|
+
if (!barretenbergSyncSingletonPromise) {
|
|
187
|
+
barretenbergSyncSingletonPromise = BarretenbergSync.new(wasmPath, logger);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
barretenbergSyncSingleton = await barretenbergSyncSingletonPromise;
|
|
191
|
+
return barretenbergSyncSingleton;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
static getSingleton() {
|
|
195
|
+
if (!barretenbergSyncSingleton) {
|
|
196
|
+
throw new Error('First call BarretenbergSync.initSingleton() on @aztec/bb.js module.');
|
|
197
|
+
}
|
|
198
|
+
return barretenbergSyncSingleton;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
getWasm() {
|
|
202
|
+
return this.wasm;
|
|
203
|
+
}
|
|
204
|
+
}
|