Skip to content

Commit bd608bb

Browse files
authored
Merge pull request #3216 from o1-labs/dw/add-help-for-build
plonk-wasm: add some documentation
2 parents 4f9a8ce + 0f05e16 commit bd608bb

File tree

9 files changed

+195
-130
lines changed

9 files changed

+195
-130
lines changed

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,18 @@ ${O1VM_MIPS_BIN_DIR}/%.o: ${O1VM_MIPS_SOURCE_DIR}/%.asm
242242
fclean: clean ## Clean the tooling artefacts in addition to running clean
243243
rm -rf ${RISCV32_TOOLCHAIN_PATH}
244244

245-
build-nodejs:
245+
.PHONY: build-nodejs
246+
build-nodejs: ## Compile the Kimchi library into WebAssembly to be used in NodeJS
246247
cargo +nightly xtask build-wasm \
247248
--target nodejs \
248249
--out-dir ${PLONK_WASM_NODEJS_OUTDIR} \
249250
--rust-version ${NIGHTLY_RUST_VERSION}
250251

251-
build-web:
252+
.PHONY: build-web
253+
build-web: ## Compile the Kimchi library into WebAssembly to be used in the browser
252254
cargo +nightly xtask build-wasm \
253255
--target web \
254256
--out-dir ${PLONK_WASM_WEB_OUTDIR} \
255257
--rust-version ${NIGHTLY_RUST_VERSION}
256258

257-
.PHONY: all setup install-test-deps clean build release test-doc test-doc-with-coverage test test-with-coverage test-heavy test-heavy-with-coverage test-all test-all-with-coverage nextest nextest-with-coverage nextest-heavy nextest-heavy-with-coverage nextest-all nextest-all-with-coverage format lint generate-test-coverage-report generate-doc setup-riscv32-toolchain help fclean build-riscv32-programs build-mips-programs check-format build-web build-nodejs
259+
.PHONY: all setup install-test-deps clean build release test-doc test-doc-with-coverage test test-with-coverage test-heavy test-heavy-with-coverage test-all test-all-with-coverage nextest nextest-with-coverage nextest-heavy nextest-heavy-with-coverage nextest-all nextest-all-with-coverage format lint generate-test-coverage-report generate-doc setup-riscv32-toolchain help fclean build-riscv32-programs build-mips-programs check-format

kimchi-stubs/src/field_vector.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! We implement a custom type for field vectors in order to quickly build field vectors from the OCaml side and avoid large vector clones.
1+
//! We implement a custom type for field vectors in order to quickly build field
2+
//! vectors from the OCaml side and avoid large vector clones.
23
34
use paste::paste;
45

plonk-wasm/README.md

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,89 @@
1-
# Kimchi WASM
1+
# Kimchi compatibility layer for WebAssembly
22

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.
48

5-
## Requirements
9+
The WebAssembly module will be used in particular to define frameworks like
10+
[o1js](https://github.com/o1-labs/o1js).
611

7-
For this to work, you will need to install the following dependencies:
12+
## Usage
813

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.
1122

12-
## Usage
23+
## Architecture and Inner Workings
1324

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:
1527

16-
```console
17-
$ wasm-pack build --mode no-install --target nodejs --out-dir ./nodejs ./. -- --features nodejs
18-
```
28+
### 1. Core Components
1929

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.
2136

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:
2574

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
2777

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`.
3084

3185
## Resources
3286

3387
- [Rust WASM book](https://rustwasm.github.io/docs/book/game-of-life/hello-world.html)
3488
- [WASM-bindgen book](https://rustwasm.github.io/docs/wasm-bindgen/)
89+
- [Kimchi Documentation](./../kimchi/README.md)

plonk-wasm/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ pub mod rayon;
9494
/// Arkworks types
9595
pub mod arkworks;
9696

97-
/// Utils
98-
pub mod urs_utils; // TODO: move this logic to proof-systems
99-
10097
/// Vectors
10198
pub mod gate_vector;
10299

plonk-wasm/src/poseidon.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//! This file defines wrapper for the Poseidon hash function that are used in
2+
//! the Mina codebase.
3+
//!
4+
//! It is a wrapper around the Poseidon implementation in the `mina_poseidon` crate.
5+
//! It is required as the native OCaml implementation of Mina does use the Rust
6+
//! implementation defined in the crate `mina_poseidon` instead of defining its
7+
//! own natively in OCaml for performance reasons. The bindings in OCaml can be
8+
//! found in `src/lib/crypto/kimchi_bindings/pasta_fp_poseidon` and
9+
//! `src/lib/crypto/kimchi_bindings/pasta_fq_poseidon` in the Mina codebase.
10+
111
use mina_curves::pasta::{Fp, Fq};
212
use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, permutation::poseidon_block_cipher};
313
use wasm_bindgen::prelude::*;

plonk-wasm/src/rayon.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ extern "C" {
4343
#[wasm_bindgen(js_name = startWorkers)]
4444
fn start_workers(module: JsValue, memory: JsValue, builder: PoolBuilder) -> Promise;
4545
}
46+
4647
#[cfg(feature = "nodejs")]
4748
#[wasm_bindgen]
4849
extern "C" {
4950
#[wasm_bindgen(js_name = startWorkers)]
5051
fn start_workers(module: JsString, memory: JsValue, builder: PoolBuilder) -> Promise;
5152
}
53+
5254
#[wasm_bindgen]
5355
extern "C" {
5456
#[wasm_bindgen(js_name = terminateWorkers)]

plonk-wasm/src/srs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ macro_rules! impl_srs {
213213
crate::rayon::run_in_pool(|| {
214214
let comms: Vec<_> = comms.into_iter().map(Into::into).collect();
215215
let chals: Vec<_> = chals.into_iter().map(Into::into).collect();
216-
crate::urs_utils::batch_dlog_accumulator_check(&srs, &comms, &chals)
216+
poly_commitment::utils::batch_dlog_accumulator_check(&srs, &comms, &chals)
217217
})
218218
}
219219

@@ -223,7 +223,7 @@ macro_rules! impl_srs {
223223
comms: i32,
224224
chals: WasmFlatVector<$WasmF>,
225225
) -> WasmVector<$WasmG> {
226-
crate::urs_utils::batch_dlog_accumulator_generate::<$G>(
226+
poly_commitment::utils::batch_dlog_accumulator_generate::<$G>(
227227
&srs,
228228
comms as usize,
229229
&chals.into_iter().map(From::from).collect(),

plonk-wasm/src/urs_utils.rs

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)