plonk_wasm/
lib.rs

1//! The Marlin_plonk_stubs crate exports some functionalities
2//! and structures from the following the Rust crates to OCaml:
3//!
4//! * [Marlin](https://github.com/o1-labs/marlin),
5//!   a PLONK implementation.
6//! * [Arkworks](http://arkworks.rs/),
7//!   a math library that Marlin builds on top of.
8//!
9
10use wasm_bindgen::prelude::*;
11
12mod wasm_vector;
13
14#[wasm_bindgen]
15extern "C" {
16    pub fn alert(s: &str);
17}
18
19#[wasm_bindgen]
20extern "C" {
21    #[wasm_bindgen(js_namespace = console)]
22    fn log(s: &str);
23}
24
25// produces a warning, but can be useful
26// macro_rules! console_log {
27//     ($($t:tt)*) => (crate::log(&format_args!($($t)*).to_string()))
28// }
29
30#[wasm_bindgen]
31pub fn console_log(s: &str) {
32    log(s);
33}
34
35#[wasm_bindgen]
36pub fn create_zero_u32_ptr() -> *mut u32 {
37    Box::into_raw(std::boxed::Box::new(0))
38}
39
40/// Free a pointer. This method is exported in the WebAssembly module to be used
41/// on the JavaScript side, see `web-backend.js`.
42///
43/// # Safety
44///
45/// See
46/// `<https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref>`
47#[wasm_bindgen]
48pub unsafe fn free_u32_ptr(ptr: *mut u32) {
49    let _drop_me = unsafe { std::boxed::Box::from_raw(ptr) };
50}
51
52/// Set the value of a pointer. This method is exported in the WebAssembly
53/// module to be used on the JavaScript side, see `web-backend.js`.
54///
55/// # Safety
56///
57/// See
58/// `<https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref>`
59#[wasm_bindgen]
60pub unsafe fn set_u32_ptr(ptr: *mut u32, arg: u32) {
61    // The rust docs explicitly forbid using this for cross-thread syncronization. Oh well, we
62    // don't have anything better. As long as it works in practice, we haven't upset the undefined
63    // behavior dragons.
64    unsafe {
65        core::ptr::write_volatile(ptr, arg);
66    }
67}
68
69/// This method is exported in the WebAssembly to be used on the JavaScript
70/// side, see `web-backend.js`.
71///
72/// # Safety
73///
74/// See
75/// `<https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref>`
76#[allow(unreachable_code)]
77#[wasm_bindgen]
78pub unsafe fn wait_until_non_zero(ptr: *const u32) -> u32 {
79    // The rust docs explicitly forbid using this for cross-thread syncronization. Oh well, we
80    // don't have anything better. As long as it works in practice, we haven't upset the undefined
81    // behavior dragons.
82    loop {
83        let contents = unsafe { core::ptr::read_volatile(ptr) };
84        if contents != 0 {
85            return contents;
86        }
87    }
88    unreachable!();
89}
90
91/// This method is exported in the WebAssembly to check the memory used on the
92/// JavaScript
93#[wasm_bindgen]
94pub fn get_memory() -> JsValue {
95    wasm_bindgen::memory()
96}
97
98/// Returns the number of bytes used by the WebAssembly memory.
99#[wasm_bindgen]
100pub fn get_memory_byte_length() -> usize {
101    let buffer = wasm_bindgen::memory()
102        .dyn_into::<js_sys::WebAssembly::Memory>()
103        .unwrap()
104        .buffer();
105    buffer.unchecked_into::<js_sys::ArrayBuffer>().byte_length() as usize
106}
107
108pub mod rayon;
109
110/// Vectors
111pub mod gate_vector;
112
113pub mod poly_comm;
114/// Curves
115pub mod projective;
116
117/// SRS
118pub mod srs;
119
120/// Indexes
121pub mod pasta_fp_plonk_index;
122pub mod pasta_fq_plonk_index;
123
124/// Verifier indexes/keys
125pub mod plonk_verifier_index;
126
127/// Oracles
128pub mod oracles;
129
130/// Proofs
131pub mod plonk_proof;
132
133/// Poseidon
134pub mod poseidon;
135
136// exposes circuit for inspection
137pub mod circuit;
138
139pub mod wasm_ocaml_serde;