-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: function signatures of extractSubstrIdxes
- Loading branch information
Showing
8 changed files
with
641 additions
and
642 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,99 @@ | ||
import circom_tester from "circom_tester"; | ||
import * as path from "path"; | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import apis from "../../apis/pkg"; | ||
import compiler from "../../compiler/pkg"; | ||
import circom_tester from 'circom_tester'; | ||
import * as path from 'path'; | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import apis from '../../apis/pkg'; | ||
import compiler from '../../compiler/pkg'; | ||
const option = { | ||
include: path.join(__dirname, "../../../node_modules"), | ||
include: path.join(__dirname, '../../../node_modules') | ||
}; | ||
const wasm_tester = circom_tester.wasm; | ||
|
||
jest.setTimeout(600000); | ||
describe("Bodyhash Regex", () => { | ||
let circuit; | ||
beforeAll(async () => { | ||
const email_addr_json = readFileSync( | ||
path.join(__dirname, "../circuits/common/body_hash.json"), | ||
"utf8" | ||
); | ||
const circom = compiler.genFromDecomposed( | ||
email_addr_json, | ||
"BodyHashRegex" | ||
); | ||
writeFileSync( | ||
path.join(__dirname, "../circuits/common/body_hash_regex.circom"), | ||
circom | ||
); | ||
describe('Bodyhash Regex', () => { | ||
let circuit; | ||
beforeAll(async () => { | ||
const email_addr_json = readFileSync( | ||
path.join(__dirname, '../circuits/common/body_hash.json'), | ||
'utf8' | ||
); | ||
const circom = compiler.genFromDecomposed( | ||
email_addr_json, | ||
'BodyHashRegex' | ||
); | ||
writeFileSync( | ||
path.join(__dirname, '../circuits/common/body_hash_regex.circom'), | ||
circom | ||
); | ||
|
||
circuit = await wasm_tester( | ||
path.join(__dirname, "./circuits/test_body_hash_regex.circom"), | ||
option | ||
); | ||
}); | ||
circuit = await wasm_tester( | ||
path.join(__dirname, './circuits/test_body_hash_regex.circom'), | ||
option | ||
); | ||
}); | ||
|
||
it("bodyhash in the header", async () => { | ||
const signatureField = `dkim-signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694989812; x=1695594612; dara=google.com; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BWETwQ9JDReS4GyR2v2TTR8Bpzj9ayumsWQJ3q7vehs=; b=`; | ||
const paddedStr = apis.padString(signatureField, 1024); | ||
const circuitInputs = { | ||
msg: paddedStr, | ||
}; | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
expect(1n).toEqual(witness[1]); | ||
const prefixIdxes = apis.extractSubstrIdxes( | ||
signatureField, | ||
readFileSync( | ||
path.join(__dirname, "../circuits/common/body_hash.json"), | ||
"utf8" | ||
) | ||
)[0]; | ||
for (let idx = 0; idx < 1024; ++idx) { | ||
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { | ||
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); | ||
} else { | ||
expect(0n).toEqual(witness[2 + idx]); | ||
} | ||
} | ||
}); | ||
it('bodyhash in the header', async () => { | ||
const signatureField = `dkim-signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694989812; x=1695594612; dara=google.com; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BWETwQ9JDReS4GyR2v2TTR8Bpzj9ayumsWQJ3q7vehs=; b=`; | ||
const paddedStr = apis.padString(signatureField, 1024); | ||
const circuitInputs = { | ||
msg: paddedStr | ||
}; | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
expect(1n).toEqual(witness[1]); | ||
const prefixIdxes = apis.extractSubstrIdxes( | ||
signatureField, | ||
readFileSync( | ||
path.join(__dirname, '../circuits/common/body_hash.json'), | ||
'utf8' | ||
), | ||
false | ||
)[0]; | ||
for (let idx = 0; idx < 1024; ++idx) { | ||
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { | ||
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); | ||
} else { | ||
expect(0n).toEqual(witness[2 + idx]); | ||
} | ||
} | ||
}); | ||
|
||
it("bodyhash after new line", async () => { | ||
const signatureField = `\r\ndkim-signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694989812; x=1695594612; dara=google.com; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BWETwQ9JDReS4GyR2v2TTR8Bpzj9ayumsWQJ3q7vehs=; b=`; | ||
const paddedStr = apis.padString(signatureField, 1024); | ||
const circuitInputs = { | ||
msg: paddedStr, | ||
}; | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
expect(1n).toEqual(witness[1]); | ||
const prefixIdxes = apis.extractSubstrIdxes( | ||
signatureField, | ||
readFileSync( | ||
path.join(__dirname, "../circuits/common/body_hash.json"), | ||
"utf8" | ||
) | ||
)[0]; | ||
for (let idx = 0; idx < 1024; ++idx) { | ||
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { | ||
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); | ||
} else { | ||
expect(0n).toEqual(witness[2 + idx]); | ||
} | ||
} | ||
}); | ||
it('bodyhash after new line', async () => { | ||
const signatureField = `\r\ndkim-signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694989812; x=1695594612; dara=google.com; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BWETwQ9JDReS4GyR2v2TTR8Bpzj9ayumsWQJ3q7vehs=; b=`; | ||
const paddedStr = apis.padString(signatureField, 1024); | ||
const circuitInputs = { | ||
msg: paddedStr | ||
}; | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
expect(1n).toEqual(witness[1]); | ||
const prefixIdxes = apis.extractSubstrIdxes( | ||
signatureField, | ||
readFileSync( | ||
path.join(__dirname, '../circuits/common/body_hash.json'), | ||
'utf8' | ||
), | ||
false | ||
)[0]; | ||
for (let idx = 0; idx < 1024; ++idx) { | ||
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { | ||
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); | ||
} else { | ||
expect(0n).toEqual(witness[2 + idx]); | ||
} | ||
} | ||
}); | ||
|
||
it("invalid bodyhash", async () => { | ||
const signatureField = `\r\nto: dkim-signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694989812; x=1695594612; dara=google.com; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BWETwQ9JDReS4GyR2v2TTR8Bpzj9ayumsWQJ3q7vehs=; b=`; | ||
const paddedStr = apis.padString(signatureField, 1024); | ||
const circuitInputs = { | ||
msg: paddedStr, | ||
}; | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
expect(0n).toEqual(witness[1]); | ||
for (let idx = 0; idx < 1024; ++idx) { | ||
expect(0n).toEqual(witness[2 + idx]); | ||
} | ||
}); | ||
it('invalid bodyhash', async () => { | ||
const signatureField = `\r\nto: dkim-signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694989812; x=1695594612; dara=google.com; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BWETwQ9JDReS4GyR2v2TTR8Bpzj9ayumsWQJ3q7vehs=; b=`; | ||
const paddedStr = apis.padString(signatureField, 1024); | ||
const circuitInputs = { | ||
msg: paddedStr | ||
}; | ||
const witness = await circuit.calculateWitness(circuitInputs); | ||
await circuit.checkConstraints(witness); | ||
expect(0n).toEqual(witness[1]); | ||
for (let idx = 0; idx < 1024; ++idx) { | ||
expect(0n).toEqual(witness[2 + idx]); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.