Skip to content

Commit

Permalink
Micro-optimize randomFill. NFC
Browse files Browse the repository at this point in the history
Only use the node fallback code when its actually needed, otherwise
assume that `crypto.getRandomValues` is available out-of-the-box (which
it is on node v19 and above.
  • Loading branch information
sbc100 committed Jan 6, 2025
1 parent 1b8b227 commit 61bcbd4
Show file tree
Hide file tree
Showing 23 changed files with 31 additions and 39 deletions.
26 changes: 9 additions & 17 deletions src/library_wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,28 +561,20 @@ var WasiLibrary = {
// random.h

$initRandomFill: () => {
if (typeof crypto == 'object' && typeof crypto.getRandomValues == 'function') {
#if SHARED_MEMORY
// like with most Web APIs, we can't use Web Crypto API directly on shared memory,
// so we need to create an intermediate buffer and copy it to the destination
return (view) => view.set(crypto.getRandomValues(new Uint8Array(view.byteLength)));
#else
return (view) => crypto.getRandomValues(view);
#endif
}

#if ENVIRONMENT_MAY_BE_NODE
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
if (ENVIRONMENT_IS_NODE) {
return (view) => require('crypto').randomFillSync(view);
// Node v19 and above has globalThis.getRandomValues builtin
var nodeCrypto = require('crypto');
return (view) => nodeCrypto.randomFillSync(view);
}
#endif // ENVIRONMENT_MAY_BE_NODE

// we couldn't find a proper implementation, as Math.random() is not
// suitable for /dev/random, see emscripten-core/emscripten/pull/7096
#if ASSERTIONS
abort('no cryptographic support found for random function. consider polyfilling it if you want to use something insecure like Math.random(), e.g. put this in a --pre-js: var crypto = { getRandomValues: (array) => { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } };');
#if SHARED_MEMORY
// like with most Web APIs, we can't use Web Crypto API directly on shared memory,
// so we need to create an intermediate buffer and copy it to the destination
return (view) => view.set(crypto.getRandomValues(new Uint8Array(view.byteLength)));
#else
abort();
return (view) => crypto.getRandomValues(view);
#endif
},

Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8346
8332
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20320
20255
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8328
8314
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20288
20223
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9348
9336
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24088
24023
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8290
8280
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20213
20148
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8290
8280
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20213
20148
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8360
8346
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20397
20331
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9354
9341
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24089
24023
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8346
8332
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20320
20255
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3574
3564
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7741
7676
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7655
7640
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18867
18801
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2817
2804
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6050
5984

0 comments on commit 61bcbd4

Please sign in to comment.