From ef00a00475df83038266a4de5a4c5c3344f6e74b Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 13 Jun 2025 19:02:59 +0700 Subject: [PATCH 1/4] enable worker logging for nodejs --- plonk-wasm/src/lib.rs | 32 ++++++++++++++++++++++++++ plonk-wasm/src/pasta_fp_plonk_index.rs | 11 +++++++++ 2 files changed, 43 insertions(+) diff --git a/plonk-wasm/src/lib.rs b/plonk-wasm/src/lib.rs index e62068ba947..760cf7b84e1 100644 --- a/plonk-wasm/src/lib.rs +++ b/plonk-wasm/src/lib.rs @@ -11,6 +11,38 @@ use wasm_bindgen::prelude::*; mod wasm_vector; +use js_sys::Function; +use wasm_bindgen::JsValue; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_name = postMessage)] + fn postMessageToMain(data: &JsValue); +} + +static mut MESSAGE_HANDLER: Option = None; + +#[wasm_bindgen] +pub unsafe fn set_message_handler(handler: Function) { + unsafe { + console_log("setting message handler"); + MESSAGE_HANDLER = Some(handler); + console_log("set"); + } +} + +pub fn send_message(msg: &str) { + unsafe { + if let Some(ref handler) = MESSAGE_HANDLER { + console_log("sending "); + + let result = JsValue::from_str(msg); // creates a string + let _ = handler.call1(&JsValue::NULL, &result); + console_log("sent "); + } + } +} + #[wasm_bindgen] extern "C" { pub fn alert(s: &str); diff --git a/plonk-wasm/src/pasta_fp_plonk_index.rs b/plonk-wasm/src/pasta_fp_plonk_index.rs index b56347c23f1..e8b23b46686 100644 --- a/plonk-wasm/src/pasta_fp_plonk_index.rs +++ b/plonk-wasm/src/pasta_fp_plonk_index.rs @@ -111,7 +111,10 @@ pub fn caml_pasta_fp_plonk_index_create( lazy_mode: bool, ) -> Result { console_error_panic_hook::set_once(); + super::postMessageToMain(&JsValue::from_str("hello from Rayon thread!")); let index = crate::rayon::run_in_pool(|| { + super::postMessageToMain(&JsValue::from_str("hello from Rayon worker!")); + // flatten the permutation information (because OCaml has a different way of keeping track of permutations) let gates: Vec<_> = gates .0 @@ -122,6 +125,11 @@ pub fn caml_pasta_fp_plonk_index_create( coeffs: gate.coeffs.clone(), }) .collect(); + super::postMessageToMain(&JsValue::from_str("hello from Rayon again!")); + super::postMessageToMain(&JsValue::from_str(&format!( + "logging something from rayon {:?}", + gates[0].typ + ))); let rust_runtime_table_cfgs: Vec> = runtime_table_cfgs.into_iter().map(Into::into).collect(); @@ -148,6 +156,7 @@ pub fn caml_pasta_fp_plonk_index_create( } Ok(cs) => cs, }; + super::send_message(&format!("cs table done")); // endo let (endo_q, _endo_r) = poly_commitment::ipa::endos::(); @@ -160,6 +169,8 @@ pub fn caml_pasta_fp_plonk_index_create( srs.0.clone(), lazy_mode, ); + super::send_message(&format!("index table done")); + // Compute and cache the verifier index digest index.compute_verifier_index_digest::>(); Ok(index) From 3d9c65be13efc9cae0d6fff10ea4e5770295e366 Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 13 Jun 2025 19:07:20 +0700 Subject: [PATCH 2/4] remove weird handler --- plonk-wasm/src/lib.rs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/plonk-wasm/src/lib.rs b/plonk-wasm/src/lib.rs index 760cf7b84e1..6db350153de 100644 --- a/plonk-wasm/src/lib.rs +++ b/plonk-wasm/src/lib.rs @@ -22,27 +22,6 @@ extern "C" { static mut MESSAGE_HANDLER: Option = None; -#[wasm_bindgen] -pub unsafe fn set_message_handler(handler: Function) { - unsafe { - console_log("setting message handler"); - MESSAGE_HANDLER = Some(handler); - console_log("set"); - } -} - -pub fn send_message(msg: &str) { - unsafe { - if let Some(ref handler) = MESSAGE_HANDLER { - console_log("sending "); - - let result = JsValue::from_str(msg); // creates a string - let _ = handler.call1(&JsValue::NULL, &result); - console_log("sent "); - } - } -} - #[wasm_bindgen] extern "C" { pub fn alert(s: &str); From d3d097f61bbf4a2888124e2df5613a036cc80bcf Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 13 Jun 2025 19:07:31 +0700 Subject: [PATCH 3/4] add web-sys --- Cargo.lock | 1 + Cargo.toml | 1 + plonk-wasm/Cargo.toml | 1 + 3 files changed, 3 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 65c4a0562ec..d7412d3b9e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2857,6 +2857,7 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-test", "wasm-types", + "web-sys", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 87a9dd71ae3..a9bfdd31a67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,6 +56,7 @@ hex = { version = "0.4", features = ["serde"] } iai = "0.1" itertools = "0.12.1" js-sys = "=0.3.64" +web-sys = { version = "0.3.64", features = ["DedicatedWorkerGlobalScope"] } libc = "=0.2.169" libflate = "2" log = "0.4.20" diff --git a/plonk-wasm/Cargo.toml b/plonk-wasm/Cargo.toml index 79bfc2b2256..34b830ed8c3 100644 --- a/plonk-wasm/Cargo.toml +++ b/plonk-wasm/Cargo.toml @@ -18,6 +18,7 @@ base64.workspace = true console_error_panic_hook.workspace = true getrandom = { workspace = true, features = ["js"] } js-sys.workspace = true +web-sys.workspace = true libc.workspace = true num-bigint.workspace = true once_cell.workspace = true From 83bef836b175c401c81cf022e45460b6a227a386 Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 13 Jun 2025 20:36:15 +0700 Subject: [PATCH 4/4] log both web and node --- Cargo.toml | 2 +- plonk-wasm/src/lib.rs | 14 +++++++++++--- plonk-wasm/src/pasta_fp_plonk_index.rs | 19 ++++++++++++------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a9bfdd31a67..3bb70e4db1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ hex = { version = "0.4", features = ["serde"] } iai = "0.1" itertools = "0.12.1" js-sys = "=0.3.64" -web-sys = { version = "0.3.64", features = ["DedicatedWorkerGlobalScope"] } +web-sys = { version = "0.3.64", features = ["DedicatedWorkerGlobalScope", "console","MessageEvent"] } libc = "=0.2.169" libflate = "2" log = "0.4.20" diff --git a/plonk-wasm/src/lib.rs b/plonk-wasm/src/lib.rs index 6db350153de..763349a05c7 100644 --- a/plonk-wasm/src/lib.rs +++ b/plonk-wasm/src/lib.rs @@ -11,17 +11,25 @@ use wasm_bindgen::prelude::*; mod wasm_vector; -use js_sys::Function; use wasm_bindgen::JsValue; +use wasm_bindgen::JsCast; +use web_sys; + +fn send_message_from_worker(msg: &JsValue) { + // TODO: Use a more structured approach and msg format + // also TODO: ideally use worker communication - look into how web_sys does it + // right now this looks to the console right away, which is cool, but having actual worker comms is more useful + web_sys::console::log_1(msg); + postMessageToMain(msg); +} + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_name = postMessage)] fn postMessageToMain(data: &JsValue); } -static mut MESSAGE_HANDLER: Option = None; - #[wasm_bindgen] extern "C" { pub fn alert(s: &str); diff --git a/plonk-wasm/src/pasta_fp_plonk_index.rs b/plonk-wasm/src/pasta_fp_plonk_index.rs index e8b23b46686..af2b33b0192 100644 --- a/plonk-wasm/src/pasta_fp_plonk_index.rs +++ b/plonk-wasm/src/pasta_fp_plonk_index.rs @@ -111,9 +111,15 @@ pub fn caml_pasta_fp_plonk_index_create( lazy_mode: bool, ) -> Result { console_error_panic_hook::set_once(); - super::postMessageToMain(&JsValue::from_str("hello from Rayon thread!")); - let index = crate::rayon::run_in_pool(|| { - super::postMessageToMain(&JsValue::from_str("hello from Rayon worker!")); + super::send_message_from_worker(&JsValue::from_str("hello from Rayon thread!")); + let index: Result< + ProverIndex< + ark_ec::short_weierstrass::Affine, + OpeningProof>, + >, + &str, + > = crate::rayon::run_in_pool(|| { + super::send_message_from_worker(&JsValue::from_str("hello from Rayon worker!")); // flatten the permutation information (because OCaml has a different way of keeping track of permutations) let gates: Vec<_> = gates @@ -125,8 +131,9 @@ pub fn caml_pasta_fp_plonk_index_create( coeffs: gate.coeffs.clone(), }) .collect(); - super::postMessageToMain(&JsValue::from_str("hello from Rayon again!")); - super::postMessageToMain(&JsValue::from_str(&format!( + + super::send_message_from_worker(&JsValue::from_str("hello from Rayon again!")); + super::send_message_from_worker(&JsValue::from_str(&format!( "logging something from rayon {:?}", gates[0].typ ))); @@ -156,7 +163,6 @@ pub fn caml_pasta_fp_plonk_index_create( } Ok(cs) => cs, }; - super::send_message(&format!("cs table done")); // endo let (endo_q, _endo_r) = poly_commitment::ipa::endos::(); @@ -169,7 +175,6 @@ pub fn caml_pasta_fp_plonk_index_create( srs.0.clone(), lazy_mode, ); - super::send_message(&format!("index table done")); // Compute and cache the verifier index digest index.compute_verifier_index_digest::>();