js-rln/README.md

178 lines
4.7 KiB
Markdown
Raw Normal View History

2023-02-24 15:26:33 +11:00
# `@waku/rln`
2022-08-26 18:13:15 +10:00
2023-02-24 15:26:33 +11:00
This browser library enables the usage of RLN with Waku, as specified in the [Waku v2 RLN Relay RFC](https://rfc.vac.dev/spec/17/).
2022-09-06 16:40:19 -04:00
2023-02-24 15:26:33 +11:00
## Purpose
### RLN Cryptography
The RLN cryptographic function are provided by [zerokit](https://github.com/vacp2p/zerokit/).
This is imported via the `@waku/zerokit-rln-wasm` dependencies which contains a WASM extract of zerokit's RLN functions.
Note that RLN Credentials generated with `zerokit`, and hence `@waku/rln`, are compatible with semaphore credentials.
2023-02-24 15:26:33 +11:00
Note that the WASM blob uses browser APIs, **NodeJS is not supported**.
### Waku Interfaces
This library implements the [`IEncoder`](https://github.com/waku-org/js-waku/blob/604ba1a889f1994bd27f5749107c3a5b2ef490d5/packages/interfaces/src/message.ts#L43)
and [`IDecoder`](https://github.com/waku-org/js-waku/blob/604ba1a889f1994bd27f5749107c3a5b2ef490d5/packages/interfaces/src/message.ts#L58)
interfaces of [js-waku](https://github.com/waku-org/js-waku).
This enables a seamless usage with js-waku applications, as demonstrated in the [rln-js example](https://github.com/waku-org/js-waku-examples/tree/master/examples/rln-js).
### Comparison to Existing Work
[Rate-Limiting-Nullifier/rlnjs](https://github.com/Rate-Limiting-Nullifier/rlnjs)
is an existing JavaScript / TypeScript library that already provides RLN cryptographic functionalities for the browser.
The core difference is that `@waku/rln` uses [zerokit](https://github.com/vacp2p/zerokit/) for cryptography and provide opinionated interfaces to use RLN specifically in the context of Waku.
## Install
2022-09-06 16:40:19 -04:00
```
npm install @waku/rln
# or with yarn
yarn add @waku/rln
```
2023-02-24 15:26:33 +11:00
Or to use ESM import directly from a `<script>` tag:
```html
<script type="module">
import * as rln from "https://unpkg.com/@waku/rln@0.0.13/bundle/index.js";
</script>
```
## Running example app
2022-09-20 08:44:03 -04:00
```
git clone https://github.com/waku-org/js-rln
cd js-rln/example
npm install # or yarn
npm start
```
Browse http://localhost:8080 and open the dev tools console to see the proof being generated and its verification
2023-02-24 15:26:33 +11:00
## Usage
2022-09-06 16:40:19 -04:00
2023-02-24 15:26:33 +11:00
### Initializing the Library
2022-09-06 16:40:19 -04:00
```js
import * as rln from "@waku/rln";
2023-02-24 15:26:33 +11:00
const rlnInstance = await rln.create();
2022-09-06 16:40:19 -04:00
```
2023-02-24 15:26:33 +11:00
### Generating RLN Membership Keypair
2022-09-06 16:40:19 -04:00
```js
let memKeys = rlnInstance.generateMembershipKey();
```
2023-02-24 15:26:33 +11:00
### Generating RLN Membership Keypair Using a Seed
This can be used to generate credentials from a signature hash (e.g. signed by an Ethereum account).
```js
let memKeys = rlnInstance.generateSeededMembershipKey(seed);
```
2022-09-06 16:40:19 -04:00
2023-02-24 15:26:33 +11:00
### Adding Membership Keys Into Merkle Tree
2022-09-06 16:40:19 -04:00
```js
2022-09-23 21:35:17 -04:00
rlnInstance.insertMember(memKeys.IDCommitment);
2022-09-06 16:40:19 -04:00
```
2023-02-24 15:26:33 +11:00
### Generating a Proof
2022-09-06 16:40:19 -04:00
```js
// prepare the message
const uint8Msg = Uint8Array.from(
"Hello World".split("").map((x) => x.charCodeAt())
);
2022-09-06 16:40:19 -04:00
// setting up the epoch (With 0s for the test)
const epoch = new Uint8Array(32);
// generating a proof
const proof = await rlnInstance.generateProof(
uint8Msg,
index,
epoch,
memKeys.IDKey
);
2022-09-06 16:40:19 -04:00
```
2023-02-24 15:26:33 +11:00
### Verifying a Proof
2022-09-06 16:40:19 -04:00
```js
try {
// verify the proof
2023-02-24 15:26:33 +11:00
const verificationResult = rlnInstance.verifyProof(proof, uint8Msg);
console.log("Is proof verified?", verificationResult ? "yes" : "no");
2022-09-06 16:40:19 -04:00
} catch (err) {
console.log("Invalid proof");
2022-09-06 16:40:19 -04:00
}
```
2023-02-24 15:26:33 +11:00
### Updating Circuit, Verification Key and Zkey
2023-02-24 15:26:33 +11:00
The RLN specs defines the defaults.
These values are fixed and should not change.
Currently, these [resources](https://github.com/vacp2p/zerokit/tree/master/rln/resources/tree_height_20) are being used.
If they change, this file needs to be updated in `resources.ts` which
2022-09-06 16:40:19 -04:00
contains these values encoded in base64 in this format:
```
const verification_key = "...";
const circuit = "..."; // wasm file generated by circom
const zkey = "...";
export {verification_key, circuit, zkey};
```
A tool like GNU's `base64` could be used to encode this data.
2022-09-06 16:40:19 -04:00
2023-02-24 15:26:33 +11:00
### Updating Zerokit
2023-02-24 15:26:33 +11:00
1. Make sure you have NodeJS installed and a C compiler
2022-09-06 16:40:19 -04:00
2. Install wasm-pack
2022-09-06 16:40:19 -04:00
```
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
```
2022-09-06 16:40:19 -04:00
3. Compile RLN for wasm
2022-09-06 16:40:19 -04:00
```
git clone https://github.com/vacp2p/zerokit
cd zerokit/rln-wasm
wasm-pack build --release
```
4. Copy `pkg/rln*` into `src/zerokit`
2022-09-06 16:40:19 -04:00
## Bugs, Questions & Features
If you encounter any bug or would like to propose new features, feel free to [open an issue](https://github.com/waku-org/js-rln/issues/new/).
For more general discussion, help and latest news, join [Vac Discord](https://discord.gg/PQFdubGt6d) or [Telegram](https://t.me/vacp2p).
2022-09-06 16:40:19 -04:00
## License
2022-09-06 16:40:19 -04:00
Licensed and distributed under either of
- MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
2022-09-06 16:40:19 -04:00
or
2023-02-24 15:26:33 +11:00
- Apache License, Version 2.0, ([LICENSE-APACHE-v2](LICENSE-APACHE-v2) or http://www.apache.org/licenses/LICENSE-2.0)
2022-09-06 16:40:19 -04:00
at your option. These files may not be copied, modified, or distributed except according to those terms.