Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wasm-bindgen-rayon #5

Open
wants to merge 10 commits into
base: feat/wasm-proof-generation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[target.'cfg(not(target_arch = "wasm32"))']
rustflags = ["-C", "target-cpu=native"]

[target.wasm32-unknown-unknown]
rustflags = ["-C", "target-feature=+atomics,+bulk-memory,+mutable-globals"]

[unstable]
build-std = ["panic_abort", "std"]
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ version = "1.0"
path="../../snarkVM/synthesizer"
version = "0.9.11"
default-features = false
features = [ "wasm" ]
features = [ "wasm", "parallel" ]

[dependencies.wasm-bindgen-rayon]
version = "1.0"
path = "../../wasm-bindgen-rayon"

[dependencies.rayon]
version = "1.5"

[dependencies.snarkvm-console]
path="../../snarkVM/console"
version = "0.9.11"
default-features = false
features = [ "parallel" ]

[dependencies.snarkvm-wasm]
path="../../snarkVM/wasm"
Expand Down Expand Up @@ -80,4 +88,7 @@ opt-level = 3
lto = true

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-O4", "--fast-math"]
wasm-opt = ["-O4"]

# [features]
# thread_local_internals = [ ]
1 change: 1 addition & 0 deletions wasm/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2022-12-12
2 changes: 2 additions & 0 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ pub use program::*;

pub(crate) mod types;
pub(crate) use types::*;

pub use wasm_bindgen_rayon::init_thread_pool;
3 changes: 2 additions & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"webpack-dev-server": "^4.7.4"
},
"scripts": {
"start": "yarn upgrade @aleohq/wasm && webpack-dev-server",
"build-wasm": "cd ../wasm && wasm-pack build --release --target web",
"start": "yarn build-wasm && yarn upgrade @aleohq/wasm && webpack-dev-server",
"build": "webpack --config webpack.config.js",
"predeploy": "yarn build",
"deploy": "gh-pages -d build",
Expand Down
Empty file removed website/pkg/index.js
Empty file.
3 changes: 2 additions & 1 deletion website/src/tabs/transaction/SendCredits.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export const SendCredits = () => {
startTime = performance.now();

// Build transaction
worker.postMessage({privateKey, transferProverBytes: transferProver.bytes, amount, toAddress, plaintext})
worker.postMessage({privateKey, transferProverBytes: transferProver.bytes, amount, toAddress, plaintext});

console.log('Called web worker');
} catch (error) { console.error(error) }
}
Expand Down
40 changes: 21 additions & 19 deletions website/src/workers/worker.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import "babel-polyfill";
import init, * as aleo from '@aleohq/wasm';

import("@aleohq/wasm").then(aleo => {
self.addEventListener("message", ev => {
const {privateKey, transferProverBytes, toAddress, amount, plaintext} = ev.data;
console.log('Web worker: Started Transfer...');
console.log(ev.data);
let startTime = performance.now();
const pK = aleo.PrivateKey.from_string(privateKey);
const provingKey = aleo.ProvingKey.from_bytes(transferProverBytes);
console.log(`Web worker: Deserialized proving key Completed: ${performance.now() - startTime} ms`);
startTime = performance.now();
const address = aleo.Address.from_string(toAddress);
const rec = aleo.RecordPlaintext.fromString(plaintext);
await init();

startTime = performance.now();
const result = aleo.TransactionBuilder.build_transfer_full(pK, provingKey, address, BigInt(amount), rec);
console.log(`Web worker: Transaction Completed: ${performance.now() - startTime} ms`);
console.log(result);
self.postMessage({ transaction: result });
});
await aleo.initThreadPool(navigator.hardwareConcurrency);

self.addEventListener("message", ev => {
const {privateKey, transferProverBytes, toAddress, amount, plaintext} = ev.data;
console.log('Web worker: Started Transfer...');
console.log(ev.data);
let startTime = performance.now();
const pK = aleo.PrivateKey.from_string(privateKey);
const provingKey = aleo.ProvingKey.from_bytes(transferProverBytes);
console.log(`Web worker: Deserialized proving key Completed: ${performance.now() - startTime} ms`);
startTime = performance.now();
const address = aleo.Address.from_string(toAddress);
const rec = aleo.RecordPlaintext.fromString(plaintext);

startTime = performance.now();
const result = aleo.TransactionBuilder.build_transfer_full(pK, provingKey, address, BigInt(amount), rec);
console.log(`Web worker: Transaction Completed: ${performance.now() - startTime} ms`);
console.log(result);
self.postMessage({ transaction: result });
});
11 changes: 9 additions & 2 deletions website/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const appConfig = {
},
devServer: {
port: 3000,
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp'
},
},
module: {
rules: [
Expand All @@ -35,7 +39,8 @@ const appConfig = {
maxAssetSize: 8388608
},
experiments: {
asyncWebAssembly: true
asyncWebAssembly: true,
topLevelAwait: true
},
devtool: 'source-map',
}
Expand All @@ -52,7 +57,9 @@ const workerConfig = {
filename: "worker.js"
},
experiments: {
asyncWebAssembly: true
syncWebAssembly: true,
asyncWebAssembly: true,
topLevelAwait: true
},
devtool: 'source-map',
};
Expand Down