|
1 | | -# Kimchi WASM |
| 1 | +# Kimchi compatibility layer for WebAssembly |
2 | 2 |
|
3 | | -This code allows us to compile parts of Kimchi into [Web Assembly (WASM)](https://webassembly.org/). |
| 3 | +This code allows us to compile parts of [Kimchi](./../kimchi) into [Web Assembly |
| 4 | +(WASM)](https://webassembly.org/), making the proof system available for use in |
| 5 | +Web and NodeJS applications. |
| 6 | +The WebAssembly module is also exposing all the methods and primitives that the |
| 7 | +OCaml Mina codebase requires. |
4 | 8 |
|
5 | | -## Requirements |
| 9 | +The WebAssembly module will be used in particular to define frameworks like |
| 10 | +[o1js](https://github.com/o1-labs/o1js). |
6 | 11 |
|
7 | | -For this to work, you will need to install the following dependencies: |
| 12 | +## Usage |
8 | 13 |
|
9 | | -- [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/) |
10 | | -- [wasm-bindgen-cli](https://rustwasm.github.io/docs/wasm-bindgen/reference/cli.html) (optional) |
| 14 | +Makefile targets are defined at the top-level of this repository. |
| 15 | +From that location, you can run |
| 16 | +``` |
| 17 | +make build-nodejs # (for NodeJS environments) |
| 18 | +make build-web # (for web browsers) |
| 19 | +``` |
| 20 | +to compile the Kimchi library (and its dependencies) into a WebAssembly module |
| 21 | +ready to be run in NodeJS or in the browser. |
11 | 22 |
|
12 | | -## Usage |
| 23 | +## Architecture and Inner Workings |
13 | 24 |
|
14 | | -To build for nodejs: |
| 25 | +The plonk-wasm library serves as a bridge between the Rust-based Kimchi proof |
| 26 | +system and JavaScript environments. It consists of several key components: |
15 | 27 |
|
16 | | -```console |
17 | | -$ wasm-pack build --mode no-install --target nodejs --out-dir ./nodejs ./. -- --features nodejs |
18 | | -``` |
| 28 | +### 1. Core Components |
19 | 29 |
|
20 | | -To build for web browsers: |
| 30 | +- **WebAssembly Bindings**: Uses |
| 31 | + [wasm-bindgen](https://rustwasm.github.io/wasm-bindgen) to expose Rust |
| 32 | + functions and types to JavaScript. |
| 33 | +- **OCaml Compatibility**: Special serialization/deserialization layer |
| 34 | + (`wasm_ocaml_serde`) that handles conversions between Rust structures and |
| 35 | + JavaScript values following OCaml conventions. |
21 | 36 |
|
22 | | -```console |
23 | | -$ wasm-pack build --mode no-install --target web --out-dir ./web ./. |
24 | | -``` |
| 37 | +### 2. Exposed Functionality |
| 38 | + |
| 39 | +The library exposes several core components from the Kimchi proof system: |
| 40 | + |
| 41 | +- **Cryptographic primitives**: Field elements, elliptic curve operations, and |
| 42 | + polynomial commitments, SRS |
| 43 | +- **Proof generation and verification**: Tools to generate and verify proofs |
| 44 | + generated by the Kimchi system |
| 45 | +- **Poseidon hash**: Bindings to the Poseidon implementation used by the Kimchi |
| 46 | + proof system |
| 47 | + |
| 48 | +The methods that will be exposed in the WebAssembly module are surrounded by the |
| 49 | +macro `#[wasm_bindgen]`. |
| 50 | +All Rust symbols used by the OCaml client of Mina are required to be exposed in |
| 51 | +the WebAssembly module, even if not directly desired in the user API. |
| 52 | + |
| 53 | +### 3. Build System |
| 54 | + |
| 55 | +The build process uses: |
| 56 | + |
| 57 | +- [wasm-pack](https://github.com/rustwasm/wasm-pack): For compiling and |
| 58 | + packaging the Rust code into WebAssembly. |
| 59 | +- **Nightly Rust**. There is no apparent reasons to use nightly, but without |
| 60 | + using nightly, users have encountered issues like |
| 61 | + [DataCloneError](https://github.com/o1-labs/o1js/issues/1989) |
| 62 | + |
| 63 | +### 4. Memory Management |
| 64 | + |
| 65 | +The library provides: |
| 66 | + |
| 67 | +- Low-level pointer manipulation for efficient buffer management |
| 68 | +- Custom vector implementations for Rust/JavaScript interoperability |
| 69 | +- Support for handling large cryptographic structures efficiently |
| 70 | + |
| 71 | +### 5. Module Structure |
| 72 | + |
| 73 | +Key modules include: |
25 | 74 |
|
26 | | -Note that optimized versions of these commands are available in: |
| 75 | +- `arkworks/`: WebAssembly wrappers around the arkworks library |
| 76 | +- `wasm_ocaml_serde/`: OCaml-compatible serialization |
27 | 77 |
|
28 | | -- [/src/lib/crypto/kimchi_bindings/js/node_js/build.sh](/src/lib/crypto/kimchi_bindings/js/node_js/build.sh) (also called from the `dune` file in the same folder) |
29 | | -- [/src/lib/crypto/kimchi_bindings/js/web/build.sh](/src/lib/crypto/kimchi_bindings/js/web/build.sh) (also called from the `dune` file in the same folder) |
| 78 | +- Cryptographic primitives: |
| 79 | + - `circuit.rs`, `srs.rs`, `gate_vector.rs`, `oracle.rs`, `poly_comm.rs`, |
| 80 | + `poseidon.rs`, `projective.rs`, `srs.rs` |
| 81 | +- Kimchi proof systems: |
| 82 | +- `pasta_fp_plonk_index.rs`, `pasta_fq_plonk_index.rs`, `plonk_proof.rs` and |
| 83 | + `plonk_verifier_index.rs`. |
30 | 84 |
|
31 | 85 | ## Resources |
32 | 86 |
|
33 | 87 | - [Rust WASM book](https://rustwasm.github.io/docs/book/game-of-life/hello-world.html) |
34 | 88 | - [WASM-bindgen book](https://rustwasm.github.io/docs/wasm-bindgen/) |
| 89 | +- [Kimchi Documentation](./../kimchi/README.md) |
0 commit comments