feat: Implement testing using hardhat-circom utilities (#8)

chore: Utilize calculateLabeledWitness
chore: Update lockfile
This commit is contained in:
Blaine Bublitz
2022-06-20 17:26:42 -07:00
committed by GitHub
parent df584b815a
commit f53e6251b2
6 changed files with 1085 additions and 628 deletions

View File

@@ -1,28 +1,37 @@
require('hardhat-circom');
require("hardhat-circom");
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: "0.6.7",
solidity: {
compilers: [
{
version: "0.6.11",
},
{
version: "0.8.9",
},
],
},
circom: {
inputBasePath: "./circuits",
ptau: "https://hermezptau.blob.core.windows.net/ptau/powersOfTau28_hez_final_15.ptau",
circuits: [
{
name: "division"
name: "division",
// No protocol, so it defaults to groth16
},
{
name: "simple-polynomial",
// Generate PLONK
protocol: 'plonk'
protocol: "plonk",
},
{
name: "hash",
// Explicitly generate groth16
protocol: "groth16"
}
protocol: "groth16",
},
],
},
};

View File

@@ -5,15 +5,18 @@
"license": "MIT",
"dependencies": {},
"devDependencies": {
"chai": "^4.3.6",
"circomlib": "^2.0.3",
"circomlibjs": "^0.1.2",
"hardhat": "^2.9.1",
"hardhat-circom": "^3.1.0"
"hardhat-circom": "^3.3.0"
},
"engines": {
"node": ">=16"
},
"scripts": {
"circom:dev": "hardhat circom --deterministic --debug --verbose",
"circom:prod": "hardhat circom --verbose"
"circom:prod": "hardhat circom --verbose",
"test": "hardhat test"
}
}

43
test/division.test.js Normal file
View File

@@ -0,0 +1,43 @@
const hre = require("hardhat");
const { assert } = require("chai");
describe("division circuit", () => {
let circuit;
const sampleInput = {
x1: "13",
x2: "7",
x3: "4",
x4: "2",
};
const sanityCheck = true;
before(async () => {
circuit = await hre.circuitTest.setup("division");
});
it("produces a witness with valid constraints", async () => {
const witness = await circuit.calculateWitness(sampleInput, sanityCheck);
await circuit.checkConstraints(witness);
});
it("has expected witness values", async () => {
const witness = await circuit.calculateLabeledWitness(
sampleInput,
sanityCheck
);
assert.propertyVal(witness, "main.x1", sampleInput.x1);
assert.propertyVal(witness, "main.x2", sampleInput.x2);
assert.propertyVal(witness, "main.x3", sampleInput.x3);
assert.propertyVal(witness, "main.x4", sampleInput.x4);
assert.propertyVal(witness, "main.y1", undefined);
assert.propertyVal(witness, "main.y2", undefined);
assert.propertyVal(witness, "main.out", "3");
});
it("has the correct output", async () => {
const expected = { out: 3 };
const witness = await circuit.calculateWitness(sampleInput, sanityCheck);
await circuit.assertOut(witness, expected);
});
});

46
test/hash.test.js Normal file
View File

@@ -0,0 +1,46 @@
const hre = require("hardhat");
const { assert } = require("chai");
const { buildMimcSponge } = require("circomlibjs");
describe("hash circuit", () => {
let circuit;
let mimc;
const mimcKey = 0;
const mimcNumOutputs = 1;
const sampleInput = {
x: "1764",
};
const sanityCheck = true;
before(async () => {
mimc = await buildMimcSponge();
circuit = await hre.circuitTest.setup("hash");
});
it("produces a witness with valid constraints", async () => {
const witness = await circuit.calculateWitness(sampleInput, sanityCheck);
await circuit.checkConstraints(witness);
});
it("has expected witness values", async () => {
const witness = await circuit.calculateLabeledWitness(
sampleInput,
sanityCheck
);
assert.propertyVal(witness, "main.x", sampleInput.x);
// You might want to test some intermediate values in the mimc sponge
assert.propertyVal(
witness,
"main.out",
"15893827533473716138720882070731822975159228540693753428689375377280130954696"
);
});
it("has the correct output", async () => {
const mimcResult = mimc.multiHash([sampleInput.x], mimcKey, mimcNumOutputs);
const expected = { out: mimc.F.toObject(mimcResult) };
const witness = await circuit.calculateWitness(sampleInput, sanityCheck);
await circuit.assertOut(witness, expected);
});
});

View File

@@ -0,0 +1,37 @@
const hre = require("hardhat");
const { assert } = require("chai");
describe("simple-polynomial circuit", () => {
let circuit;
const sampleInput = {
x: 5,
};
const sanityCheck = true;
before(async () => {
circuit = await hre.circuitTest.setup("simple-polynomial");
});
it("produces a witness with valid constraints", async () => {
const witness = await circuit.calculateWitness(sampleInput, sanityCheck);
await circuit.checkConstraints(witness);
});
it("has expected witness values", async () => {
const witness = await circuit.calculateLabeledWitness(
sampleInput,
sanityCheck
);
assert.propertyVal(witness, "main.x", "5");
assert.propertyVal(witness, "main.x_squared", "25");
assert.propertyVal(witness, "main.x_cubed", undefined);
assert.propertyVal(witness, "main.out", "127");
});
it("has the correct output", async () => {
const expected = { out: 127 };
const witness = await circuit.calculateWitness(sampleInput, sanityCheck);
await circuit.assertOut(witness, expected);
});
});

1559
yarn.lock

File diff suppressed because it is too large Load Diff