Skip to content

Commit

Permalink
bump to 6.1.4 and eslint and published to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
Divide-By-0 committed Aug 20, 2024
1 parent 8685d35 commit 4151b9f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 105 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "6.1.3",
"version": "6.1.4",
"license": "MIT",
"private": true,
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/circuits/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zk-email/contracts",
"version": "6.1.3",
"version": "6.1.4",
"license": "MIT",
"scripts": {
"build": "forge build",
Expand Down
2 changes: 1 addition & 1 deletion packages/helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zk-email/helpers",
"version": "6.1.3",
"version": "6.1.4",
"license": "MIT",
"main": "dist",
"scripts": {
Expand Down
203 changes: 102 additions & 101 deletions packages/helpers/src/input-generators.ts
Original file line number Diff line number Diff line change
@@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

0 comments on commit 4151b9f

Please sign in to comment.