Skip to content

Commit 679d456

Browse files
committed
add multithreading support
1 parent 162b01e commit 679d456

File tree

7 files changed

+142
-95
lines changed

7 files changed

+142
-95
lines changed

.cargo/config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
[target.'cfg(not(target_arch = "wasm32"))']
22
rustflags = ["-C", "target-cpu=native"]
3+
4+
[target.wasm32-unknown-unknown]
5+
rustflags = ["-C", "target-feature=+atomics,+bulk-memory,+mutable-globals"]
6+
7+
[unstable]
8+
build-std = ["panic_abort", "std"]

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm/Cargo.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ features = [ "preserve_order" ]
4747
path="../../snarkVM/synthesizer"
4848
version = "0.9.13"
4949
default-features = false
50-
features = [ "wasm" ]
50+
features = [ "wasm", "parallel", "web" ]
5151

5252
[dependencies.snarkvm-console]
5353
path="../../snarkVM/console"
5454
version = "0.9.13"
5555
default-features = false
56+
features = ["parallel"]
5657

5758
[dependencies.snarkvm-wasm]
5859
path="../../snarkVM/wasm"
@@ -82,12 +83,21 @@ version = "0.1.7"
8283
[dependencies.rand_chacha]
8384
version = "0.3.1"
8485

86+
[dependencies.wasm-bindgen-rayon]
87+
version = "1.0"
88+
89+
[dependencies.rayon]
90+
version = "1.5"
91+
8592
[dev-dependencies.wasm-bindgen-test]
8693
version = "0.3.33"
8794

8895
[profile.release]
8996
opt-level = 3
9097
lto = true
9198

99+
[package.metadata.wasm-pack.profile.debug]
100+
wasm-opt = ["-O4"]
101+
92102
[package.metadata.wasm-pack.profile.release]
93-
wasm-opt = ["-O4", "--fast-math"]
103+
wasm-opt = ["-O4"]

wasm/rust-toolchain

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nightly-2022-12-12

wasm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ pub mod program;
2424
pub use program::*;
2525

2626
pub(crate) mod types;
27+
28+
pub use wasm_bindgen_rayon::init_thread_pool;

website/src/workers/worker.js

Lines changed: 93 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,105 @@
1-
import "babel-polyfill";
1+
import init, * as aleo from '@aleohq/wasm';
2+
3+
await init();
4+
5+
await aleo.initThreadPool(navigator.hardwareConcurrency);
26

37
let TRANSFER_KEY;
48
let INCLUSION_KEY;
59

6-
import("@aleohq/wasm").then(aleo => {
7-
self.addEventListener("message", ev => {
8-
// Load Transfer Prover Key
9-
if (ev.data.type == 'ALEO_LOAD_TRANSFER_KEY') {
10-
console.log('Web worker: Deserialize Transfer Key...');
11-
let startTime = performance.now();
12-
TRANSFER_KEY = aleo.ProvingKey.from_bytes(ev.data.transferProverBytes);
13-
console.log(`Web worker: Deserialized transfer proving key Completed: ${performance.now() - startTime} ms`);
14-
self.postMessage({ type: 'TRANSFER_KEY_DESERIALIZED' });
15-
}
16-
// Load Inclusion Prover Key
17-
else if (ev.data.type == 'ALEO_LOAD_INCLUSION_KEY') {
18-
console.log('Web worker: Deserialize Inclusion Key...');
19-
console.log(ev.data);
20-
let startTime = performance.now();
21-
INCLUSION_KEY = aleo.ProvingKey.from_bytes(ev.data.inclusionProverBytes);
22-
console.log(`Web worker: Deserialized inclusion proving key Completed: ${performance.now() - startTime} ms`);
23-
self.postMessage({ type: 'INCLUSION_KEY_DESERIALIZED' });
24-
}
25-
// Create Transition
26-
else if (ev.data.type == 'ALEO_CREATE_TRANSITION') {
27-
const {
28-
privateKey,
29-
toAddress,
30-
amount,
31-
plaintext
32-
} = ev.data;
33-
const program = aleo.Program.credits();
10+
self.addEventListener("message", ev => {
11+
// Load Transfer Prover Key
12+
if (ev.data.type == 'ALEO_LOAD_TRANSFER_KEY') {
13+
console.log('Web worker: Deserialize Transfer Key...');
14+
let startTime = performance.now();
15+
TRANSFER_KEY = aleo.ProvingKey.from_bytes(ev.data.transferProverBytes);
16+
console.log(`Web worker: Deserialized transfer proving key Completed: ${performance.now() - startTime} ms`);
17+
self.postMessage({ type: 'TRANSFER_KEY_DESERIALIZED' });
18+
}
19+
// Load Inclusion Prover Key
20+
else if (ev.data.type == 'ALEO_LOAD_INCLUSION_KEY') {
21+
console.log('Web worker: Deserialize Inclusion Key...');
22+
console.log(ev.data);
23+
let startTime = performance.now();
24+
INCLUSION_KEY = aleo.ProvingKey.from_bytes(ev.data.inclusionProverBytes);
25+
console.log(`Web worker: Deserialized inclusion proving key Completed: ${performance.now() - startTime} ms`);
26+
self.postMessage({ type: 'INCLUSION_KEY_DESERIALIZED' });
27+
}
28+
// Create Transition
29+
else if (ev.data.type == 'ALEO_CREATE_TRANSITION') {
30+
const {
31+
privateKey,
32+
toAddress,
33+
amount,
34+
plaintext
35+
} = ev.data;
36+
const program = aleo.Program.credits();
3437

35-
console.log('Web worker: Building Transition for program: ', program.id());
36-
let startTime = performance.now();
38+
console.log('Web worker: Building Transition for program: ', program.id());
39+
let startTime = performance.now();
3740

38-
// Prepare inputs
39-
const pK = aleo.PrivateKey.from_string(privateKey);
40-
const inputs = JSON.stringify([plaintext, toAddress, `${amount}u64`])
41+
// Prepare inputs
42+
const pK = aleo.PrivateKey.from_string(privateKey);
43+
const inputs = JSON.stringify([plaintext, toAddress, `${amount}u64`])
4144

42-
const transition = aleo.TransactionBuilder.build_transition(
43-
program,
44-
'transfer',
45-
inputs,
46-
pK,
47-
TRANSFER_KEY
48-
);
49-
console.log(`Web worker: Transition Completed: ${performance.now() - startTime} ms`);
50-
console.log(`Transition: ${transition}`);
51-
self.postMessage({ type: 'TRANSITION_COMPLETED', transition });
52-
}
53-
else if (ev.data.type == 'ALEO_CREATE_TRANSACTION') {
54-
const {
55-
transition,
56-
inputIds,
57-
stateRoot,
58-
statePaths
59-
} = ev.data;
45+
const transition = aleo.TransactionBuilder.build_transition(
46+
program,
47+
'transfer',
48+
inputs,
49+
pK,
50+
TRANSFER_KEY
51+
);
52+
console.log(`Web worker: Transition Completed: ${performance.now() - startTime} ms`);
53+
console.log(`Transition: ${transition}`);
54+
self.postMessage({ type: 'TRANSITION_COMPLETED', transition });
55+
}
56+
else if (ev.data.type == 'ALEO_CREATE_TRANSACTION') {
57+
const {
58+
transition,
59+
inputIds,
60+
stateRoot,
61+
statePaths
62+
} = ev.data;
6063

61-
console.log('Web worker: Building Transaction...');
62-
let startTime = performance.now();
63-
let transitionParsed = JSON.parse(transition);
64-
let inputIdsParsed = JSON.parse(inputIds);
64+
console.log('Web worker: Building Transaction...');
65+
let startTime = performance.now();
66+
let transitionParsed = JSON.parse(transition);
67+
let inputIdsParsed = JSON.parse(inputIds);
6568

66-
const transaction = aleo.TransactionBuilder.build_transaction(
67-
INCLUSION_KEY,
68-
JSON.stringify([{ transition: transitionParsed, input_ids: inputIdsParsed}]),
69-
stateRoot,
70-
statePaths
71-
);
72-
console.log(`Web worker: Transaction Completed: ${performance.now() - startTime} ms`);
73-
console.log(`Transaction: ${transaction}`);
74-
self.postMessage({ type: 'TRANSACTION_COMPLETED', transaction });
75-
}
76-
else if (ev.data.type == 'ALEO_VERIFY_TRANSACTION') {
77-
const {
78-
transaction,
79-
transferVerifierBytes,
80-
inclusionVerifierBytes
81-
} = ev.data;
82-
console.log('Web worker: Verifying Transaction...');
83-
let startTime = performance.now();
69+
const transaction = aleo.TransactionBuilder.build_transaction(
70+
INCLUSION_KEY,
71+
JSON.stringify([{ transition: transitionParsed, input_ids: inputIdsParsed}]),
72+
stateRoot,
73+
statePaths
74+
);
75+
console.log(`Web worker: Transaction Completed: ${performance.now() - startTime} ms`);
76+
console.log(`Transaction: ${transaction}`);
77+
self.postMessage({ type: 'TRANSACTION_COMPLETED', transaction });
78+
}
79+
else if (ev.data.type == 'ALEO_VERIFY_TRANSACTION') {
80+
const {
81+
transaction,
82+
transferVerifierBytes,
83+
inclusionVerifierBytes
84+
} = ev.data;
85+
console.log('Web worker: Verifying Transaction...');
86+
let startTime = performance.now();
8487

85-
const program = aleo.Program.credits();
86-
const functionName = 'transfer';
87-
const transferVerifyingKey = aleo.VerifyingKey.from_bytes(transferVerifierBytes);
88-
const inclusionVerifyingKey = aleo.VerifyingKey.from_bytes(inclusionVerifierBytes);
88+
const program = aleo.Program.credits();
89+
const functionName = 'transfer';
90+
const transferVerifyingKey = aleo.VerifyingKey.from_bytes(transferVerifierBytes);
91+
const inclusionVerifyingKey = aleo.VerifyingKey.from_bytes(inclusionVerifierBytes);
8992

90-
const verified = aleo.TransactionBuilder.verify_transaction(
91-
transaction,
92-
program,
93-
functionName,
94-
transferVerifyingKey,
95-
inclusionVerifyingKey,
96-
true,
97-
true
98-
);
99-
console.log(`Web worker: Transaction Verified: ${performance.now() - startTime} ms`);
100-
console.log(verified);
101-
}
102-
});
93+
const verified = aleo.TransactionBuilder.verify_transaction(
94+
transaction,
95+
program,
96+
functionName,
97+
transferVerifyingKey,
98+
inclusionVerifyingKey,
99+
true,
100+
true
101+
);
102+
console.log(`Web worker: Transaction Verified: ${performance.now() - startTime} ms`);
103+
console.log(verified);
104+
}
103105
});

website/webpack.config.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const appConfig = {
1212
},
1313
devServer: {
1414
port: 3000,
15+
headers: {
16+
'Cross-Origin-Opener-Policy': 'same-origin',
17+
'Cross-Origin-Embedder-Policy': 'require-corp'
18+
},
1519
},
1620
module: {
1721
rules: [
@@ -34,7 +38,8 @@ const appConfig = {
3438
maxAssetSize: 8388608
3539
},
3640
experiments: {
37-
asyncWebAssembly: true
41+
asyncWebAssembly: true,
42+
topLevelAwait: true
3843
},
3944
devtool: 'source-map',
4045
}
@@ -51,7 +56,8 @@ const workerConfig = {
5156
filename: "worker.js"
5257
},
5358
experiments: {
54-
asyncWebAssembly: true
59+
asyncWebAssembly: true,
60+
topLevelAwait: true
5561
},
5662
devtool: 'source-map',
5763
};

0 commit comments

Comments
 (0)