Skip to content

Commit

Permalink
refactor(circuits): fix ts types, improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Dec 11, 2023
1 parent 1e3387f commit ae17891
Show file tree
Hide file tree
Showing 36 changed files with 387 additions and 314 deletions.
8 changes: 7 additions & 1 deletion circuits/circom/processMessages.circom
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ template ProcessMessages(
// messages in a batch
// voteOptionTreeDepth: depth of the vote option tree

// we want to ensure that the params are > 0
// we want to ensure that the trees have a valid structure
assert(stateTreeDepth > 0);
assert(msgBatchDepth > 0);
assert(voteOptionTreeDepth > 0);
Expand Down Expand Up @@ -682,6 +682,12 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
newBallotRoot <== newBallotQip.root;
}

// A template which accepts a number of inputs
// and produces a sha256 hash.
// Please note that certain inputs
// are packed into a single element, and this
// template will unpack them and ensure their
// validity.
template ProcessMessagesInputHasher() {
// Combine the following into 1 input element:
// - maxVoteOptions (50 bits)
Expand Down
2 changes: 1 addition & 1 deletion circuits/scripts/build_arm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ do
circom --O0 --wasm --r1cs --sym --output "$OUTPUT_PATH" "$circuit"

cd "$CWD"
done
done
10 changes: 5 additions & 5 deletions circuits/ts/__tests__/CalculateTotal.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { join } from "path";
const tester = require("circom_tester").wasm;
import path from "path";
import tester from "circom_tester";
import { expect } from "chai";
import { getSignal } from "./utils/utils";

describe("CalculateTotal circuit", () => {
const circuitPath = join(__dirname, "../../circom/test", `calculateTotal_test.circom`);
let circuit: any;
const circuitPath = path.resolve(__dirname, "../../circom/test", `calculateTotal_test.circom`);
let circuit: tester.WasmTester;

before(async () => {
circuit = await tester(circuitPath);
circuit = await tester.wasm(circuitPath);
});

it("should correctly sum a list of values", async () => {
Expand Down
10 changes: 5 additions & 5 deletions circuits/ts/__tests__/Ecdh.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { stringifyBigInts } from "maci-crypto";
import { Keypair } from "maci-domainobjs";
import { join } from "path";
const tester = require("circom_tester").wasm;
import path from "path";
import tester from "circom_tester";
import { expect } from "chai";
import { getSignal } from "./utils/utils";

describe("Public key derivation circuit", () => {
const circuitPath = join(__dirname, "../../circom/test", `ecdh_test.circom`);
let circuit: any;
const circuitPath = path.resolve(__dirname, "../../circom/test", `ecdh_test.circom`);
let circuit: tester.WasmTester;

before(async () => {
circuit = await tester(circuitPath);
circuit = await tester.wasm(circuitPath);
});

it("correctly computes a public key", async () => {
Expand Down
42 changes: 21 additions & 21 deletions circuits/ts/__tests__/Hasher.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { PCommand, Keypair } from "maci-domainobjs";
const tester = require("circom_tester").wasm;
import tester from "circom_tester";
import { stringifyBigInts, genRandomSalt, sha256Hash, hashLeftRight, hash13, hash5, hash4, hash3 } from "maci-crypto";
import { join } from "path";
import path from "path";
import { expect } from "chai";
import { getSignal } from "./utils/utils";

describe("Poseidon hash circuits", function () {
this.timeout(30000);

let circuit: any;
let circuit: tester.WasmTester;

describe("SHA256", () => {
describe("Sha256HashLeftRight", () => {
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `sha256HashLeftRight_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `sha256HashLeftRight_test.circom`);
circuit = await tester.wasm(circuitPath);
});

it("correctly hashes two random values", async () => {
Expand All @@ -35,8 +35,8 @@ describe("Poseidon hash circuits", function () {

describe("Sha256Hasher4", () => {
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `sha256Hasher4_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `sha256Hasher4_test.circom`);
circuit = await tester.wasm(circuitPath);
});

it("correctly hashes 4 random values", async () => {
Expand All @@ -61,8 +61,8 @@ describe("Poseidon hash circuits", function () {

describe("Sha256Hasher6", () => {
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `sha256Hasher6_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `sha256Hasher6_test.circom`);
circuit = await tester.wasm(circuitPath);
});
it("correctly hashes 6 random values", async () => {
const preImages: any = [];
Expand All @@ -88,8 +88,8 @@ describe("Poseidon hash circuits", function () {
describe("Poseidon", () => {
describe("Hasher5", () => {
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `hasher5_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `hasher5_test.circom`);
circuit = await tester.wasm(circuitPath);
});
it("correctly hashes 5 random values", async () => {
const preImages: any = [];
Expand All @@ -113,8 +113,8 @@ describe("Poseidon hash circuits", function () {

describe("Hasher4", () => {
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `hasher4_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `hasher4_test.circom`);
circuit = await tester.wasm(circuitPath);
});
it("correctly hashes 4 random values", async () => {
const preImages: any = [];
Expand All @@ -138,8 +138,8 @@ describe("Poseidon hash circuits", function () {

describe("Hasher3", () => {
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `hasher3_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `hasher3_test.circom`);
circuit = await tester.wasm(circuitPath);
});

it("correctly hashes 3 random values", async () => {
Expand All @@ -164,8 +164,8 @@ describe("Poseidon hash circuits", function () {

describe("Hasher13", () => {
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `hasher13_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `hasher13_test.circom`);
circuit = await tester.wasm(circuitPath);
});
it("correctly hashes 13 random values", async () => {
const preImages: any = [];
Expand All @@ -188,8 +188,8 @@ describe("Poseidon hash circuits", function () {

describe("HashLeftRight", () => {
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `hashleftright_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `hashleftright_test.circom`);
circuit = await tester.wasm(circuitPath);
});
it("correctly hashes two random values", async () => {
const left = genRandomSalt();
Expand All @@ -210,8 +210,8 @@ describe("Poseidon hash circuits", function () {

describe("MessageHasher", () => {
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `messageHasher_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `messageHasher_test.circom`);
circuit = await tester.wasm(circuitPath);
});
it("correctly hashes a message", async () => {
const k = new Keypair();
Expand Down
10 changes: 5 additions & 5 deletions circuits/ts/__tests__/MessageToCommand.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { stringifyBigInts, genRandomSalt } from "maci-crypto";
import { Keypair, PCommand } from "maci-domainobjs";
import { join } from "path";
import path from "path";
import { expect } from "chai";
const tester = require("circom_tester").wasm;
import tester from "circom_tester";
import { getSignal } from "./utils/utils";

describe("MessageToCommand circuit", () => {
let circuit: any;
let circuit: tester.WasmTester;
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `messageToCommand_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `messageToCommand_test.circom`);
circuit = await tester.wasm(circuitPath);
});
it("Should decrypt a Message and output the fields of a Command", async () => {
const { privKey } = new Keypair();
Expand Down
10 changes: 5 additions & 5 deletions circuits/ts/__tests__/MessageValidator.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { stringifyBigInts, genRandomSalt } from "maci-crypto";
import { PCommand, Keypair } from "maci-domainobjs";
import { join } from "path";
import path from "path";
import { expect } from "chai";
const tester = require("circom_tester").wasm;
import tester from "circom_tester";
import { getSignal } from "./utils/utils";

describe("MessageValidator circuit", function () {
this.timeout(90000);
let circuitInputs;
let circuit: any;
let circuit: tester.WasmTester;
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `messageValidator_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `messageValidator_test.circom`);
circuit = await tester.wasm(circuitPath);
});

before(() => {
Expand Down
10 changes: 5 additions & 5 deletions circuits/ts/__tests__/PrivToPubKey.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Keypair } from "maci-domainobjs";
import { stringifyBigInts } from "maci-crypto";
import { join } from "path";
import path from "path";
import { expect } from "chai";
const tester = require("circom_tester").wasm;
import tester from "circom_tester";
import { getSignal } from "./utils/utils";

describe("Public key derivation circuit", function () {
this.timeout(90000);
let circuit: any;
let circuit: tester.WasmTester;
before(async () => {
const circuitPath = join(__dirname, "../../circom/test", `privToPubKey_test.circom`);
circuit = await tester(circuitPath);
const circuitPath = path.resolve(__dirname, "../../circom/test", `privToPubKey_test.circom`);
circuit = await tester.wasm(circuitPath);
});

it("correctly computes a public key", async () => {
Expand Down
Loading

0 comments on commit ae17891

Please sign in to comment.