Skip to content

Commit

Permalink
feat: use identity credentials, and expose hash, bulk insert and dele…
Browse files Browse the repository at this point in the history
…te members functions

Also update the resources and karma.conf because proof generation is
taking longer than usual
  • Loading branch information
richard-ramos committed May 1, 2023
1 parent 7e0966a commit 2bc0ec2
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 166 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,24 @@ import * as rln from "@waku/rln";
const rlnInstance = await rln.create();
```

### Generating RLN Membership Keypair
#### Generating RLN membership credentials

```js
let memKeys = rlnInstance.generateMembershipKey();
let credentials = rlnInstance.generateIdentityCredentials();
```

### 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);
let credentials = rlnInstance.generateSeededIdentityCredentials(seed);
```

### Adding Membership Keys Into Merkle Tree

```js
rlnInstance.insertMember(memKeys.IDCommitment);
rlnInstance.insertMember(credentials.IDCommitment);
```

### Generating a Proof
Expand All @@ -106,7 +106,7 @@ const proof = await rlnInstance.generateProof(
uint8Msg,
index,
epoch,
memKeys.IDKey
credentials.IDSecretHash
);
```

Expand Down
10 changes: 5 additions & 5 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as rln from "@waku/rln";

rln.create().then(async rlnInstance => {
let memKeys = rlnInstance.generateMembershipKey();
let credentials = rlnInstance.generateIdentityCredentials();

//peer's index in the Merkle Tree
const index = 5
Expand All @@ -10,11 +10,11 @@ rln.create().then(async rlnInstance => {
for (let i = 0; i < 10; i++) {
if (i == index) {
// insert the current peer's pk
rlnInstance.insertMember(memKeys.IDCommitment);
rlnInstance.insertMember(credentials.IDCommitment);
} else {
// create a new key pair
let memKeys = rlnInstance.generateMembershipKey(); // TODO: handle error
rlnInstance.insertMember(memKeys.IDCommitment);
let credentials = rlnInstance.generateIdentityCredentials(); // TODO: handle error
rlnInstance.insertMember(credentials.IDCommitment);

}
}
Expand All @@ -27,7 +27,7 @@ rln.create().then(async rlnInstance => {

console.log("Generating proof...");
console.time("proof_gen_timer");
let proof = await rlnInstance.generateRLNProof(uint8Msg, index, epoch, memKeys.IDKey)
let proof = await rlnInstance.generateRLNProof(uint8Msg, index, epoch, credentials.IDSecretHash)
console.timeEnd("proof_gen_timer");
console.log("Proof", proof)

Expand Down
3 changes: 2 additions & 1 deletion karma.conf.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ module.exports = function (config) {
envPreprocessor: ["CI"],
reporters: ["progress"],
browsers: ["ChromeHeadless"],
pingTimeout: 60000,
singleRun: true,
client: {
mocha: {
timeout: 6000, // Default is 2s
timeout: 60000, // Default is 2s
},
},
webpack: {
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@waku/rln",
"version": "0.0.14",
"version": "0.1.0",
"description": "Rate Limit Nullifier for js-waku",
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
Expand Down Expand Up @@ -130,7 +130,7 @@
},
"dependencies": {
"@waku/utils": "^0.0.4",
"@waku/zerokit-rln-wasm": "^0.0.5",
"@waku/zerokit-rln-wasm": "^0.0.8",
"ethers": "^5.7.2"
}
}
42 changes: 21 additions & 21 deletions src/codec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ const EMPTY_PUBSUB_TOPIC = "";
describe("RLN codec with version 0", () => {
it("toWire", async function () {
const rlnInstance = await rln.create();
const memKeys = rlnInstance.generateMembershipKey();
const credential = rlnInstance.generateIdentityCredentials();
const index = 0;
const payload = new Uint8Array([1, 2, 3, 4, 5]);

rlnInstance.insertMember(memKeys.IDCommitment);
rlnInstance.insertMember(credential.IDCommitment);

const rlnEncoder = createRLNEncoder({
encoder: createEncoder({ contentTopic: TestContentTopic }),
rlnInstance,
index,
membershipKey: memKeys,
credential,
});
const rlnDecoder = createRLNDecoder({
rlnInstance,
Expand Down Expand Up @@ -76,17 +76,17 @@ describe("RLN codec with version 0", () => {

it("toProtoObj", async function () {
const rlnInstance = await rln.create();
const memKeys = rlnInstance.generateMembershipKey();
const credential = rlnInstance.generateIdentityCredentials();
const index = 0;
const payload = new Uint8Array([1, 2, 3, 4, 5]);

rlnInstance.insertMember(memKeys.IDCommitment);
rlnInstance.insertMember(credential.IDCommitment);

const rlnEncoder = new RLNEncoder(
createEncoder({ contentTopic: TestContentTopic }),
rlnInstance,
index,
memKeys
credential
);
const rlnDecoder = new RLNDecoder(
rlnInstance,
Expand Down Expand Up @@ -119,11 +119,11 @@ describe("RLN codec with version 0", () => {
describe("RLN codec with version 1", () => {
it("Symmetric, toWire", async function () {
const rlnInstance = await rln.create();
const memKeys = rlnInstance.generateMembershipKey();
const credential = rlnInstance.generateIdentityCredentials();
const index = 0;
const payload = new Uint8Array([1, 2, 3, 4, 5]);

rlnInstance.insertMember(memKeys.IDCommitment);
rlnInstance.insertMember(credential.IDCommitment);

const symKey = generateSymmetricKey();

Expand All @@ -134,7 +134,7 @@ describe("RLN codec with version 1", () => {
}),
rlnInstance,
index,
memKeys
credential
);
const rlnDecoder = new RLNDecoder(
rlnInstance,
Expand Down Expand Up @@ -166,11 +166,11 @@ describe("RLN codec with version 1", () => {

it("Symmetric, toProtoObj", async function () {
const rlnInstance = await rln.create();
const memKeys = rlnInstance.generateMembershipKey();
const credential = rlnInstance.generateIdentityCredentials();
const index = 0;
const payload = new Uint8Array([1, 2, 3, 4, 5]);

rlnInstance.insertMember(memKeys.IDCommitment);
rlnInstance.insertMember(credential.IDCommitment);

const symKey = generateSymmetricKey();

Expand All @@ -181,7 +181,7 @@ describe("RLN codec with version 1", () => {
}),
rlnInstance,
index,
memKeys
credential
);
const rlnDecoder = new RLNDecoder(
rlnInstance,
Expand Down Expand Up @@ -212,11 +212,11 @@ describe("RLN codec with version 1", () => {

it("Asymmetric, toWire", async function () {
const rlnInstance = await rln.create();
const memKeys = rlnInstance.generateMembershipKey();
const credential = rlnInstance.generateIdentityCredentials();
const index = 0;
const payload = new Uint8Array([1, 2, 3, 4, 5]);

rlnInstance.insertMember(memKeys.IDCommitment);
rlnInstance.insertMember(credential.IDCommitment);

const privateKey = generatePrivateKey();
const publicKey = getPublicKey(privateKey);
Expand All @@ -228,7 +228,7 @@ describe("RLN codec with version 1", () => {
}),
rlnInstance,
index,
memKeys
credential
);
const rlnDecoder = new RLNDecoder(
rlnInstance,
Expand Down Expand Up @@ -260,11 +260,11 @@ describe("RLN codec with version 1", () => {

it("Asymmetric, toProtoObj", async function () {
const rlnInstance = await rln.create();
const memKeys = rlnInstance.generateMembershipKey();
const credential = rlnInstance.generateIdentityCredentials();
const index = 0;
const payload = new Uint8Array([1, 2, 3, 4, 5]);

rlnInstance.insertMember(memKeys.IDCommitment);
rlnInstance.insertMember(credential.IDCommitment);

const privateKey = generatePrivateKey();
const publicKey = getPublicKey(privateKey);
Expand All @@ -276,7 +276,7 @@ describe("RLN codec with version 1", () => {
}),
rlnInstance,
index,
memKeys
credential
);
const rlnDecoder = new RLNDecoder(
rlnInstance,
Expand Down Expand Up @@ -309,17 +309,17 @@ describe("RLN codec with version 1", () => {
describe("RLN Codec - epoch", () => {
it("toProtoObj", async function () {
const rlnInstance = await rln.create();
const memKeys = rlnInstance.generateMembershipKey();
const credential = rlnInstance.generateIdentityCredentials();
const index = 0;
const payload = new Uint8Array([1, 2, 3, 4, 5]);

rlnInstance.insertMember(memKeys.IDCommitment);
rlnInstance.insertMember(credential.IDCommitment);

const rlnEncoder = new RLNEncoder(
createEncoder({ contentTopic: TestContentTopic }),
rlnInstance,
index,
memKeys
credential
);
const rlnDecoder = new RLNDecoder(
rlnInstance,
Expand Down
14 changes: 7 additions & 7 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import type {
import debug from "debug";

import { RlnMessage, toRLNSignal } from "./message.js";
import { MembershipKey, RLNInstance } from "./rln.js";
import { IdentityCredential, RLNInstance } from "./rln.js";

const log = debug("waku:rln:encoder");

export class RLNEncoder implements IEncoder {
private readonly idKey: Uint8Array;
private readonly idSecretHash: Uint8Array;

constructor(
private encoder: IEncoder,
private rlnInstance: RLNInstance,
private index: number,
membershipKey: MembershipKey
identityCredential: IdentityCredential
) {
if (index < 0) throw "invalid membership index";
this.idKey = membershipKey.IDKey;
this.idSecretHash = identityCredential.IDSecretHash;
}

async toWire(message: IMessage): Promise<Uint8Array | undefined> {
Expand All @@ -50,7 +50,7 @@ export class RLNEncoder implements IEncoder {
signal,
this.index,
message.timestamp,
this.idKey
this.idSecretHash
);
console.timeEnd("proof_gen_timer");
return proof;
Expand All @@ -69,15 +69,15 @@ type RLNEncoderOptions = {
encoder: IEncoder;
rlnInstance: RLNInstance;
index: number;
membershipKey: MembershipKey;
credential: IdentityCredential;
};

export const createRLNEncoder = (options: RLNEncoderOptions): RLNEncoder => {
return new RLNEncoder(
options.encoder,
options.rlnInstance,
options.index,
options.membershipKey
options.credential
);
};

Expand Down
Loading

0 comments on commit 2bc0ec2

Please sign in to comment.