mirror of
https://github.com/zkemail/zk-email-verify.git
synced 2026-01-08 21:18:09 -05:00
helpers: add poseidonLarge JS version
This commit is contained in:
@@ -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,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
10
packages/helpers/src/hash.ts
Normal file
10
packages/helpers/src/hash.ts
Normal 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>;
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user