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