Skip to content

Commit abfb6a0

Browse files
tests: added withdraw benchmarks and readme
1 parent 1324a0d commit abfb6a0

File tree

2 files changed

+52
-35
lines changed

2 files changed

+52
-35
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ The core of RLN is in the [circuit logic](https://github.com/Rate-Limiting-Nulli
3232

3333
[`RLN`](./src/rln.ts) class is the core of RLNjs. It allows user to generate proofs, verify proofs, and detect spams. Also, user can register to RLN, withdraw, and slash spammers.
3434

35+
| _Tests Ran on an M2 Macbook_ | Time |
36+
| ---------------------------- | ------ |
37+
| RLN Proof | ~800ms |
38+
| RLN Proof Verification | ~130ms |
39+
| Withdraw Proof | ~260ms |
40+
| Withdraw Proof Verification | ~145ms |
41+
3542
## Install
3643

3744
Install rlnjs with npm:

tests/circuit-wrapper.test.ts

+45-35
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
1-
import { RLNProver, RLNVerifier, WithdrawProver, WithdrawVerifier } from "../src/circuit-wrapper";
2-
import { rlnParams, withdrawParams } from "./configs";
3-
import { fieldFactory, generateMerkleProof } from "./utils";
4-
import poseidon from "poseidon-lite";
5-
import { DEFAULT_REGISTRY_TREE_DEPTH } from "../src/registry";
1+
import { RLNProver, RLNVerifier, WithdrawProver, WithdrawVerifier } from '../src/circuit-wrapper';
2+
import { rlnParams, withdrawParams } from './configs';
3+
import { fieldFactory, generateMerkleProof } from './utils';
4+
import poseidon from 'poseidon-lite';
5+
import { DEFAULT_REGISTRY_TREE_DEPTH } from '../src/registry';
66

77
// `userMessageLimit` is at most 16 bits
88
// Ref: https://github.com/Rate-Limiting-Nullifier/rln-circuits-v2/blob/b40dfa63b7b1248527d7ab417d0d9cf538cad93a/circuits/utils.circom#L36-L37
9-
const LIMIT_BIT_SIZE = 16
9+
const LIMIT_BIT_SIZE = 16;
1010

11-
describe("RLN", function () {
12-
const rlnIdentifier = fieldFactory()
13-
const rlnProver = new RLNProver(rlnParams.wasmFilePath, rlnParams.finalZkeyPath)
14-
const rlnVerifier = new RLNVerifier(rlnParams.verificationKey)
15-
const identitySecret = fieldFactory()
16-
const identityCommitment = poseidon([identitySecret])
17-
const leaves = [identityCommitment]
18-
const userMessageLimit = (BigInt(1) << BigInt(LIMIT_BIT_SIZE)) - BigInt(1)
19-
const messageId = BigInt(0)
20-
const x = fieldFactory()
21-
const epoch = fieldFactory()
22-
const treeDepth = DEFAULT_REGISTRY_TREE_DEPTH
11+
describe('RLN', function () {
12+
const rlnIdentifier = fieldFactory();
13+
const rlnProver = new RLNProver(rlnParams.wasmFilePath, rlnParams.finalZkeyPath);
14+
const rlnVerifier = new RLNVerifier(rlnParams.verificationKey);
15+
const identitySecret = fieldFactory();
16+
const identityCommitment = poseidon([identitySecret]);
17+
const leaves = [identityCommitment];
18+
const userMessageLimit = (BigInt(1) << BigInt(LIMIT_BIT_SIZE)) - BigInt(1);
19+
const messageId = BigInt(0);
20+
const x = fieldFactory();
21+
const epoch = fieldFactory();
22+
const treeDepth = DEFAULT_REGISTRY_TREE_DEPTH;
2323

24+
test('should generate valid proof', async function () {
2425
const m0 = performance.now();
2526
const merkleProof = generateMerkleProof(rlnIdentifier, leaves, treeDepth, 0);
2627
const m1 = performance.now();
27-
const proof = await rlnProver.generateProof({
28-
rlnIdentifier,
29-
identitySecret,
30-
userMessageLimit,
31-
messageId,
32-
merkleProof,
33-
x,
28+
const proof = await rlnProver.generateProof({
29+
rlnIdentifier,
30+
identitySecret,
31+
userMessageLimit,
32+
messageId,
33+
merkleProof,
34+
x,
3435
epoch
3536
});
3637
const m2 = performance.now();
@@ -40,17 +41,26 @@ describe("RLN", function () {
4041
console.log(`RLN proof generation: ${m2 - m1} ms`);
4142
console.log(`RLN proof verification: ${m3 - m2} ms`);
4243
expect(isValid).toBeTruthy();
43-
});
44+
});
4445
});
4546

46-
describe("Withdraw", function () {
47-
const withdrawProver = new WithdrawProver(withdrawParams.wasmFilePath, withdrawParams.finalZkeyPath)
48-
const withdrawVerifier = new WithdrawVerifier(withdrawParams.verificationKey)
47+
describe('Withdraw', function () {
48+
const withdrawProver = new WithdrawProver(
49+
withdrawParams.wasmFilePath,
50+
withdrawParams.finalZkeyPath
51+
);
52+
const withdrawVerifier = new WithdrawVerifier(withdrawParams.verificationKey);
4953

50-
test("should generate valid proof", async function () {
51-
const identitySecret = fieldFactory()
52-
const address = fieldFactory()
53-
const proof = await withdrawProver.generateProof({identitySecret, address})
54-
expect(await withdrawVerifier.verifyProof(proof)).toBeTruthy()
55-
});
54+
test('should generate valid proof', async function () {
55+
const identitySecret = fieldFactory();
56+
const address = fieldFactory();
57+
const m0 = performance.now();
58+
const proof = await withdrawProver.generateProof({ identitySecret, address });
59+
const m1 = performance.now();
60+
const isValid = await withdrawVerifier.verifyProof(proof);
61+
const m2 = performance.now();
62+
console.log(`Withdraw proof generation: ${m1 - m0} ms`);
63+
console.log(`Withdraw proof verification: ${m2 - m1} ms`);
64+
expect(isValid).toBeTruthy();
65+
});
5666
});

0 commit comments

Comments
 (0)