From 4151b9fed2329d3ab10aa583135e11d2b99ce6fc Mon Sep 17 00:00:00 2001 From: Divide-By-0 Date: Tue, 20 Aug 2024 02:12:53 -0700 Subject: [PATCH] bump to 6.1.4 and eslint and published to npm --- package.json | 2 +- packages/circuits/package.json | 2 +- packages/contracts/package.json | 2 +- packages/helpers/package.json | 2 +- packages/helpers/src/input-generators.ts | 203 ++++++++++++----------- 5 files changed, 106 insertions(+), 105 deletions(-) diff --git a/package.json b/package.json index a11a49e3..42027e25 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "6.1.3", + "version": "6.1.4", "license": "MIT", "private": true, "scripts": { diff --git a/packages/circuits/package.json b/packages/circuits/package.json index e4975a91..a152aceb 100644 --- a/packages/circuits/package.json +++ b/packages/circuits/package.json @@ -1,6 +1,6 @@ { "name": "@zk-email/circuits", - "version": "6.1.3", + "version": "6.1.4", "license": "MIT", "scripts": { "publish": "yarn npm publish --access=public", diff --git a/packages/contracts/package.json b/packages/contracts/package.json index 303c5ff4..dfbb1c87 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -1,6 +1,6 @@ { "name": "@zk-email/contracts", - "version": "6.1.3", + "version": "6.1.4", "license": "MIT", "scripts": { "build": "forge build", diff --git a/packages/helpers/package.json b/packages/helpers/package.json index 3c089a58..eb4e2429 100644 --- a/packages/helpers/package.json +++ b/packages/helpers/package.json @@ -1,6 +1,6 @@ { "name": "@zk-email/helpers", - "version": "6.1.3", + "version": "6.1.4", "license": "MIT", "main": "dist", "scripts": { diff --git a/packages/helpers/src/input-generators.ts b/packages/helpers/src/input-generators.ts index 13a7407b..c6e76c41 100644 --- a/packages/helpers/src/input-generators.ts +++ b/packages/helpers/src/input-generators.ts @@ -1,54 +1,54 @@ -import { Uint8ArrayToCharArray, toCircomBigIntBytes } from "./binary-format"; -import { MAX_BODY_PADDED_BYTES, MAX_HEADER_PADDED_BYTES } from "./constants"; -import { DKIMVerificationResult, verifyDKIMSignature } from "./dkim"; -import { generatePartialSHA, sha256Pad } from "./sha-utils"; +import { Uint8ArrayToCharArray, toCircomBigIntBytes } from './binary-format'; +import { MAX_BODY_PADDED_BYTES, MAX_HEADER_PADDED_BYTES } from './constants'; +import { DKIMVerificationResult, verifyDKIMSignature } from './dkim'; +import { generatePartialSHA, sha256Pad } from './sha-utils'; type CircuitInput = { - emailHeader: string[]; - emailHeaderLength: string; - pubkey: string[]; - signature: string[]; - emailBody?: string[]; - emailBodyLength?: string; - precomputedSHA?: string[]; - bodyHashIndex?: string; - decodedEmailBodyIn?: string[]; - mask?: number[]; + emailHeader: string[]; + emailHeaderLength: string; + pubkey: string[]; + signature: string[]; + emailBody?: string[]; + emailBodyLength?: string; + precomputedSHA?: string[]; + bodyHashIndex?: string; + decodedEmailBodyIn?: string[]; + mask?: number[]; }; type InputGenerationArgs = { - ignoreBodyHashCheck?: boolean; - enableBodyMasking?: boolean; - shaPrecomputeSelector?: string; - maxHeadersLength?: number; // Max length of the email header including padding - maxBodyLength?: number; // Max length of the email body after shaPrecomputeSelector including padding - removeSoftLineBreaks?: boolean; - mask?: number[]; + ignoreBodyHashCheck?: boolean; + enableBodyMasking?: boolean; + shaPrecomputeSelector?: string; + maxHeadersLength?: number; // Max length of the email header including padding + maxBodyLength?: number; // Max length of the email body after shaPrecomputeSelector including padding + removeSoftLineBreaks?: boolean; + mask?: number[]; }; function removeSoftLineBreaks(body: string[]): string[] { - const result = []; - let i = 0; - while (i < body.length) { - if ( - i + 2 < body.length && - body[i] === "61" && // '=' character - body[i + 1] === "13" && // '\r' character - body[i + 2] === "10" - ) { - // '\n' character - // Skip the soft line break sequence - i += 3; // Move past the soft line break - } else { - result.push(body[i]); - i++; - } + const result = []; + let i = 0; + while (i < body.length) { + if ( + i + 2 < body.length + && body[i] === '61' // '=' character + && body[i + 1] === '13' // '\r' character + && body[i + 2] === '10' + ) { + // '\n' character + // Skip the soft line break sequence + i += 3; // Move past the soft line break + } else { + result.push(body[i]); + i++; } - // Pad the result with zeros to make it the same length as the body - while (result.length < body.length) { - result.push("0"); - } - return result; + } + // Pad the result with zeros to make it the same length as the body + while (result.length < body.length) { + result.push('0'); + } + return result; } /** @@ -59,12 +59,12 @@ function removeSoftLineBreaks(body: string[]): string[] { * @returns Circuit inputs for the EmailVerifier circuit */ export async function generateEmailVerifierInputs( - rawEmail: Buffer | string, - params: InputGenerationArgs = {} + rawEmail: Buffer | string, + params: InputGenerationArgs = {}, ) { - const dkimResult = await verifyDKIMSignature(rawEmail); + const dkimResult = await verifyDKIMSignature(rawEmail); - return generateEmailVerifierInputsFromDKIMResult(dkimResult, params); + return generateEmailVerifierInputsFromDKIMResult(dkimResult, params); } /** @@ -75,65 +75,66 @@ export async function generateEmailVerifierInputs( * @returns Circuit inputs for the EmailVerifier circuit */ export function generateEmailVerifierInputsFromDKIMResult( - dkimResult: DKIMVerificationResult, - params: InputGenerationArgs = {} + dkimResult: DKIMVerificationResult, + params: InputGenerationArgs = {}, ): CircuitInput { - const { headers, body, bodyHash, publicKey, signature } = dkimResult; + const { + headers, body, bodyHash, publicKey, signature, + } = dkimResult; + + // SHA add padding + const [messagePadded, messagePaddedLen] = sha256Pad( + headers, + params.maxHeadersLength || MAX_HEADER_PADDED_BYTES, + ); + + const circuitInputs: CircuitInput = { + emailHeader: Uint8ArrayToCharArray(messagePadded), // Packed into 1 byte signals + emailHeaderLength: messagePaddedLen.toString(), + pubkey: toCircomBigIntBytes(publicKey), + signature: toCircomBigIntBytes(signature), + }; + + if (!params.ignoreBodyHashCheck) { + if (!body || !bodyHash) { + throw new Error( + 'body and bodyHash are required when ignoreBodyHashCheck is false', + ); + } + + const bodyHashIndex = headers.toString().indexOf(bodyHash); + const maxBodyLength = params.maxBodyLength || MAX_BODY_PADDED_BYTES; - // SHA add padding - const [messagePadded, messagePaddedLen] = sha256Pad( - headers, - params.maxHeadersLength || MAX_HEADER_PADDED_BYTES + // 65 comes from the 64 at the end and the 1 bit in the start, then 63 comes from the formula to round it up to the nearest 64. + // see sha256algorithm.com for a more full explanation of padding length + const bodySHALength = Math.floor((body.length + 63 + 65) / 64) * 64; + const [bodyPadded, bodyPaddedLen] = sha256Pad( + body, + Math.max(maxBodyLength, bodySHALength), ); - const circuitInputs: CircuitInput = { - emailHeader: Uint8ArrayToCharArray(messagePadded), // Packed into 1 byte signals - emailHeaderLength: messagePaddedLen.toString(), - pubkey: toCircomBigIntBytes(publicKey), - signature: toCircomBigIntBytes(signature), - }; - - if (!params.ignoreBodyHashCheck) { - if (!body || !bodyHash) { - throw new Error( - "body and bodyHash are required when ignoreBodyHashCheck is false" - ); - } - - const bodyHashIndex = headers.toString().indexOf(bodyHash); - const maxBodyLength = params.maxBodyLength || MAX_BODY_PADDED_BYTES; - - // 65 comes from the 64 at the end and the 1 bit in the start, then 63 comes from the formula to round it up to the nearest 64. - // see sha256algorithm.com for a more full explanation of padding length - const bodySHALength = Math.floor((body.length + 63 + 65) / 64) * 64; - const [bodyPadded, bodyPaddedLen] = sha256Pad( - body, - Math.max(maxBodyLength, bodySHALength) - ); - - const { precomputedSha, bodyRemaining, bodyRemainingLength } = - generatePartialSHA({ - body: bodyPadded, - bodyLength: bodyPaddedLen, - selectorString: params.shaPrecomputeSelector, - maxRemainingBodyLength: maxBodyLength, - }); - - circuitInputs.emailBodyLength = bodyRemainingLength.toString(); - circuitInputs.precomputedSHA = Uint8ArrayToCharArray(precomputedSha); - circuitInputs.bodyHashIndex = bodyHashIndex.toString(); - circuitInputs.emailBody = Uint8ArrayToCharArray(bodyRemaining); - - if (params.removeSoftLineBreaks) { - circuitInputs.decodedEmailBodyIn = removeSoftLineBreaks( - circuitInputs.emailBody - ); - } - - if (params.enableBodyMasking) { - circuitInputs.mask = params.mask; - } + const { precomputedSha, bodyRemaining, bodyRemainingLength } = generatePartialSHA({ + body: bodyPadded, + bodyLength: bodyPaddedLen, + selectorString: params.shaPrecomputeSelector, + maxRemainingBodyLength: maxBodyLength, + }); + + circuitInputs.emailBodyLength = bodyRemainingLength.toString(); + circuitInputs.precomputedSHA = Uint8ArrayToCharArray(precomputedSha); + circuitInputs.bodyHashIndex = bodyHashIndex.toString(); + circuitInputs.emailBody = Uint8ArrayToCharArray(bodyRemaining); + + if (params.removeSoftLineBreaks) { + circuitInputs.decodedEmailBodyIn = removeSoftLineBreaks( + circuitInputs.emailBody, + ); + } + + if (params.enableBodyMasking) { + circuitInputs.mask = params.mask; } + } - return circuitInputs; + return circuitInputs; }