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

Modify random_get to use crypto.getRandomValues #75

Merged
merged 8 commits into from
May 3, 2024
Merged
Changes from 6 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
18 changes: 16 additions & 2 deletions src/wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class WASI {
inst: { exports: { memory: WebAssembly.Memory } };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
wasiImport: { [key: string]: (...args: Array<any>) => unknown };
private readonly hasCrypto: boolean = "crypto" in Function("return this")();

/// Start a WASI command
start(instance: {
Expand Down Expand Up @@ -826,8 +827,21 @@ export default class WASI {
sched_yield() {},
random_get(buf: number, buf_len: number) {
const buffer8 = new Uint8Array(self.inst.exports.memory.buffer);
for (let i = 0; i < buf_len; i++) {
buffer8[buf + i] = (Math.random() * 256) | 0;
const end = buf + buf_len;

if (self.hasCrypto) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check "crypto" in globalThis instead and not cache it in an instance variable?

Copy link
Contributor Author

@Aandreba Aandreba May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried, but it didn't pass the tests. Looks like globalThis doesn't exist on all environments 😞

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, caching it is most probably faster than checking every time random_get gets called

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did it fail in node.js? Maybe the node.js version that was used is too old? globalThis is supposed to work in all environments. It was introduced as way to unify window, self and global across the various javascript environments.

As for performance, if random_get is called rarely a tiny slowdown shouldn't matter and if it is called frequently, I would expect the JIT to optimize it down to a couple instructions max, which is much less than even the overhead of making a native call, let alone actually running the PRNG.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remembered it failing on some test, but it seems I was mistaken. I've pushed the uncached "crypto" in globalThis version.

let i = buf;
for (; i < end; ) {
const next_i = i + 65_536;
crypto.getRandomValues(
buffer8.subarray(i, next_i > end ? end : next_i),
);
i = next_i;
}
bjorn3 marked this conversation as resolved.
Show resolved Hide resolved
} else {
for (let i = buf; i < end; i++) {
buffer8[i] = (Math.random() * 256) | 0;
}
}
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
Loading