Skip to content

Commit

Permalink
circuit simulation in js working + refactor some
Browse files Browse the repository at this point in the history
  • Loading branch information
jp4g committed Nov 3, 2024
1 parent 9474eae commit 50aa5db
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 146 deletions.
34 changes: 17 additions & 17 deletions examples/remove_soft_line_breaks/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,31 @@ fn main(
assert(header.len() <= MAX_EMAIL_HEADER_LENGTH);
assert(body.len() <= MAX_EMAIL_BODY_LENGTH);

// // ~ 86,553 constraints
// // verify the dkim signature over the header
// pubkey.verify_dkim_signature(header, signature);
// ~ 86,553 constraints
// verify the dkim signature over the header
pubkey.verify_dkim_signature(header, signature);

// // ~ 6,289 constraints
// // extract the body hash from the header
// let signed_body_hash = get_body_hash(header, dkim_header_sequence, body_hash_index);
// ~ 6,289 constraints
// extract the body hash from the header
let signed_body_hash = get_body_hash(header, dkim_header_sequence, body_hash_index);

// // ~ 113,962 constraints
// // hash the asserted body
// let computed_body_hash: [u8; 32] = sha256_var(body.storage(), body.len() as u64);
// ~ 113,962 constraints
// hash the asserted body
let computed_body_hash: [u8; 32] = sha256_var(body.storage(), body.len() as u64);

// // compare the body hashes
// assert(
// signed_body_hash == computed_body_hash, "SHA256 hash computed over body does not match body hash found in DKIM-signed header"
// );
// compare the body hashes
assert(
signed_body_hash == computed_body_hash, "SHA256 hash computed over body does not match body hash found in DKIM-signed header"
);

// ~ 37,982 constraints
// ensure the decoded body is the same as the original body
assert(
remove_soft_line_breaks(body.storage(), decoded_body.storage()),
"Decoded body does not properly remove soft line breaks"
);

// // ~ 10,255 constraints
// // hash the pubkey and signature for the standard outputs
// standard_outputs(pubkey.modulus, signature)
[0, 0]
// ~ 10,255 constraints
// hash the pubkey and signature for the standard outputs
standard_outputs(pubkey.modulus, signature)
}
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
"@types/jest": "^29.5.13",
"@types/jest": "^29.5.14",
"@types/mocha": "^10.0.8",
"@types/node": "^22.5.5",
"@typescript-eslint/eslint-plugin": "7",
Expand Down
31 changes: 19 additions & 12 deletions js/tests/circuits.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from "fs";
import path from "path";
import { describe, beforeAll, } from "jest";
import { ZKEmailProver } from "../src/prover";
import { generateEmailVerifierInputs } from "../src/index";
import { toProverToml } from "../src/utils";
Expand All @@ -9,7 +8,7 @@ import circuit2048 from "../../examples/verify_email_2048_bit_dkim/target/verify
import circuitPartialHash from "../../examples/partial_hash/target/partial_hash.json";
import circuitEmailMask from "../../examples/email_mask/target/email_mask.json";
import circuitExtractAddresses from "../../examples/extract_addresses/target/extract_addresses.json";
import circuitRemoveSoftLineBreak from "../examples/remove_soft_line_breaks/target/remove_soft_line_breaks.json";
import circuitRemoveSoftLineBreak from "../../examples/remove_soft_line_breaks/target/remove_soft_line_breaks.json";

const emails = {
small: fs.readFileSync(path.join(__dirname, "./test-data/email-good.eml")),
Expand All @@ -25,25 +24,25 @@ const inputParams = {
};

describe("ZKEmail.nr Circuit Unit Tests", () => {
// todo: get a github email from a throwaway account to verify
// let prover1024: ZKEmailProver;
const selectorText = "All nodes in the Bitcoin network can consult it";
let prover2048: ZKEmailProver;
let proverPartialHash: ZKEmailProver;
let proverMasked: ZKEmailProver;
let proverExtractAddresses: ZKEmailProver;
let proverRemoveSoftLineBreak: ZKEmailProver;

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

afterAll(async () => {
Expand All @@ -52,20 +51,21 @@ describe("ZKEmail.nr Circuit Unit Tests", () => {
await proverPartialHash.destroy();
await proverMasked.destroy();
await proverExtractAddresses.destroy();
await proverRemoveSoftLineBreak.destroy();
});

describe("Successful Cases", () => {
describe("Simulate Witnesses", () => {
it("2048-bit DKIM", async () => {
const inputs = await generateEmailVerifierInputs(
emails.small,
inputParams
);
await prover2048.simulateWitness(inputs);
console.log(toProverToml(inputs));
// console.log(toProverToml(inputs));
});
it("Partial Hash", async () => {
const inputs = await generateEmailVerifierInputs(emails.large, {
shaPrecomputeSelector: selectorText,
shaPrecomputeSelector: "All nodes in the Bitcoin network can consult it",
maxHeadersLength: 512,
maxBodyLength: 192,
});
Expand Down Expand Up @@ -141,5 +141,12 @@ describe("ZKEmail.nr Circuit Unit Tests", () => {
expect(expectedFrom).toEqual(actualFrom);
expect(expectedTo).toEqual(actualTo);
});
it("Remove Soft Line Breaks", async () => {
const inputs = await generateEmailVerifierInputs(emails.large, {
removeSoftLineBreaks: true,
...inputParams,
});
await proverRemoveSoftLineBreak.simulateWitness(inputs);
})
});
});
8 changes: 4 additions & 4 deletions js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1782,10 +1782,10 @@
dependencies:
"@types/istanbul-lib-report" "*"

"@types/jest@^29.5.13":
version "29.5.13"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.13.tgz#8bc571659f401e6a719a7bf0dbcb8b78c71a8adc"
integrity sha512-wd+MVEZCHt23V0/L642O5APvspWply/rGY5BcW4SUETo2UzPU3Z26qr8jC2qxpimI2jjx9h7+2cj2FwIr01bXg==
"@types/jest@^29.5.14":
version "29.5.14"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.14.tgz#2b910912fa1d6856cadcd0c1f95af7df1d6049e5"
integrity sha512-ZN+4sdnLUbo8EVvVc2ao0GFW6oVrQRPn4K2lglySj7APvSrgzxHiNNK99us4WDMi57xxA2yggblIAMNhXOotLQ==
dependencies:
expect "^29.0.0"
pretty-format "^29.0.0"
Expand Down
Loading

0 comments on commit 50aa5db

Please sign in to comment.