mirror of
https://github.com/privacy-scaling-explorations/zk-kit.git
synced 2026-04-22 03:00:15 -04:00
refactor: create signal hash fun for each protocol
Former-commit-id: bef48c536a82eef209822fec6f94340b3580a9b0 [formerly ec8a7bbd4065bbd362d9130e8451430b1c20bbd3] [formerly e5d28c0afe1f613825d1cad0a5fe95d7a47b025e [formerly 841825b85e7dddc6f14e0cd59339f4a75ac9a4f2]] [formerly 191f0d3be51223b88e8da247fdf1fb04614eec09 [formerly 0bf61e4b416a74f3ed1c8153c02b20ebb78dd669] [formerly 07f45250eed45dd26a8c70e866640fa77078f6a6 [formerly 3c5c0f4941]]]
Former-commit-id: d954008119ef5891901c45a8f47faa86cfbebc37 [formerly d58f804086b7ad0d93dd10195d20628c177a66d7] [formerly 163440e2014cc70b2ce5408ad152b8fe1dc5e17a [formerly 34d17255f9306071bf9a12dd4dc128279922b97b]]
Former-commit-id: 3e1458b5866d13eb624b2b2db22e7f638945c60d [formerly 5ec59a56b3597f8f9baa2ae72b2df1b6fa832043]
Former-commit-id: dd88371a293a9553644a70c975f3c55084fe20bf
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { MerkleProof } from "@zk-kit/incremental-merkle-tree"
|
||||
import RLN from "./rln"
|
||||
import Semaphore from "./semaphore"
|
||||
import { generateMerkleProof, generateMerkleTree, genExternalNullifier, genSignalHash } from "./utils"
|
||||
import { generateMerkleProof, generateMerkleTree, genExternalNullifier } from "./utils"
|
||||
|
||||
export { Semaphore, RLN, generateMerkleProof, generateMerkleTree, genExternalNullifier, genSignalHash, MerkleProof }
|
||||
export { Semaphore, RLN, generateMerkleProof, generateMerkleTree, genExternalNullifier, MerkleProof }
|
||||
export * from "./types"
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { hexlify } from "@ethersproject/bytes"
|
||||
import { keccak256 } from "@ethersproject/solidity"
|
||||
import { toUtf8Bytes } from "@ethersproject/strings"
|
||||
import { MerkleProof } from "@zk-kit/incremental-merkle-tree"
|
||||
import { poseidon } from "circomlibjs"
|
||||
import { groth16 } from "snarkjs"
|
||||
import { RLNFullProof, StrBigInt } from "./types"
|
||||
import { Fq, genSignalHash } from "./utils"
|
||||
import { Fq } from "./utils"
|
||||
|
||||
export default class RLN {
|
||||
/**
|
||||
@@ -73,7 +76,7 @@ export default class RLN {
|
||||
identity_secret: identitySecret,
|
||||
path_elements: merkleProof.siblings,
|
||||
identity_path_index: merkleProof.pathIndices,
|
||||
x: shouldHash ? genSignalHash(signal) : signal,
|
||||
x: shouldHash ? RLN.genSignalHash(signal) : signal,
|
||||
epoch,
|
||||
rln_identifier: rlnIdentifier
|
||||
}
|
||||
@@ -105,6 +108,17 @@ export default class RLN {
|
||||
return poseidon([a1, rlnIdentifier])
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes a signal string with Keccak256.
|
||||
* @param signal The RLN signal.
|
||||
* @returns The signal hash.
|
||||
*/
|
||||
public static genSignalHash(signal: string): bigint {
|
||||
const converted = hexlify(toUtf8Bytes(signal))
|
||||
|
||||
return BigInt(keccak256(["bytes"], [converted])) >> BigInt(8)
|
||||
}
|
||||
|
||||
/**
|
||||
* When spam occurs, identity secret can be retrieved
|
||||
* @param x1 x1
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { keccak256 } from "@ethersproject/solidity"
|
||||
import { formatBytes32String } from "@ethersproject/strings"
|
||||
import { MerkleProof } from "@zk-kit/incremental-merkle-tree"
|
||||
import { poseidon } from "circomlibjs"
|
||||
import { groth16 } from "snarkjs"
|
||||
import { Proof, SemaphoreFullProof, SemaphoreSolidityProof, SemaphoreWitness, StrBigInt } from "./types"
|
||||
import { genSignalHash } from "./utils"
|
||||
|
||||
export default class Semaphore {
|
||||
/**
|
||||
@@ -62,8 +63,7 @@ export default class Semaphore {
|
||||
identityNullifier: StrBigInt,
|
||||
merkleProof: MerkleProof,
|
||||
externalNullifier: StrBigInt,
|
||||
signal: string,
|
||||
shouldHash = true
|
||||
signal: string
|
||||
): SemaphoreWitness {
|
||||
return {
|
||||
identityNullifier,
|
||||
@@ -71,7 +71,7 @@ export default class Semaphore {
|
||||
treePathIndices: merkleProof.pathIndices,
|
||||
treeSiblings: merkleProof.siblings,
|
||||
externalNullifier,
|
||||
signalHash: shouldHash ? genSignalHash(signal) : signal
|
||||
signalHash: Semaphore.genSignalHash(signal)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +85,15 @@ export default class Semaphore {
|
||||
return poseidon([BigInt(externalNullifier), BigInt(identityNullifier)])
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes a signal string with Keccak256.
|
||||
* @param signal The Semaphore signal.
|
||||
* @returns The signal hash.
|
||||
*/
|
||||
public static genSignalHash(signal: string): bigint {
|
||||
return BigInt(keccak256(["bytes32"], [formatBytes32String(signal)])) >> BigInt(8)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a full proof in a proof compatible with the Verifier.sol method inputs.
|
||||
* @param fullProof The proof generated with SnarkJS.
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { hexlify } from "@ethersproject/bytes"
|
||||
import { keccak256 } from "@ethersproject/solidity"
|
||||
import { toUtf8Bytes } from "@ethersproject/strings"
|
||||
import { IncrementalMerkleTree, MerkleProof } from "@zk-kit/incremental-merkle-tree"
|
||||
import { poseidon } from "circomlibjs"
|
||||
import { ZqField } from "ffjavascript"
|
||||
@@ -10,12 +8,6 @@ export const SNARK_FIELD_SIZE = BigInt("2188824287183927522224640574525727508854
|
||||
|
||||
export const Fq = new ZqField(SNARK_FIELD_SIZE)
|
||||
|
||||
export function genSignalHash(signal: string): bigint {
|
||||
const converted = hexlify(toUtf8Bytes(signal))
|
||||
|
||||
return BigInt(keccak256(["bytes"], [converted])) >> BigInt(8)
|
||||
}
|
||||
|
||||
export function genExternalNullifier(plaintext: string): string {
|
||||
const hashed = keccak256(["string"], [plaintext])
|
||||
const hexStr = `0x${hashed.slice(8)}`
|
||||
|
||||
@@ -3,7 +3,7 @@ import { getCurveFromName } from "ffjavascript"
|
||||
import * as fs from "fs"
|
||||
import * as path from "path"
|
||||
import { RLN } from "../src"
|
||||
import { generateMerkleProof, genExternalNullifier, genSignalHash } from "../src/utils"
|
||||
import { generateMerkleProof, genExternalNullifier } from "../src/utils"
|
||||
|
||||
describe("RLN", () => {
|
||||
const zkeyFiles = "./packages/protocols/zkeyFiles"
|
||||
@@ -62,9 +62,9 @@ describe("RLN", () => {
|
||||
const secretHash = identity.getSecretHash()
|
||||
|
||||
const signal1 = "hey hey"
|
||||
const signalHash1 = genSignalHash(signal1)
|
||||
const signalHash1 = RLN.genSignalHash(signal1)
|
||||
const signal2 = "hey hey again"
|
||||
const signalHash2 = genSignalHash(signal2)
|
||||
const signalHash2 = RLN.genSignalHash(signal2)
|
||||
|
||||
const epoch = genExternalNullifier("test-epoch")
|
||||
const rlnIdentifier = RLN.genIdentifier()
|
||||
|
||||
Reference in New Issue
Block a user