Merge pull request #22 from appliedzkp/refactor/signal-hash

refactor: create signal hash fun for each protocol
This commit is contained in:
Omar Desogus
2022-03-04 12:23:51 +01:00
committed by GitHub
5 changed files with 34 additions and 19 deletions

View File

@@ -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"

View File

@@ -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

View File

@@ -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.

View File

@@ -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)}`

View File

@@ -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()