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

Type Refactor #53

Merged
merged 3 commits into from
Apr 13, 2023
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
4 changes: 2 additions & 2 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": "rlnjs",
"version": "2.0.6",
"version": "2.0.7",
"description": "Client library for generating and using RLN ZK proofs.",
"license": "MIT",
"repository": "https://github.com/Rate-Limiting-Nullifier/rlnjs",
Expand Down Expand Up @@ -93,4 +93,4 @@
"tslib": "^2.5.0",
"typescript": "^4.9.5"
}
}
}
10 changes: 5 additions & 5 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { RLNFullProof, StrBigInt } from './types'
import RLN from './rln'
import { isSameProof } from './utils'

type EpochCacheT = {
[nullifier: string]: RLNFullProof[];
type EpochCache = {
[nullifier: string]: RLNFullProof[]
}

type CacheT = {
[epoch: string]: EpochCacheT;
type CacheMap = {
[epoch: string]: EpochCache
}

export enum Status {
Expand All @@ -32,7 +32,7 @@ export default class Cache {

rlnIdentifier: bigint

cache: CacheT
cache: CacheMap

epochs: string[]

Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ export {
RLNFullProof,
Proof,
RLNPublicSignals,
RLNSNARKProof,
VerificationKey,
RLNWitness,
CircuitParamsFilePath,
} from './types'
14 changes: 7 additions & 7 deletions src/rln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import poseidon from 'poseidon-lite'
import { Identity } from '@semaphore-protocol/identity'

// Types
import { RLNFullProof, RLNSNARKProof, RLNWitnessT, StrBigInt, VerificationKeyT } from './types'
import { RLNFullProof, RLNSNARKProof, RLNWitness, StrBigInt, VerificationKey } from './types'
import { instantiateBn254, deserializeJSRLNProof, serializeJSRLNProof } from './waku'


Expand All @@ -29,7 +29,7 @@ export default class RLN {

finalZkeyPath: string

verificationKey: VerificationKeyT
verificationKey: VerificationKey

rlnIdentifier: bigint

Expand All @@ -39,7 +39,7 @@ export default class RLN {

secretIdentity: bigint

constructor(wasmFilePath: string, finalZkeyPath: string, verificationKey: VerificationKeyT, rlnIdentifier?: bigint, identity?: string) {
constructor(wasmFilePath: string, finalZkeyPath: string, verificationKey: VerificationKey, rlnIdentifier?: bigint, identity?: string) {
this.wasmFilePath = wasmFilePath
this.finalZkeyPath = finalZkeyPath
this.verificationKey = verificationKey
Expand Down Expand Up @@ -75,7 +75,7 @@ export default class RLN {
*/
public async _genProof(
epoch: bigint,
witness: RLNWitnessT,
witness: RLNWitness,
): Promise<RLNFullProof> {
const snarkProof: RLNSNARKProof = await RLN._genSNARKProof(witness, this.wasmFilePath, this.finalZkeyPath)
return {
Expand All @@ -93,7 +93,7 @@ export default class RLN {
* @returns The full SnarkJS proof.
*/
public static async _genSNARKProof(
witness: RLNWitnessT, wasmFilePath: string, finalZkeyPath: string,
witness: RLNWitness, wasmFilePath: string, finalZkeyPath: string,
): Promise<RLNSNARKProof> {
const { proof, publicSignals } = await groth16.fullProve(
witness,
Expand Down Expand Up @@ -136,7 +136,7 @@ export default class RLN {
* @param fullProof The SnarkJS full proof.
* @returns True if the proof is valid, false otherwise.
*/
public static async verifySNARKProof(verificationKey: VerificationKeyT,
public static async verifySNARKProof(verificationKey: VerificationKey,
{ proof, publicSignals }: RLNSNARKProof,
): Promise<boolean> {
return groth16.verify(
Expand Down Expand Up @@ -165,7 +165,7 @@ export default class RLN {
epoch: StrBigInt,
signal: string,
shouldHash = true,
): RLNWitnessT {
): RLNWitness {
return {
identitySecret: this.secretIdentity,
pathElements: merkleProof.siblings,
Expand Down
9 changes: 3 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export type RLNFullProof = {
rlnIdentifier: bigint
}


export type VerificationKeyT = {
export type VerificationKey = {
protocol: string,
curve: string,
nPublic: number,
Expand All @@ -55,8 +54,7 @@ export type VerificationKeyT = {
IC: string[][],
}


export type RLNWitnessT = {
export type RLNWitness = {
identitySecret: bigint,
// Ignore `no-explicit-any` because the type of `identity_path_elements` in zk-kit is `any[]`
pathElements: any[], // eslint-disable-line @typescript-eslint/no-explicit-any
Expand All @@ -65,8 +63,7 @@ export type RLNWitnessT = {
externalNullifier: bigint,
}


export type CircuitParamsFilePathT = {
export type CircuitParamsFilePath = {
vkeyPath: string,
wasmFilePath: string,
finalZkeyPath: string,
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ZqField } from 'ffjavascript'
import { RLNFullProof, VerificationKeyT } from './types'
import { RLNFullProof, VerificationKey } from './types'

/*
This is the "Baby Jubjub" curve described here:
Expand All @@ -23,7 +23,7 @@ export function concatUint8Arrays(...arrays: Uint8Array[]): Uint8Array {
}


export function parseVerificationKeyJSON(json: string): VerificationKeyT {
export function parseVerificationKeyJSON(json: string): VerificationKey {
const o = JSON.parse(json)
// NOTE: This is not a complete check, to do better we can check values are of the correct type
if (!o.protocol) throw new Error('Verification key has no protocol')
Expand Down
2 changes: 1 addition & 1 deletion src/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function deserializePointCompressed(curve: CurveT, bytesLE: Uint8Array, sizeComp
if (bytesLE.length !== sizeCompressed) {
throw new Error(
'bytes length is not equal to `sizeCompressed: ' +
`bytesLE.length=${bytesLE.length}, sizeCompressed=${sizeCompressed}`,
`bytesLE.length=${bytesLE.length}, sizeCompressed=${sizeCompressed}`,
)
}
if (!isCompressionValid(bytesLE)) {
Expand Down
8 changes: 4 additions & 4 deletions tests/configs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as path from "path"
import { CircuitParamsFilePathT } from "../src/types";
import { CircuitParamsFilePath } from "../src/types"


const thisFileDirname = __dirname


function getParamsPath(paramsDir: string): CircuitParamsFilePathT {
function getParamsPath(paramsDir: string): CircuitParamsFilePath {
return {
vkeyPath: path.join(paramsDir, "verification_key.json"),
wasmFilePath: path.join(paramsDir, "rln.wasm"),
Expand All @@ -14,8 +14,8 @@ function getParamsPath(paramsDir: string): CircuitParamsFilePathT {
}


const defaultParamsDirname = path.join(thisFileDirname, "..", "zkeyFiles", "rln");
const jsRLNParamsDirname = path.join(thisFileDirname, "..", "zkeyFiles", "js-rln");
const defaultParamsDirname = path.join(thisFileDirname, "..", "zkeyFiles", "rln")
const jsRLNParamsDirname = path.join(thisFileDirname, "..", "zkeyFiles", "js-rln")

export const defaultParamsPath = getParamsPath(defaultParamsDirname)
export const jsRLNParamsPath = getParamsPath(jsRLNParamsDirname)
14 changes: 7 additions & 7 deletions tests/factories.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as fs from "fs";
import { RLN } from "../src";
import { CircuitParamsFilePathT } from "../src/types";
import { Fq, parseVerificationKeyJSON } from "../src/utils";
import { defaultParamsPath } from "./configs";
import * as fs from "fs"
import { RLN } from "../src"
import { CircuitParamsFilePath } from "../src/types"
import { Fq, parseVerificationKeyJSON } from "../src/utils"
import { defaultParamsPath } from "./configs"


export function rlnInstanceFactory (
paramsPath: CircuitParamsFilePathT = defaultParamsPath,
export function rlnInstanceFactory(
paramsPath: CircuitParamsFilePath = defaultParamsPath,
rlnIdentifier?: bigint,
identity?: string,
) {
Expand Down