From 11a2b2a9421ddf3bb397603120db3222d9eef142 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Fri, 27 Oct 2023 00:05:38 +0200 Subject: [PATCH 1/4] Add system_random and random_bytes for js target --- core/crypto/rand_generic.odin | 4 ++-- core/crypto/rand_js.odin | 20 ++++++++++++++++++++ core/math/rand/system_js.odin | 11 +++++++++++ vendor/wasm/js/runtime.js | 6 ++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 core/crypto/rand_js.odin create mode 100644 core/math/rand/system_js.odin diff --git a/core/crypto/rand_generic.odin b/core/crypto/rand_generic.odin index 52abfe4d7..fde91f85a 100644 --- a/core/crypto/rand_generic.odin +++ b/core/crypto/rand_generic.odin @@ -1,7 +1,7 @@ package crypto -when ODIN_OS != .Linux && ODIN_OS != .OpenBSD && ODIN_OS != .Windows { - _rand_bytes :: proc (dst: []byte) { +when ODIN_OS != .Linux && ODIN_OS != .OpenBSD && ODIN_OS != .Windows && ODIN_OS != .JS { + _rand_bytes :: proc(dst: []byte) { unimplemented("crypto: rand_bytes not supported on this OS") } } diff --git a/core/crypto/rand_js.odin b/core/crypto/rand_js.odin new file mode 100644 index 000000000..99a5bbcbc --- /dev/null +++ b/core/crypto/rand_js.odin @@ -0,0 +1,20 @@ +package crypto + +foreign import "odin_env" +foreign odin_env { + @(link_name = "rand_bytes") + env_rand_bytes :: proc "contextless" (buf: []byte) --- +} + +_MAX_PER_CALL_BYTES :: 65536 + +_rand_bytes :: proc(dst: []byte) { + dst := dst + + for len(dst) > 0 { + to_read := min(len(dst), _MAX_PER_CALL_BYTES) + env_rand_bytes(dst[:to_read]) + + dst = dst[to_read:] + } +} diff --git a/core/math/rand/system_js.odin b/core/math/rand/system_js.odin new file mode 100644 index 000000000..11e1385a2 --- /dev/null +++ b/core/math/rand/system_js.odin @@ -0,0 +1,11 @@ +package rand + +foreign import "odin_env" +foreign odin_env { + rand :: proc "contextless" () -> f64 --- +} + +@(require_results) +_system_random :: proc() -> u64 { + return u64(rand() * 0x1fffffffffffff) +} diff --git a/vendor/wasm/js/runtime.js b/vendor/wasm/js/runtime.js index d5ab383f0..33ce9640e 100644 --- a/vendor/wasm/js/runtime.js +++ b/vendor/wasm/js/runtime.js @@ -1349,6 +1349,12 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { ln: Math.log, exp: Math.exp, ldexp: (x, exp) => x * Math.pow(2, exp), + + rand: Math.random, + rand_bytes: (ptr, len) => { + const view = new Uint8Array(wasm_memory.buffer, ptr, len) + crypto.getRandomValues(view) + }, }, "odin_dom": { init_event_raw: (ep) => { From 166803a2a5ae2670d23b069f86f1aa8090f4bb14 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Fri, 27 Oct 2023 00:18:41 +0200 Subject: [PATCH 2/4] Rename rand to rand_f64 --- core/math/rand/system_js.odin | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/math/rand/system_js.odin b/core/math/rand/system_js.odin index 11e1385a2..14949000e 100644 --- a/core/math/rand/system_js.odin +++ b/core/math/rand/system_js.odin @@ -2,10 +2,11 @@ package rand foreign import "odin_env" foreign odin_env { - rand :: proc "contextless" () -> f64 --- + @(link_name = "rand") + rand_f64 :: proc "contextless" () -> f64 --- } @(require_results) _system_random :: proc() -> u64 { - return u64(rand() * 0x1fffffffffffff) + return u64(rand_f64() * 0x1fffffffffffff) } From 8b2f62000addd72098e277b8b4e691a74c3c40a8 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Fri, 27 Oct 2023 00:50:29 +0200 Subject: [PATCH 3/4] Use wasmMemoryInterface for rand_bytes --- vendor/wasm/js/runtime.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/wasm/js/runtime.js b/vendor/wasm/js/runtime.js index 33ce9640e..16ef5d3aa 100644 --- a/vendor/wasm/js/runtime.js +++ b/vendor/wasm/js/runtime.js @@ -1352,7 +1352,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { rand: Math.random, rand_bytes: (ptr, len) => { - const view = new Uint8Array(wasm_memory.buffer, ptr, len) + const view = new Uint8Array(wasmMemoryInterface.memory.buffer, ptr, len) crypto.getRandomValues(view) }, }, From 49da19e013cc26abad75b552f06ee2ba925718c9 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Fri, 27 Oct 2023 12:06:35 +0200 Subject: [PATCH 4/4] Replace Math.random with crypto.getRandomValues for _system_number --- core/crypto/rand_js.odin | 2 +- core/math/rand/system_js.odin | 8 +++++--- vendor/wasm/js/runtime.js | 1 - 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/crypto/rand_js.odin b/core/crypto/rand_js.odin index 99a5bbcbc..353b1e6b9 100644 --- a/core/crypto/rand_js.odin +++ b/core/crypto/rand_js.odin @@ -6,7 +6,7 @@ foreign odin_env { env_rand_bytes :: proc "contextless" (buf: []byte) --- } -_MAX_PER_CALL_BYTES :: 65536 +_MAX_PER_CALL_BYTES :: 65536 // 64kiB _rand_bytes :: proc(dst: []byte) { dst := dst diff --git a/core/math/rand/system_js.odin b/core/math/rand/system_js.odin index 14949000e..b9b71c4a6 100644 --- a/core/math/rand/system_js.odin +++ b/core/math/rand/system_js.odin @@ -2,11 +2,13 @@ package rand foreign import "odin_env" foreign odin_env { - @(link_name = "rand") - rand_f64 :: proc "contextless" () -> f64 --- + @(link_name = "rand_bytes") + env_rand_bytes :: proc "contextless" (buf: []byte) --- } @(require_results) _system_random :: proc() -> u64 { - return u64(rand_f64() * 0x1fffffffffffff) + buf: [8]u8 + env_rand_bytes(buf[:]) + return transmute(u64)buf } diff --git a/vendor/wasm/js/runtime.js b/vendor/wasm/js/runtime.js index 16ef5d3aa..78fdcca18 100644 --- a/vendor/wasm/js/runtime.js +++ b/vendor/wasm/js/runtime.js @@ -1350,7 +1350,6 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { exp: Math.exp, ldexp: (x, exp) => x * Math.pow(2, exp), - rand: Math.random, rand_bytes: (ptr, len) => { const view = new Uint8Array(wasmMemoryInterface.memory.buffer, ptr, len) crypto.getRandomValues(view)