From a520e3c0f44f8496536c76f4b13dcaa83a9880c2 Mon Sep 17 00:00:00 2001 From: Saleel Date: Fri, 17 May 2024 08:08:09 +0400 Subject: [PATCH] helpers: add poseidonLarge JS version --- packages/circuits/tests/email-verifier.test.ts | 8 +++----- packages/helpers/src/hash.ts | 10 ++++++++++ scripts/dkim/update-dkim-registry.ts | 8 ++++---- 3 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 packages/helpers/src/hash.ts diff --git a/packages/circuits/tests/email-verifier.test.ts b/packages/circuits/tests/email-verifier.test.ts index 6aad3ccb7..b87a02235 100644 --- a/packages/circuits/tests/email-verifier.test.ts +++ b/packages/circuits/tests/email-verifier.test.ts @@ -5,7 +5,7 @@ import path from "path"; import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src/input-generators"; import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim"; -import { bigIntToChunkedBytes } from "@zk-email/helpers/src/binary-format"; +import { poseidonLarge } from "@zk-email/helpers/src/hash"; describe("EmailVerifier", () => { @@ -169,15 +169,13 @@ describe("EmailVerifier", () => { }); // Calculate the Poseidon hash with pubkey chunked to 9*242 like in circuit - const poseidon = await buildPoseidon(); - const pubkeyChunked = bigIntToChunkedBytes(dkimResult.publicKey, 242, 9); - const hash = poseidon(pubkeyChunked); + const poseidonHash = await poseidonLarge(dkimResult.publicKey, 9, 242); // Calculate the hash using the circuit const witness = await circuit.calculateWitness(emailVerifierInputs); await circuit.assertOut(witness, { - pubkeyHash: poseidon.F.toObject(hash), + pubkeyHash: poseidonHash, }); }); }); diff --git a/packages/helpers/src/hash.ts b/packages/helpers/src/hash.ts new file mode 100644 index 000000000..c5b662f6a --- /dev/null +++ b/packages/helpers/src/hash.ts @@ -0,0 +1,10 @@ +import { buildPoseidon } from 'circomlibjs'; +import { bigIntToChunkedBytes } from './binary-format'; + +export async function poseidonLarge(input: bigint, numChunks: number, bitsPerChunk: number) { + const poseidon = await buildPoseidon(); + const pubkeyChunked = bigIntToChunkedBytes(input, bitsPerChunk, numChunks); + const hash = poseidon(pubkeyChunked); + + return poseidon.F.toObject(hash) as Promise; +} diff --git a/scripts/dkim/update-dkim-registry.ts b/scripts/dkim/update-dkim-registry.ts index 9bc649358..9bd1ef5b9 100644 --- a/scripts/dkim/update-dkim-registry.ts +++ b/scripts/dkim/update-dkim-registry.ts @@ -6,6 +6,7 @@ import forge from "node-forge"; import { bigIntToChunkedBytes } from "@zk-email/helpers/src/binaryFormat"; const fs = require("fs"); import { abi } from "../abis/DKIMRegistry.json"; +import { poseidonLarge } from "@zk-email/helpers/src/hash"; require("dotenv").config(); async function updateContract(domain: string, pubkeyHashes: string[]) { @@ -252,16 +253,15 @@ async function updateDKIMRegistry({ // Generate pub key hash using 242 * 9 chunks (Poseidon lib don't take more than 16 inputs) const domainHashedPubKeyMap: { [key: string]: string[] } = {}; - const poseidon = await buildPoseidon(); + for (let domain of Object.keys(domainPubKeyMap)) { for (let { publicKey } of domainPubKeyMap[domain]) { - const pubkeyChunked = bigIntToChunkedBytes(BigInt(publicKey), 242, 9); - const hash = poseidon(pubkeyChunked); + const poseidonHash = await poseidonLarge(BigInt(publicKey), 9, 242); if (!domainHashedPubKeyMap[domain]) { domainHashedPubKeyMap[domain] = []; } - domainHashedPubKeyMap[domain].push(poseidon.F.toObject(hash).toString()); + domainHashedPubKeyMap[domain].push(poseidonHash.toString()); } } _writeToFile("dkim-keys-hashed.json", domainHashedPubKeyMap);