diff --git a/src/codec.spec.ts b/src/codec.spec.ts index ba2e012..a909839 100644 --- a/src/codec.spec.ts +++ b/src/codec.spec.ts @@ -53,6 +53,7 @@ describe("RLN codec with version 0", () => { expect(msg.rateLimitProof).to.not.be.undefined; expect(msg.verify()).to.be.true; + expect(msg.verifyNoRoot()).to.be.true; expect(msg.epoch).to.not.be.undefined; expect(msg.epoch).to.be.gt(0); @@ -92,6 +93,7 @@ describe("RLN codec with version 0", () => { expect(msg.rateLimitProof).to.not.be.undefined; expect(msg.verify()).to.be.true; + expect(msg.verifyNoRoot()).to.be.true; expect(msg.epoch).to.not.be.undefined; expect(msg.epoch).to.be.gt(0); @@ -134,6 +136,7 @@ describe("RLN codec with version 1", () => { expect(msg.rateLimitProof).to.not.be.undefined; expect(msg.verify()).to.be.true; + expect(msg.verifyNoRoot()).to.be.true; expect(msg.epoch).to.not.be.undefined; expect(msg.epoch).to.be.gt(0); @@ -175,6 +178,7 @@ describe("RLN codec with version 1", () => { expect(msg.rateLimitProof).to.not.be.undefined; expect(msg.verify()).to.be.true; + expect(msg.verifyNoRoot()).to.be.true; expect(msg.epoch).to.not.be.undefined; expect(msg.epoch).to.be.gt(0); @@ -216,6 +220,7 @@ describe("RLN codec with version 1", () => { expect(msg.rateLimitProof).to.not.be.undefined; expect(msg.verify()).to.be.true; + expect(msg.verifyNoRoot()).to.be.true; expect(msg.epoch).to.not.be.undefined; expect(msg.epoch).to.be.gt(0); @@ -258,6 +263,7 @@ describe("RLN codec with version 1", () => { expect(msg.rateLimitProof).to.not.be.undefined; expect(msg.verify()).to.be.true; + expect(msg.verifyNoRoot()).to.be.true; expect(msg.epoch).to.not.be.undefined; expect(msg.epoch).to.be.gt(0); @@ -302,6 +308,7 @@ describe("RLN Codec - epoch", () => { expect(msg.rateLimitProof).to.not.be.undefined; expect(msg.verify()).to.be.true; + expect(msg.verifyNoRoot()).to.be.true; expect(msg.epoch).to.not.be.undefined; expect(msg.epoch!.toString(10).length).to.eq(9); expect(msg.epoch).to.eq(epoch); diff --git a/src/message.ts b/src/message.ts index 6b903aa..7ac64b7 100644 --- a/src/message.ts +++ b/src/message.ts @@ -22,6 +22,15 @@ export class RlnMessage implements Message { : undefined; } + public verifyNoRoot(): boolean | undefined { + return this.rateLimitProof + ? this.rlnInstance.verifyWithNoRoot( + this.rateLimitProof, + toRLNSignal(this) + ) // this.rlnInstance.verifyRLNProof once issue status-im/nwaku#1248 is fixed + : undefined; + } + get payload(): Uint8Array | undefined { return this.msg.payload; } diff --git a/src/rln.ts b/src/rln.ts index fb23864..0aee04b 100644 --- a/src/rln.ts +++ b/src/rln.ts @@ -225,4 +225,25 @@ export class RLNInstance { root ); } + + verifyWithNoRoot( + proof: RateLimitProof | Uint8Array, + msg: Uint8Array + ): boolean { + let pBytes: Uint8Array; + if (proof instanceof Uint8Array) { + pBytes = proof; + } else { + pBytes = proofToBytes(proof); + } + + // calculate message length + const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8); + + return zerokitRLN.verifyWithRoots( + this.zkRLN, + concatenate(pBytes, msgLen, msg), + new Uint8Array() + ); + } }