Skip to content

Commit

Permalink
add masking
Browse files Browse the repository at this point in the history
  • Loading branch information
jp4g committed Oct 18, 2024
1 parent 32fcfc7 commit acfb3a4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
21 changes: 15 additions & 6 deletions js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ export type CircuitInput = {
body_hash_index?: string;
partial_body_real_length?: string;
partial_body_hash?: string[];
header_mask: string[];
body_mask: string[];
};

export type InputGenerationArgs = {
ignoreBodyHashCheck?: 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
maxHeadersLength?: number;
maxBodyLength?: number;
removeSoftLineBreaks?: boolean;
headerMask?: number[];
bodyMask?: number[];
};

// copied without modification, but not publicly exported in original
Expand Down Expand Up @@ -152,7 +156,7 @@ export function generateEmailVerifierInputsFromDKIMResult(
maxRemainingBodyLength: maxBodyLength,
}
);

// code smell but it passes the linter
let { bodyRemaining } = rest;
// idk why this gets out of sync, todo: fix
Expand All @@ -179,11 +183,16 @@ export function generateEmailVerifierInputsFromDKIMResult(
circuitInputs.body.len = remainingBodyLength.toString();

// format back into u32 so noir doesn't have to do it
circuitInputs.partial_body_hash = Array.from(
u8ToU32(precomputedSha)
).map((x) => x.toString());
circuitInputs.partial_body_hash = Array.from(u8ToU32(precomputedSha)).map(
(x) => x.toString()
);
}

// masking
if (params.headerMask)
circuitInputs.header_mask = params.headerMask.map((x) => x.toString());
if (params.bodyMask)
circuitInputs.body_mask = params.bodyMask.map((x) => x.toString());
}

return circuitInputs;
Expand Down
53 changes: 46 additions & 7 deletions js/tests/circuits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { generateEmailVerifierInputs } from "../src/index";
import { makeEmailAddressCharTable } from "../src/utils";
// import circuit1024 from "../../examples/verify_email_1024_bit_dkim/target/verify_email_1024_bit_dkim.json";
import circuit2048 from "../../examples/verify_email_2048_bit_dkim/target/verify_email_2048_bit_dkim.json";
import partialHash from "../../examples/partial_hash/target/partial_hash.json";
import circuitPartialHash from "../../examples/partial_hash/target/partial_hash.json";
import circuitEmailMask from "../../examples/email_mask/target/email_mask.json";

const emails = {
small: fs.readFileSync(path.join(__dirname, "./test-data/email-good.eml")),
large: fs.readFileSync(
Expand All @@ -25,26 +27,30 @@ describe("ZKEmail.nr Circuit Unit Tests", () => {
const selectorText = "All nodes in the Bitcoin network can consult it";
let prover2048: ZKEmailProver;
let proverPartialHash: ZKEmailProver;
let proverMasked: ZKEmailProver;

beforeAll(() => {
//@ts-ignore
// prover1024 = new ZKEmailProver(circuit1024, "all");
//@ts-ignore
prover2048 = new ZKEmailProver(circuit2048, "all");
//@ts-ignore
proverPartialHash = new ZKEmailProver(partialHash, "all");
proverPartialHash = new ZKEmailProver(circuitPartialHash, "all");
//@ts-ignore
proverMasked = new ZKEmailProver(circuitEmailMask, "all");
});

afterAll(async () => {
// await prover1024.destroy();
await prover2048.destroy();
await proverPartialHash.destroy();
await proverMasked.destroy();
});

describe("Successful Cases", () => {
it("Char table: ", async () => {
console.log(makeEmailAddressCharTable());
})
// it("Char table: ", async () => {
// console.log(makeEmailAddressCharTable());
// })
xit("2048-bit DKIM", async () => {
const inputs = await generateEmailVerifierInputs(
emails.small,
Expand All @@ -59,6 +65,39 @@ describe("ZKEmail.nr Circuit Unit Tests", () => {
maxBodyLength: 192,
});
await proverPartialHash.simulateWitness(inputs);
})
})
});
it("Masked Header/ Body", async () => {
// make masks
const headerMask = Array.from(
{ length: inputParams.maxHeadersLength },
() => Math.floor(Math.random() * 2)
);
const bodyMask = Array.from({ length: inputParams.maxBodyLength }, () =>
Math.floor(Math.random() * 2)
);
const inputs = await generateEmailVerifierInputs(emails.small, {
headerMask,
bodyMask,
...inputParams,
});
// simulate witness
const result = await proverMasked.simulateWitness(inputs);
// compute mask locally
const expectedMaskedHeader = inputs.header.storage.map((byte, i) =>
headerMask[i] === 1 ? parseInt(byte) : 0
);
const expectedMaskedBody = inputs.body!.storage.map((byte, i) =>
bodyMask[i] === 1 ? parseInt(byte) : 0
);
// compare results
const acutalMaskedHeader = result.returnValue[1].map((byte) =>
parseInt(byte, 16)
);
const acutalMaskedBody = result.returnValue[2].map((byte) =>
parseInt(byte, 16)
);
expect(expectedMaskedHeader).toEqual(acutalMaskedHeader);
expect(expectedMaskedBody).toEqual(acutalMaskedBody);
});
});
});

0 comments on commit acfb3a4

Please sign in to comment.