mirror of
https://github.com/privacy-scaling-explorations/zk-kit.git
synced 2026-04-22 03:00:15 -04:00
different functions for identity commitment generation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@libsem/identity",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.7",
|
||||
"description": "Library for managing identites for Semaphore and Rln protocols",
|
||||
"main": "dist/index.node.js",
|
||||
"types": "dist/types/index.d.ts",
|
||||
|
||||
@@ -14,7 +14,7 @@ class ZkIdentity {
|
||||
* @param metadata additional data needed to create identity for given strategy
|
||||
* @returns Identity
|
||||
*/
|
||||
genIdentity(strategy = "random", metadata: any = {}): Identity {
|
||||
genIdentity(strategy: "random" | "signedMessage" = "random", metadata: any = {}): Identity {
|
||||
if (strategy === "random") return genRandomIdentity();
|
||||
else if (strategy === "signedMessage") return genIdentityFromSignedMessage(metadata);
|
||||
|
||||
@@ -48,11 +48,21 @@ class ZkIdentity {
|
||||
* @param secret identity secret
|
||||
* @returns identity commitment
|
||||
*/
|
||||
genIdentityCommitment(secret: bigint[]): bigint {
|
||||
genIdentityCommitmentFromSecret(secret: bigint[]): bigint {
|
||||
const secretHash = poseidonHash(secret);
|
||||
return poseidonHash([secretHash]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate commitment from identity
|
||||
* @param identity identity
|
||||
* @returns identity commitment
|
||||
*/
|
||||
genIdentityCommitment(identity: Identity): bigint {
|
||||
const secretHash = poseidonHash([identity.identityNullifier, identity.identityTrapdoor]);
|
||||
return poseidonHash([secretHash]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes identity
|
||||
* @param identity to serialize
|
||||
|
||||
@@ -23,17 +23,16 @@ describe("Semaphore identity", () => {
|
||||
expect(typeof identitySecret).toEqual("object")
|
||||
})
|
||||
|
||||
it("Should generate identity commitment from identity secret", async () => {
|
||||
it("Should generate identity commitment from identity", async () => {
|
||||
const identity: Identity = ZkIdentity.genIdentity();
|
||||
const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity);
|
||||
expect(typeof identityCommitment).toEqual("bigint")
|
||||
})
|
||||
|
||||
it("Should generate identity commitment from random secret", async () => {
|
||||
const secretParts = 5;
|
||||
const identitySecret: bigint[] = ZkIdentity.genRandomSecret(secretParts);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitmentFromSecret(identitySecret);
|
||||
expect(typeof identityCommitment).toEqual("bigint")
|
||||
})
|
||||
|
||||
@@ -49,4 +48,4 @@ describe("Semaphore identity", () => {
|
||||
expect(unserialized).toStrictEqual(identity)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@libsem/protocols",
|
||||
"version": "1.0.7",
|
||||
"version": "1.0.8",
|
||||
"description": "Client library for generating and verifying Semaphore & Rln ZK proofs",
|
||||
"main": "dist/index.node.js",
|
||||
"types": "dist/types/index.d.ts",
|
||||
|
||||
@@ -12,8 +12,7 @@ beforeAll(() => {
|
||||
|
||||
for (let i=0; i<leafIndex;i++) {
|
||||
const tmpIdentity = ZkIdentity.genIdentity();
|
||||
const tmpIdentitySecret = ZkIdentity.genSecretFromIdentity(tmpIdentity);
|
||||
const tmpCommitment: any = ZkIdentity.genIdentityCommitment(tmpIdentitySecret);
|
||||
const tmpCommitment: any = ZkIdentity.genIdentityCommitment(tmpIdentity);
|
||||
identityCommitments.push(tmpCommitment);
|
||||
}
|
||||
})
|
||||
@@ -23,7 +22,7 @@ describe("Rln", () => {
|
||||
it("Generate rln witness", () => {
|
||||
const identity: Identity = ZkIdentity.genIdentity();
|
||||
const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity);
|
||||
const secretHash: bigint = poseidonHash(identitySecret);
|
||||
|
||||
const commitments: Array<bigint> = Object.assign([], identityCommitments);
|
||||
@@ -46,7 +45,7 @@ describe("Rln", () => {
|
||||
const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity);
|
||||
const secretHash: bigint = poseidonHash(identitySecret);
|
||||
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity);
|
||||
|
||||
const commitments: Array<bigint> = Object.assign([], identityCommitments);
|
||||
commitments.push(identityCommitment);
|
||||
|
||||
@@ -15,8 +15,7 @@ beforeAll(() => {
|
||||
|
||||
for (let i=0; i<leafIndex;i++) {
|
||||
const tmpIdentity = ZkIdentity.genIdentity();
|
||||
const tmpIdentitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(tmpIdentity);
|
||||
const tmpCommitment: bigint = ZkIdentity.genIdentityCommitment(tmpIdentitySecret);
|
||||
const tmpCommitment: bigint = ZkIdentity.genIdentityCommitment(tmpIdentity);
|
||||
identityCommitments.push(tmpCommitment);
|
||||
}
|
||||
})
|
||||
@@ -27,8 +26,7 @@ describe("Semaphore", () => {
|
||||
const identity: Identity = ZkIdentity.genIdentity();
|
||||
const externalNullifier: string = genExternalNullifier("voting_1");
|
||||
const signal = '0x111';
|
||||
const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity);
|
||||
|
||||
const commitments: Array<bigint> = Object.assign([], identityCommitments);
|
||||
commitments.push(identityCommitment);
|
||||
@@ -46,8 +44,7 @@ describe("Semaphore", () => {
|
||||
const externalNullifier: string = genExternalNullifier("voting_1");
|
||||
const signal = '0x111';
|
||||
const nullifierHash: bigint = Semaphore.genNullifierHash(externalNullifier, identity.identityNullifier, 20);
|
||||
const identitySecret: bigint[] = ZkIdentity.genSecretFromIdentity(identity);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identitySecret);
|
||||
const identityCommitment: bigint = ZkIdentity.genIdentityCommitment(identity);
|
||||
|
||||
const commitments: Array<bigint> = Object.assign([], identityCommitments);
|
||||
commitments.push(identityCommitment);
|
||||
|
||||
Reference in New Issue
Block a user