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

Documentation and exports cleanup #13

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,47 @@ Clone this repo and build following the [Compilation] instructions below.
Import the module:

```
import { ec_add, ec_mul, ec_pairing } from 'rustbn'
import { initRustBN } from 'rustbn'

const bn128 = await initRustBN()
```

Curve Addition

```
let inputHexString = ...
let outputHexString = ec_add(inputHexString)
let outputHexString = bn128.ec_add(inputHexString)
```

Curve Multiplication

```
let inputHexString = ...
let outputHexString = ec_mul(inputHexString)
let outputHexString = bn128.ec_mul(inputHexString)
```

Curve Pairing
```
let inputHexString = ...
let outputHexString = ec_pairing(inputHexString)
let outputHexString = bn128.ec_pairing(inputHexString)
```

## Developer

### Building the module

For basic setup:

- Install `rust` and `wasm-pack` via the [`wasm-pack` prerequisites](https://rustwasm.github.io/docs/wasm-pack/prerequisites/index.html)
- Clone this repo
- Install JS dependencies - `npm i`
- Run `npm run build`
- Run `npm run build:wasm` to compile the Rust code to WASM
- Run `npm run wasm2b64` to convert the WASM bytecode to a base64 string consumable by the Typescript wrapper
- Run `npm run build` to build the final JS outputs

### Build Outputs

The build process outputs both CommonJS and ESM builds of the library and specifies entry points based on the `main` and `module` fields in `package.json`. The CommonJS build is the direct output of `wasm-pack build --target nodejs` while the ESM build is a customized version of the output from `wasm-pack build --target web` that removes the need to use a bundler by converting the wasm bytecode to a base64 string and then placed in a JSON object that ESM environments can load directly. Note, the ESM glue code is found in [`src.ts`](./src.ts/rustbn.ts)
The build process outputs both CommonJS and ESM builds of the library and specifies entry points based on the `main`, `module`, and alternatively the `exports` fields in `package.json`. Both builds start with the wasm build from `wasm-bindgen` which is then loaded by a Typescript wrapper of the WASM output. Our Typescript code is slightly different than the original Javascript generated by `wasm-bindgen` because the default ESM output would require a bundler.

## License

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
"build": "rm -rf dist && scripts/ts-build.sh",
"prepare": "npm run build",
"test": "vitest run test/*",
"build:wasm": "wasm-pack build --target web -d wasm",
"wasm2b64": "node scripts/wasmToB64.js"
},
"main": "dist/cjs/rustbn.js",
"module": "dist/esm/rustbn.js",
"exports": {
"require": "dist/cjs/rustbn.js",
"import": "dist/esm/rustbn.js"
"require": "./dist/cjs/rustbn.js",
"import": "./dist/esm/rustbn.js"
},
"dependencies": {
"@scure/base": "^1.1.5"
Expand Down