helpers: add poseidonLarge JS version

This commit is contained in:
Saleel
2024-05-17 08:08:09 +04:00
parent 3b9ffd9cc5
commit a520e3c0f4
3 changed files with 17 additions and 9 deletions

View File

@@ -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,
});
});
});

View File

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

View File

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