Skip to content

Commit

Permalink
feat: Add Wasm Bindings (#62)
Browse files Browse the repository at this point in the history
* update 2 files, create 6 files and delete 1 file

* update 1 file and create 1 file

* node.rs

* wasm

* update circom.rs and lib.rs

* wasm_bindgen gen_from_raw_memory

* reverse_string

* call gen_from_raw_memory from js

* disable neon for wasm

* chore: delete .yarnrc

* toolchain, docs

* remove pkg dir

* Revert "update 1 file and create 1 file"

This reverts commit ae5b918.

* cleanup

* target_family wasm

* remove neon from compiler

* test stub

* README

* update package.json, wasm.test.js and packages/compiler/README.md

* wasm-pack web README

* tests

* change from unwrap to expect in gen_from_raw_memory, formatting

* gen_from_decomposed_memory

* test: update email_addr.test.js

* update tests

* test: update simple_regex.test.js

* chore: delete wasm.test.js

* update circom.rs and lib.rs

* add wasm-bindgen to api

* feat: add wasm

* chore: update version

---------

Co-authored-by: Olof Andersson <[email protected]>
  • Loading branch information
Bisht13 and foolo authored Mar 22, 2024
1 parent 9995d9b commit d1ef521
Show file tree
Hide file tree
Showing 29 changed files with 605 additions and 910 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
toolchain: stable
override: true
components: rustfmt, clippy
- name: Install wasm-pack
run: cargo install wasm-pack
- name: Download circom (Linux)
run: wget https://github.com/iden3/circom/releases/latest/download/circom-linux-amd64 -O /usr/local/bin/circom && chmod +x /usr/local/bin/circom
- name: Install yarn
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ build/*

target

index.node
Cargo.lock

.DS_Store
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "@zk-email/zk-regex",
"version": "1.0.0",
"version": "2.0.0",
"private": true,
"description": "zk regex circuit for content attestation",
"main": "node-apis/index.node",
"main": "pkg/zk_regex_compiler_bg.wasm",
"workspaces": [
"packages/*"
],
"contributors": [
"Sora Suegami <[email protected]>",
"Yush G <[email protected]>",
"Javier Su <[email protected]>",
"Kata Choi <[email protected]>"
"Kata Choi <[email protected]>",
"Aditya Bisht <[email protected]>"
],
"scripts": {
"install": "yarn workspaces -pt run install",
Expand Down
1 change: 0 additions & 1 deletion packages/apis/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
target
index.node
**/node_modules
**/.DS_Store
npm-debug.log*
18 changes: 6 additions & 12 deletions packages/apis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "zk-regex-apis"
version = "1.2.0"
version = "2.0.0"
license = "MIT"
edition = "2018"
exclude = ["index.node"]
authors = [
"Javier Su <[email protected]>",
"Kata Choi <[email protected]>",
"Sora Suegami <[email protected]>",
"Yush G <[email protected]>",
"Aditya Bisht <[email protected]>",
]

[lib]
Expand All @@ -23,13 +23,7 @@ fancy-regex = "0.11.0"
itertools = "0.10.3"
thiserror = "1.0.40"
serde_json = "1.0.95"


[dependencies.neon]
version = "0.10"
default-features = false
features = ["napi-6"]

[features]
default = ["export_neon_main"]
export_neon_main = []
wasm-bindgen = "0.2"
serde-wasm-bindgen = "0.6.5"
js-sys = "0.3.69"
wasm-bindgen-test = "0.3.42"
56 changes: 28 additions & 28 deletions packages/apis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

Helpful nodejs/Rust APIs in [zk-regex](https://github.com/zkemail/zk-regex/tree/main).

This project was bootstrapped by [create-neon](https://www.npmjs.com/package/create-neon).

## Installing zk-regex-apis

Installing zk-regex-apis requires a [supported version of Node and Rust](https://github.com/neon-bindings/neon#platform-support).
Expand All @@ -24,17 +22,36 @@ If you have already installed the project and only want to run the build, run:
$ npm run build
```

This command uses the [cargo-cp-artifact](https://github.com/neon-bindings/cargo-cp-artifact) utility to run the Rust build and copy the built library into `./index.node`.
## Compiling zk-regex-apis to wasm

### For web usage
Install `wasm-pack` if not already installed

## Exploring zk-regex-apis
```sh
cargo install wasm-pack
```

After building zk-regex-apis, you can explore its exports at the Node REPL:
Compile the web package

```sh
$ npm install
$ node
> require('.').hello()
"hello node"
wasm-pack build --target nodejs
```

Pack the package (optional)

```sh
wasm-pack build --target nodejs
cd pkg
npm pkg set type='module'
wasm-pack pack
```

The output package file will be `packages/compiler/pkg/zk-regex-apis-1.1.1.tgz`

### For tests

```sh
wasm-pack test --node
```

## Available Scripts
Expand All @@ -47,8 +64,6 @@ Installs the project, including running `npm run build`.

### `npm build`

Builds the Node addon (`index.node`) from source.

Additional [`cargo build`](https://doc.rust-lang.org/cargo/commands/cargo-build.html) arguments may be passed to `npm build` and `npm build-*` commands. For example, to enable a [cargo feature](https://doc.rust-lang.org/cargo/reference/features.html):

```
Expand All @@ -65,17 +80,16 @@ Same as [`npm build`](#npm-build) but, builds the module with the [`release`](ht

### `npm test`

Runs the unit tests by calling `cargo test`. You can learn more about [adding tests to your Rust code](https://doc.rust-lang.org/book/ch11-01-writing-tests.html) from the [Rust book](https://doc.rust-lang.org/book/).
Runs the unit tests by calling `wasm-pack test --node`.

## Project Layout

The directory structure of this project is:

```
zk-regex-apis/
zk-regex-compiler/
├── Cargo.toml
├── README.md
├── index.node
├── package.json
├── src/
| └── lib.rs
Expand All @@ -90,12 +104,6 @@ The Cargo [manifest file](https://doc.rust-lang.org/cargo/reference/manifest.htm

This file.

### index.node

The Node addon—i.e., a binary Node module—generated by building the project. This is the main module for this package, as dictated by the `"main"` key in `package.json`.

Under the hood, a [Node addon](https://nodejs.org/api/addons.html) is a [dynamically-linked shared object](https://en.wikipedia.org/wiki/Library_(computing)#Shared_libraries). The `"build"` script produces this file by copying it from within the `target/` directory, which is where the Rust build produces the shared object.

### package.json

The npm [manifest file](https://docs.npmjs.com/cli/v7/configuring-npm/package-json), which informs the `npm` command.
Expand All @@ -111,11 +119,3 @@ The Rust library's main module.
### target/

Binary artifacts generated by the Rust build.

## Learn More

To learn more about Neon, see the [Neon documentation](https://neon-bindings.com).

To learn more about Rust, see the [Rust documentation](https://www.rust-lang.org).

To learn more about Node, see the [Node documentation](https://nodejs.org).
33 changes: 10 additions & 23 deletions packages/apis/package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
{
"name": "@zk-email/zk-regex-apis",
"version": "1.2.0",
"version": "2.0.0",
"description": "apis compatible with [zk-regex](https://github.com/zkemail/zk-regex/tree/main).",
"contributors": [
"Javier Su <[email protected]>",
"Kata Choi <[email protected]>",
"Sora Suegami <[email protected]>",
"Yush G <[email protected]>"
"Yush G <[email protected]>",
"Aditya Bisht <[email protected]>"
],
"main": "index.node",
"repository": {
"type": "git",
"url": "git+https://github.com/zkemail/zk-regex.git"
},
"scripts": {
"build": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics",
"build": "wasm-pack build --target nodejs --out-dir ./pkg/",
"build-debug": "npm run build --",
"build-release": "npm run build -- --release",
"build-release": "npm run build --",
"install": "npm run build-debug",
"install-release": "node-pre-gyp install --update-binary --fallback-to-build=false || npm run build-release",
"test": "cargo test",
"package": "node-pre-gyp package",
"upload-binary": "npm run package && node-pre-gyp-github publish"
"install-release": "npm run build-release",
"test": "wasm-pack test --node",
"upload-binary": "wasm-pack publish -t nodejs"
},
"license": "MIT",
"dependencies": {
"cargo-cp-artifact": "^0.1",
"@mapbox/node-pre-gyp": "^1.0",
"node-pre-gyp-github": "https://github.com/ultamatt/node-pre-gyp-github.git"
},
"binary": {
"module_name": "index",
"host": "https://github.com/zkemail/zk-regex/releases/download/",
"remote_path": "{version}",
"package_name": "apis-{node_abi}-{platform}-{arch}.tar.gz",
"module_path": "./"
}
}
"license": "MIT"
}
Loading

0 comments on commit d1ef521

Please sign in to comment.