forked from zkemail/zkemail.nr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuits.test.ts
151 lines (145 loc) · 5.59 KB
/
circuits.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import fs from "fs";
import path from "path";
import { ZKEmailProver } from "../src/prover";
import { generateEmailVerifierInputs } from "../src/index";
// 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 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";
const emails = {
small: fs.readFileSync(path.join(__dirname, "./test-data/email-good.eml")),
large: fs.readFileSync(
path.join(__dirname, "./test-data/email-good-large.eml")
),
};
// default header/ body lengths to use for input gen
const inputParams = {
maxHeadersLength: 512,
maxBodyLength: 1024,
};
describe("ZKEmail.nr Circuit Unit Tests", () => {
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);
//@ts-ignore
proverPartialHash = new ZKEmailProver(circuitPartialHash);
//@ts-ignore
proverMasked = new ZKEmailProver(circuitEmailMask);
//@ts-ignore
proverExtractAddresses = new ZKEmailProver(circuitExtractAddresses);
//@ts-ignore
proverRemoveSoftLineBreak = new ZKEmailProver(circuitRemoveSoftLineBreak);
});
afterAll(async () => {
// await prover1024.destroy();
await prover2048.destroy();
await proverPartialHash.destroy();
await proverMasked.destroy();
await proverExtractAddresses.destroy();
await proverRemoveSoftLineBreak.destroy();
});
describe("Simulate Witnesses", () => {
it("2048-bit DKIM", async () => {
const inputs = await generateEmailVerifierInputs(
emails.small,
inputParams
);
await prover2048.simulateWitness(inputs);
// console.log(toProverToml(inputs));
});
it("Partial Hash", async () => {
const inputs = await generateEmailVerifierInputs(emails.large, {
shaPrecomputeSelector: "All nodes in the Bitcoin network can consult it",
maxHeadersLength: 512,
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);
});
it("Extract Sender/ Recipient", async () => {
const inputs = await generateEmailVerifierInputs(emails.small, {
extractFrom: true,
extractTo: true,
...inputParams,
});
// simulate witness
const result = await proverExtractAddresses.simulateWitness(inputs);
// parse expected addresses
const header = Buffer.from(
inputs.header.storage.map((byte) => parseInt(byte))
).toString();
const fromAddressStart = parseInt(inputs.from_address_sequence!.index);
const fromAddressEnd =
fromAddressStart + parseInt(inputs.from_address_sequence!.length);
const expectedFrom = header.slice(fromAddressStart, fromAddressEnd);
const toAddressStart = parseInt(inputs.to_address_sequence!.index);
const toAddressEnd =
toAddressStart + parseInt(inputs.to_address_sequence!.length);
const expectedTo = header.slice(toAddressStart, toAddressEnd);
// parse actual addresses
const parseSequence = (len: string, storage: string[]) => {
return Buffer.from(
storage.slice(0, parseInt(len)).map((byte) => parseInt(byte))
).toString();
};
const actualFrom = parseSequence(
result.returnValue[1].len,
result.returnValue[1].storage
);
const actualTo = parseSequence(
result.returnValue[2].len,
result.returnValue[2].storage
);
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);
})
});
});