Skip to content

Commit

Permalink
test: added test for sha256
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Sep 25, 2023
1 parent 209a111 commit 23350b0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/circuits/tests/sha256-test.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.1.5;

include "../helpers/sha.circom";

component main { public [in_padded, in_len_padded_bytes] } = Sha256Bytes(640);
57 changes: 57 additions & 0 deletions packages/circuits/tests/sha256.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import fs from "fs";
import { buildMimcSponge } from "circomlibjs";
import { wasm as wasm_tester } from "circom_tester";
import { Scalar } from "ffjavascript";
import path from "path";
import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim";
import { generateCircuitInputs } from "@zk-email/helpers/src/input-helpers";
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim";
import { sha256Pad, shaHash } from "@zk-email/helpers/src/shaHash";
import { MAX_HEADER_PADDED_BYTES } from "@twitter-verifier/circuits/helpers";
import { Uint8ArrayToCharArray, uint8ToBits } from "@zk-email/helpers/src/binaryFormat";

exports.p = Scalar.fromString(
"21888242871839275222246405745257275088548364400416034343698204186575808495617"
);

describe("SHA256 for email header", () => {
jest.setTimeout(10 * 60 * 1000); // 10 minutes

let circuit: any;

beforeAll(async () => {
circuit = await wasm_tester(
path.join(__dirname, "./sha256-test.circom"),
{
// @dev During development recompile can be set to false if you are only making changes in the tests.
// This will save time by not recompiling the circuit every time.
// Compile: circom "./tests/email-verifier-test.circom" --r1cs --wasm --sym --c --wat --output "./tests/compiled-test-circuit"
recompile: true,
output: path.join(__dirname, "./compiled-test-circuit"),
include: path.join(__dirname, "../../../node_modules"),
}
);
});

it("should hash correctly", async function () {
const inputs = [
"0", "hello world", ""
]
for (const input of inputs) {
const [
paddedMsg,
messageLen,
] = sha256Pad(
Buffer.from(input, "ascii"), 640
)

const witness = await circuit.calculateWitness({
in_len_padded_bytes: messageLen,
in_padded: Uint8ArrayToCharArray(paddedMsg)
});

await circuit.checkConstraints(witness);
await circuit.assertOut(witness, { out: [...uint8ToBits(shaHash(Buffer.from(input, "ascii")))] })
}
});
});

0 comments on commit 23350b0

Please sign in to comment.