Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Wasm Bindings #62

Merged
merged 31 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
21d9a20
update 2 files, create 6 files and delete 1 file
foolo Mar 10, 2024
ae5b918
update 1 file and create 1 file
foolo Mar 10, 2024
3a9c82e
node.rs
foolo Mar 10, 2024
ef16b82
wasm
foolo Mar 10, 2024
e45cb93
update circom.rs and lib.rs
foolo Mar 10, 2024
b84abe2
wasm_bindgen gen_from_raw_memory
foolo Mar 10, 2024
eefa39d
reverse_string
foolo Mar 10, 2024
d65a3b5
call gen_from_raw_memory from js
foolo Mar 10, 2024
44c6cb0
disable neon for wasm
foolo Mar 14, 2024
1044418
chore: delete .yarnrc
foolo Mar 14, 2024
1d75e5e
toolchain, docs
foolo Mar 14, 2024
54e4a41
remove pkg dir
foolo Mar 14, 2024
72621b7
Revert "update 1 file and create 1 file"
foolo Mar 14, 2024
397b526
cleanup
foolo Mar 14, 2024
4344247
target_family wasm
foolo Mar 14, 2024
f4f86b3
remove neon from compiler
foolo Mar 16, 2024
b7804b6
test stub
foolo Mar 17, 2024
419c32c
README
foolo Mar 17, 2024
e169d3c
update package.json, wasm.test.js and packages/compiler/README.md
foolo Mar 17, 2024
ffbba37
wasm-pack web README
foolo Mar 17, 2024
8a33a39
tests
foolo Mar 18, 2024
75e321a
change from unwrap to expect in gen_from_raw_memory, formatting
foolo Mar 18, 2024
2219f27
gen_from_decomposed_memory
foolo Mar 18, 2024
8005665
test: update email_addr.test.js
foolo Mar 18, 2024
abe34d8
update tests
foolo Mar 18, 2024
082c273
test: update simple_regex.test.js
foolo Mar 18, 2024
80ce1b2
chore: delete wasm.test.js
foolo Mar 18, 2024
4642a78
update circom.rs and lib.rs
foolo Mar 18, 2024
d8802bc
add wasm-bindgen to api
foolo Mar 18, 2024
e5d4017
feat: add wasm
Bisht13 Mar 22, 2024
29dd7d7
chore: update version
Bisht13 Mar 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <suegamisora@gmail.com>",
"Yush G <aayushg@mit.edu>",
"Javier Su <javier.su.weijie@gmail.com>",
"Kata Choi <kata.choi@gmail.com>"
"Kata Choi <kata.choi@gmail.com>",
"Aditya Bisht <adityabisht64@gmail.com>"
],
"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 <javier.su.weijie@gmail.com>",
"Kata Choi <kata.choi@gmail.com>",
"Sora Suegami <suegamisora@gmail.com>",
"Yush G <aayushg@mit.edu>",
"Aditya Bisht <adityabisht64@gmail.com>",
]

[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 <javier.su.weijie@gmail.com>",
"Kata Choi <kata.choi@gmail.com>",
"Sora Suegami <suegamisora@gmail.com>",
"Yush G <aayushg@mit.edu>"
"Yush G <aayushg@mit.edu>",
"Aditya Bisht <adityabisht64@gmail.com>"
],
"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
Loading