Skip to content

Commit

Permalink
Merge pull request #196 from zkemail/helpers/poseidon-large
Browse files Browse the repository at this point in the history
Add poseidonLarge JS version
  • Loading branch information
saleel committed May 27, 2024
2 parents 3b9ffd9 + a520e3c commit 1b11429
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
8 changes: 3 additions & 5 deletions packages/circuits/tests/email-verifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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,
});
});
});
Expand Down
10 changes: 10 additions & 0 deletions packages/helpers/src/hash.ts
Original file line number Diff line number Diff line change
@@ -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<bigint>;
}
8 changes: 4 additions & 4 deletions scripts/dkim/update-dkim-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1b11429

Please sign in to comment.