merge: branch 'main' of github.com:appliedzkp/zk-kit into main

Former-commit-id: 97fabee9735be6a8b9efbbfbea2184a8fff904a2 [formerly 1558ec3f527a3d57463ffc6fae6d2becd6f75556] [formerly a4ad917f472d588bc7e9693e179e1c1ae7b26f02 [formerly bdce251234]]
Former-commit-id: 755a137b400e24a84dae885032e68387eb16cc71 [formerly 3cc8fdc3d1f71c0a20613a04eda8bd3d463b57ea]
Former-commit-id: 482107411d7dd71878cec95db75e78a72ac22a06
This commit is contained in:
cedoor
2022-01-20 18:48:40 +01:00
3 changed files with 22 additions and 25 deletions

View File

@@ -15,6 +15,7 @@ export default class ZkIdentity {
private _secret: bigint[] = []
private _multipartSecret: bigint[] = []
private _defaultMultipartSecret: bigint[] = []
/**
* Generates new ZkIdentity.
@@ -30,7 +31,7 @@ export default class ZkIdentity {
this._identityTrapdoor = identityTrapdoor
this._identityNullifier = identityNullifier
this._secret = [this._identityNullifier, this._identityTrapdoor]
this.genMultipartSecret()
this._genMultipartSecret()
break
}
@@ -40,7 +41,7 @@ export default class ZkIdentity {
this._identityTrapdoor = identityTrapdoor
this._identityNullifier = identityNullifier
this._secret = [this._identityNullifier, this._identityTrapdoor]
this.genMultipartSecret()
this._genMultipartSecret()
break
}
@@ -59,6 +60,7 @@ export default class ZkIdentity {
this._identityTrapdoor = hexToBigint(identityTrapdoor)
this._secret = secret.map((item) => hexToBigint(item))
this._multipartSecret = multipartSecret.map((item) => hexToBigint(item))
this._defaultMultipartSecret = this._multipartSecret.slice(0, 2);
break
}
@@ -69,19 +71,18 @@ export default class ZkIdentity {
/**
* Generate multipart secret. To be used by RLN related apps.
* @param parts The number of parts that the secret should be composed of,
* corresponding to the spam threshold of the protocol
*/
public genMultipartSecret(parts = 2): void {
if (parts < 2) throw new Error("Invalid number of parts")
private _genMultipartSecret(): void {
const initialComponent = Fq.pow(this._identityTrapdoor, this._identityNullifier)
this._multipartSecret = [initialComponent]
for (let i = 1; i < parts; i += 1) {
for (let i = 1; i < 16; i+=1) {
this._multipartSecret.push(Fq.pow(initialComponent, BigInt(i + 1)))
}
this._defaultMultipartSecret = this._multipartSecret.slice(0, 2);
}
/**
@@ -103,16 +104,17 @@ export default class ZkIdentity {
return this._secret
}
public getMultipartSecret(): bigint[] {
return this._multipartSecret
public getMultipartSecret(secretParts: number = 2): bigint[] {
return secretParts === 2 ? this._defaultMultipartSecret : this._multipartSecret.slice(0, secretParts);
}
public getSecretHash(): bigint {
return poseidon(this._secret)
}
public getMultipartSecretHash(): bigint {
return poseidon(this._multipartSecret)
public getMultipartSecretHash(secretParts: number = 2): bigint {
const multipartSecret = this.getMultipartSecret(secretParts);
return poseidon(multipartSecret)
}
/**
@@ -120,12 +122,12 @@ export default class ZkIdentity {
* @param secretType The secret type for which to generate identity commitment
* @returns identity commitment
*/
public genIdentityCommitment(secretType: SecretType = SecretType.GENERIC): bigint {
public genIdentityCommitment(secretType: SecretType = SecretType.GENERIC, secretParts: number = 2): bigint {
switch (secretType) {
case SecretType.GENERIC:
return poseidon([this.getSecretHash()])
case SecretType.MULTIPART_SECRET:
return poseidon([this.getMultipartSecretHash()])
return poseidon([this.getMultipartSecretHash(secretParts)])
default:
throw new Error("Provided secret type is not supported")
}

View File

@@ -13,8 +13,7 @@ beforeAll(() => {
for (let i = 0; i < leafIndex; i += 1) {
const tmpIdentity = new ZkIdentity()
tmpIdentity.genMultipartSecret(SPAM_TRESHOLD)
const tmpCommitment: bigint = tmpIdentity.genIdentityCommitment(SecretType.MULTIPART_SECRET)
const tmpCommitment: bigint = tmpIdentity.genIdentityCommitment(SecretType.MULTIPART_SECRET, SPAM_TRESHOLD)
identityCommitments.push(tmpCommitment)
}
})
@@ -23,10 +22,9 @@ describe("NRLN", () => {
describe("NRLN features", () => {
it("Generate NRLN witness", () => {
const identity: ZkIdentity = new ZkIdentity()
identity.genMultipartSecret(SPAM_TRESHOLD)
const identityCommitment: bigint = identity.genIdentityCommitment(SecretType.MULTIPART_SECRET)
const identitySecret: bigint[] = identity.getMultipartSecret()
const identityCommitment: bigint = identity.genIdentityCommitment(SecretType.MULTIPART_SECRET, SPAM_TRESHOLD)
const identitySecret: bigint[] = identity.getMultipartSecret(SPAM_TRESHOLD)
const commitments: Array<bigint> = Object.assign([], identityCommitments)
commitments.push(identityCommitment)
@@ -45,11 +43,9 @@ describe("NRLN", () => {
* Compiled RLN circuits are needed to run this test so it's being skipped in hooks
*/
const identity: ZkIdentity = new ZkIdentity()
identity.genMultipartSecret(SPAM_TRESHOLD)
const identityCommitment: bigint = identity.genIdentityCommitment(SecretType.MULTIPART_SECRET)
const identitySecret: bigint[] = identity.getMultipartSecret()
const identityCommitment: bigint = identity.genIdentityCommitment(SecretType.MULTIPART_SECRET, SPAM_TRESHOLD)
const identitySecret: bigint[] = identity.getMultipartSecret(SPAM_TRESHOLD)
const commitments: Array<bigint> = Object.assign([], identityCommitments)
commitments.push(identityCommitment)
@@ -83,9 +79,8 @@ describe("NRLN", () => {
}, 30000)
it("Should retrieve user secret after spaming", () => {
const identity: ZkIdentity = new ZkIdentity()
identity.genMultipartSecret(SPAM_TRESHOLD)
const identitySecret: bigint[] = identity.getMultipartSecret()
const identitySecret: bigint[] = identity.getMultipartSecret(SPAM_TRESHOLD)
const signal1 = "hey 1"
const signalHash1 = genSignalHash(signal1)

View File

@@ -1 +1 @@
84e86da6e86f333fb01d6cd1576b7b3657dca3e6
a70254b53a6a13e3a41991a4b375cbd3edf42d89