forked from Rate-Limiting-Nullifier/rlnjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b731ad
commit 45f4ddb
Showing
9 changed files
with
7,763 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
node_modules/ | ||
coverage | ||
zkeyFiles | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { RLNFullProof, StrBigInt } from './types'; | ||
type EpochCacheT = { | ||
nullifiers?: RLNFullProof[]; | ||
}; | ||
export declare enum Status { | ||
ADDED = "added", | ||
BREACH = "breach", | ||
INVALID = "invalid" | ||
} | ||
export type EvaluatedProof = { | ||
status: Status; | ||
nullifier?: StrBigInt; | ||
secret?: StrBigInt; | ||
msg?: string; | ||
}; | ||
/** | ||
* Cache for storing proofs and automatically evaluating them for rate limit breaches | ||
*/ | ||
export default class Cache { | ||
cache: { | ||
string?: EpochCacheT; | ||
}; | ||
epochs: string[]; | ||
cacheLength: number; | ||
rln_identifier: StrBigInt; | ||
/** | ||
* | ||
* @param cacheLength the maximum number of epochs to store in the cache, default is 100, set to 0 to automatic pruning | ||
*/ | ||
constructor(rln_identifier: StrBigInt, cacheLength?: number); | ||
get _cache(): { | ||
string?: EpochCacheT | undefined; | ||
}; | ||
get _epochs(): string[]; | ||
/** | ||
* Adds a proof to the cache | ||
* @param proof the RLNFullProof to add to the cache | ||
* @returns an object with the status of the proof and the nullifier and secret if the proof is a breach | ||
*/ | ||
addProof(proof: RLNFullProof): EvaluatedProof; | ||
private evaluateNullifierAtEpoch; | ||
private evaluateEpoch; | ||
private removeEpoch; | ||
export(): Promise<string>; | ||
static import(cache: string): Promise<Cache>; | ||
} | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import RLN from "./rln"; | ||
import Registry from './registry'; | ||
import Cache from './cache'; | ||
import { genExternalNullifier } from "./utils"; | ||
export { RLN, Registry, Cache, genExternalNullifier }; | ||
export { StrBigInt, RLNFullProof, Proof, RLNPublicSignals } from "./types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { MerkleProof } from "@zk-kit/incremental-merkle-tree"; | ||
import { StrBigInt } from './types'; | ||
export default class Registry { | ||
private _registry; | ||
private _slashed; | ||
_treeDepth: number; | ||
_zeroValue: BigInt; | ||
/** | ||
* Initializes the registry with the tree depth and the zero value. | ||
* @param treeDepth Tree depth (int). | ||
* @param zeroValue Zero values for zeroes. | ||
*/ | ||
constructor(treeDepth?: number, zeroValue?: BigInt); | ||
/** | ||
* Returns the root hash of the registry merkle tree. | ||
* @returns Root hash. | ||
*/ | ||
get root(): BigInt; | ||
/** | ||
* Returns the root hash of the slashed registry merkle tree. | ||
* @returns Root hash. | ||
*/ | ||
get slashedRoot(): BigInt; | ||
/** | ||
* Returns the members (i.e. identity commitments) of the registry. | ||
* @returns List of members. | ||
*/ | ||
get members(): bigint[]; | ||
/** | ||
* Returns the members (i.e. identity commitments) of the slashed registry. | ||
* @returns List of slashed members. | ||
*/ | ||
get slashedMembers(): bigint[]; | ||
/** | ||
* Returns the index of a member. If the member does not exist it returns -1. | ||
* @param member Registry member. | ||
* @returns Index of the member. | ||
*/ | ||
indexOf(member: BigInt): number; | ||
/** | ||
* Adds a new member to the registry. | ||
* If a member exists in the slashed registry, the member can't be added. | ||
* @param identityCommitment New member. | ||
*/ | ||
addMember(identityCommitment: BigInt): void; | ||
/** | ||
* Adds new members to the registry. | ||
* @param identityCommitments New members. | ||
*/ | ||
addMembers(identityCommitments: BigInt[]): void; | ||
/** | ||
* Removes a member from the registry and adds them to the slashed registry. | ||
* @param identityCommitment IdentityCommitment of the member to be removed. | ||
*/ | ||
slashMember(identityCommitment: BigInt): void; | ||
/** | ||
* Adds a new member to the slahed registry. | ||
* If a member exists in the registry, the member can't be added to the slashed. | ||
* @param identityCommitment New member. | ||
*/ | ||
addSlashedMember(identityCommitment: BigInt): void; | ||
/** | ||
* Adds new members to the slashed registry. | ||
* @param identityCommitments New members. | ||
*/ | ||
addSlashedMembers(identityCommitments: BigInt[]): void; | ||
/** | ||
* Removes a member from the registry. | ||
* @param identityCommitment IdentityCommitment of the member to be removed. | ||
*/ | ||
removeMember(identityCommitment: BigInt): void; | ||
/** | ||
* Creates a Merkle Proof. | ||
* @param idCommitment The leaf for which Merkle proof should be created. | ||
* @returns The Merkle proof. | ||
*/ | ||
generateMerkleProof(idCommitment: StrBigInt): Promise<MerkleProof>; | ||
/** | ||
* Creates a Merkle Proof. | ||
* @param depth The depth of the tree. | ||
* @param zeroValue The zero value of the tree. | ||
* @param leaves The list of the leaves of the tree. | ||
* @param leaf The leaf for which Merkle proof should be created. | ||
* @returns The Merkle proof. | ||
*/ | ||
static generateMerkleProof(depth: number, zeroValue: StrBigInt, leaves: StrBigInt[], leaf: StrBigInt): Promise<MerkleProof>; | ||
export(): Promise<string>; | ||
static import(registry: string): Promise<Registry>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { MerkleProof } from '@zk-kit/incremental-merkle-tree'; | ||
import { Identity } from '@semaphore-protocol/identity'; | ||
import { RLNFullProof, StrBigInt } from './types'; | ||
/** | ||
RLN is a class that represents a single RLN identity. | ||
**/ | ||
export default class RLN { | ||
wasmFilePath: string; | ||
finalZkeyPath: string; | ||
verificationKey: Object; | ||
rlnIdentifier: bigint; | ||
identity: Identity; | ||
commitment: bigint; | ||
secretIdentity: bigint; | ||
constructor(wasmFilePath: string, finalZkeyPath: string, verificationKey: Object, rlnIdentifier?: bigint, identity?: string); | ||
/** | ||
* Generates an RLN Proof. | ||
* @param signal This is usually the raw message. | ||
* @param merkleProof This is the merkle proof for the identity commitment. | ||
* @param epoch This is the time component for the proof, if no epoch is set, unix epoch time rounded to 1 second will be used. | ||
* @returns The full SnarkJS proof. | ||
*/ | ||
generateProof(signal: string, merkleProof: MerkleProof, epoch?: StrBigInt): Promise<RLNFullProof>; | ||
/** | ||
* Generates an RLN Proof. | ||
* @param signal This is usually the raw message. | ||
* @param merkleProof This is the merkle proof for the identity commitment. | ||
* @param epoch This is the time component for the proof, if no epoch is set, unix epoch time rounded to 1 second will be used. | ||
* @returns The full SnarkJS proof. | ||
*/ | ||
static generateProof(signal: string, merkleProof: MerkleProof, epoch: StrBigInt, rlnIdentifier: any, secretIdentity: any, wasmFilePath: string, finalZkeyPath: string, shouldHash?: boolean): Promise<RLNFullProof>; | ||
/** | ||
* Generates a SnarkJS full proof with Groth16. | ||
* @param witness The parameters for creating the proof. | ||
* @returns The full SnarkJS proof. | ||
*/ | ||
_genProof(witness: any): Promise<RLNFullProof>; | ||
/** | ||
* Generates a SnarkJS full proof with Groth16. | ||
* @param witness The parameters for creating the proof. | ||
* @returns The full SnarkJS proof. | ||
*/ | ||
static _genProof(witness: any, wasmFilePath: string, finalZkeyPath: string): Promise<RLNFullProof>; | ||
/** | ||
* Verifies a zero-knowledge SnarkJS proof. | ||
* @param fullProof The SnarkJS full proof. | ||
* @returns True if the proof is valid, false otherwise. | ||
*/ | ||
verifyProof(this: any, { proof, publicSignals }: RLNFullProof): Promise<boolean>; | ||
/** | ||
* Verifies a zero-knowledge SnarkJS proof. | ||
* @param fullProof The SnarkJS full proof. | ||
* @returns True if the proof is valid, false otherwise. | ||
*/ | ||
static verifyProof(verificationKey: Object, { proof, publicSignals }: RLNFullProof): Promise<boolean>; | ||
/** | ||
* Creates witness for rln proof | ||
* @param merkleProof merkle proof that identity exists in RLN tree | ||
* @param epoch epoch on which signal is broadcasted | ||
* @param signal signal that is being broadcasted | ||
* @param shouldHash should the signal be hashed, default is true | ||
* @returns rln witness | ||
*/ | ||
_genWitness(merkleProof: MerkleProof, epoch: StrBigInt, signal: string, shouldHash?: boolean): any; | ||
/** | ||
* Calculates Output | ||
* @param identitySecret identity secret | ||
* @param epoch epoch on which signal is broadcasted | ||
* @param rlnIdentifier unique identifier of rln dapp | ||
* @param signalHash signal hash | ||
* @returns y_share (share) & slashing nullfier | ||
*/ | ||
_calculateOutput(epoch: bigint, signalHash: bigint): Promise<bigint[]>; | ||
/** | ||
* | ||
* @param a1 y = a1 * signalHash + a0 (a1 = poseidon(identity secret, epoch, rlnIdentifier)) | ||
* @param rlnIdentifier unique identifier of rln dapp | ||
* @returns rln slashing nullifier | ||
*/ | ||
static _genNullifier(a1: bigint, rlnIdentifier: bigint): Promise<bigint>; | ||
/** | ||
* Hashes a signal string with Keccak256. | ||
* @param signal The RLN signal. | ||
* @returns The signal hash. | ||
*/ | ||
static _genSignalHash(signal: string): bigint; | ||
/** | ||
* Recovers secret from two shares | ||
* @param x1 signal hash of first message | ||
* @param x2 signal hash of second message | ||
* @param y1 yshare of first message | ||
* @param y2 yshare of second message | ||
* @returns identity secret | ||
*/ | ||
static _shamirRecovery(x1: bigint, x2: bigint, y1: bigint, y2: bigint): bigint; | ||
/** | ||
* Recovers secret from two shares from the same internalNullifier (user) and epoch | ||
* @param proof1 x1 | ||
* @param proof2 x2 | ||
* @returns identity secret | ||
*/ | ||
static retreiveSecret(proof1: RLNFullProof, proof2: RLNFullProof): bigint; | ||
/** | ||
* | ||
* @returns unique identifier of the rln dapp | ||
*/ | ||
static _genIdentifier(): bigint; | ||
static _bigintToUint8Array(input: bigint): Uint8Array; | ||
export(): Promise<Object>; | ||
static import(rln_instance: Object): Promise<RLN>; | ||
} |
Oops, something went wrong.